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<MapKey, MapValue extends LRUObject<MapKey, MapValue>> implements Persistable {
-
+
+ public abstract Logger getLogger();
public static boolean VERIFY = true;
// Final stuff
final protected LRU<MapKey, MapValue> LRU;
+ final protected FileCache fileCache;
final private Semaphore mutex = new Semaphore(1);
final private MapKey key;
final private String fileName;
private Thread mutexOwner;
// for loading
- public LRUObject(LRU<MapKey, MapValue> LRU, MapKey key, Path readDirectory, String fileName, int offset, int length, boolean dirty, boolean resident) {
+ public LRUObject(LRU<MapKey, MapValue> 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;
}
// for creating
- public LRUObject(LRU<MapKey, MapValue> LRU, MapKey key, Path readDirectory, String fileName, boolean dirty, boolean resident) {
- this(LRU, key, readDirectory, fileName, -1, -1, dirty, resident);
+ public LRUObject(LRU<MapKey, MapValue> LRU, FileCache fileCache, MapKey key, Path readDirectory, String fileName, boolean dirty, boolean resident) {
+ this(LRU, fileCache, key, readDirectory, fileName, -1, -1, dirty, resident);
}
/*
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);
+ getLogger().info("Mutex is taking a long time to acquire - owner is " + mutexOwner);
}
if(VERIFY)
Pair<byte[], Integer> pair = toBytes();
byte[] data = pair.first;
int length = pair.second;
- FileIO fio = FileIO.get(bytes);
+ FileIO fio = fileCache.get(bytes);
int offset = fio.saveBytes(data, length, overwrite());
setPosition(offset, length);
} catch (AcornAccessVerificationException | IllegalAcornStateException e) {
if(VERIFY) verifyAccess();
Path dir = getDirectory();
Path f = dir.resolve(getFileName());
- FileIO fio = FileIO.get(f);
+ FileIO fio = fileCache.get(f);
return fio.readBytes(getOffset(), getLength());
}
if(VERIFY) verifyAccess();
return readDirectory;
}
+
+ public void moveTo(Path path) {
+ readDirectory = path;
+ }
}
\ No newline at end of file