*** empty log message ***
[IRC.git] / Robust / Transactions / TransactionalIO / src / TransactionalIO / core / ExtendedTransaction.java
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package TransactionalIO.core;
6
7
8
9
10
11 import TransactionalIO.exceptions.AbortedException;
12 import TransactionalIO.benchmarks.benchmark;
13 import TransactionalIO.benchmarks.customhandler;
14 import TransactionalIO.benchmarks.customhandler;
15 import TransactionalIO.interfaces.BlockAccessModesEnum;
16 import TransactionalIO.interfaces.ContentionManager;
17 import TransactionalIO.interfaces.TransactionStatu;
18 //import dstm2.file.managers.BaseManager;
19 import java.awt.event.ActionListener;
20 import java.beans.EventHandler;
21 import java.beans.PropertyChangeEvent;
22 import java.beans.PropertyChangeListener;
23 import java.beans.PropertyChangeSupport;
24 import java.io.FileDescriptor;
25 import java.io.IOException;
26 import java.io.RandomAccessFile;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.Map;
31 import java.util.TreeMap;
32 import java.util.Vector;
33 import java.util.concurrent.locks.Lock;
34 import java.util.concurrent.locks.ReentrantLock;
35 import java.util.concurrent.locks.ReentrantReadWriteLock;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38
39 /**
40  *
41  * @author navid
42  */
43 public class ExtendedTransaction implements TransactionStatu {
44     
45     
46     private native int nativepwrite(byte buff[], long offset, int size, FileDescriptor fd);
47     
48    // {
49    //     System.load("/home/navid/libkooni.so");
50    // }
51     
52     private boolean flag = true;
53     public TransactionStatu memorystate; 
54     private PropertyChangeSupport changes = new PropertyChangeSupport(this);
55     public int starttime;
56     public int endtime;
57     
58     public TreeMap msg = new TreeMap();
59     public int numberofwrites;
60     public int numberofreads;
61     
62     public enum Status {ABORTED, ACTIVE, COMMITTED};
63     private boolean writesmerged = true;
64     
65     //private Vector<ReentrantLock> heldoffsetlocks;    
66     private Vector heldoffsetlocks;    
67     
68     //private Vector<ReentrantLock> heldblocklocks;    
69     private Vector heldblocklocks;
70     
71     //private HashMap<INode, Vector<TransactionalFile>> AccessedFiles;
72     private HashMap AccessedFiles;  
73     
74  
75     //private HashMap<INode, HashMap<Integer, BlockAccessModesEnum> > accessedBlocks;
76     private HashMap accessedBlocks; 
77    
78      //private HashMap<TransactionalFile, TransactionLocalFileAttributes> LocaltoGlobalMappings;
79     private HashMap GlobaltoLocalMappings;
80     
81     public HashMap merge_for_writes_done;
82     
83    
84    
85     
86     private HashMap writeBuffer;
87     
88     private ContentionManager contentionmanager;
89     private /*volatile*/ Status status;
90     
91     private int id;
92
93
94     
95     public ExtendedTransaction() {
96       //  super();
97        // id = Integer.valueOf(Thread.currentThread().getName().substring(7));
98         heldblocklocks = new Vector() ;
99         heldoffsetlocks= new Vector();
100         AccessedFiles = new HashMap();
101         GlobaltoLocalMappings = new HashMap/*<TransactionalFile, TransactionLocalFileAttributes >*/();
102         writeBuffer = new HashMap();
103         status = Status.ACTIVE;
104         accessedBlocks = new HashMap();
105         merge_for_writes_done = new HashMap();
106         writesmerged = true;
107      //   setContentionmanager(new BaseManager());
108     //    beginTransaction();
109         
110     }
111     
112     public ExtendedTransaction(TransactionStatu memorystate){
113         this();
114         /*    heldblocklocks = new Vector() ;
115         heldoffsetlocks= new Vector();
116         AccessedFiles = new HashMap();
117         GlobaltoLocalMappings = new HashMap();
118         writeBuffer = new HashMap();
119         status = Status.ACTIVE;
120         accessedBlocks = new HashMap();
121         merge_for_writes_done = new HashMap();
122         writesmerged = true;*/
123         this.memorystate = memorystate ;
124     }
125     
126      private int invokeNativepwrite(byte buff[], long offset, int size, RandomAccessFile file) {
127         try {
128             //System.out.println(buff.length);
129            // System.out.println(offset);
130             return nativepwrite(buff, offset, buff.length, file.getFD());
131         } catch (IOException ex) {
132             
133             Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex);
134             return -1;
135         }
136         
137     }
138     
139     public void beginTransaction(){
140         this.addPropertyChangeListener(new customhandler(Status.ABORTED));
141     }
142     
143     
144    
145   
146     public void abort() {
147         synchronized(this){
148           //  Status oldst = getStatus();         
149     /*        synchronized(benchmark.lock){
150                     System.out.println("be ga raftim 0");
151                 }*/
152             this.status = Status.ABORTED;
153             if (this.memorystate !=null && !(this.memorystate).isAborted()){
154         /*        synchronized(benchmark.lock){
155                     System.out.println(Thread.currentThread() +" be ga raftim 1 file");
156                 }*/
157                 this.memorystate.abortThisSystem();
158                /* synchronized(benchmark.lock){
159                     System.out.println(Thread.currentThread() + " be ga raftim 2 file");
160                 }*/
161             }
162            // Thread[] group = new Thread[30];
163           //  Thread.currentThread().enumerate(group);
164           //  group[this.id].interrupt();
165             /*synchronized(benchmark.lock){
166                 System.out.println("/////////////");
167                 System.out.println(Thread.currentThread() + " " +Thread.currentThread().enumerate(group));
168                 System.out.println(Thread.currentThread() + " " +group[0]);
169                 System.out.println(Thread.currentThread() + " " +group[1]);
170                 System.out.println(Thread.currentThread() + " " +group[2]);
171                 System.out.println("/////////////");
172             }*/
173             
174             
175           //  this.changes.firePropertyChange("status", oldst, Status.ABORTED);
176         }
177
178     }
179         
180     public Status getStatus() {
181         return status;
182     }
183   
184     public boolean isActive() {
185         return this.getStatus() == Status.ACTIVE;
186     }
187   
188   
189     public boolean isAborted() {
190         return this.getStatus() == Status.ABORTED;
191     }
192     
193     public ContentionManager getContentionmanager() {
194         return contentionmanager;
195     }
196
197     public void setContentionmanager(ContentionManager contentionmanager) {
198         this.contentionmanager = contentionmanager;
199     }
200     
201
202     public HashMap getWriteBuffer() {
203         return writeBuffer;
204     }
205     
206     public HashMap getAccessedFiles() {
207         return AccessedFiles;
208     }
209     
210     public boolean isWritesmerged() {
211         return writesmerged;
212     }
213
214     public void setWritesmerged(boolean writesmerged) {
215         this.writesmerged = writesmerged;
216     }
217
218
219
220
221     
222     
223     public HashMap getGlobaltoLocalMappings() {
224         return GlobaltoLocalMappings;
225     }
226
227     public HashMap getAccessedBlocks() {
228         return accessedBlocks;
229     }
230
231     
232     public ContentionManager getBlockContentionManager(){
233         return ManagerRepository.getBlockcm();
234     }
235     
236     public ContentionManager getOffsetContentionManager(){
237         return ManagerRepository.getOffsetcm();
238     }
239     
240     public TreeMap getSortedFileAccessMap(HashMap hmap) {
241         /*TreeMap sortedMap = new TreeMap(hmap);
242         return sortedMap;*/
243         return new TreeMap(hmap);
244     }
245     
246     
247     public void setStatus(Status st){
248         Status oldst = getStatus();
249         this.status = st;
250         this.changes.firePropertyChange("status", oldst, st);
251     }
252
253     
254     
255
256     public void addFile(TransactionalFile tf, long offstenumber) {
257
258        
259             //tf.lockOffset(this);
260   
261            TransactionLocalFileAttributes tmp = new TransactionLocalFileAttributes(offstenumber/*, tf.getInodestate().commitedfilesize.get()*/);
262   
263             //tf.offsetlock.unlock();     
264             
265             
266           
267             Vector dummy;     
268             
269             if (AccessedFiles.containsKey(tf.getInode())){
270                     dummy = (Vector) AccessedFiles.get(tf.getInode());
271             }
272             else{ 
273                 dummy = new Vector();
274                 AccessedFiles.put(tf.getInode(), dummy);
275             }
276             
277             dummy.add(tf);
278             GlobaltoLocalMappings.put(tf, tmp);
279             merge_for_writes_done.put(tf.getInode(), Boolean.TRUE);
280
281     }
282
283
284     public boolean lockOffsets() {   /// Locking offsets for File Descriptors
285
286
287         TreeMap hm = getSortedFileAccessMap(AccessedFiles);
288         Iterator iter = hm.keySet().iterator();     
289     
290         while (iter.hasNext() && (this.getStatus() == Status.ACTIVE)) {
291             INode key = (INode) iter.next();
292             
293             Vector vec = (Vector) AccessedFiles.get(key);
294             Collections.sort(vec);
295             Iterator it = vec.iterator();
296             while (it.hasNext()){
297                 TransactionalFile value = (TransactionalFile) it.next();
298                 while (this.getStatus() ==Status.ACTIVE){
299                     //if (value.offsetlock.tryLock()) {
300                     value.offsetlock.lock();
301                   
302                      //   synchronized(value.getCommitedoffset()){
303                      //       value.getCommitedoffset().setOffsetOwner(this);
304       
305          //               this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Locked the offset lock in commit for file " + value.getInode() + " from descriptor "+ value.getSequenceNum() +"\n");
306                         heldoffsetlocks.add(value.offsetlock);
307   
308                         //else 
309                         //    getContentionmanager().resolveConflict(this, value.getCommitedoffset());
310                         break;
311                     //}
312                 }
313                 if (this.getStatus() != Status.ACTIVE){
314   
315                            
316                     return false;
317                 }
318             }
319            // outercounter++;
320         }
321         if (this.getStatus() != Status.ACTIVE){
322     
323             
324             return false;
325         }
326         return true;
327     }         
328             
329     /*public boolean commit() {   /// Locking offsets for File Descriptors
330
331         Map hm = getSortedFileAccessMap(FilesAccesses);
332         //lock phase
333         Iterator iter = hm.keySet().iterator();
334         TransactionLocalFileAttributes value;
335         while (iter.hasNext() && (this.getStatus() == Status.ACTIVE)) {
336             INode key = (INode) iter.next();
337             value = (TransactionLocalFileAttributes) hm.get(key);
338             synchronized(value.getCurrentcommitedoffset()){
339                 if (value.offsetlock.tryLock()) {
340                     value.getCurrentcommitedoffset().setOffsetOwner(this);
341                     heldblocklocks.add(value.offsetlock);
342                     Iterator it =  value.getCurrentcommitedoffset().getOffsetReaders().iterator(); // for in-place aborting visible readers strategy
343                     while (it.hasNext())
344                     {
345                         ExtendedTransaction tr = (ExtendedTransaction) it.next();
346                         tr.abort();
347                     }
348                 }
349                 } 
350             }
351             getOffsetContentionManager().resolveConflict(this, value.getCurrentcommitedoffset().getOffsetOwner());
352         }
353         return true;
354     } */        
355     
356    /*public boolean commit() {   /// Locking offsets for File Descriptors with checking strategy
357
358         Map hm = getSortedFileAccessMap(FilesAccesses);
359         //lock phase
360         Iterator iter = hm.keySet().iterator();
361         TransactionLocalFileAttributes value;
362         while (iter.hasNext() && (this.getStatus() == Status.ACTIVE)) {
363             INode key = (INode) iter.next();
364             value = (TransactionLocalFileAttributes) hm.get(key);
365     
366              if (value.isValidatelocaloffset()) {
367                 if (value.getCopylocaloffset() == value.currentcommitedoffset.getOffsetnumber()) {
368                     value.offsetlock.lock();
369                     heldoffsetlocks.add(value.offsetlock);
370                     if (!(value.getCopylocaloffset() == value.currentcommitedoffset.getOffsetnumber())) {
371                         unlockAllLocks();
372                         return false;
373                     }
374                 } else {
375                     unlockAllLocks();
376                     return false;
377                 }
378             } else {
379                 value.offsetlock.lock();
380                 heldoffsetlocks.add(value.offsetlock);
381             }
382         }
383     }*/
384
385
386    
387
388     public boolean lockBlock(BlockDataStructure block, BlockAccessModesEnum mode/*, GlobalINodeState adapter, BlockAccessModesEnum mode, int expvalue, INode inode, TransactionLocalFileAttributes tf*/) {
389
390         
391         //boolean locked = false;
392         Lock lock;
393       
394         
395         
396         if (mode == BlockAccessModesEnum.READ){
397                 lock = block.getLock().readLock();     
398         }
399         else {
400             lock = block.getLock().writeLock();
401         }
402         
403         while (this.getStatus() == Status.ACTIVE) {
404                 lock.lock();    
405                     heldblocklocks.add(lock);
406                     return true;
407         }
408         
409         return false;
410     }
411        /*
412     public boolean lockBlock(BlockDataStructure block, Adapter adapter, BlockAccessModesEnum mode, int expvalue) { // from here for visible readers strategy
413         while (this.getStatus() == Status.ACTIVE) {
414              if (lock.tryLock()) {
415                 Thread.onAbortOnce(new Runnable() {
416
417                     public void run() {
418                         lock.unlock();
419                     }
420                 });
421
422                 heldblocklocks.add(lock);
423   
424                 synchronized (adapter) {
425                     block.setOwner(this);
426             //        Iterator it =  block.getReaders().iterator(); 
427             //        while (it.hasNext())
428             //        {
429             //            ExtendedTransaction tr = (ExtendedTransaction) it.next();
430             //            tr.abort();
431             //       }
432                 }
433
434                 return true;
435             } else {
436                 getBlockContentionManager().resolveConflict(this, block.getOwner());
437             }
438         }
439         return false;*/
440         
441         
442     /*
443     public boolean lockBlock(BlockDataStructure block, Adapter adapter, BlockAccessModesEnum mode, int expvalue) { // versioning strat
444          while (this.getStatus() == Status.ACTIVE) {
445             if (lock.tryLock()) {
446                 Thread.onAbortOnce(new Runnable() {
447
448                     public void run() {
449                         lock.unlock();
450                     }
451                 });
452
453                 heldblocklocks.add(lock);
454                 if (mode != BlockAccessModesEnum.WRITE) {   egy
455                     if (block.getVersion().get() != expvalue) {
456                         unlockAllLocks();
457                         return false;
458                     }
459                 }
460                 synchronized (adapter) {
461                     block.setOwner(this);
462                 }
463
464                 return true;
465             } else {
466                 getContentionManager().resolveConflict(this, block.getOwner());
467             }
468         }
469         return false;
470     }*/
471
472     public void prepareCommit() {
473         if (this.status != Status.ACTIVE)
474             throw new AbortedException();
475         
476         boolean ok = true;
477         if (!lockOffsets())
478         {
479 //            unlockAllLocks();
480         //    this.msg.put(System.nanoTime(),Thread.currentThread().getName() + " Aborted \n");
481           /*  synchronized(benchmark.lock){
482                 benchmark.msg += Thread.currentThread().getName() + " Aborted in prepare commit\n";
483             }*/
484             //Thread.currentThread().stop();
485             throw new AbortedException();
486         }
487         
488
489         ///////////////////////////
490         
491         
492         Map hm = getWriteBuffer();
493         
494         Iterator iter = hm.keySet().iterator();
495         WriteOperations value;
496         Vector vec = new Vector();
497         while (iter.hasNext() && (this.getStatus() == Status.ACTIVE) && ok) {
498             //int expvalue = 0;
499             
500             INode key = (INode) iter.next();
501             vec = (Vector) hm.get(key);
502             Collections.sort(vec);
503             Iterator it = vec.iterator();
504             while (it.hasNext()){
505           
506                 value = (WriteOperations) it.next();
507                 if (value.isUnknownoffset()){
508                    
509                     long start;
510                     long end;
511                     
512                     //synchronized(value.getOwnertransactionalFile().getCommitedoffset()){
513                         start = value.getRange().getStart() - value.getTFA().getCopylocaloffset() + value.getOwnerTF().getCommitedoffset().getOffsetnumber();
514                         end = value.getRange().getEnd() - value.getTFA().getCopylocaloffset() + value.getOwnerTF().getCommitedoffset().getOffsetnumber();
515                         if (value.getTFA().isUnknown_inital_offset_for_write()){
516                             value.getTFA().setLocaloffset(value.getTFA().getLocaloffset() - value.getTFA().getCopylocaloffset() + value.getOwnerTF().getCommitedoffset().getOffsetnumber());
517                             value.getTFA().setUnknown_inital_offset_for_write(false);
518                         }
519                     
520                     //}
521                  //   System.out.println("start write " + start);
522                   ///  System.out.println("end write " + end);
523                     int startblock = FileBlockManager.getCurrentFragmentIndexofTheFile(start);
524                     int targetblock = FileBlockManager.getTargetFragmentIndexofTheFile(start, value.getRange().getEnd() - value.getRange().getStart());
525                     
526                     TreeMap sset;
527                     if (this.getAccessedBlocks().get(key) != null){
528                        sset = (TreeMap) this.getAccessedBlocks().get(key);
529                     }
530                     
531                     else{
532                        sset = new TreeMap();
533                        this.getAccessedBlocks().put(key, sset);
534                     } 
535
536                     
537                     for (int i = startblock; i <= targetblock; i++) {
538                         if (sset.containsKey(Integer.valueOf(i))){
539                             if (sset.get(Integer.valueOf(i)) != BlockAccessModesEnum.WRITE) 
540                                 sset.put(Integer.valueOf(i), BlockAccessModesEnum.READ_WRITE);
541                         }
542                         else
543                             sset.put(Integer.valueOf(i), BlockAccessModesEnum.WRITE);
544                         
545                        // tt.add(Integer.valueOf(i));
546                     }
547                     
548                     value.getRange().setStart(start);
549                     value.getRange().setEnd(end);
550                     
551                  //  System.out.println(Thread.currentThread().);
552                  //   System.out.println(value.getRange().getStart());
553                  //   System.out.println(value.getRange().getEnd());
554                  //   System.out.println("---------------");
555                     //this.getAccessedBlocks().put(value.getOwnertransactionalFile().getInode(), sset);
556                 }
557             }
558
559         }
560
561         Iterator it = this.getAccessedBlocks().keySet().iterator();
562         while (it.hasNext() && (this.getStatus() == Status.ACTIVE)) {
563           INode inode = (INode) it.next();
564           GlobalINodeState inodestate = TransactionalFileWrapperFactory.getTateransactionalFileINodeState(inode);
565           TreeMap vec2 = (TreeMap) this.getAccessedBlocks().get(inode);
566           Iterator iter2 = vec2.keySet().iterator();
567           while(iter2.hasNext()){
568             Integer num = (Integer) iter2.next();         
569             
570             //BlockDataStructure blockobj = (BlockDataStructure) inodestate.lockmap.get(num);
571             BlockDataStructure blockobj;
572           //  if (((BlockAccessModesEnum)vec2.get(num)) == BlockAccessModesEnum.WRITE){
573                 blockobj = inodestate.getBlockDataStructure(num);
574           //  }
575           //  else 
576           //      blockobj = (BlockDataStructure) inodestate.lockmap.get(num);
577             
578             ok = this.lockBlock(blockobj, (BlockAccessModesEnum)vec2.get(num));
579             if (ok == false) 
580                 break;
581           /*  synchronized(benchmark.lock){
582                 benchmark.msg += Thread.currentThread().getName() + " Locked the Block Number " + blockobj.getBlocknumber() +" for " + inode + "\n";
583             }*/
584      //       this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Locked the Block Number " + blockobj.getBlocknumber() + " for file " + inode + "\n");
585           }
586         }
587          
588        if (this.getStatus() != Status.ACTIVE){ 
589           //  unlockAllLocks();
590        //           this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Aborted \n");
591            /* synchronized(benchmark.lock){
592                     benchmark.msg += Thread.currentThread().getName() + " Aborted \n";
593             }*/
594            // Thread.currentThread().stop();
595             throw new AbortedException(); 
596        }
597        abortAllReaders();  
598           
599             // }
600             //expvalue = ((Integer) value.getBlockversions().get(it)).intValue(); //for versioning strategy
601             /*if (!(value.isValidatelocaloffset())) {
602                 if (((BlockAccessModesEnum) (value.getAccesedblocks().get(blockno))) != BlockAccessModesEnum.WRITE) { //versioning strategy
603
604                     /if (blockobj.getVersion().get() == expvalue) {
605
606                         ok = this.lock(blockobj, value.adapter, (BlockAccessModesEnum) (value.getAccesedblocks().get(blockno)), expvalue);
607                         if (ok == false) {
608                             //        unlockAllLocks();
609                             break;
610                         }
611                     } else {
612                         ok = false;
613                         break;
614                     }
615                 } else {
616
617                     ok = this.lock(blockobj, value.adapter, (BlockAccessModesEnum) (value.getAccesedblocks().get(blockno)), expvalue);
618                     if (ok == false) {
619                         break;
620                     }
621                 }
622             }
623
624
625         if (!(ok)) {
626            unlockAllLocks();
627            throw new AbortedException();
628         }*/
629     }
630         
631         public void commitChanges(){
632
633         //   this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " is committing \n");
634             
635           
636            
637           //synchronized(benchmark.lock){
638             //    System.out.println(Thread.currentThread().getName() + " is commiting");
639           //}
640             
641             
642             Map hm = getWriteBuffer();
643             Iterator iter = hm.keySet().iterator();
644             Iterator it;
645             WriteOperations writeop;
646             Vector vec;
647             while (iter.hasNext() && (this.getStatus() == Status.ACTIVE)) {
648                 INode key = (INode) iter.next();
649                  
650                 vec = (Vector) hm.get(key);
651                 Collections.sort(vec);
652                 it = vec.iterator();
653                 while (it.hasNext()){
654                  
655           
656                     //value = (WriteOperations) it.next();
657                    // writeop = (WriteOperations) writeBuffer.get(key);
658                     writeop = (WriteOperations) it.next();
659                   //  System.out.println(writeop);
660                     Byte[] data = new Byte[(int) (writeop.getRange().getEnd() - writeop.getRange().getStart())];
661                     byte[] bytedata = new byte[(int) (writeop.getRange().getEnd() - writeop.getRange().getStart())];
662                     data = (Byte[]) writeop.getData();
663
664                     for (int i = 0; i < data.length; i++) {
665                         bytedata[i] = data[i];
666                     }
667                 
668                //     try {
669                    //     
670                 //        writeop.getOwnertransactionalFile().file.seek(writeop.getRange().getStart());
671                    //    System.out.println(Thread.currentThread() + " range " + writeop.getRange().getStart());
672                  //       writeop.getOwnertransactionalFile().file.write(bytedata);
673                         invokeNativepwrite(bytedata, writeop.getRange().getStart(), bytedata.length, writeop.getOwnerTF().file);
674                        // System.out.println(Thread.currentThread() + " " + bytedata);
675                         
676                  //  } catch (IOException ex) {
677                  //       Logger.getLogger(ExtendedTransaction.class.getName()).log(Level.SEVERE, null, ex);
678                   // }
679                 //
680                 
681                 }
682             }
683                 
684                 /*if (((FileAccessModesEum) (this.FilesAccessModes.get(key))) == FileAccessModesEum.APPEND) {
685                     try {
686                         Range range = (Range) value.getWrittendata().firstKey();
687
688
689                         //synchronized(value.adapter){
690                         //value.f.seek(value.adapter.commitedfilesize.get());
691                         value.f.seek(value.getFilelength());
692                         //}
693
694                         Byte[] data = new Byte[(int) (range.getEnd() - range.getStart())];
695                         byte[] bytedata = new byte[(int) (range.getEnd() - range.getStart())];
696                         data = (Byte[]) value.getWrittendata().get(range);
697
698                         for (int i = 0; i < data.length; i++) {
699                             bytedata[i] = data[i];
700                         }
701                         value.f.write(bytedata);
702
703                     } catch (IOException ex) {
704                         Logger.getLogger(ExtendedTransaction.class.getName()).log(Level.SEVERE, null, ex);
705                     }
706
707                 } else if (((FileAccessModesEum) (this.FilesAccessModes.get(key))) == FileAccessModesEum.READ) {
708                     continue;
709                 }
710                 else if (value.relocatablewrite && value.getContinious_written_data() != null){
711                     
712                     
713                 }
714                 else if (!(value.getNon_Speculative_Writtendata().isEmpty())) {
715                     int tobeaddedoffset = 0;
716
717                     if (value.isValidatelocaloffset()) {
718                         tobeaddedoffset = 0;
719                     } else {
720                         tobeaddedoffset = (int) (value.getCurrentcommitedoffset().getOffsetnumber() - value.getCopylocaloffset());
721                     }
722                     Iterator it = value.getNon_Speculative_Writtendata().keySet().iterator();
723                     int counter = 0;
724                     while (it.hasNext() && (this.getStatus() == Status.ACTIVE)) {
725                         try {
726                             Range range = (Range) it.next();
727
728                            
729                             value.f.seek(range.getStart() + tobeaddedoffset);
730
731                             Byte[] data = new Byte[(int) (range.getEnd() - range.getStart())];
732                             byte[] bytedata = new byte[(int) (range.getEnd() - range.getStart())];
733                             data = (Byte[]) value.getNon_Speculative_Writtendata().get(range);
734
735                             for (int i = 0; i < data.length; i++) {
736                                 bytedata[i] = data[i];
737                             }
738                             value.f.write(bytedata);
739                             counter++;
740
741                         } catch (IOException ex) {
742                             Logger.getLogger(ExtendedTransaction.class.getName()).log(Level.SEVERE, null, ex);
743                         }
744                     }
745                 } else {
746                     continue;
747                 }
748             }
749
750
751             iter = hm.keySet().iterator();
752             while (iter.hasNext() ) {
753                 INode key = (INode) iter.next();
754                 value = (TransactionLocalFileAttributes) hm.get(key);
755                 Iterator it = value.getAccesedblocks().keySet().iterator();
756
757                 while (it.hasNext()) {
758                     Integer blockno = (Integer) it.next();
759                     synchronized (value.adapter) {
760                         //BlockDataStructure blockobj = (BlockDataStructure) value.adapter.lockmap.get(blockno);
761                         //blockobj.getVersion().getAndIncrement(); for versioning strategy
762                         //value.getCurrentcommitedoffset().setOffsetnumber(value.getLocaloffset());
763                         //value.adapter.commitedfilesize.getAndSet(value.getFilelength());
764                     }
765                 }
766             }*/
767         Iterator k = GlobaltoLocalMappings.keySet().iterator();
768         while (k.hasNext()){
769             TransactionalFile trf = (TransactionalFile) (k.next());
770         //    synchronized(trf.getCommitedoffset()){
771                 trf.getCommitedoffset().setOffsetnumber(((TransactionLocalFileAttributes)GlobaltoLocalMappings.get(trf)).getLocaloffset());
772                 /*synchronized(benchmark.lock){
773                     System.out.println(Thread.currentThread() + " KIRIR " +GlobaltoLocalMappings.get(trf).getLocaloffset());
774                 }*/
775         //    }
776         }
777         //unlockAllLocks();
778
779     }
780
781     public void unlockAllLocks() {
782         Iterator it = heldblocklocks.iterator();
783
784         while (it.hasNext()) {
785             
786            Lock lock = (Lock) it.next();    
787            lock.unlock();
788            
789             /*synchronized(benchmark.lock){
790                 System.out.println(Thread.currentThread().getName() + " Released the block lock for " + lock);
791             }*/
792         }
793         heldblocklocks.clear();
794         
795         it = heldoffsetlocks.iterator();
796         while (it.hasNext()) {
797             ReentrantLock lock = (ReentrantLock) it.next(); 
798             lock.unlock();   
799         //    synchronized(benchmark.lock){
800        //         System.out.println(Thread.currentThread().getName() + " Released the offset lock for "+ lock +"\n");
801        //    }
802         }
803         heldoffsetlocks.clear();
804     }
805     
806     public void abortAllReaders(){
807         TreeMap hm = getSortedFileAccessMap(AccessedFiles);
808         //lock phase
809         Iterator iter = hm.keySet().iterator();
810         TransactionalFile value;
811         while (iter.hasNext()) {
812             INode key = (INode) iter.next();
813             Vector vec = (Vector) AccessedFiles.get(key);
814             Iterator it = vec.iterator();
815             while (it.hasNext())
816             {
817                
818                 value = (TransactionalFile)it.next();
819            
820             //value = (TransactionalFile) hm.get(key);
821                 //System.out.println(value.getCommitedoffset().getOffsetReaders());
822
823                     Iterator it2 =  value.getCommitedoffset().getOffsetReaders().iterator(); // for visible readers strategy
824                     while ( it2.hasNext())
825                     {
826                       
827                         ExtendedTransaction tr = (ExtendedTransaction) it2.next();
828                         if (tr != this)
829                             tr.abort();
830                     }
831                     value.getCommitedoffset().getOffsetReaders().clear();
832                 //}
833             }
834             
835             
836             
837             TreeMap vec2;
838             if (accessedBlocks.get(key) != null){
839                 vec2 = (TreeMap) accessedBlocks.get(key);
840             }
841             else{
842                 vec2 = new TreeMap();
843
844             }
845             GlobalINodeState inodestate = TransactionalFileWrapperFactory.getTateransactionalFileINodeState(key);
846             Iterator it2 = vec2.keySet().iterator();
847           
848             while (it2.hasNext())
849             {
850               
851                 Integer num = (Integer)it2.next();
852                 if (vec2.get(num) != BlockAccessModesEnum.READ)
853                 {
854                   BlockDataStructure blockobj = (BlockDataStructure) inodestate.getBlockDataStructure(num);//lockmap.get(num);
855                     Iterator it4 =  blockobj.getReaders().iterator(); // from here for visible readers strategy
856                 
857                     while (it4.hasNext())
858                     {
859                         
860                         ExtendedTransaction tr = (ExtendedTransaction) it4.next();
861                         if (this != tr)
862                             tr.abort();
863                     }
864                     blockobj.getReaders().clear();
865                     
866                 }
867             }
868         
869         
870         
871                 
872        /*         SortedSet sst = (SortedSet) this.getAccessedBlocks().get(key);
873                 Iterator it3 =  sst.iterator();
874                 while (it3.hasNext()){
875                     Integer num = (Integer)it.next();
876                     BlockDataStructure blockobj = (BlockDataStructure) value.getInodestate().lockmap.get(num);
877                     Iterator it4 =  blockobj.getReaders().iterator(); // from here for visible readers strategy
878                     while (it4.hasNext())
879                     {
880                         ExtendedTransaction tr = (ExtendedTransaction) it3.next();
881                         tr.abort();
882                     }
883
884                 }*/
885             
886         }
887     }
888     
889     public void addPropertyChangeListener(PropertyChangeListener listener){
890         this.changes.addPropertyChangeListener("status",listener);
891     }
892     
893      public void removePropertyChangeListener(PropertyChangeListener listener){
894         this.changes.removePropertyChangeListener("status",listener);
895     }
896
897     public TransactionStatu getOtherSystem() {
898         return memorystate;
899     }
900
901     public void setOtherSystem(TransactionStatu othersystem) {
902         memorystate = othersystem;
903     }
904
905     public Vector getHeldblocklocks() {
906         return heldblocklocks;
907     }
908
909     public void setHeldblocklocks(Vector heldblocklocks) {
910         this.heldblocklocks = heldblocklocks;
911     }
912
913     public Vector getHeldoffsetlocks() {
914         return heldoffsetlocks;
915     }
916
917     public void setHeldoffsetlocks(Vector heldoffsetlocks) {
918         this.heldoffsetlocks = heldoffsetlocks;
919     }
920
921     public void abortThisSystem() {
922         abort();
923     }
924
925     public boolean isCommitted() {
926         if (this.status == Status.COMMITTED)
927             return true;
928             
929          return  false;
930         
931     }
932     
933     
934     
935 }
936      
937     
938    
939
940
941