1 package org.apache.lucene.search; 2 3 18 19 import java.io.IOException ; 20 import java.util.WeakHashMap ; 21 import java.util.BitSet ; 22 import org.apache.lucene.index.IndexReader; 23 24 36 public class QueryFilter extends Filter { 37 private Query query; 38 private transient WeakHashMap cache = null; 39 40 43 public QueryFilter(Query query) { 44 this.query = query; 45 } 46 47 public BitSet bits(IndexReader reader) throws IOException { 48 49 if (cache == null) { 50 cache = new WeakHashMap (); 51 } 52 53 synchronized (cache) { BitSet cached = (BitSet ) cache.get(reader); 55 if (cached != null) { 56 return cached; 57 } 58 } 59 60 final BitSet bits = new BitSet (reader.maxDoc()); 61 62 new IndexSearcher(reader).search(query, new HitCollector() { 63 public final void collect(int doc, float score) { 64 bits.set(doc); } 66 }); 67 68 synchronized (cache) { cache.put(reader, bits); 70 } 71 72 return bits; 73 } 74 75 public String toString() { 76 return "QueryFilter("+query+")"; 77 } 78 79 public boolean equals(Object o) { 80 if (!(o instanceof QueryFilter)) return false; 81 return this.query.equals(((QueryFilter)o).query); 82 } 83 84 public int hashCode() { 85 return query.hashCode() ^ 0x923F64B9; 86 } 87 } 88 | Popular Tags |