]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.acorn/src/org/simantics/acorn/lru/LRUObject.java
Acorn: Fix WriteRunnable.runReally() and other fixes
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / lru / LRUObject.java
index 1079bf5f0decedacf409d1404d6a5d05b957a333..3194d591e0b37e8712e8cabf3cc1afcb9549a754 100644 (file)
@@ -7,6 +7,8 @@ import java.util.concurrent.TimeUnit;
 
 import org.simantics.acorn.FileIO;
 import org.simantics.acorn.Persistable;
+import org.simantics.acorn.exception.AcornAccessVerificationException;
+import org.simantics.acorn.exception.IllegalAcornStateException;
 import org.simantics.utils.datastructures.Pair;
 
 public abstract class LRUObject<MapKey, MapValue extends LRUObject<MapKey, MapValue>> implements Persistable {
@@ -59,10 +61,8 @@ public abstract class LRUObject<MapKey, MapValue extends LRUObject<MapKey, MapVa
                return key;
        }
        
-       public void acquireMutex() {
-               
+       public void acquireMutex() throws IllegalAcornStateException {
                try {
-
                        while(!mutex.tryAcquire(3, TimeUnit.SECONDS)) {
                                System.err.println("Mutex is taking a long time to acquire - owner is " + mutexOwner);
                        }
@@ -71,7 +71,7 @@ public abstract class LRUObject<MapKey, MapValue extends LRUObject<MapKey, MapVa
                                mutexOwner = Thread.currentThread();
 
                } catch (InterruptedException e) {
-                       throw new IllegalStateException(e);
+                       throw new IllegalAcornStateException(e);
                }
        }
        
@@ -85,21 +85,31 @@ public abstract class LRUObject<MapKey, MapValue extends LRUObject<MapKey, MapVa
 
        @Override
        public void toFile(Path bytes) throws IOException {
-               if(VERIFY) verifyAccess();
-               Pair<byte[],Integer> pair = toBytes();
-               byte[] data = pair.first;
-               int length = pair.second;
-               FileIO fio = FileIO.get(bytes);
-               int offset = fio.saveBytes(data, length, overwrite());
-               setPosition(offset, length);
-       }
-       
-       public int makeResident() {
+               if(VERIFY) {
+                   try {
+                verifyAccess();
+            } catch (AcornAccessVerificationException e) {
+                throw new IOException("Exception occured during toFile for file " + fileName, e);
+            }
+               }
+        try {
+            Pair<byte[], Integer> pair = toBytes();
+            byte[] data = pair.first;
+            int length = pair.second;
+            FileIO fio = FileIO.get(bytes);
+            int offset = fio.saveBytes(data, length, overwrite());
+            setPosition(offset, length);
+        } catch (AcornAccessVerificationException | IllegalAcornStateException e) {
+            throw new IOException("Exception occured during toFile for file " + fileName, e);
+        }
+    }
+       
+       public int makeResident() throws AcornAccessVerificationException, IllegalAcornStateException {
                if(VERIFY) verifyAccess();
                return LRU.makeResident(this, false);
        }
 
-       public int makeResident(boolean keepResident) {
+       public int makeResident(boolean keepResident) throws AcornAccessVerificationException, IllegalAcornStateException {
                if(VERIFY) verifyAccess();
                return LRU.makeResident(this, true);
        }
@@ -111,24 +121,24 @@ public abstract class LRUObject<MapKey, MapValue extends LRUObject<MapKey, MapVa
        abstract void release();
        abstract String getExtension();
        
-       String getStateKey() {
+       String getStateKey() throws IllegalAcornStateException, AcornAccessVerificationException {
                String result = getKey().toString() + "#" + getDirectory().getFileName() + "#" + getOffset() + "#" + getLength(); 
                if(offset == -1)
-                   throw new IllegalStateException(result);
+                   throw new IllegalAcornStateException(result);
                return result; 
        }
 
-       long getLastAccessTime() {
+       long getLastAccessTime() throws AcornAccessVerificationException {
                if(VERIFY) verifyAccess();
                return accessTime;
        }
        
-       void accessed() {
+       void accessed() throws AcornAccessVerificationException {
                if(VERIFY) verifyAccess();
                accessTime = AccessTime.getInstance().getAccessTime();
        }
        
-       boolean persist() {
+       boolean persist() throws AcornAccessVerificationException {
                if(VERIFY) verifyAccess();
                if(LRU.persist(this)) {
                        readDirectory = LRU.getDirectory();
@@ -138,44 +148,44 @@ public abstract class LRUObject<MapKey, MapValue extends LRUObject<MapKey, MapVa
                }
        }
        
-       void setForceResident(boolean value) {
+       void setForceResident(boolean value) throws AcornAccessVerificationException {
         if(VERIFY) verifyAccess();
         forceResident = value;
 //        isForceResidentSetAfterLastGet = true;
        }
        
-       boolean canBePersisted() {
+       boolean canBePersisted() throws AcornAccessVerificationException {
                if(VERIFY) verifyAccess();
 //             isForceResidentSetAfterLastGet = false;
                return !forceResident;
        }
        
-       boolean isDirty() {
+       boolean isDirty() throws AcornAccessVerificationException {
                if(VERIFY) verifyAccess();
                return dirty;
        }
        
-       boolean isResident() {
+       boolean isResident() throws AcornAccessVerificationException {
                if(VERIFY) verifyAccess();
                return resident;
        }
        
-       String getFileName() {
+       String getFileName() throws AcornAccessVerificationException {
                if(VERIFY) verifyAccess();
                return fileName;
        }
 
-       void setResident(boolean value) {
+       void setResident(boolean value) throws AcornAccessVerificationException {
                if(VERIFY) verifyAccess();
                resident = value;
        }
        
-       void setDirty(boolean value) {
+       void setDirty(boolean value) throws AcornAccessVerificationException {
                if(VERIFY) verifyAccess();
                dirty = value;
        }
        
-       byte[] readFile() throws IOException {
+       byte[] readFile() throws IOException, AcornAccessVerificationException {
                if(VERIFY) verifyAccess();
                Path dir = getDirectory();
                Path f = dir.resolve(getFileName());
@@ -189,18 +199,19 @@ public abstract class LRUObject<MapKey, MapValue extends LRUObject<MapKey, MapVa
 
        abstract protected boolean overwrite();
        
-       abstract protected Pair<byte[],Integer> toBytes();
+       abstract protected Pair<byte[],Integer> toBytes() throws IllegalAcornStateException;
        
-       protected void setDirty() {
+       protected void setDirty() throws AcornAccessVerificationException {
                if(VERIFY) verifyAccess();
                dirty = true;
        }
        
-       protected void verifyAccess() {
-               assert(mutex.availablePermits() == 0);
+       protected void verifyAccess() throws AcornAccessVerificationException {
+        if (mutex.availablePermits() != 0)
+            throw new AcornAccessVerificationException("fileName=" + fileName + " mutex has " + mutex.availablePermits() + " available permits, should be 0! Current mutexOwner is " + mutexOwner);
        }
 
-       protected synchronized void cancelForceResident() {
+       protected synchronized void cancelForceResident() throws AcornAccessVerificationException {
                setForceResident(false);
        }
        
@@ -208,27 +219,27 @@ public abstract class LRUObject<MapKey, MapValue extends LRUObject<MapKey, MapVa
         * Private implementation details
         */
        
-       private int getOffset() {
+       private int getOffset() throws AcornAccessVerificationException {
                if(VERIFY) verifyAccess();
                return offset;
        }
        
-       private int getLength() {
+       private int getLength() throws AcornAccessVerificationException {
                if(VERIFY) verifyAccess();
                return length;
        }
        
-       private void setPosition(int offset, int length) {
+       private void setPosition(int offset, int length) throws AcornAccessVerificationException, IllegalAcornStateException {
                if(VERIFY) verifyAccess();
                if(offset == -1)
-                   throw new IllegalStateException();
+                   throw new IllegalAcornStateException("offset == -1 for " + fileName + " in " + readDirectory.toAbsolutePath() + ", dirty=" + dirty + ", resident=" + resident + ", forceResident=" + forceResident);
                this.offset = offset;
                this.length = length;
                if(overwrite() && offset > 0)
-                       throw new IllegalStateException();
+                   throw new IllegalAcornStateException("overwrite() == true &&  offset > 0 for " + fileName + " in " + readDirectory.toAbsolutePath() + ", dirty=" + dirty + ", resident=" + resident + ", forceResident=" + forceResident);
        }
        
-       private Path getDirectory() {
+       private Path getDirectory() throws AcornAccessVerificationException {
                if(VERIFY) verifyAccess();
                return readDirectory;
        }