LUCENE-1459: fix CachingWrapperFilter to do the right thing if the wrong type of bitset is in its cache

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@719716 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-11-21 21:17:06 +00:00
parent 898cfe87cd
commit 129a630839
2 changed files with 25 additions and 9 deletions

View File

@ -18,6 +18,7 @@ package org.apache.lucene.search;
*/
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.DocIdBitSet;
import java.util.BitSet;
import java.util.WeakHashMap;
import java.util.Map;
@ -51,11 +52,17 @@ public class CachingWrapperFilter extends Filter {
cache = new WeakHashMap();
}
Object cached = null;
synchronized (cache) { // check cache
BitSet cached = (BitSet) cache.get(reader);
if (cached != null) {
return cached;
}
cached = cache.get(reader);
}
if (cached != null) {
if (cached instanceof BitSet) {
return (BitSet) cached;
} else if (cached instanceof DocIdBitSet)
return ((DocIdBitSet) cached).getBitSet();
// It would be nice to handle the DocIdSet case, but that's not really possible
}
final BitSet bits = filter.bits(reader);
@ -72,11 +79,16 @@ public class CachingWrapperFilter extends Filter {
cache = new WeakHashMap();
}
Object cached = null;
synchronized (cache) { // check cache
DocIdSet cached = (DocIdSet) cache.get(reader);
if (cached != null) {
return cached;
}
cached = cache.get(reader);
}
if (cached != null) {
if (cached instanceof DocIdSet)
return (DocIdSet) cached;
else
return new DocIdBitSet((BitSet) cached);
}
final DocIdSet docIdSet = filter.getDocIdSet(reader);

View File

@ -39,11 +39,15 @@ public class TestCachingWrapperFilter extends LuceneTestCase {
cacher.getDocIdSet(reader);
assertTrue("first time", filter.wasCalled());
// make sure no exception if cache is holding the wrong bitset
cacher.bits(reader);
cacher.getDocIdSet(reader);
// second time, nested filter should not be called
filter.clear();
cacher.getDocIdSet(reader);
assertFalse("second time", filter.wasCalled());
reader.close();
}
}
}