X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.acorn%2Fsrc%2Forg%2Fsimantics%2Facorn%2Flru%2FLRUObject.java;h=5230723b70b5bd25fc6b5e3a6ac5412c071615a1;hp=1079bf5f0decedacf409d1404d6a5d05b957a333;hb=e5c006a3e29dcb1f29ae5bcc21ac28573bd37648;hpb=35a513a86eb0e30c29ebf094cc778b08e5bbded4 diff --git a/bundles/org.simantics.acorn/src/org/simantics/acorn/lru/LRUObject.java b/bundles/org.simantics.acorn/src/org/simantics/acorn/lru/LRUObject.java index 1079bf5f0..5230723b7 100644 --- a/bundles/org.simantics.acorn/src/org/simantics/acorn/lru/LRUObject.java +++ b/bundles/org.simantics.acorn/src/org/simantics/acorn/lru/LRUObject.java @@ -5,16 +5,22 @@ import java.nio.file.Path; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; +import org.simantics.acorn.FileCache; 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; +import org.slf4j.Logger; public abstract class LRUObject> implements Persistable { - + + public abstract Logger getLogger(); public static boolean VERIFY = true; // Final stuff final protected LRU LRU; + final protected FileCache fileCache; final private Semaphore mutex = new Semaphore(1); final private MapKey key; final private String fileName; @@ -35,8 +41,9 @@ public abstract class LRUObject LRU, MapKey key, Path readDirectory, String fileName, int offset, int length, boolean dirty, boolean resident) { + public LRUObject(LRU LRU, FileCache fileCache, MapKey key, Path readDirectory, String fileName, int offset, int length, boolean dirty, boolean resident) { this.LRU = LRU; + this.fileCache = fileCache; this.key = key; this.fileName = fileName; this.offset = offset; @@ -47,8 +54,8 @@ public abstract class LRUObject LRU, MapKey key, Path readDirectory, String fileName, boolean dirty, boolean resident) { - this(LRU, key, readDirectory, fileName, -1, -1, dirty, resident); + public LRUObject(LRU LRU, FileCache fileCache, MapKey key, Path readDirectory, String fileName, boolean dirty, boolean resident) { + this(LRU, fileCache, key, readDirectory, fileName, -1, -1, dirty, resident); } /* @@ -59,19 +66,17 @@ public abstract class LRUObject 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); - } + if(VERIFY) { + try { + verifyAccess(); + } catch (AcornAccessVerificationException e) { + throw new IOException("Exception occured during toFile for file " + fileName, e); + } + } + try { + Pair pair = toBytes(); + byte[] data = pair.first; + int length = pair.second; + FileIO fio = fileCache.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() { + 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 +126,24 @@ public abstract class LRUObject toBytes(); + abstract protected Pair 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,29 +224,33 @@ public abstract class LRUObject 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; } + + public void moveTo(Path path) { + readDirectory = path; + } } \ No newline at end of file