[teiid-commits] teiid SVN: r2962 - in trunk/engine/src: test/java/org/teiid/cache and 1 other directory.
teiid-commits at lists.jboss.org
teiid-commits at lists.jboss.org
Thu Mar 3 16:36:25 EST 2011
Author: shawkins
Date: 2011-03-03 16:36:25 -0500 (Thu, 03 Mar 2011)
New Revision: 2962
Modified:
trunk/engine/src/main/java/org/teiid/cache/DefaultCache.java
trunk/engine/src/test/java/org/teiid/cache/TestDefaultCache.java
Log:
TEIID-1492 fixing default cache entry removal
Modified: trunk/engine/src/main/java/org/teiid/cache/DefaultCache.java
===================================================================
--- trunk/engine/src/main/java/org/teiid/cache/DefaultCache.java 2011-03-03 19:32:23 UTC (rev 2961)
+++ trunk/engine/src/main/java/org/teiid/cache/DefaultCache.java 2011-03-03 21:36:25 UTC (rev 2962)
@@ -24,9 +24,9 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
-import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -71,7 +71,7 @@
protected Map<String, Cache> children = new ConcurrentHashMap<String, Cache>();
protected String name;
protected long ttl;
- protected LinkedHashSet<ExpirationEntry<K, V>> expirationQueue = new LinkedHashSet<ExpirationEntry<K, V>>();
+ protected Set<ExpirationEntry<K, V>> expirationQueue;
public DefaultCache(String name) {
this(name, DEFAULT_MAX_SIZE_TOTAL, DEFAULT_MAX_SIZE_TOTAL);
@@ -83,12 +83,14 @@
protected boolean removeEldestEntry(java.util.Map.Entry<K, ExpirationEntry<K, V>> eldest) {
if (super.removeEldestEntry(eldest)) {
Iterator<ExpirationEntry<K, V>> iter = expirationQueue.iterator();
- return validate(iter.next()) != null;
+ if (validate(iter.next()) != null) {
+ DefaultCache.this.remove(eldest.getKey());
+ }
}
return false;
}
};
-
+ this.expirationQueue = Collections.newSetFromMap(new LRUCache<ExpirationEntry<K, V>, Boolean>(maxEntries));
this.name = name;
this.ttl = ttl;
}
@@ -147,10 +149,13 @@
}
public V put(K key, V value, Long timeToLive) {
+ if (this.map.getSpaceLimit() == 0) {
+ return null;
+ }
synchronized (map) {
ExpirationEntry<K, V> entry = new ExpirationEntry<K, V>(getExpirationTime(ttl, timeToLive), key, value);
+ ExpirationEntry<K, V> result = map.put(key, entry);
expirationQueue.add(entry);
- ExpirationEntry<K, V> result = map.put(key, entry);
if (result != null) {
return result.value;
}
@@ -160,10 +165,9 @@
public V remove(K key) {
synchronized (map) {
- ExpirationEntry<K, V> entry = new ExpirationEntry<K, V>(-1, key, null);
- ExpirationEntry<K, V> result = map.put(key, entry);
+ ExpirationEntry<K, V> result = map.remove(key);
if (result != null) {
- expirationQueue.remove(entry);
+ expirationQueue.remove(result);
return result.value;
}
return null;
@@ -225,4 +229,12 @@
}
}
+ Set<ExpirationEntry<K, V>> getExpirationQueue() {
+ return expirationQueue;
+ }
+
+ LRUCache<K, ExpirationEntry<K, V>> getCacheMap() {
+ return map;
+ }
+
}
\ No newline at end of file
Modified: trunk/engine/src/test/java/org/teiid/cache/TestDefaultCache.java
===================================================================
--- trunk/engine/src/test/java/org/teiid/cache/TestDefaultCache.java 2011-03-03 19:32:23 UTC (rev 2961)
+++ trunk/engine/src/test/java/org/teiid/cache/TestDefaultCache.java 2011-03-03 21:36:25 UTC (rev 2962)
@@ -43,5 +43,29 @@
//preferred to purge 2 instead of 3
assertNotNull(cache.get(3));
}
+
+ @Test public void testExpirationAtMaxSize() throws Exception {
+ DefaultCache<Integer, Integer> cache = new DefaultCache<Integer, Integer>("foo", 2, 70);
+ cache.put(1, 1);
+ cache.put(2, 2);
+ cache.put(3, 3);
+ assertEquals(2, cache.getCacheMap().size());
+ assertEquals(2, cache.getExpirationQueue().size());
+ Thread.sleep(100);
+ cache.put(4, 4);
+ cache.put(5, 5);
+ cache.get(4);
+ cache.put(6, 6);
+ assertEquals(2, cache.getCacheMap().size());
+ assertEquals(2, cache.getExpirationQueue().size());
+ assertNotNull(cache.get(4));
+ assertNotNull(cache.get(6));
+ }
+
+ @Test public void testZeroSize() throws Exception {
+ DefaultCache<Integer, Integer> cache = new DefaultCache<Integer, Integer>("foo", 0, 70);
+ cache.put(1, 1);
+ assertEquals(0, cache.size());
+ }
}
More information about the teiid-commits
mailing list