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