1 package org.apache.lucene.search;
3 * Copyright 2005 The Apache Software Foundation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 import org.apache.lucene.index.IndexReader;
21 import java.io.IOException;
24 * Wraps another SpanFilter's result and caches it. The purpose is to allow
25 * filters to simply filter, and then wrap with this class to add caching.
27 public class CachingSpanFilter extends SpanFilter {
28 private SpanFilter filter;
31 * A transient Filter cache (package private because of test)
33 private final CachingWrapperFilter.FilterCache<SpanFilterResult> cache;
36 * New deletions always result in a cache miss, by default
37 * ({@link CachingWrapperFilter.DeletesMode#RECACHE}.
38 * @param filter Filter to cache results of
40 public CachingSpanFilter(SpanFilter filter) {
41 this(filter, CachingWrapperFilter.DeletesMode.RECACHE);
45 * @param filter Filter to cache results of
46 * @param deletesMode See {@link CachingWrapperFilter.DeletesMode}
48 public CachingSpanFilter(SpanFilter filter, CachingWrapperFilter.DeletesMode deletesMode) {
50 if (deletesMode == CachingWrapperFilter.DeletesMode.DYNAMIC) {
51 throw new IllegalArgumentException("DeletesMode.DYNAMIC is not supported");
53 this.cache = new CachingWrapperFilter.FilterCache<SpanFilterResult>(deletesMode) {
55 protected SpanFilterResult mergeDeletes(final IndexReader reader, final SpanFilterResult value) {
56 throw new IllegalStateException("DeletesMode.DYNAMIC is not supported");
62 public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
63 SpanFilterResult result = getCachedResult(reader);
64 return result != null ? result.getDocIdSet() : null;
68 int hitCount, missCount;
70 private SpanFilterResult getCachedResult(IndexReader reader) throws IOException {
72 final Object coreKey = reader.getCoreCacheKey();
73 final Object delCoreKey = reader.hasDeletions() ? reader.getDeletesCacheKey() : coreKey;
75 SpanFilterResult result = cache.get(reader, coreKey, delCoreKey);
82 result = filter.bitSpans(reader);
84 cache.put(coreKey, delCoreKey, result);
90 public SpanFilterResult bitSpans(IndexReader reader) throws IOException {
91 return getCachedResult(reader);
95 public String toString() {
96 return "CachingSpanFilter("+filter+")";
100 public boolean equals(Object o) {
101 if (!(o instanceof CachingSpanFilter)) return false;
102 return this.filter.equals(((CachingSpanFilter)o).filter);
106 public int hashCode() {
107 return filter.hashCode() ^ 0x1117BF25;