Index: lucene/src/java/org/apache/lucene/search/CachingWrapperFilter.java =================================================================== --- lucene/src/java/org/apache/lucene/search/CachingWrapperFilter.java (revision 1213973) +++ lucene/src/java/org/apache/lucene/search/CachingWrapperFilter.java (working copy) @@ -37,29 +37,51 @@ // level of the readers hierarchy it should be cached. Filter filter; - protected final FilterCache cache; + protected final FilterCache cache = new FilterCache(); private final boolean recacheDeletes; - private static class FilterCache { + // helper for adding an identity reference as key in WeakHashMap + static final class IdentityKeyWrapper { + public final T key; + private final int hashCode; - /** - * A transient Filter cache (package private because of test) - */ - private final Map> cache = new WeakHashMap>(); + public IdentityKeyWrapper(T key) { + this.key = key; + this.hashCode = System.identityHashCode(key); + } - public synchronized T get(IndexReader reader, Object coreKey, Object coreSubKey) throws IOException { - Map innerCache = cache.get(coreKey); + public int hashCode() { + return hashCode; + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + public boolean equals(Object o) { + if (o == this) return true; + if (o instanceof IdentityKeyWrapper) { + return ((IdentityKeyWrapper) o).key == this.key; + } + return false; + } + + } + + private static class FilterCache { + private final Map,DocIdSet>> cache = new WeakHashMap,DocIdSet>>(); + + public synchronized DocIdSet get(IndexReader reader, Object coreKey, Bits coreSubKey) throws IOException { + Map,DocIdSet> innerCache = cache.get(coreKey); if (innerCache == null) { - innerCache = new WeakHashMap(); + innerCache = new WeakHashMap,DocIdSet>(); cache.put(coreKey, innerCache); } - return innerCache.get(coreSubKey); + return innerCache.get(new IdentityKeyWrapper(coreSubKey)); } - public synchronized void put(Object coreKey, Object coreSubKey, T value) { - cache.get(coreKey).put(coreSubKey, value); + public synchronized void put(Object coreKey, Bits coreSubKey, DocIdSet value) { + cache.get(coreKey).put(new IdentityKeyWrapper(coreSubKey), value); } + } /** Wraps another filter's result and caches it. @@ -79,7 +101,6 @@ public CachingWrapperFilter(Filter filter, boolean recacheDeletes) { this.filter = filter; this.recacheDeletes = recacheDeletes; - cache = new FilterCache(); } /** Provide the DocIdSet to be cached, using the DocIdSet provided