01: package org.apache.lucene.search;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
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.
18: */
19:
20: import org.apache.lucene.index.IndexReader;
21: import java.util.BitSet;
22: import java.util.WeakHashMap;
23: import java.util.Map;
24: import java.io.IOException;
25:
26: /**
27: * Wraps another filter's result and caches it. The purpose is to allow
28: * filters to simply filter, and then wrap with this class to add caching.
29: */
30: public class CachingWrapperFilter extends Filter {
31: protected Filter filter;
32:
33: /**
34: * A transient Filter cache. To cache Filters even when using {@link RemoteSearchable} use
35: * {@link RemoteCachingWrapperFilter} instead.
36: */
37: protected transient Map cache;
38:
39: /**
40: * @param filter Filter to cache results of
41: */
42: public CachingWrapperFilter(Filter filter) {
43: this .filter = filter;
44: }
45:
46: public BitSet bits(IndexReader reader) throws IOException {
47: if (cache == null) {
48: cache = new WeakHashMap();
49: }
50:
51: synchronized (cache) { // check cache
52: BitSet cached = (BitSet) cache.get(reader);
53: if (cached != null) {
54: return cached;
55: }
56: }
57:
58: final BitSet bits = filter.bits(reader);
59:
60: synchronized (cache) { // update cache
61: cache.put(reader, bits);
62: }
63:
64: return bits;
65: }
66:
67: public String toString() {
68: return "CachingWrapperFilter(" + filter + ")";
69: }
70:
71: public boolean equals(Object o) {
72: if (!(o instanceof CachingWrapperFilter))
73: return false;
74: return this .filter.equals(((CachingWrapperFilter) o).filter);
75: }
76:
77: public int hashCode() {
78: return filter.hashCode() ^ 0x1117BF25;
79: }
80: }
|