Skip to content

Apache Lucene open-source search software

SOLR-2135: Fix behavior of ConcurrentLRUCache when asking for getLatestAccessedItems(0) or getOldestAccessedItems(0)

Chris M. Hostetter1cdc4e655e80
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 019f93b58bb..31510ff42c1 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -505,6 +505,10 @@ Bug Fixes
wildcard support. For example, a query of *zemog* would match documents that contain
'gomez'. (Landon Kuhn via Robert Muir)
+* SOLR-2135: Fix behavior of ConcurrentLRUCache when asking for
+ getLatestAccessedItems(0) or getOldestAccessedItems(0).
+ (David Smiley via hossman)
+
Other Changes
----------------------
diff --git a/solr/src/common/org/apache/solr/common/util/ConcurrentLRUCache.java b/solr/src/common/org/apache/solr/common/util/ConcurrentLRUCache.java
index ac21ef4f2d0..ed352f2e186 100644
--- a/solr/src/common/org/apache/solr/common/util/ConcurrentLRUCache.java
+++ b/solr/src/common/org/apache/solr/common/util/ConcurrentLRUCache.java
@@ -398,6 +398,8 @@ public class ConcurrentLRUCache<K,V> {
*/
public Map<K, V> getOldestAccessedItems(int n) {
Map<K, V> result = new LinkedHashMap<K, V>();
+ if (n <= 0)
+ return result;
TreeSet<CacheEntry> tree = new TreeSet<CacheEntry>();
markAndSweepLock.lock();
try {
@@ -424,6 +426,8 @@ public class ConcurrentLRUCache<K,V> {
public Map<K,V> getLatestAccessedItems(int n) {
Map<K,V> result = new LinkedHashMap<K,V>();
+ if (n <= 0)
+ return result;
TreeSet<CacheEntry> tree = new TreeSet<CacheEntry>();
// we need to grab the lock since we are changing lastAccessedCopy
markAndSweepLock.lock();
diff --git a/solr/src/test/org/apache/solr/search/TestFastLRUCache.java b/solr/src/test/org/apache/solr/search/TestFastLRUCache.java
index 5733bf2b295..6c42eacf283 100644
--- a/solr/src/test/org/apache/solr/search/TestFastLRUCache.java
+++ b/solr/src/test/org/apache/solr/search/TestFastLRUCache.java
@@ -243,6 +243,14 @@ public class TestFastLRUCache extends LuceneTestCase {
assertNotNull(m.get(5));
assertNotNull(m.get(4));
assertNotNull(m.get(2));
+
+ m = cache.getOldestAccessedItems(0);
+ assertTrue(m.isEmpty());
+
+ //test this too
+ m = cache.getLatestAccessedItems(0);
+ assertTrue(m.isEmpty());
+
cache.destroy();
}