getlenght support added
[IRC.git] / Robust / Transactions / TransactionalIO / src / TransactionalIO / core / ExtendedTransaction.java
index 916298e53662e6c242250d96c4ae4ae55a5abdb1..90f5b7644e7336e41fde623c33b00f0abfdc7495 100644 (file)
@@ -38,7 +38,7 @@ import java.util.logging.Logger;
  */
 public class ExtendedTransaction implements TransactionStatu {
 
-    private native int nativepwrite(byte buff[], long offset, int size, FileDescriptor fd);
+    private static native int nativepwrite(byte buff[], long offset, int size, FileDescriptor fd);
     
 
     {
@@ -58,6 +58,7 @@ public class ExtendedTransaction implements TransactionStatu {
         ABORTED, ACTIVE, COMMITTED
     };
     private boolean writesmerged = true;
+    private Vector heldlengthlocks;
     //private Vector<ReentrantLock> heldoffsetlocks;    
     private Vector heldoffsetlocks;
     //private Vector<ReentrantLock> heldblocklocks;    
@@ -73,10 +74,18 @@ public class ExtendedTransaction implements TransactionStatu {
     private ContentionManager contentionmanager;
     private volatile Status status;
     private int id;
+    
+    
+    public ReentrantLock[] toholoffsetlocks;
+    public int offsetcount = 0;
+    
+    public Lock[] toholdblocklocks;
+    public int blockcount = 0;
 
     public ExtendedTransaction() {
         //  super();
         // id = Integer.valueOf(Thread.currentThread().getName().substring(7));
+        heldlengthlocks = new Vector();
         heldblocklocks = new Vector();
         heldoffsetlocks = new Vector();
         AccessedFiles = new HashMap();
@@ -97,7 +106,7 @@ public class ExtendedTransaction implements TransactionStatu {
         this.memorystate = memorystate;
     }
 
-    private int invokeNativepwrite(byte buff[], long offset, int size, RandomAccessFile file) {
+    public static int invokeNativepwrite(byte buff[], long offset, int size, RandomAccessFile file) {
         try {
             return nativepwrite(buff, offset, buff.length, file.getFD());
         } catch (IOException ex) {
@@ -187,7 +196,7 @@ public class ExtendedTransaction implements TransactionStatu {
 
     public void addFile(TransactionalFile tf, long offsetnumber/*, TransactionLocalFileAttributes tmp*/) {
 
-        TransactionLocalFileAttributes tmp = new TransactionLocalFileAttributes(offsetnumber/*, tf.getInodestate().commitedfilesize.get()*/);
+        TransactionLocalFileAttributes tmp = new TransactionLocalFileAttributes(offsetnumber, tf.getInodestate().commitedfilesize.getLength());
         Vector dummy;
 
         if (AccessedFiles.containsKey(tf.getInode())) {
@@ -203,28 +212,54 @@ public class ExtendedTransaction implements TransactionStatu {
 
     public boolean lockOffsets() {   /// Locking offsets for File Descriptors
 
-
+      //  toholoffsetlocks = new ReentrantLock[30];
+        
         TreeMap hm = getSortedFileAccessMap(AccessedFiles);
         Iterator iter = hm.keySet().iterator();
-        
+        offsetcount = 0;
         while (iter.hasNext() && (this.getStatus() == Status.ACTIVE)) {
             INode key = (INode) iter.next();
             Vector vec = (Vector) AccessedFiles.get(key);
+            
+       /*     if (vec.size() == 1){
+                TransactionalFile tf = ((TransactionalFile)vec.firstElement());
+                tf.offsetlock.lock();
+             //   toholoffsetlocks[offsetcount] = tf.offsetlock;
+            //   offsetcount++;
+                heldoffsetlocks.add(tf.offsetlock);
+                continue;
+            }*/
+            
             Collections.sort(vec);
             Iterator it = vec.iterator();
-            while (it.hasNext() && this.getStatus() == Status.ACTIVE) {
-                TransactionalFile value = (TransactionalFile) it.next();
-                value.offsetlock.lock();
+            while (it.hasNext() /*&& this.getStatus() == Status.ACTIVE*/) {
+               TransactionalFile value = (TransactionalFile) it.next();
+               value.offsetlock.lock();
+             //   toholoffsetlocks[offsetcount] = value.offsetlock;
+            //    offsetcount++;
                 heldoffsetlocks.add(value.offsetlock);
+                
+                if (((TransactionLocalFileAttributes) GlobaltoLocalMappings.get(value)).lenght_read){ 
+                    if (!(value.getInodestate().commitedfilesize.lengthlock.isHeldByCurrentThread())){
+                        value.getInodestate().commitedfilesize.lengthlock.lock();
+                        heldlengthlocks.add(value.getInodestate().commitedfilesize.lengthlock);
+                    }
+                }
                 break;
             }
         }
 
         if (this.getStatus() != Status.ACTIVE) {
+         //   for (int j=0; j<offsetcount; j++){
+         //       heldoffsetlocks.add(toholoffsetlocks[j]);
+         //   }
             return false;
         }
         return true;
     }
+    
+    
+    
 
     public boolean lockBlock(BlockDataStructure block, BlockAccessModesEnum mode/*, GlobalINodeState adapter, BlockAccessModesEnum mode, int expvalue, INode inode, TransactionLocalFileAttributes tf*/) {
 
@@ -237,6 +272,8 @@ public class ExtendedTransaction implements TransactionStatu {
         }
         
         lock.lock();
+      //  toholdblocklocks[blockcount] = lock;
+      //  blockcount++;
         heldblocklocks.add(lock);
         return true;
        
@@ -246,10 +283,15 @@ public class ExtendedTransaction implements TransactionStatu {
         if (this.status != Status.ACTIVE) {
             throw new AbortedException();
         }
-        boolean ok = true;
+        boolean offsetsok = true;
         if (!lockOffsets()) {
             throw new AbortedException();
         }
+        
+      //  boolean lengthslock = true;
+     //   if (!lockOffsets()) {
+     //       throw new AbortedException();
+     //   }
 
 
         ///////////////////////////
@@ -260,7 +302,7 @@ public class ExtendedTransaction implements TransactionStatu {
         Iterator iter = hm.keySet().iterator();
         WriteOperations value;
         Vector vec = new Vector();
-        while (iter.hasNext() && (this.getStatus() == Status.ACTIVE) && ok) {
+        while (iter.hasNext() && (this.getStatus() == Status.ACTIVE) && offsetsok) {
             INode key = (INode) iter.next();
             vec = (Vector) hm.get(key);
             Collections.sort(vec);
@@ -308,10 +350,11 @@ public class ExtendedTransaction implements TransactionStatu {
 
         }
         
+        //toholdblocklocks = new Lock[100];
         
         Iterator it = this.getAccessedBlocks().keySet().iterator();
         BlockDataStructure[] blocks = new BlockDataStructure[100];
-        if (this.getStatus() == Status.ACTIVE)
+        //if (this.getStatus() == Status.ACTIVE)
             while (it.hasNext() /*&& (this.getStatus() == Status.ACTIVE)*/) {
                 INode inode = (INode) it.next();
                 GlobalINodeState inodestate = TransactionalFileWrapperFactory.getTateransactionalFileINodeState(inode);
@@ -327,7 +370,8 @@ public class ExtendedTransaction implements TransactionStatu {
             }
 
         if (this.getStatus() != Status.ACTIVE) {
-
+        //    for (int i=0; i<blockcount; i++)
+        //        heldblocklocks.add(toholdblocklocks[i]); 
             throw new AbortedException();
         }
         abortAllReaders();
@@ -341,7 +385,7 @@ public class ExtendedTransaction implements TransactionStatu {
         Iterator it;
         WriteOperations writeop;
         Vector vec;
-        while (iter.hasNext() && (this.getStatus() == Status.ACTIVE)) {
+        while (iter.hasNext()) {
             INode key = (INode) iter.next();
 
             vec = (Vector) hm.get(key);
@@ -365,7 +409,48 @@ public class ExtendedTransaction implements TransactionStatu {
         while (k.hasNext()) {
             TransactionalFile trf = (TransactionalFile) (k.next());
             trf.getCommitedoffset().setOffsetnumber(((TransactionLocalFileAttributes) GlobaltoLocalMappings.get(trf)).getLocaloffset());
+            if (((TransactionLocalFileAttributes) GlobaltoLocalMappings.get(trf)).getInitiallocallength() != ((TransactionLocalFileAttributes) GlobaltoLocalMappings.get(trf)).getLocalsize()){
+                try {
+                    if (!(trf.getInodestate().commitedfilesize.lengthlock.isHeldByCurrentThread()))
+                        trf.getInodestate().commitedfilesize.lengthlock.lock();
+                    
+                    Iterator it2 = trf.getInodestate().commitedfilesize.getLengthReaders().iterator();
+                    if (((TransactionLocalFileAttributes)getGlobaltoLocalMappings().get(trf)).getInitiallocallength() != ((TransactionLocalFileAttributes)getGlobaltoLocalMappings().get(trf)).getLocalsize())
+                    {
+                        while (it2.hasNext()) {
+                            ExtendedTransaction tr = (ExtendedTransaction) it2.next();
+                            if (tr != this) {
+                                tr.abort();
+                            }
+                        }
+                        trf.getInodestate().commitedfilesize.getLengthReaders().clear();
+                    }
+                    trf.getInodestate().commitedfilesize.setLength(trf.file.length());
+                    
+                    if (trf.getInodestate().commitedfilesize.lengthlock.isHeldByCurrentThread()){
+                        heldlengthlocks.remove(trf.getInodestate().commitedfilesize.lengthlock);
+                        trf.getInodestate().commitedfilesize.lengthlock.unlock();
+                    }
+                    
+                } catch (IOException ex) {
+                    Logger.getLogger(ExtendedTransaction.class.getName()).log(Level.SEVERE, null, ex);
+                }
+            }
+            
+            if (((TransactionLocalFileAttributes) GlobaltoLocalMappings.get(trf)).lenght_read){
+                trf.getInodestate().commitedfilesize.getLengthReaders().remove(this);
+                heldlengthlocks.remove(trf.getInodestate().commitedfilesize.lengthlock);
+                trf.getInodestate().commitedfilesize.lengthlock.unlock();
+            }
         }
+        
+        
+      /*  for (int i =0; i<blockcount; i++){
+            toholdblocklocks[i].unlock();
+        }
+        for (int i =0; i<offsetcount; i++){
+            toholoffsetlocks[i].unlock();
+        }*/
     }
 
     public void unlockAllLocks() {
@@ -376,14 +461,21 @@ public class ExtendedTransaction implements TransactionStatu {
             Lock lock = (Lock) it.next();
             lock.unlock();
         }
-   //     heldblocklocks.clear();
+        heldblocklocks.clear();
 
         it = heldoffsetlocks.iterator();
         while (it.hasNext()) {
             ReentrantLock lock = (ReentrantLock) it.next();
             lock.unlock();
         }
-    //    heldoffsetlocks.clear();
+        heldoffsetlocks.clear();
+        
+       it = heldlengthlocks.iterator(); 
+       while (it.hasNext()) {
+            ReentrantLock lock = (ReentrantLock) it.next();
+            lock.unlock();
+        }
+        heldlengthlocks.clear();
     }
 
     public void abortAllReaders() {
@@ -407,6 +499,9 @@ public class ExtendedTransaction implements TransactionStatu {
                     }
                 }
                 value.getCommitedoffset().getOffsetReaders().clear();
+                
+            
+                
             }
 
             TreeMap vec2;
@@ -441,6 +536,8 @@ public class ExtendedTransaction implements TransactionStatu {
 
         }
     }
+    
+  
 
     public void addPropertyChangeListener(PropertyChangeListener listener) {
         this.changes.addPropertyChangeListener("status", listener);
@@ -469,6 +566,10 @@ public class ExtendedTransaction implements TransactionStatu {
     public Vector getHeldoffsetlocks() {
         return heldoffsetlocks;
     }
+    
+    public Vector getHeldlengthlocks() {
+        return heldlengthlocks;
+    }
 
     public void setHeldoffsetlocks(Vector heldoffsetlocks) {
         this.heldoffsetlocks = heldoffsetlocks;