*** empty log message ***
[IRC.git] / Robust / Transactions / TransactionalIO / src / TransactionalIO / core / TransactionalFile.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 import TransactionalIO.Utilities.Conversions;
8 import TransactionalIO.Utilities.Range;
9 import TransactionalIO.exceptions.AbortedException;
10 import TransactionalIO.exceptions.PanicException;
11 import TransactionalIO.benchmarks.benchmark;
12 import TransactionalIO.core.ExtendedTransaction.Status;
13 import TransactionalIO.interfaces.BlockAccessModesEnum;
14 import TransactionalIO.interfaces.OffsetDependency;
15 import com.sun.org.apache.bcel.internal.generic.IFEQ;
16 import java.io.DataOutputStream;
17 import java.io.File;
18 import java.io.FileDescriptor;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.RandomAccessFile;
22 import java.io.UTFDataFormatException;
23 import java.nio.ByteBuffer;
24 import java.util.Collections;
25 import java.util.Iterator;
26 import java.util.TreeMap;
27 import java.util.Vector;
28 import java.util.concurrent.atomic.AtomicBoolean;
29 import java.util.concurrent.locks.Lock;
30
31 import java.util.concurrent.locks.ReentrantLock;
32 import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
33 import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
34 import java.util.logging.Level;
35 import java.util.logging.Logger;
36
37 /**
38  *
39  * @author navid
40  */
41 public class TransactionalFile implements Comparable {
42
43     private native int nativepread(byte buff[], long offset, int size, FileDescriptor fd);
44
45     private native int nativepwrite(byte buff[], long offset, int size, FileDescriptor fd);
46     
47
48     {
49         System.load("/home/navid/libkooni.so");
50     }
51     public RandomAccessFile file;
52     private INode inode;
53     private int sequenceNum = 0;
54     public static int currenSeqNumforInode = 0;
55     /*  public AtomicLong commitedoffset;
56     public AtomicLong commitedfilesize;*/
57     public boolean to_be_created = false;
58     public boolean writemode = false;
59     public boolean appendmode = false;
60     public ReentrantLock offsetlock;
61     private GlobalOffset committedoffset;
62     AtomicBoolean open = new AtomicBoolean(true);
63     
64    
65     private GlobalINodeState inodestate;
66     Lock[] locks;
67
68     public TransactionalFile(File f, String mode) {
69         
70         if ((!(f.exists()))) {
71             to_be_created = true;
72             file = null;
73
74         } else {
75
76             try {
77
78                 offsetlock = new ReentrantLock();
79                 file = new RandomAccessFile(f, mode);
80             } catch (FileNotFoundException ex) {
81
82                 Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex);
83             }
84
85         }
86         inode = TransactionalFileWrapperFactory.getINodefromFileName(f.getAbsolutePath());
87         inodestate = TransactionalFileWrapperFactory.createTransactionalFile(inode, f.getAbsolutePath(), mode);
88
89
90         sequenceNum = inodestate.seqNum;
91         inodestate.seqNum++;
92
93         if (mode.equals("rw")) {
94             writemode = true;
95         } else if (mode.equals("a")) {
96             appendmode = true;
97         }
98
99         if (inodestate != null) {
100             synchronized (inodestate) {
101                 committedoffset = new GlobalOffset(0);
102                 open .set(true);
103                 
104             }
105         }
106     }
107     
108     public boolean isOpen(){
109         return open.get();
110     }
111     
112     public TransactionalFile(String filename, String mode) {
113
114
115         File f = new File(filename);
116
117         if ((!(f.exists()))) {
118             to_be_created = true;
119             file = null;
120
121         } else {
122
123             try {
124
125                 offsetlock = new ReentrantLock();
126                 file = new RandomAccessFile(f, mode);
127             } catch (FileNotFoundException ex) {
128
129                 Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex);
130             }
131
132         }
133    /*     synchronized(this){
134             if (to_be_created){
135                 try {
136                     offsetlock = new ReentrantLock();
137                     file = new RandomAccessFile(filename, mode);
138                     try {
139                         System.out.println("ll " + file.length());
140                     } catch (IOException ex) {
141                         Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex);
142                     }
143                 } catch (FileNotFoundException ex) {
144                     Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex);
145                 }
146             }
147         }*/
148         inode = TransactionalFileWrapperFactory.getINodefromFileName(filename);
149         inodestate = TransactionalFileWrapperFactory.createTransactionalFile(inode, filename, mode);
150         
151         sequenceNum = inodestate.seqNum;
152         inodestate.seqNum++;
153
154         if (mode.equals("rw")) {
155             writemode = true;
156         } else if (mode.equals("a")) {
157             appendmode = true;
158         }
159
160         if (inodestate != null) {
161             synchronized (inodestate) {
162                 committedoffset = new GlobalOffset(0);
163                 open.set(true);
164             }
165         }
166
167
168     }
169
170     private int invokeNativepread(byte buff[], long offset, int size) {
171         try {
172  
173                     
174             return nativepread(buff, offset, size, file.getFD());
175         } catch (IOException ex) {
176
177             Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex);
178             return -1;
179         }
180
181     }
182
183     public int invokeNativepwrite(byte buff[], long offset, int size, RandomAccessFile file) {
184         try {
185             return nativepwrite(buff, offset, buff.length, file.getFD());
186         } catch (IOException ex) {
187
188             Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex);
189             return -1;
190         }
191
192     }
193
194     public int getSequenceNum() {
195         return sequenceNum;
196     }
197
198     public GlobalOffset getCommitedoffset() {
199         return committedoffset;
200     }
201     
202    
203
204
205     public GlobalINodeState getInodestate() {
206         return inodestate;
207     }
208      
209     public INode getInode() {
210         return inode;
211     }
212
213     public void close() throws IOException {
214         ExtendedTransaction me = Wrapper.getTransaction();
215         if (!(open.get()))
216             throw new IOException();
217             
218         if (me == null) {
219             open.set(false);
220             file.close();
221             return;
222         }
223
224         if (!(me.getGlobaltoLocalMappings().containsKey(this))) {
225             me.addFile(this, 0);
226         }
227         
228         TransactionLocalFileAttributes tmp = (TransactionLocalFileAttributes) me.getGlobaltoLocalMappings().get(this);
229         tmp.setOpen(false);
230     }
231     
232     
233     
234     
235     public long length() throws IOException{
236         if (!(open.get()))
237             throw new IOException();
238         ExtendedTransaction me = Wrapper.getTransaction();
239
240         if (me == null) {
241             long length = -1;
242             inodestate.commitedfilesize.lengthlock.lock();
243             length = inodestate.commitedfilesize.getLength();
244             inodestate.commitedfilesize.lengthlock.unlock();
245
246             return length;
247         }
248
249         if (!(me.getGlobaltoLocalMappings().containsKey(this))) {
250             me.addFile(this, 0);
251         }
252         
253         TransactionLocalFileAttributes tmp = (TransactionLocalFileAttributes) me.getGlobaltoLocalMappings().get(this);
254         
255         lockLength(me);
256
257             if (!(this.inodestate.commitedfilesize.getLengthReaders().contains(me))) {
258                 this.inodestate.commitedfilesize.getLengthReaders().add(me);
259             }
260         
261            tmp.setLocalsize(this.inodestate.commitedfilesize.getLength());
262            tmp.lenght_read = true;
263         
264         this.inodestate.commitedfilesize.lengthlock.unlock();
265
266         return tmp.getLocalsize();
267     }
268     public long getFilePointer() throws IOException {
269         if (!(open.get()))
270             throw new IOException();
271         ExtendedTransaction me = Wrapper.getTransaction();
272
273         if (me == null) {
274             return non_Transactional_getFilePointer();
275         }
276
277         if (!(me.getGlobaltoLocalMappings().containsKey(this))) {
278             me.addFile(this, 0);
279         }
280
281         TransactionLocalFileAttributes tmp = (TransactionLocalFileAttributes) me.getGlobaltoLocalMappings().get(this);
282         if ((tmp.getOffsetdependency() == OffsetDependency.WRITE_DEPENDENCY_1) || (tmp.getOffsetdependency() == OffsetDependency.NO_ACCESS)) {
283             tmp.setOffsetdependency(OffsetDependency.READ_DEPENDENCY);
284
285             long target;
286             lockOffset(me);
287
288             if (!(this.committedoffset.getOffsetReaders().contains(me))) {
289                 this.committedoffset.getOffsetReaders().add(me);
290             }
291
292             tmp.setLocaloffset(tmp.getLocaloffset() + this.committedoffset.getOffsetnumber() - tmp.getCopylocaloffset());
293             target = this.committedoffset.getOffsetnumber() - tmp.getCopylocaloffset();
294
295             offsetlock.unlock();
296
297             Iterator it;
298
299             if ((me.getWriteBuffer().get(inode)) != null) {
300
301                 it = ((Vector) (me.getWriteBuffer().get(inode))).iterator();
302                 while (it.hasNext()) {
303                     WriteOperations wrp = (WriteOperations) it.next();
304                     if (wrp.getBelongingto() == tmp && wrp.isUnknownoffset()) {
305                         wrp.setUnknownoffset(false);
306                     }
307                     wrp.getRange().setStart(target + wrp.getRange().getStart());
308                     wrp.getRange().setEnd(target + wrp.getRange().getEnd());
309                 }
310             }
311
312         }
313
314
315         tmp.setUnknown_inital_offset_for_write(false);
316         return tmp.getLocaloffset();
317     }
318
319     public void force(){
320         
321     }
322     
323     public void seek(long offset) throws IOException {
324         
325         if (!(open.get()))
326             throw new IOException();
327         ExtendedTransaction me = Wrapper.getTransaction();
328
329         if (me == null) {
330             non_Transactional_Seek(offset);
331             return;
332         }
333
334         if (!(me.getGlobaltoLocalMappings().containsKey(this))) {
335             me.addFile(this, offset);
336         }
337
338         TransactionLocalFileAttributes tmp = (TransactionLocalFileAttributes) me.getGlobaltoLocalMappings().get(this);
339
340         if (tmp.getOffsetdependency() == OffsetDependency.NO_ACCESS) {
341             tmp.setOffsetdependency(OffsetDependency.NO_DEPENDENCY);
342         } else if (tmp.getOffsetdependency() == OffsetDependency.WRITE_DEPENDENCY_1) {
343             tmp.setOffsetdependency(OffsetDependency.WRITE_DEPENDENCY_2);
344         }
345         tmp.setUnknown_inital_offset_for_write(false);
346         tmp.setLocaloffset(offset);
347
348     }
349     
350     
351      public int skipBytes(int n) throws IOException {
352         long pos;
353         long len;
354         long newpos;
355
356         if (n <= 0) {
357             return 0;
358         }
359         pos = getFilePointer();
360        // len = length();
361         newpos = pos + n;
362       //  if (newpos > len) {
363      //       newpos = len;
364       //  }
365         seek(newpos);
366
367          /* return the actual number of bytes skipped */
368         return (int) (newpos - pos);
369     }
370
371     public final byte readByte() throws IOException{
372         byte[] data = new byte[1];
373         read(data);
374         byte result = (byte)(data[0]);
375         return result;
376     }
377     
378     public final boolean readBoolean() throws IOException{
379         byte[] data = new byte[1];
380         read(data);
381         if (data[0] == 0 )
382             return false;
383         return true;
384         //return ((boolean)data[0]);// != 0);
385     }
386     
387     public final char readChar() throws IOException{
388         byte[] data = new byte[2];
389         read(data);
390         char result = (char)((data[0] << 8) | data[0]);
391         return result;
392     }
393     
394     public final short readShort() throws IOException{
395         byte[] data = new byte[2];
396         read(data);
397         short result = (short)((data[0] << 8) | data[1]);
398      //   System.out.println("res " + result);
399         return result;
400     }
401     
402     public final int readUnsignedShort() throws IOException {
403         byte[] data = new byte[2];
404         read(data);
405         return (data[0] << 8) + (data[1] << 0);    
406     }
407     
408     public final String readUTF() throws UTFDataFormatException, IOException{
409             int utflen = -1;
410             byte[] bytearr = null;
411             char[] chararr = null;
412         try {
413             utflen = readUnsignedShort();
414         } catch (IOException ex) {
415             Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex);
416         }
417             bytearr = new byte[utflen];
418             chararr = new char[utflen];
419             
420
421                 int c, char2, char3;
422                 int count = 0;
423                 int chararr_count = 0;
424                // System.out.println("size " +bytearr);
425                 read(bytearr);
426
427                 while (count < utflen) {
428                     c = (int) bytearr[count] & 0xff;
429                     if (c > 127)
430                         break;
431                     count++;
432                     chararr[chararr_count++] = (char) c;
433                 }
434
435                 while (count < utflen) {
436                     c = (int) bytearr[count] & 0xff;
437                     switch (c >> 4) {
438                     case 0:
439                     case 1:
440                     case 2:
441                     case 3:
442                     case 4:
443                     case 5:
444                     case 6:
445                     case 7:
446                         /* 0xxxxxxx*/
447                         count++;
448                         chararr[chararr_count++] = (char) c;
449                         break;
450                     case 12:
451                     case 13:
452                         /* 110x xxxx   10xx xxxx*/
453                         count += 2;
454                         if (count > utflen)
455                             throw new UTFDataFormatException(
456                                     "malformed input: partial character at end");
457                         char2 = (int) bytearr[count - 1];
458                         if ((char2 & 0xC0) != 0x80)
459                             throw new UTFDataFormatException(
460                                     "malformed input around byte " + count);
461                         chararr[chararr_count++] = (char) (((c & 0x1F) << 6) | (char2 & 0x3F));
462                         break;
463                     case 14:
464                         /* 1110 xxxx  10xx xxxx  10xx xxxx */
465                         count += 3;
466                         if (count > utflen)
467                             throw new UTFDataFormatException(
468                                     "malformed input: partial character at end");
469                         char2 = (int) bytearr[count - 2];
470                         char3 = (int) bytearr[count - 1];
471                         if (((char2 & 0xC0) != 0x80)
472                                 || ((char3 & 0xC0) != 0x80))
473                             throw new UTFDataFormatException(
474                                     "malformed input around byte "
475                                             + (count - 1));
476                         chararr[chararr_count++] = (char) (((c & 0x0F) << 12)
477                                 | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
478                         break;
479                     default:
480                         /* 10xx xxxx,  1111 xxxx */
481                         throw new UTFDataFormatException(
482                                "malformed input around byte " + count);
483                    }
484               }
485              // The number of chars produced may be less than utflen
486              return new String(chararr, 0, chararr_count);
487         
488     }
489     
490     public final float readFloat() throws IOException{
491        // byte[] data = new byte[4];
492        // int k = read(data);
493         return Float.intBitsToFloat(readInt());
494        // float result = Conversions.bytes2float(data);
495         //int result = (data[0] << 24) | (data[1] << 16) + (data[2] << 8) + (data[3]<<0);
496       //  System.out.println("int res " + result);
497       //  return result;
498     }
499     
500     public final double readDouble() throws IOException{
501         return Double.longBitsToDouble(readLong());
502     }
503     
504     
505     public final int readInt() throws IOException{
506         byte[] data = new byte[4];
507         int k = read(data);
508         
509         int result = Conversions.bytes2int(data);
510         //int result = (data[0] << 24) | (data[1] << 16) + (data[2] << 8) + (data[3]<<0);
511       //  System.out.println("int res " + result);
512         return result;
513     }
514     
515     public final long readLong() throws IOException{
516         //long result = ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
517         byte[] data = new byte[8];
518         read(data);
519         //long result = ((long)data[0] << 56) + ((long)data[1] << 48) + ((long)data[2] << 40) + ((long)data[3] << 32) + ((long)data[4] << 24) + ((long)data[5] << 16)+ ((long)data[6] << 8) + data[7];
520         long result = Conversions.bytes2long(data);
521     //    System.out.println("long res " + result);
522         return result;
523     }
524     
525     
526     public final void writeByte(int b){
527         try{
528             byte[] result = new byte[1];
529             result[0] = (byte)b;
530             write(result);
531         }catch(IOException ex){
532             Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex);
533         }
534         
535     }
536     
537     public final void writeChar(int value){
538         try{
539             byte[] result = new byte[2];
540             result[0] = (byte)(value >> 8);
541             result[1] = (byte)(value);
542             write(result);
543         }catch(IOException ex){
544             Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex);
545         }
546         
547     }
548       
549       
550         public final void writeShort(int value){
551         try{
552             byte[] result = new byte[2];
553             result[0] = (byte)(value >> 8);
554             result[1] = (byte)(value);
555             write(result);
556         }catch(IOException ex){
557             Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex);
558         }
559         
560     }
561     
562     public final void writeInt(int value){
563         try {
564             byte[] result = new byte[4];
565             result[0] = (byte) (value >>> 24 & 0xFF);
566             result[1] = (byte) (value >>> 16 & 0xFF);
567             result[2] = (byte) (value >>> 8 & 0xFF);
568             result[3] = (byte) (value);
569             write(result);
570         } catch (IOException ex) {
571             Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex);
572         }
573
574     }
575     
576     public final void writeFloat(Float v){
577         writeInt(Float.floatToIntBits(v));
578     }
579     
580      public final void writeLong(long value){
581         try {
582             byte[] result = new byte[8];
583             result[0] = (byte)(value >>> 56);
584             result[1] = (byte)(value >>> 48);
585             result[2] = (byte)(value >>> 40);
586             result[3] = (byte)(value >>> 32);
587             result[4] = (byte)(value >>> 24);
588             result[5] = (byte)(value >>> 16);
589             result[6] = (byte)(value >>> 8);
590             result[7] = (byte)(value);
591             write(result);
592         } catch (IOException ex) {
593             Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex);
594         }
595
596     }
597      
598      public final void writeDouble(Double v) {
599         writeLong(Double.doubleToLongBits(v));
600      }
601      
602      public final void writeBoolean(boolean v){
603           writeByte(v ? 1 : 0);
604      }
605      
606      public final void writeChars(String s) throws IOException {
607             int clen = s.length();
608             int blen = 2*clen;
609             byte[] b = new byte[blen];
610             char[] c = new char[clen];
611             s.getChars(0, clen, c, 0);
612             for (int i = 0, j = 0; i < clen; i++) {
613               b[j++] = (byte)(c[i] >>> 8);
614               b[j++] = (byte)(c[i] >>> 0);
615             }
616             write(b);
617     }
618      
619     public final int writeUTF(String str) throws UTFDataFormatException{
620         int strlen = str.length();
621         int utflen = 0;
622         int c, count = 0;
623             /* use charAt instead of copying String to char array */
624         for (int i = 0; i < strlen; i++) {
625             c = str.charAt(i);
626             if ((c >= 0x0001) && (c <= 0x007F)) {
627                 utflen++;
628             } else if (c > 0x07FF) {
629                 utflen += 3;
630             } else {
631                 utflen += 2;
632             }
633         }
634
635         if (utflen > 65535)
636               throw new UTFDataFormatException(
637                   "encoded string too long: " + utflen + " bytes");
638
639                byte[] bytearr = null;
640                 
641                bytearr = new byte[utflen + 2];
642                 
643
644                 bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
645                 bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);
646
647                 int i = 0;
648                for (i = 0; i < strlen; i++) {
649                     c = str.charAt(i);
650                     if (!((c >= 0x0001) && (c <= 0x007F)))
651                        break;
652                     bytearr[count++] = (byte) c;
653                 }
654
655                 for (; i < strlen; i++) {
656                     c = str.charAt(i);
657                     if ((c >= 0x0001) && (c <= 0x007F)) {
658                         bytearr[count++] = (byte) c;
659
660                     } else if (c > 0x07FF) {
661                         bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
662                         bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
663                         bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
664                     } else {
665                         bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
666                         bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
667                    }
668                }
669         try {
670             write(bytearr);
671         } catch (IOException ex) {
672             Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex);
673         }
674                //write(bytearr, 0, utflen + 2);
675                return utflen + 2;
676     }
677     
678     public int read(byte[] b) throws IOException {
679
680           if (!(open.get()))
681             throw new IOException();
682           
683         ExtendedTransaction me = Wrapper.getTransaction();
684         int size = b.length;
685         int result = 0;
686         if (me == null) {  // not a transaction, but any I/O operation even though within a non-transaction is considered a single opertion transactiion 
687             return non_Transactional_Read(b);
688         }
689
690         if (!(me.getGlobaltoLocalMappings().containsKey(this))) { // if this is the first time the file is accessed by the transcation
691             me.addFile(this, 0);
692         }
693
694
695         TransactionLocalFileAttributes tmp = (TransactionLocalFileAttributes) me.getGlobaltoLocalMappings().get(this);
696         tmp.setUnknown_inital_offset_for_write(false);
697
698         OffsetDependency dep = tmp.getOffsetdependency();
699         if ((dep == OffsetDependency.WRITE_DEPENDENCY_1) ||
700                 (dep == OffsetDependency.NO_ACCESS) ||
701                 (dep == OffsetDependency.WRITE_DEPENDENCY_2)) {
702             tmp.setOffsetdependency(OffsetDependency.READ_DEPENDENCY);
703             lockOffset(me);
704
705             if (dep != OffsetDependency.WRITE_DEPENDENCY_2) {
706                 tmp.setLocaloffset(tmp.getLocaloffset() + this.committedoffset.getOffsetnumber() - tmp.getCopylocaloffset());
707             }
708
709             if (!(this.committedoffset.getOffsetReaders().contains(me))) {
710                 this.committedoffset.getOffsetReaders().add(me);
711
712             }
713
714             offsetlock.unlock();
715         }
716
717         if (me.getWriteBuffer().get(inode) != null) {
718             makeWritestDependent(me);
719         }
720         if ((Boolean) me.merge_for_writes_done.get(inode) == Boolean.FALSE) {
721             mergeWrittenData(me);
722         }
723
724         long loffset = tmp.getLocaloffset();
725         markAccessedBlocks(me, loffset, size, BlockAccessModesEnum.READ);
726
727
728         Vector writebuffer = null;
729         if ((me.getWriteBuffer().get(this.inode)) != null) {
730             writebuffer = (Vector) (me.getWriteBuffer().get(this.inode));
731
732         } else {
733             result = readFromFile(me, b, tmp);
734             return result;
735         //writebuffer = new Vector();
736         //me.getWriteBuffer().put(this.inode, writebuffer);
737
738         }
739         Range readrange = new Range(loffset, loffset + size);
740         Range writerange = null;
741         Range[] intersectedrange = new Range[writebuffer.size()];
742         WriteOperations[] markedwriteop = new WriteOperations[writebuffer.size()];
743
744         int counter = 0;
745         boolean in_local_buffer = false;
746
747
748         Iterator it = writebuffer.iterator();
749         while (it.hasNext()) {
750
751             WriteOperations wrp = (WriteOperations) it.next();
752             writerange = wrp.getRange();
753             if (writerange.includes(readrange)) {
754                 markedwriteop[counter] = wrp;
755                 in_local_buffer = true;
756                 break;
757             }
758
759             if (writerange.hasIntersection(readrange)) {
760                 intersectedrange[counter] = readrange.intersection(writerange);
761                 markedwriteop[counter] = wrp;
762
763                 counter++;
764             }
765         }
766
767         if (in_local_buffer) { // the read one from local buffer
768
769             result = readFromBuffer(b, tmp, markedwriteop[counter], writerange);
770             return result;
771
772         } else {
773
774             if (counter == 0) { // all the read straight from file
775
776                 result = readFromFile(me, b, tmp);
777             } else {    // some parts from file others from buffer
778
779                 for (int i = 0; i < counter; i++) {
780                     Byte[] data = markedwriteop[i].getData();
781                     byte[] copydata = new byte[data.length];
782                     for (int j = 0; j < data.length; j++) {
783                         copydata[j] = data[j].byteValue();
784                     }
785                     System.arraycopy(copydata, (int) (intersectedrange[i].getStart() - markedwriteop[i].getRange().getStart()), b, (int) (intersectedrange[i].getStart() - readrange.getStart()), (int) (Math.min(intersectedrange[i].getEnd(), readrange.getEnd()) - intersectedrange[i].getStart()));
786                     result += Math.min(intersectedrange[i].getEnd(), readrange.getEnd()) - intersectedrange[i].getStart();
787                 }
788
789                 Range[] non_intersected_ranges = readrange.minus(intersectedrange, counter);
790                 Vector occupiedblocks = new Vector();
791                 for (int i = 0; i < non_intersected_ranges.length; i++) {
792                     int st = FileBlockManager.getCurrentFragmentIndexofTheFile(non_intersected_ranges[i].getStart());
793                     int en = FileBlockManager.getCurrentFragmentIndexofTheFile(non_intersected_ranges[i].getEnd());
794                     for (int j = st; j <= en; j++) {
795                         if (!(occupiedblocks.contains(Integer.valueOf(j)))) {
796                             occupiedblocks.add(Integer.valueOf(j));
797                         }
798                     }
799                 }
800
801                 //  lockOffset(me);
802
803                 me.getHeldoffsetlocks().add(offsetlock);
804                 BlockDataStructure block;
805                 int k;
806                 for (k = 0; k < occupiedblocks.size() && me.getStatus() == Status.ACTIVE; k++) {   // locking the block locks
807                         block = this.inodestate.getBlockDataStructure((Integer) (occupiedblocks.get(k)));//(BlockDataStructure) tmp.adapter.lockmap.get(Integer.valueOf(k)));
808
809                         block.getLock().readLock().lock();
810                         if (!(block.getReaders().contains(me))) {
811                             block.getReaders().add(me);
812                         }
813
814                }
815                if (k<occupiedblocks.size()){ 
816                     for (int i = 0; i <k; i++) {
817                             block = this.inodestate.getBlockDataStructure((Integer) (occupiedblocks.get(k)));
818                             me.getHeldblocklocks().add(block.getLock().readLock());
819                     }    
820                     throw new AbortedException();
821                }
822                 
823
824                 for (int i = 0; i < non_intersected_ranges.length; i++) {
825
826                     int sizetoread = (int) (non_intersected_ranges[i].getEnd() - non_intersected_ranges[i].getStart());
827                     byte[] tmpdt = new byte[(int) (non_intersected_ranges[i].getEnd() - non_intersected_ranges[i].getStart())];
828                     int tmpsize = invokeNativepread(tmpdt, non_intersected_ranges[i].getStart(), sizetoread);
829                     System.arraycopy(tmpdt, 0, b, (int) (non_intersected_ranges[i].getStart() - readrange.getStart()), sizetoread);
830                     //file.seek(non_intersected_ranges[i].getStart());
831                     //int tmpsize = file.read(b, (int) (non_intersected_ranges[i].getStart() - readrange.getStart()), (int) (non_intersected_ranges[i].getEnd() - non_intersected_ranges[i].getStart()));
832                     result += tmpsize;
833
834                 }
835
836                 if (me.getStatus() == Status.ABORTED) {
837                     for (k = 0; k < occupiedblocks.size(); k++) {
838                         block = this.inodestate.getBlockDataStructure((Integer) (occupiedblocks.get(k)));
839                         me.getHeldblocklocks().add(block.getLock().readLock());
840                     }
841                     throw new AbortedException();
842                 }
843                 for (k = 0; k < occupiedblocks.size(); k++) {
844                     block = this.inodestate.getBlockDataStructure((Integer) (occupiedblocks.get(k)));
845                     block.getLock().readLock().unlock();
846                 }
847                 // offsetlock.unlock();
848                 tmp.setLocaloffset(tmp.getLocaloffset() + result);
849             }
850
851             return result;
852         }
853
854     }
855
856     public void write(byte[] data) throws IOException {
857       if (!(open.get()))
858             throw new IOException();
859         if (!(writemode)) {
860             throw new IOException();
861
862         }
863
864         ExtendedTransaction me = Wrapper.getTransaction();
865         int size = data.length;
866
867
868         if (me == null) // not a transaction 
869         {
870
871             non_Transactional_Write(data);
872             return;
873         }
874
875         if (!(me.getGlobaltoLocalMappings().containsKey(this))) {
876             me.addFile(this, 0);
877         }
878
879         //  if (me.getGlobaltoLocalMappings().containsKey(this)) // 
880         //   {
881
882
883         Byte[] by = new Byte[size];
884         for (int i = 0; i < size; i++) {
885             by[i] = Byte.valueOf(data[i]);
886         }
887         TransactionLocalFileAttributes tmp = ((TransactionLocalFileAttributes) (me.getGlobaltoLocalMappings().get(this)));
888
889         Vector dummy;
890         if (((Vector) (me.getWriteBuffer().get(this.inode))) != null) {
891             dummy = new Vector((Vector) (me.getWriteBuffer().get(this.inode)));
892         } else {
893             dummy = new Vector();
894         }
895         dummy.add(new WriteOperations(by, new Range(tmp.getLocaloffset(), tmp.getLocaloffset() + by.length), tmp.isUnknown_inital_offset_for_write(), this, tmp));
896         me.getWriteBuffer().put(this.inode, dummy);
897
898         long loffset = tmp.getLocaloffset();
899
900
901         tmp.setLocaloffset(tmp.getLocaloffset() + by.length);
902         
903         if (tmp.getLocaloffset() > tmp.getLocalsize())
904             tmp.setLocalsize(tmp.getLocaloffset());
905
906         me.merge_for_writes_done.put(inode, Boolean.FALSE);
907
908         if (!(tmp.isUnknown_inital_offset_for_write())) {
909             markAccessedBlocks(me, loffset, size, BlockAccessModesEnum.WRITE);
910
911         }
912         if (tmp.getOffsetdependency() == OffsetDependency.NO_ACCESS) {
913             tmp.offsetdependency = OffsetDependency.WRITE_DEPENDENCY_1;
914         }
915     }
916
917     private void markAccessedBlocks(ExtendedTransaction me, long loffset, int size, BlockAccessModesEnum mode) {
918
919
920         TreeMap map;
921
922         if (me.getAccessedBlocks().get(this.getInode()) != null) {
923             map = (TreeMap) me.getAccessedBlocks().get(this.getInode());
924         } else {
925             map = new TreeMap();
926             me.getAccessedBlocks().put(this.inode, map);
927         }
928         int startblock = (int) ((loffset / Defaults.FILEFRAGMENTSIZE));//FileBlockManager.getCurrentFragmentIndexofTheFile(loffset);
929         int targetblock = (int) (((size + loffset) / Defaults.FILEFRAGMENTSIZE));//FileBlockManager.getTargetFragmentIndexofTheFile(loffset, size);
930         for (int i = startblock; i <= targetblock; i++) {
931             if (map.get(Integer.valueOf(i)) == null) {
932                   map.put(Integer.valueOf(i), mode);
933             } else if (map.get(Integer.valueOf(i)) != mode) {
934                   map.put(Integer.valueOf(i), BlockAccessModesEnum.READ_WRITE);
935             }
936         }
937     }
938
939     private int readFromFile(ExtendedTransaction me, byte[] readdata, TransactionLocalFileAttributes tmp) {
940 //Inline these method calls for performance...
941 //int st = FileBlockManager.getCurrentFragmentIndexofTheFile(tmp.getLocaloffset());//(int) ((tmp.getLocaloffset() / Defaults.FILEFRAGMENTSIZE));//
942 //int end = FileBlockManager.getTargetFragmentIndexofTheFile(tmp.getLocaloffset(), readdata.length);//(int) (((tmp.getLocaloffset() + readdata.length) / Defaults.FILEFRAGMENTSIZE));
943         int st = (int) ((tmp.getLocaloffset() / Defaults.FILEFRAGMENTSIZE));
944         int end =(int) (((tmp.getLocaloffset() + readdata.length) / Defaults.FILEFRAGMENTSIZE));
945
946         BlockDataStructure block = null;
947         
948         Lock[] locks = new Lock[end -st +1];
949         
950         int k;
951         //int cou = st;
952
953         for (k = st; k <= end /*&& me.getStatus() == Status.ACTIVE*/; k++) {
954             block = this.inodestate.getBlockDataStructure(Integer.valueOf(k));
955             block.getLock().readLock().lock();
956             
957           //  locks[k-st] = block.getLock().readLock();
958             if (!(block.getReaders().contains(me))) {
959                 block.getReaders().add(me);
960             }
961         }
962
963         //Optimization here...not actually needed...may be worth checking
964         //whether this improves performance
965         if (k<=end) {
966             //We aborted here if k is less than or equal to end
967             me.blockcount = k - st;
968             for (int i = st; i < k; i++) {
969                // block = this.inodestate.getBlockDataStructure(Integer.valueOf(i));
970                me.getHeldblocklocks().add(block.getLock().readLock());
971                 //me.toholdblocklocks[i-st] = this.inodestate.getBlockDataStructure(Integer.valueOf(i)).getLock().readLock();
972                // me.getHeldblocklocks().add(locks[i-st]);
973             }
974             throw new AbortedException();
975         }
976
977         //Do the read
978         int size = -1;
979         size = invokeNativepread(readdata, tmp.getLocaloffset(), readdata.length);
980         tmp.setLocaloffset(tmp.getLocaloffset() + size);
981         
982         //Handle EOF
983         if (size == 0) {
984             size = -1;
985         }
986
987         //Needed to make sure that transaction only sees consistent data
988         if (me.getStatus() == Status.ABORTED) {
989             me.blockcount = end - st + 1;
990             for (int i = st; i <= end; i++) {
991                 block = this.inodestate.getBlockDataStructure(Integer.valueOf(i));
992                // me.toholdblocklocks[i-st] = this.inodestate.getBlockDataStructure(Integer.valueOf(i)).getLock().readLock();
993                 me.getHeldblocklocks().add(block.getLock().readLock());
994               //  me.getHeldblocklocks().add(locks[i-st]);
995             }
996             throw new AbortedException();
997         }
998
999         //unlock the locks
1000         for (k = st; k <= end; k++) {
1001             block = this.inodestate.getBlockDataStructure(Integer.valueOf(k));
1002             block.getLock().readLock().unlock();
1003             //locks[k-st].unlock();
1004         }
1005         return size;
1006     }
1007
1008     private int readFromBuffer(byte[] readdata, TransactionLocalFileAttributes tmp, WriteOperations wrp, Range writerange) {
1009         long loffset = tmp.getLocaloffset();
1010
1011         Byte[] data = (Byte[]) wrp.getData();
1012         byte[] copydata = new byte[data.length];
1013
1014         for (int i = 0; i < data.length; i++) {
1015             copydata[i] = data[i].byteValue();
1016         }
1017         System.arraycopy(copydata, (int) (loffset - writerange.getStart()), readdata, 0, readdata.length);
1018         tmp.setLocaloffset(tmp.getLocaloffset() + readdata.length);
1019         return readdata.length;
1020
1021     }
1022
1023     public void simpleWritetoBuffer(Byte[] data, Range newwriterange, TreeMap tm) {
1024         tm.put(newwriterange, data);
1025     }
1026
1027     public void unlockLocks(Vector heldlocks) {
1028         for (int i = 0; i < heldlocks.size(); i++) {
1029             ((Lock) heldlocks.get(i)).unlock();
1030         }
1031     }
1032
1033     public void setInode(INode inode) {
1034         this.inode = inode;
1035     }
1036
1037     public void lockOffset(ExtendedTransaction me) {
1038         boolean locked = false;
1039         if (me.getStatus() == Status.ACTIVE) {                        //locking the offset
1040             offsetlock.lock();
1041             locked = true;
1042         }
1043
1044         if (me.getStatus() != Status.ACTIVE) {
1045             if (locked) {
1046                 me.getHeldoffsetlocks().add(offsetlock);
1047             }
1048             throw new AbortedException();
1049         }
1050
1051     }
1052
1053      public void lockLength(ExtendedTransaction me) {
1054         boolean locked = false;
1055         if (me.getStatus() == Status.ACTIVE) {                        //locking the offset
1056
1057             this.inodestate.commitedfilesize.lengthlock.lock();
1058             locked = true;
1059         }
1060
1061         if (me.getStatus() != Status.ACTIVE) {
1062             if (locked) {
1063                 me.getHeldlengthlocks().add(this.inodestate.commitedfilesize.lengthlock);
1064             }
1065             throw new AbortedException();
1066         }
1067
1068     }
1069     
1070     public void mergeWrittenData(ExtendedTransaction me/*TreeMap target, byte[] data, Range to_be_merged_data_range*/) {
1071
1072         boolean flag = false;
1073         Vector vec = (Vector) me.getWriteBuffer().get(this.inode);
1074         Range intersectedrange = new Range(0, 0);
1075         Iterator it1 = vec.iterator();
1076         WriteOperations wrp;
1077         WriteOperations wrp2;
1078         Vector toberemoved = new Vector();
1079         while (it1.hasNext()) {
1080             wrp = (WriteOperations) (it1.next());
1081
1082             if (toberemoved.contains(wrp)) {
1083                 continue;
1084             }
1085
1086             Iterator it2 = vec.listIterator();
1087             while (it2.hasNext()) {
1088                 flag = false;
1089                 wrp2 = (WriteOperations) (it2.next());
1090
1091                 if ((wrp2 == wrp) || toberemoved.contains(wrp2)) {
1092                     continue;
1093                 }
1094
1095                 if (wrp.getRange().hasIntersection(wrp2.getRange())) {
1096                     flag = true;
1097                     intersectedrange = wrp2.getRange().intersection(wrp.getRange());
1098                     toberemoved.add(wrp2);
1099                 }
1100
1101
1102                 long startprefix = 0;
1103                 long endsuffix = 0;
1104                 long startsuffix = 0;
1105                 int prefixsize = 0;
1106                 int suffixsize = 0;
1107                 int intermediatesize = 0;
1108                 Byte[] prefixdata = null;
1109                 Byte[] suffixdata = null;
1110                 boolean prefix = false;
1111                 boolean suffix = false;
1112                 if (flag) {
1113
1114
1115                     if (wrp.getRange().getStart() < wrp2.getRange().getStart()) {
1116                         prefixdata = new Byte[(int) (wrp2.getRange().getStart() - wrp.getRange().getStart())];
1117                         prefixdata = (Byte[]) (wrp.getData());
1118                         startprefix = wrp.getRange().getStart();
1119                         prefixsize = (int) (intersectedrange.getStart() - startprefix);
1120                         intermediatesize = (int) (intersectedrange.getEnd() - intersectedrange.getStart());
1121                         prefix = true;
1122                     } else if (wrp2.getRange().getStart() <= wrp.getRange().getStart()) {
1123                         prefixdata = new Byte[(int) (wrp.getRange().getStart() - wrp2.getRange().getStart())];
1124                         prefixdata = (Byte[]) (wrp2.getData());
1125                         startprefix = wrp2.getRange().getStart();
1126                         prefixsize = (int) (intersectedrange.getStart() - startprefix);
1127                         intermediatesize = (int) (intersectedrange.getEnd() - intersectedrange.getStart());
1128                         prefix = true;
1129                     }
1130
1131                     if (wrp2.getRange().getEnd() >= wrp.getRange().getEnd()) {
1132
1133                         suffixdata = new Byte[(int) (wrp2.getRange().getEnd() - intersectedrange.getEnd())];
1134                         suffixdata = (Byte[]) (wrp2.getData());
1135                         startsuffix = intersectedrange.getEnd() - wrp2.getRange().getStart();
1136                         suffixsize = (int) (wrp2.getRange().getEnd() - intersectedrange.getEnd());
1137                         suffix = true;
1138                         endsuffix = wrp2.getRange().getEnd();
1139                     } else if (wrp.getRange().getEnd() > wrp2.getRange().getEnd()) {
1140                         suffixdata = new Byte[(int) (wrp.getRange().getEnd() - intersectedrange.getEnd())];
1141                         suffixdata = (Byte[]) (wrp.getData());
1142                         startsuffix = intersectedrange.getEnd() - wrp.getRange().getStart();
1143                         suffixsize = (int) (wrp.getRange().getEnd() - intersectedrange.getEnd());
1144                         endsuffix = wrp.getRange().getEnd();
1145                         suffix = true;
1146
1147                     }
1148
1149                     Byte[] data_to_insert;
1150
1151                     if ((prefix) && (suffix)) {
1152                         data_to_insert = new Byte[(int) (endsuffix - startprefix)];
1153                         System.arraycopy(prefixdata, 0, data_to_insert, 0, prefixsize);
1154                         System.arraycopy(wrp2.getData(), (int) (intersectedrange.getStart() - wrp2.getRange().getStart()), data_to_insert, prefixsize, intermediatesize);
1155                         System.arraycopy(suffixdata, (int) startsuffix, data_to_insert, (prefixsize + intermediatesize), suffixsize);
1156                         wrp.setData(data_to_insert);
1157                         wrp.setRange(new Range(startprefix, endsuffix));
1158                     }
1159
1160                 }
1161             }
1162         }
1163         Iterator it = toberemoved.iterator();
1164         while (it.hasNext()) {
1165             vec.remove(it.next());
1166         }
1167         toberemoved.clear();
1168         Collections.sort(vec);
1169         me.merge_for_writes_done.put(inode, Boolean.TRUE);
1170
1171     }
1172
1173     public void non_Transactional_Write(byte[] data) {
1174
1175         Vector heldlocks = new Vector();
1176         offsetlock.lock();
1177         int startblock = FileBlockManager.getCurrentFragmentIndexofTheFile(committedoffset.getOffsetnumber());
1178         int targetblock = FileBlockManager.getTargetFragmentIndexofTheFile(committedoffset.getOffsetnumber(), data.length);
1179         
1180         
1181         WriteLock[] blocksar;
1182         blocksar = new WriteLock[targetblock-startblock+1];
1183         for (int i = startblock; i <= targetblock; i++) {
1184             BlockDataStructure block = this.inodestate.getBlockDataStructure(i);
1185             block.getLock().writeLock().lock();
1186             blocksar[i-startblock] = block.getLock().writeLock();
1187             //heldlocks.add(block.getLock().writeLock());
1188         }
1189
1190         try {
1191             ExtendedTransaction.invokeNativepwrite(data, committedoffset.getOffsetnumber(), data.length, file);
1192             //file.seek(committedoffset.getOffsetnumber());
1193             //file.write(data);
1194             committedoffset.setOffsetnumber(committedoffset.getOffsetnumber() + data.length);
1195
1196         } finally {
1197          //   unlockLocks(heldlocks);
1198             for (int i = startblock; i <= targetblock; i++) {
1199                 blocksar[i-startblock].unlock();
1200             }
1201             offsetlock.unlock();
1202         }
1203     }
1204
1205     public int non_Transactional_Read(byte[] b) {
1206         int size = -1;
1207
1208         
1209         offsetlock.lock();
1210         
1211         int startblock;
1212         int targetblock;
1213         startblock = FileBlockManager.getCurrentFragmentIndexofTheFile(committedoffset.getOffsetnumber());
1214         targetblock = FileBlockManager.getTargetFragmentIndexofTheFile(committedoffset.getOffsetnumber(), size);
1215
1216         ReadLock[] blocksar;
1217         blocksar = new ReadLock[targetblock-startblock+1];
1218         
1219         for (int i = startblock; i <= targetblock; i++) {
1220             BlockDataStructure block = this.inodestate.getBlockDataStructure(i);
1221             block.getLock().readLock().lock();
1222             blocksar[i-startblock] = block.getLock().readLock();
1223         }
1224
1225         size = invokeNativepread(b, committedoffset.getOffsetnumber(), b.length);
1226         committedoffset.setOffsetnumber(committedoffset.getOffsetnumber() + size);
1227         if (!(committedoffset.getOffsetReaders().isEmpty())) {
1228             Iterator it2 = committedoffset.getOffsetReaders().iterator(); // for visible readers strategy
1229
1230             while (it2.hasNext()) {
1231                 ExtendedTransaction tr = (ExtendedTransaction) it2.next();
1232                 tr.abort();
1233             }
1234             committedoffset.getOffsetReaders().clear();
1235         }
1236         
1237         for (int i = startblock; i <= targetblock; i++) {
1238             blocksar[i-startblock].unlock();
1239         }
1240         
1241         //unlockLocks(heldlocks);
1242         offsetlock.unlock();
1243         if (size == 0) {
1244             size = -1;
1245         }
1246         return size;
1247
1248     }
1249
1250     public void non_Transactional_Seek(long offset) {
1251         offsetlock.lock();
1252         committedoffset.setOffsetnumber(offset);
1253         offsetlock.unlock();
1254     }
1255
1256     public long non_Transactional_getFilePointer() {
1257         long offset = -1;
1258
1259         offsetlock.lock();
1260         offset = committedoffset.getOffsetnumber();
1261         offsetlock.unlock();
1262
1263         return offset;
1264     }
1265
1266     public int compareTo(Object arg0) {
1267         TransactionalFile tf = (TransactionalFile) arg0;
1268         if (this.inode.getNumber() < tf.inode.getNumber()) {
1269             return -1;
1270         } else if (this.inode.getNumber() > tf.inode.getNumber()) {
1271             return 1;
1272         } else {
1273             if (this.sequenceNum < tf.sequenceNum) {
1274                 return -1;
1275             } else {
1276                 return 1;
1277             }
1278         }
1279     }
1280
1281     public void makeWritestDependent(ExtendedTransaction me) {// make the writes absolute and dependent on ofset value
1282
1283
1284
1285         Iterator it;
1286         it = ((Vector) (me.getWriteBuffer().get(inode))).iterator();
1287         while (it.hasNext()) {
1288
1289             WriteOperations wrp = (WriteOperations) it.next();
1290             if (wrp.isUnknownoffset()) {
1291                 wrp.setUnknownoffset(false);
1292                 wrp.getOwnertransactionalFile().lockOffset(me);
1293
1294                 wrp.getRange().setStart(wrp.getOwnertransactionalFile().committedoffset.getOffsetnumber() - wrp.getBelongingto().getCopylocaloffset() + wrp.getRange().getStart());
1295                 wrp.getRange().setEnd(wrp.getOwnertransactionalFile().committedoffset.getOffsetnumber() - wrp.getBelongingto().getCopylocaloffset() + wrp.getRange().getEnd());
1296                 if ((wrp.getBelongingto().getOffsetdependency() == OffsetDependency.WRITE_DEPENDENCY_1) ||
1297                         (wrp.getBelongingto().offsetdependency == OffsetDependency.NO_ACCESS) ||
1298                         (wrp.getBelongingto().getOffsetdependency() == OffsetDependency.WRITE_DEPENDENCY_2)) {
1299                     wrp.getBelongingto().setOffsetdependency(OffsetDependency.READ_DEPENDENCY);
1300                     wrp.getBelongingto().setUnknown_inital_offset_for_write(false);
1301                     if (!(wrp.getOwnertransactionalFile().committedoffset.getOffsetReaders().contains(me))) {
1302                         wrp.getOwnertransactionalFile().committedoffset.getOffsetReaders().add(me);
1303                     }
1304                     wrp.getBelongingto().setLocaloffset(wrp.getBelongingto().getLocaloffset() + wrp.getOwnertransactionalFile().committedoffset.getOffsetnumber() - wrp.getBelongingto().getCopylocaloffset());
1305                 }
1306                 wrp.getOwnertransactionalFile().offsetlock.unlock();
1307                 markAccessedBlocks(me, (int) wrp.getRange().getStart(), (int) (wrp.getRange().getEnd() - wrp.getRange().getStart()), BlockAccessModesEnum.WRITE);
1308
1309             }
1310         }
1311     //  }
1312
1313     }
1314 }
1315 // for block versioning mechanism
1316             /*if (!(validateBlocksVersions(startblock, targetblock))) { ////check to see if version are still valid 
1317
1318 throw new AbortedException();
1319
1320 }*/
1321 /*  
1322 int expvalue = ((Integer) tmp.getBlockversions().get(Integer.valueOf(k))).intValue();
1323 while (me.getStatus() == Status.ACTIVE) {
1324 BlockDataStructure block = ((BlockDataStructure) tmp.adapter.lockmap.get(Integer.valueOf(k)));
1325 if (block.getLock().tryLock()) {
1326 heldlocks.add(block.getLock());
1327 if (!(block.getVersion().get() == expvalue)) {  // for block versioning mechanism
1328 me.abort();
1329
1330 else {
1331 break;
1332 }
1333
1334 else {
1335 me.getContentionManager().resolveConflict(me, block.getOwner());
1336 }
1337 }
1338 if (me.getStatus() == Status.ABORTED) {
1339 unlockLocks(heldlocks);
1340 offsetlock.unlock();
1341 throw new AbortedException();
1342 }
1343 }
1344 }*/
1345 /*    public boolean validateBlocksVersions(int startblock, int targetblock) { // For Block Versioning Mechanism
1346 boolean valid = true;
1347 ExtendedTransaction me = ExtendedTransaction.getTransaction();
1348 TransactionLocalFileAttributes tmp = ((TransactionLocalFileAttributes) (me.getFilesAccesses().get(this.getInode())));
1349 for (int i = startblock; i <= targetblock; i++) {
1350 int expvalue = ((Integer) tmp.getBlockversions().get(Integer.valueOf(i))).intValue();
1351 BlockDataStructure block = ((BlockDataStructure) tmp.adapter.lockmap.get(Integer.valueOf(i)));
1352 if (expvalue != block.getVersion().get()) {
1353 valid = false;
1354 break;
1355 }
1356 }
1357
1358 return valid;
1359 }*/
1360
1361