1
package org.apache.lucene.search;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import java.io.Serializable;
21
import java.io.IOException;
23
import java.util.WeakHashMap;
25
import org.apache.lucene.index.IndexReader;
26
import org.apache.lucene.util.FixedBitSet;
29
* Wraps another filter's result and caches it. The purpose is to allow
30
* filters to simply filter, and then wrap with this class
33
* <p><b>NOTE</b>: if you wrap this filter as a query (eg,
34
* using ConstantScoreQuery), you'll likely want to enforce
35
* deletions (using either {@link DeletesMode#RECACHE} or
36
* {@link DeletesMode#DYNAMIC}).
38
public class CachingWrapperFilter extends Filter {
42
* Expert: Specifies how new deletions against a reopened
43
* reader should be handled.
45
* <p>The default is IGNORE, which means the cache entry
46
* will be re-used for a given segment, even when that
47
* segment has been reopened due to changes in deletions.
48
* This is a big performance gain, especially with
49
* near-real-timer readers, since you don't hit a cache
50
* miss on every reopened reader for prior segments.</p>
52
* <p>However, in some cases this can cause invalid query
53
* results, allowing deleted documents to be returned.
54
* This only happens if the main query does not rule out
55
* deleted documents on its own, such as a toplevel
56
* ConstantScoreQuery. To fix this, use RECACHE to
57
* re-create the cached filter (at a higher per-reopen
58
* cost, but at faster subsequent search performance), or
59
* use DYNAMIC to dynamically intersect deleted docs (fast
60
* reopen time but some hit to search performance).</p>
62
public static enum DeletesMode {IGNORE, RECACHE, DYNAMIC};
64
protected final FilterCache<DocIdSet> cache;
66
static abstract class FilterCache<T> implements Serializable {
69
* A transient Filter cache (package private because of test)
71
// NOTE: not final so that we can dynamically re-init
73
transient Map<Object,T> cache;
75
private final DeletesMode deletesMode;
77
public FilterCache(DeletesMode deletesMode) {
78
this.deletesMode = deletesMode;
81
public synchronized T get(IndexReader reader, Object coreKey, Object delCoreKey) throws IOException {
85
cache = new WeakHashMap<Object,T>();
88
if (deletesMode == DeletesMode.IGNORE) {
90
value = cache.get(coreKey);
91
} else if (deletesMode == DeletesMode.RECACHE) {
92
// key on deletes, if any, else core
93
value = cache.get(delCoreKey);
95
assert deletesMode == DeletesMode.DYNAMIC;
97
// first try for exact match
98
value = cache.get(delCoreKey);
101
// now for core match, but dynamically AND NOT
103
value = cache.get(coreKey);
104
if (value != null && reader.hasDeletions()) {
105
value = mergeDeletes(reader, value);
113
protected abstract T mergeDeletes(IndexReader reader, T value);
115
public synchronized void put(Object coreKey, Object delCoreKey, T value) {
116
if (deletesMode == DeletesMode.IGNORE) {
117
cache.put(coreKey, value);
118
} else if (deletesMode == DeletesMode.RECACHE) {
119
cache.put(delCoreKey, value);
121
cache.put(coreKey, value);
122
cache.put(delCoreKey, value);
128
* New deletes are ignored by default, which gives higher
129
* cache hit rate on reopened readers. Most of the time
130
* this is safe, because the filter will be AND'd with a
131
* Query that fully enforces deletions. If instead you
132
* need this filter to always enforce deletions, pass
133
* either {@link DeletesMode#RECACHE} or {@link
134
* DeletesMode#DYNAMIC}.
135
* @param filter Filter to cache results of
137
public CachingWrapperFilter(Filter filter) {
138
this(filter, DeletesMode.IGNORE);
142
* Expert: by default, the cached filter will be shared
143
* across reopened segments that only had changes to their
146
* @param filter Filter to cache results of
147
* @param deletesMode See {@link DeletesMode}
149
public CachingWrapperFilter(Filter filter, DeletesMode deletesMode) {
150
this.filter = filter;
151
cache = new FilterCache<DocIdSet>(deletesMode) {
153
public DocIdSet mergeDeletes(final IndexReader r, final DocIdSet docIdSet) {
154
return new FilteredDocIdSet(docIdSet) {
156
protected boolean match(int docID) {
157
return !r.isDeleted(docID);
164
/** Provide the DocIdSet to be cached, using the DocIdSet provided
165
* by the wrapped Filter.
166
* <p>This implementation returns the given {@link DocIdSet}, if {@link DocIdSet#isCacheable}
167
* returns <code>true</code>, else it copies the {@link DocIdSetIterator} into
168
* an {@link FixedBitSet}.
170
protected DocIdSet docIdSetToCache(DocIdSet docIdSet, IndexReader reader) throws IOException {
171
if (docIdSet == null) {
172
// this is better than returning null, as the nonnull result can be cached
173
return DocIdSet.EMPTY_DOCIDSET;
174
} else if (docIdSet.isCacheable()) {
177
final DocIdSetIterator it = docIdSet.iterator();
178
// null is allowed to be returned by iterator(),
179
// in this case we wrap with the empty set,
180
// which is cacheable.
182
return DocIdSet.EMPTY_DOCIDSET;
184
final FixedBitSet bits = new FixedBitSet(reader.maxDoc());
192
int hitCount, missCount;
195
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
197
final Object coreKey = reader.getCoreCacheKey();
198
final Object delCoreKey = reader.hasDeletions() ? reader.getDeletesCacheKey() : coreKey;
200
DocIdSet docIdSet = cache.get(reader, coreKey, delCoreKey);
201
if (docIdSet != null) {
209
docIdSet = docIdSetToCache(filter.getDocIdSet(reader), reader);
211
if (docIdSet != null) {
212
cache.put(coreKey, delCoreKey, docIdSet);
219
public String toString() {
220
return "CachingWrapperFilter("+filter+")";
224
public boolean equals(Object o) {
225
if (!(o instanceof CachingWrapperFilter)) return false;
226
return this.filter.equals(((CachingWrapperFilter)o).filter);
230
public int hashCode() {
231
return filter.hashCode() ^ 0x1117BF25;