diff --git a/source/de/anomic/kelondro/kelondroObjectCache.java b/source/de/anomic/kelondro/kelondroObjectCache.java index 0ef9450f4..e10b45c5e 100644 --- a/source/de/anomic/kelondro/kelondroObjectCache.java +++ b/source/de/anomic/kelondro/kelondroObjectCache.java @@ -60,26 +60,33 @@ import java.util.TreeMap; public class kelondroObjectCache { - private final TreeMap cache; - private final kelondroMScoreCluster ages; - private long startTime; - private int maxSize; - private long maxAge; - private long minMem; - private int readHit, readMiss, writeUnique, writeDouble; - - public kelondroObjectCache(int maxSize, long maxAge, long minMem) { + private final TreeMap cache; + private final kelondroMScoreCluster ages; + private long startTime; + private int maxSize; + private long maxAge; + private long minMem; + private int readHit, readMiss, writeUnique, writeDouble; + private String name; + + public kelondroObjectCache(String name, int maxSize, long maxAge, long minMem) { + this.name = name; this.cache = new TreeMap(); this.ages = new kelondroMScoreCluster(); this.startTime = System.currentTimeMillis(); - this.maxAge = maxAge; - this.minMem = minMem; + this.maxSize = Math.max(maxSize, 1); + this.maxAge = Math.max(maxAge, 10000); + this.minMem = Math.max(minMem, 1024 * 1024); this.readHit = 0; this.readMiss = 0; this.writeUnique = 0; this.writeDouble = 0; } + public String getName() { + return name; + } + public void setMaxAge(long maxAge) { this.maxAge = maxAge; } @@ -162,10 +169,11 @@ public class kelondroObjectCache { String k; synchronized(cache) { while ((ages.size() > 0) && + ((k = bestFlush()) != null) && ((size() > maxSize) || - (longEmit(ages.getMinScore()) > maxAge) || - (Runtime.getRuntime().freeMemory() < minMem)) && - ((k = bestFlush()) != null)) { + ((System.currentTimeMillis() - longEmit(ages.getScore(k))) > maxAge) || + (Runtime.getRuntime().freeMemory() < minMem)) + ) { cache.remove(k); ages.deleteScore(k); if (Runtime.getRuntime().freeMemory() < minMem) System.gc(); // prevent unnecessary loops diff --git a/source/de/anomic/kelondro/kelondroTree.java b/source/de/anomic/kelondro/kelondroTree.java index 448f89488..44d2479d2 100644 --- a/source/de/anomic/kelondro/kelondroTree.java +++ b/source/de/anomic/kelondro/kelondroTree.java @@ -123,7 +123,7 @@ public class kelondroTree extends kelondroRecords implements kelondroIndex { super.setLogger(log); long objectbuffersize = objectCachePercent * buffersize / (nodeCachePercent + objectCachePercent); long nodecachesize = objectbuffersize / (super.objectsize + 8 * columns.length); - this.objectCache = new kelondroObjectCache((int) nodecachesize, nodecachesize * 300 , 4*1024*1024); + this.objectCache = new kelondroObjectCache(this.filename, (int) nodecachesize, nodecachesize * 300 , 4*1024*1024); } public kelondroTree(kelondroRA ra, long buffersize, int[] columns, boolean exitOnFail) { @@ -149,7 +149,7 @@ public class kelondroTree extends kelondroRecords implements kelondroIndex { super.setLogger(log); long objectbuffersize = objectCachePercent * buffersize / (nodeCachePercent + objectCachePercent); long nodecachesize = objectbuffersize / (super.objectsize + 8 * columns.length); - this.objectCache = new kelondroObjectCache((int) nodecachesize, nodecachesize * 300 , 4*1024*1024); + this.objectCache = new kelondroObjectCache(this.filename, (int) nodecachesize, nodecachesize * 300 , 4*1024*1024); } public kelondroTree(File file, long buffersize) throws IOException { @@ -159,7 +159,7 @@ public class kelondroTree extends kelondroRecords implements kelondroIndex { super.setLogger(log); long objectbuffersize = objectCachePercent * buffersize / (nodeCachePercent + objectCachePercent); long nodecachesize = objectbuffersize / (super.objectsize + 8 * super.columns()); - this.objectCache = new kelondroObjectCache((int) nodecachesize, nodecachesize * 300 , 4*1024*1024); + this.objectCache = new kelondroObjectCache(this.filename, (int) nodecachesize, nodecachesize * 300 , 4*1024*1024); } public kelondroTree(kelondroRA ra, long buffersize) throws IOException { @@ -169,7 +169,7 @@ public class kelondroTree extends kelondroRecords implements kelondroIndex { super.setLogger(log); long objectbuffersize = objectCachePercent * buffersize / (nodeCachePercent + objectCachePercent); long nodecachesize = objectbuffersize / (super.objectsize + 8 * super.columns()); - this.objectCache = new kelondroObjectCache((int) nodecachesize, nodecachesize * 300 , 4*1024*1024); + this.objectCache = new kelondroObjectCache(this.filename, (int) nodecachesize, nodecachesize * 300 , 4*1024*1024); } private void writeOrderType() { @@ -217,16 +217,19 @@ public class kelondroTree extends kelondroRecords implements kelondroIndex { public byte[][] get(byte[] key) throws IOException { // System.out.println("kelondroTree.get " + new String(key) + " in " + filename); byte[][] result = (byte[][]) objectCache.get(key); - if (result != null) return result; + if (result != null) { + //System.out.println("cache hit in objectCache, db:" + super.filename); + return result; + } // writeLock.stay(2000, 1000); synchronized (writeSearchObj) { writeSearchObj.process(key); if (writeSearchObj.found()) { result = writeSearchObj.getMatcher().getValues(); + objectCache.put(key, result); } else { result = null; } - objectCache.put(key, result); } // writeLock.release(); return result;