[Swift-commit] cog r3879

swift at ci.uchicago.edu swift at ci.uchicago.edu
Fri Mar 7 15:15:04 CST 2014


------------------------------------------------------------------------
r3879 | hategan | 2014-03-07 15:13:21 -0600 (Fri, 07 Mar 2014) | 1 line

fixed premature removal of cached entries for cache() (swift bug 1215)
------------------------------------------------------------------------
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/CacheNode.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/CacheNode.java	(revision 3878)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/CacheNode.java	(working copy)
@@ -64,6 +64,7 @@
 					frame = stack.frameCount();
 					i++;
 				case 1:
+					// this sets "key", which is local
 					boolean shouldContinue = checkCached(stack);
 					if (!shouldContinue) {
 						break;
@@ -74,6 +75,7 @@
 						startCount++;
 					}
 					body.run(thr);
+					// this uses "key", set above
 					setValue(stack);
 			}
 		}
@@ -106,7 +108,7 @@
 			fv = (FutureObject) cache.getCachedValue(key);
 			if (fv == null) {
 				fv = new FutureObject();
-				cache.addValue(key, fv);
+				cache.addAndLock(key, fv);
 				initial = true;
 			}
 		}
@@ -129,6 +131,7 @@
 		synchronized (cache) {
 			FutureObject f = (FutureObject) cache.getCachedValue(key);
 			f.setValue(ret);
+			cache.unlock(key);
 		}
 	}
 
Index: modules/karajan/src/org/globus/cog/karajan/util/Cache.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/util/Cache.java	(revision 3878)
+++ modules/karajan/src/org/globus/cog/karajan/util/Cache.java	(working copy)
@@ -13,6 +13,7 @@
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Map;
 
 /**
  * This is a simple/generic cache used by:
@@ -27,16 +28,17 @@
 	private final static int ENTRY_POOL_MAX_SIZE = 25;
 	private final static int MAX_CACHE_SIZE = 1000;
 	private final static int CACHE_PURGE_SIZE = 100;
-	private HashMap primitives;
 	
+	private HashMap<Object, Value> store;
+	
 	private int maxCacheSize = MAX_CACHE_SIZE;
 	
 	public Cache() {
-		primitives = new HashMap();
+		store = new HashMap<Object, Value>();
 	}
 
 	public Object getCachedValue(Object key) {
-		Value value = (Value) primitives.get(key);
+		Value value = store.get(key);
 		if (value != null) {
 			value.hits++;
 			value.lastHit = System.currentTimeMillis();
@@ -46,31 +48,43 @@
 	}
 
 	public boolean isCached(Object key) {
-		return primitives.containsKey(key);
+		return store.containsKey(key);
 	}
 
 	public void addValue(Object key, Object value) {
-		primitives.put(key, new Value(value));
+		store.put(key, new Value(value));
 		checkSize();
 	}
+	
+	public void addAndLock(Object key, Object value) {
+		Value v = new Value(value);
+		v.locked = true;
+		store.put(key, v);
+		checkSize();
+	}
+	
+	public void unlock(Object key) {
+		Value v = store.get(key);
+		v.locked = false;
+	}
 
 	private void checkSize() {
-		if (maxCacheSize > 0 && primitives.size() > maxCacheSize) {
-			long[] scores = new long[primitives.size()];
+		if (maxCacheSize > 0 && store.size() > maxCacheSize) {
+			long[] scores = new long[store.size()];
 			int index = 0;
-			Iterator i = primitives.values().iterator();
-			while (i.hasNext()) {
-				Value v = (Value) i.next();
-				scores[index++] = score(v.hits, v.lastHit);
+			for (Value v : store.values()) {
+				if (!v.locked) {
+					scores[index++] = score(v.hits, v.lastHit);
+				}
 			}
 			Arrays.sort(scores);
 			long threshold = scores[CACHE_PURGE_SIZE];
-			i = primitives.keySet().iterator();
-			while (i.hasNext()) {
-				Object key = i.next();
-				Value v = (Value) primitives.get(key);
-				if (score(v.hits, v.lastHit) < threshold) {
-					i.remove();
+			Iterator<Map.Entry<Object, Value>> j = store.entrySet().iterator();
+			while (j.hasNext()) {
+				Map.Entry<Object, Value> e = j.next();
+				Value v = e.getValue();
+				if (!v.locked && score(v.hits, v.lastHit) < threshold) {
+					j.remove();
 				}
 			}
 		}
@@ -92,6 +106,7 @@
 
 		public int hits;
 		public long lastHit;
+		public boolean locked;
 		public Object object;
 
 		public Value(Object object) {



More information about the Swift-commit mailing list