*** 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/*, TransactionLocalFileAttributes tmp*/) {
257
258
259        /* if (tf.appendmode) {
260             this.addtoFileAccessModeMap(tf.getInode(), FileAccessModesEum.APPEND);
261         } else if (tf.writemode) {
262             this.addtoFileAccessModeMap(tf.getInode(), FileAccessModesEum.READ_WRITE);
263         } else {
264             this.addtoFileAccessModeMap(tf.getInode(), FileAccessModesEum.READ);
265         }*/
266       //  System.out.println("dsadssasadssa");
267                 
268           tf.lockOffset(this);
269           
270           //tf.offsetlock.lock();
271            TransactionLocalFileAttributes tmp = new TransactionLocalFileAttributes(tf.getCommitedoffset().getOffsetnumber()/*, tf.getInodestate().commitedfilesize.get()*/);
272           //this.heldoffsetlocks.remove(tf.offsetlock);  
273               tf.offsetlock.unlock();     
274           
275             Vector dummy;     
276             
277             if (AccessedFiles.containsKey(tf.getInode())){
278                     dummy = (Vector) AccessedFiles.get(tf.getInode());
279             }
280             else{ 
281                 dummy = new Vector();
282                 AccessedFiles.put(tf.getInode(), dummy);
283             }
284             
285         
286            
287       //      this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Unlocked the offset lock " + tf.offsetlock + " for file " + tf.getInode() + " form descriptor " + tf.getSequenceNum());
288             dummy.add(tf);
289             GlobaltoLocalMappings.put(tf, tmp);
290             merge_for_writes_done.put(tf.getInode(), Boolean.TRUE);
291             
292         
293  
294         //}
295
296     }
297
298
299     public boolean lockOffsets() {   /// Locking offsets for File Descriptors
300
301
302         TreeMap hm = getSortedFileAccessMap(AccessedFiles);
303         Iterator iter = hm.keySet().iterator();     
304     
305         while (iter.hasNext() && (this.getStatus() == Status.ACTIVE)) {
306             INode key = (INode) iter.next();
307             
308             Vector vec = (Vector) AccessedFiles.get(key);
309             Collections.sort(vec);
310             Iterator it = vec.iterator();
311             while (it.hasNext()){
312                 TransactionalFile value = (TransactionalFile) it.next();
313                 while (this.getStatus() ==Status.ACTIVE){
314                     //if (value.offsetlock.tryLock()) {
315                     value.offsetlock.lock();
316                   
317                      //   synchronized(value.getCommitedoffset()){
318                      //       value.getCommitedoffset().setOffsetOwner(this);
319       
320          //               this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Locked the offset lock in commit for file " + value.getInode() + " from descriptor "+ value.getSequenceNum() +"\n");
321                         heldoffsetlocks.add(value.offsetlock);
322   
323                         //else 
324                         //    getContentionmanager().resolveConflict(this, value.getCommitedoffset());
325                         break;
326                     //}
327                 }
328                 if (this.getStatus() != Status.ACTIVE){
329   
330                            
331                     return false;
332                 }
333             }
334            // outercounter++;
335         }
336         if (this.getStatus() != Status.ACTIVE){
337     
338             
339             return false;
340         }
341         return true;
342     }         
343             
344     /*public boolean commit() {   /// Locking offsets for File Descriptors
345
346         Map hm = getSortedFileAccessMap(FilesAccesses);
347         //lock phase
348         Iterator iter = hm.keySet().iterator();
349         TransactionLocalFileAttributes value;
350         while (iter.hasNext() && (this.getStatus() == Status.ACTIVE)) {
351             INode key = (INode) iter.next();
352             value = (TransactionLocalFileAttributes) hm.get(key);
353             synchronized(value.getCurrentcommitedoffset()){
354                 if (value.offsetlock.tryLock()) {
355                     value.getCurrentcommitedoffset().setOffsetOwner(this);
356                     heldblocklocks.add(value.offsetlock);
357                     Iterator it =  value.getCurrentcommitedoffset().getOffsetReaders().iterator(); // for in-place aborting visible readers strategy
358                     while (it.hasNext())
359                     {
360                         ExtendedTransaction tr = (ExtendedTransaction) it.next();
361                         tr.abort();
362                     }
363                 }
364                 } 
365             }
366             getOffsetContentionManager().resolveConflict(this, value.getCurrentcommitedoffset().getOffsetOwner());
367         }
368         return true;
369     } */        
370     
371    /*public boolean commit() {   /// Locking offsets for File Descriptors with checking strategy
372
373         Map hm = getSortedFileAccessMap(FilesAccesses);
374         //lock phase
375         Iterator iter = hm.keySet().iterator();
376         TransactionLocalFileAttributes value;
377         while (iter.hasNext() && (this.getStatus() == Status.ACTIVE)) {
378             INode key = (INode) iter.next();
379             value = (TransactionLocalFileAttributes) hm.get(key);
380     
381              if (value.isValidatelocaloffset()) {
382                 if (value.getCopylocaloffset() == value.currentcommitedoffset.getOffsetnumber()) {
383                     value.offsetlock.lock();
384                     heldoffsetlocks.add(value.offsetlock);
385                     if (!(value.getCopylocaloffset() == value.currentcommitedoffset.getOffsetnumber())) {
386                         unlockAllLocks();
387                         return false;
388                     }
389                 } else {
390                     unlockAllLocks();
391                     return false;
392                 }
393             } else {
394                 value.offsetlock.lock();
395                 heldoffsetlocks.add(value.offsetlock);
396             }
397         }
398     }*/
399
400
401    
402
403     public boolean lockBlock(BlockDataStructure block, BlockAccessModesEnum mode/*, GlobalINodeState adapter, BlockAccessModesEnum mode, int expvalue, INode inode, TransactionLocalFileAttributes tf*/) {
404
405         
406         //boolean locked = false;
407         Lock lock;
408       
409         
410         
411         if (mode == BlockAccessModesEnum.READ){
412                 lock = block.getLock().readLock();
413                   
414              
415             }
416         else {
417               
418             lock = block.getLock().writeLock();
419             
420         }
421         
422         while (this.getStatus() == Status.ACTIVE) {
423             //synchronized(block){
424                 
425               //  if (lock.tryLock()) {
426                 lock.lock();    
427                    // synchronized(benchmark.lock){
428                   //      System.out.println(Thread.currentThread() + " Lock the block lock for " + lock +" number " + block.getBlocknumber());
429                   //  }
430                     heldblocklocks.add(lock);
431                 //    block.setOwner(this);
432                     return true;
433                // }
434                 
435                 
436                     //getContentionmanager().resolveConflict(this, block);
437         }
438         
439         return false;
440     }
441        /*
442     public boolean lockBlock(BlockDataStructure block, Adapter adapter, BlockAccessModesEnum mode, int expvalue) { // from here for visible readers strategy
443         while (this.getStatus() == Status.ACTIVE) {
444              if (lock.tryLock()) {
445                 Thread.onAbortOnce(new Runnable() {
446
447                     public void run() {
448                         lock.unlock();
449                     }
450                 });
451
452                 heldblocklocks.add(lock);
453   
454                 synchronized (adapter) {
455                     block.setOwner(this);
456             //        Iterator it =  block.getReaders().iterator(); 
457             //        while (it.hasNext())
458             //        {
459             //            ExtendedTransaction tr = (ExtendedTransaction) it.next();
460             //            tr.abort();
461             //       }
462                 }
463
464                 return true;
465             } else {
466                 getBlockContentionManager().resolveConflict(this, block.getOwner());
467             }
468         }
469         return false;*/
470         
471         
472     /*
473     public boolean lockBlock(BlockDataStructure block, Adapter adapter, BlockAccessModesEnum mode, int expvalue) { // versioning strat
474          while (this.getStatus() == Status.ACTIVE) {
475             if (lock.tryLock()) {
476                 Thread.onAbortOnce(new Runnable() {
477
478                     public void run() {
479                         lock.unlock();
480                     }
481                 });
482
483                 heldblocklocks.add(lock);
484                 if (mode != BlockAccessModesEnum.WRITE) {   egy
485                     if (block.getVersion().get() != expvalue) {
486                         unlockAllLocks();
487                         return false;
488                     }
489                 }
490                 synchronized (adapter) {
491                     block.setOwner(this);
492                 }
493
494                 return true;
495             } else {
496                 getContentionManager().resolveConflict(this, block.getOwner());
497             }
498         }
499         return false;
500     }*/
501
502     public void prepareCommit() {
503         if (this.status != Status.ACTIVE)
504             throw new AbortedException();
505         
506         boolean ok = true;
507         if (!lockOffsets())
508         {
509 //            unlockAllLocks();
510         //    this.msg.put(System.nanoTime(),Thread.currentThread().getName() + " Aborted \n");
511           /*  synchronized(benchmark.lock){
512                 benchmark.msg += Thread.currentThread().getName() + " Aborted in prepare commit\n";
513             }*/
514             //Thread.currentThread().stop();
515             throw new AbortedException();
516         }
517         
518
519         ///////////////////////////
520         
521         
522         Map hm = getWriteBuffer();
523         
524         Iterator iter = hm.keySet().iterator();
525         WriteOperations value;
526         Vector vec = new Vector();
527         while (iter.hasNext() && (this.getStatus() == Status.ACTIVE) && ok) {
528             //int expvalue = 0;
529             
530             INode key = (INode) iter.next();
531             vec = (Vector) hm.get(key);
532             Collections.sort(vec);
533             Iterator it = vec.iterator();
534             while (it.hasNext()){
535           
536                 value = (WriteOperations) it.next();
537                 if (value.isUnknownoffset()){
538                    
539                     long start;
540                     long end;
541                     
542                     //synchronized(value.getOwnertransactionalFile().getCommitedoffset()){
543                         start = value.getRange().getStart() - value.getBelongingto().getCopylocaloffset() + value.getOwnertransactionalFile().getCommitedoffset().getOffsetnumber();
544                         end = value.getRange().getEnd() - value.getBelongingto().getCopylocaloffset() + value.getOwnertransactionalFile().getCommitedoffset().getOffsetnumber();
545                         if (value.getBelongingto().isUnknown_inital_offset_for_write()){
546                             value.getBelongingto().setLocaloffset(value.getBelongingto().getLocaloffset() - value.getBelongingto().getCopylocaloffset() + value.getOwnertransactionalFile().getCommitedoffset().getOffsetnumber());
547                             value.getBelongingto().setUnknown_inital_offset_for_write(false);
548                         }
549                     
550                     //}
551                  //   System.out.println("start write " + start);
552                   ///  System.out.println("end write " + end);
553                     int startblock = FileBlockManager.getCurrentFragmentIndexofTheFile(start);
554                     int targetblock = FileBlockManager.getTargetFragmentIndexofTheFile(start, value.getRange().getEnd() - value.getRange().getStart());
555                     
556                     TreeMap sset;
557                     if (this.getAccessedBlocks().get(key) != null){
558                        sset = (TreeMap) this.getAccessedBlocks().get(key);
559                     }
560                     
561                     else{
562                        sset = new TreeMap();
563                        this.getAccessedBlocks().put(key, sset);
564                     } 
565
566                     
567                     for (int i = startblock; i <= targetblock; i++) {
568                         if (sset.containsKey(Integer.valueOf(i))){
569                             if (sset.get(Integer.valueOf(i)) != BlockAccessModesEnum.WRITE) 
570                                 sset.put(Integer.valueOf(i), BlockAccessModesEnum.READ_WRITE);
571                         }
572                         else
573                             sset.put(Integer.valueOf(i), BlockAccessModesEnum.WRITE);
574                         
575                        // tt.add(Integer.valueOf(i));
576                     }
577                     
578                     value.getRange().setStart(start);
579                     value.getRange().setEnd(end);
580                     
581                  //  System.out.println(Thread.currentThread().);
582                  //   System.out.println(value.getRange().getStart());
583                  //   System.out.println(value.getRange().getEnd());
584                  //   System.out.println("---------------");
585                     //this.getAccessedBlocks().put(value.getOwnertransactionalFile().getInode(), sset);
586                 }
587             }
588
589         }
590
591         Iterator it = this.getAccessedBlocks().keySet().iterator();
592         while (it.hasNext() && (this.getStatus() == Status.ACTIVE)) {
593           INode inode = (INode) it.next();
594           GlobalINodeState inodestate = TransactionalFileWrapperFactory.getTateransactionalFileINodeState(inode);
595           TreeMap vec2 = (TreeMap) this.getAccessedBlocks().get(inode);
596           Iterator iter2 = vec2.keySet().iterator();
597           while(iter2.hasNext()){
598             Integer num = (Integer) iter2.next();         
599             
600             //BlockDataStructure blockobj = (BlockDataStructure) inodestate.lockmap.get(num);
601             BlockDataStructure blockobj;
602           //  if (((BlockAccessModesEnum)vec2.get(num)) == BlockAccessModesEnum.WRITE){
603                 blockobj = inodestate.getBlockDataStructure(num);
604           //  }
605           //  else 
606           //      blockobj = (BlockDataStructure) inodestate.lockmap.get(num);
607             
608             ok = this.lockBlock(blockobj, (BlockAccessModesEnum)vec2.get(num));
609             if (ok == false) 
610                 break;
611           /*  synchronized(benchmark.lock){
612                 benchmark.msg += Thread.currentThread().getName() + " Locked the Block Number " + blockobj.getBlocknumber() +" for " + inode + "\n";
613             }*/
614      //       this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Locked the Block Number " + blockobj.getBlocknumber() + " for file " + inode + "\n");
615           }
616         }
617          
618        if (this.getStatus() != Status.ACTIVE){ 
619           //  unlockAllLocks();
620        //           this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Aborted \n");
621            /* synchronized(benchmark.lock){
622                     benchmark.msg += Thread.currentThread().getName() + " Aborted \n";
623             }*/
624            // Thread.currentThread().stop();
625             throw new AbortedException(); 
626        }
627        abortAllReaders();  
628           
629             // }
630             //expvalue = ((Integer) value.getBlockversions().get(it)).intValue(); //for versioning strategy
631             /*if (!(value.isValidatelocaloffset())) {
632                 if (((BlockAccessModesEnum) (value.getAccesedblocks().get(blockno))) != BlockAccessModesEnum.WRITE) { //versioning strategy
633
634                     /if (blockobj.getVersion().get() == expvalue) {
635
636                         ok = this.lock(blockobj, value.adapter, (BlockAccessModesEnum) (value.getAccesedblocks().get(blockno)), expvalue);
637                         if (ok == false) {
638                             //        unlockAllLocks();
639                             break;
640                         }
641                     } else {
642                         ok = false;
643                         break;
644                     }
645                 } else {
646
647                     ok = this.lock(blockobj, value.adapter, (BlockAccessModesEnum) (value.getAccesedblocks().get(blockno)), expvalue);
648                     if (ok == false) {
649                         break;
650                     }
651                 }
652             }
653
654
655         if (!(ok)) {
656            unlockAllLocks();
657            throw new AbortedException();
658         }*/
659     }
660         
661         public void commitChanges(){
662
663         //   this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " is committing \n");
664             
665           
666            
667           //synchronized(benchmark.lock){
668             //    System.out.println(Thread.currentThread().getName() + " is commiting");
669           //}
670             
671             
672             Map hm = getWriteBuffer();
673             Iterator iter = hm.keySet().iterator();
674             Iterator it;
675             WriteOperations writeop;
676             Vector vec;
677             while (iter.hasNext() && (this.getStatus() == Status.ACTIVE)) {
678                 INode key = (INode) iter.next();
679                  
680                 vec = (Vector) hm.get(key);
681                 Collections.sort(vec);
682                 it = vec.iterator();
683                 while (it.hasNext()){
684                  
685           
686                     //value = (WriteOperations) it.next();
687                    // writeop = (WriteOperations) writeBuffer.get(key);
688                     writeop = (WriteOperations) it.next();
689                   //  System.out.println(writeop);
690                     Byte[] data = new Byte[(int) (writeop.getRange().getEnd() - writeop.getRange().getStart())];
691                     byte[] bytedata = new byte[(int) (writeop.getRange().getEnd() - writeop.getRange().getStart())];
692                     data = (Byte[]) writeop.getData();
693
694                     for (int i = 0; i < data.length; i++) {
695                         bytedata[i] = data[i];
696                     }
697                 
698                //     try {
699                    //     
700                 //        writeop.getOwnertransactionalFile().file.seek(writeop.getRange().getStart());
701                    //    System.out.println(Thread.currentThread() + " range " + writeop.getRange().getStart());
702                  //       writeop.getOwnertransactionalFile().file.write(bytedata);
703                         invokeNativepwrite(bytedata, writeop.getRange().getStart(), bytedata.length, writeop.getOwnertransactionalFile().file);
704                        // System.out.println(Thread.currentThread() + " " + bytedata);
705                         
706                  //  } catch (IOException ex) {
707                  //       Logger.getLogger(ExtendedTransaction.class.getName()).log(Level.SEVERE, null, ex);
708                   // }
709                 //
710                 
711                 }
712             }
713                 
714                 /*if (((FileAccessModesEum) (this.FilesAccessModes.get(key))) == FileAccessModesEum.APPEND) {
715                     try {
716                         Range range = (Range) value.getWrittendata().firstKey();
717
718
719                         //synchronized(value.adapter){
720                         //value.f.seek(value.adapter.commitedfilesize.get());
721                         value.f.seek(value.getFilelength());
722                         //}
723
724                         Byte[] data = new Byte[(int) (range.getEnd() - range.getStart())];
725                         byte[] bytedata = new byte[(int) (range.getEnd() - range.getStart())];
726                         data = (Byte[]) value.getWrittendata().get(range);
727
728                         for (int i = 0; i < data.length; i++) {
729                             bytedata[i] = data[i];
730                         }
731                         value.f.write(bytedata);
732
733                     } catch (IOException ex) {
734                         Logger.getLogger(ExtendedTransaction.class.getName()).log(Level.SEVERE, null, ex);
735                     }
736
737                 } else if (((FileAccessModesEum) (this.FilesAccessModes.get(key))) == FileAccessModesEum.READ) {
738                     continue;
739                 }
740                 else if (value.relocatablewrite && value.getContinious_written_data() != null){
741                     
742                     
743                 }
744                 else if (!(value.getNon_Speculative_Writtendata().isEmpty())) {
745                     int tobeaddedoffset = 0;
746
747                     if (value.isValidatelocaloffset()) {
748                         tobeaddedoffset = 0;
749                     } else {
750                         tobeaddedoffset = (int) (value.getCurrentcommitedoffset().getOffsetnumber() - value.getCopylocaloffset());
751                     }
752                     Iterator it = value.getNon_Speculative_Writtendata().keySet().iterator();
753                     int counter = 0;
754                     while (it.hasNext() && (this.getStatus() == Status.ACTIVE)) {
755                         try {
756                             Range range = (Range) it.next();
757
758                            
759                             value.f.seek(range.getStart() + tobeaddedoffset);
760
761                             Byte[] data = new Byte[(int) (range.getEnd() - range.getStart())];
762                             byte[] bytedata = new byte[(int) (range.getEnd() - range.getStart())];
763                             data = (Byte[]) value.getNon_Speculative_Writtendata().get(range);
764
765                             for (int i = 0; i < data.length; i++) {
766                                 bytedata[i] = data[i];
767                             }
768                             value.f.write(bytedata);
769                             counter++;
770
771                         } catch (IOException ex) {
772                             Logger.getLogger(ExtendedTransaction.class.getName()).log(Level.SEVERE, null, ex);
773                         }
774                     }
775                 } else {
776                     continue;
777                 }
778             }
779
780
781             iter = hm.keySet().iterator();
782             while (iter.hasNext() ) {
783                 INode key = (INode) iter.next();
784                 value = (TransactionLocalFileAttributes) hm.get(key);
785                 Iterator it = value.getAccesedblocks().keySet().iterator();
786
787                 while (it.hasNext()) {
788                     Integer blockno = (Integer) it.next();
789                     synchronized (value.adapter) {
790                         //BlockDataStructure blockobj = (BlockDataStructure) value.adapter.lockmap.get(blockno);
791                         //blockobj.getVersion().getAndIncrement(); for versioning strategy
792                         //value.getCurrentcommitedoffset().setOffsetnumber(value.getLocaloffset());
793                         //value.adapter.commitedfilesize.getAndSet(value.getFilelength());
794                     }
795                 }
796             }*/
797         Iterator k = GlobaltoLocalMappings.keySet().iterator();
798         while (k.hasNext()){
799             TransactionalFile trf = (TransactionalFile) (k.next());
800         //    synchronized(trf.getCommitedoffset()){
801                 trf.getCommitedoffset().setOffsetnumber(((TransactionLocalFileAttributes)GlobaltoLocalMappings.get(trf)).getLocaloffset());
802                 /*synchronized(benchmark.lock){
803                     System.out.println(Thread.currentThread() + " KIRIR " +GlobaltoLocalMappings.get(trf).getLocaloffset());
804                 }*/
805         //    }
806         }
807         //unlockAllLocks();
808
809     }
810
811     public void unlockAllLocks() {
812         Iterator it = heldblocklocks.iterator();
813
814         while (it.hasNext()) {
815             
816            Lock lock = (Lock) it.next();    
817            lock.unlock();
818            
819             /*synchronized(benchmark.lock){
820                 System.out.println(Thread.currentThread().getName() + " Released the block lock for " + lock);
821             }*/
822         }
823         heldblocklocks.clear();
824         
825         it = heldoffsetlocks.iterator();
826         while (it.hasNext()) {
827             ReentrantLock lock = (ReentrantLock) it.next(); 
828             lock.unlock();   
829         //    synchronized(benchmark.lock){
830        //         System.out.println(Thread.currentThread().getName() + " Released the offset lock for "+ lock +"\n");
831        //    }
832         }
833         heldoffsetlocks.clear();
834     }
835     
836     public void abortAllReaders(){
837         TreeMap hm = getSortedFileAccessMap(AccessedFiles);
838         //lock phase
839         Iterator iter = hm.keySet().iterator();
840         TransactionalFile value;
841         while (iter.hasNext()) {
842             INode key = (INode) iter.next();
843             Vector vec = (Vector) AccessedFiles.get(key);
844             Iterator it = vec.iterator();
845             while (it.hasNext())
846             {
847                
848                 value = (TransactionalFile)it.next();
849            
850             //value = (TransactionalFile) hm.get(key);
851                 //System.out.println(value.getCommitedoffset().getOffsetReaders());
852
853                     Iterator it2 =  value.getCommitedoffset().getOffsetReaders().iterator(); // for visible readers strategy
854                     while ( it2.hasNext())
855                     {
856                       
857                         ExtendedTransaction tr = (ExtendedTransaction) it2.next();
858                         if (tr != this)
859                             tr.abort();
860                     }
861                     value.getCommitedoffset().getOffsetReaders().clear();
862                 //}
863             }
864             
865             
866             
867             TreeMap vec2;
868             if (accessedBlocks.get(key) != null){
869                 vec2 = (TreeMap) accessedBlocks.get(key);
870             }
871             else{
872                 vec2 = new TreeMap();
873
874             }
875             GlobalINodeState inodestate = TransactionalFileWrapperFactory.getTateransactionalFileINodeState(key);
876             Iterator it2 = vec2.keySet().iterator();
877           
878             while (it2.hasNext())
879             {
880               
881                 Integer num = (Integer)it2.next();
882                 if (vec2.get(num) != BlockAccessModesEnum.READ)
883                 {
884                   BlockDataStructure blockobj = (BlockDataStructure) inodestate.getBlockDataStructure(num);//lockmap.get(num);
885                     Iterator it4 =  blockobj.getReaders().iterator(); // from here for visible readers strategy
886                 
887                     while (it4.hasNext())
888                     {
889                         
890                         ExtendedTransaction tr = (ExtendedTransaction) it4.next();
891                         if (this != tr)
892                             tr.abort();
893                     }
894                     blockobj.getReaders().clear();
895                     
896                 }
897             }
898         
899         
900         
901                 
902        /*         SortedSet sst = (SortedSet) this.getAccessedBlocks().get(key);
903                 Iterator it3 =  sst.iterator();
904                 while (it3.hasNext()){
905                     Integer num = (Integer)it.next();
906                     BlockDataStructure blockobj = (BlockDataStructure) value.getInodestate().lockmap.get(num);
907                     Iterator it4 =  blockobj.getReaders().iterator(); // from here for visible readers strategy
908                     while (it4.hasNext())
909                     {
910                         ExtendedTransaction tr = (ExtendedTransaction) it3.next();
911                         tr.abort();
912                     }
913
914                 }*/
915             
916         }
917     }
918     
919     public void addPropertyChangeListener(PropertyChangeListener listener){
920         this.changes.addPropertyChangeListener("status",listener);
921     }
922     
923      public void removePropertyChangeListener(PropertyChangeListener listener){
924         this.changes.removePropertyChangeListener("status",listener);
925     }
926
927     public TransactionStatu getOtherSystem() {
928         return memorystate;
929     }
930
931     public void setOtherSystem(TransactionStatu othersystem) {
932         memorystate = othersystem;
933     }
934
935     public Vector getHeldblocklocks() {
936         return heldblocklocks;
937     }
938
939     public void setHeldblocklocks(Vector heldblocklocks) {
940         this.heldblocklocks = heldblocklocks;
941     }
942
943     public Vector getHeldoffsetlocks() {
944         return heldoffsetlocks;
945     }
946
947     public void setHeldoffsetlocks(Vector heldoffsetlocks) {
948         this.heldoffsetlocks = heldoffsetlocks;
949     }
950
951     public void abortThisSystem() {
952         abort();
953     }
954
955     public boolean isCommitted() {
956         if (this.status == Status.COMMITTED)
957             return true;
958             
959          return  false;
960         
961     }
962     
963     
964     
965 }
966      
967     
968    
969
970
971