Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart7.png 63% of files have more coverage
18   87   9   3
6   43   0.5   6
6     1.5  
1    
 
  QueryFilter       Line # 36 18 9 63.3% 0.6333333
 
  (2)
 
1    package org.apache.lucene.search;
2   
3    /**
4    * Copyright 2004 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
17    */
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    /** Constrains search results to only match those which also match a provided
25    * query. Results are cached, so that searches after the first on the same
26    * index using this filter are much faster.
27    *
28    * <p> This could be used, for example, with a {@link RangeQuery} on a suitably
29    * formatted date field to implement date filtering. One could re-use a single
30    * QueryFilter that matches, e.g., only documents modified within the last
31    * week. The QueryFilter and RangeQuery would only need to be reconstructed
32    * once per day.
33    *
34    * @version $Id: QueryFilter.java 328729 2005-10-26 21:05:35Z yonik $
35    */
 
36    public class QueryFilter extends Filter {
37    private Query query;
38    private transient WeakHashMap cache = null;
39   
40    /** Constructs a filter which only matches documents matching
41    * <code>query</code>.
42    */
 
43  3 toggle public QueryFilter(Query query) {
44  3 this.query = query;
45    }
46   
 
47  3 toggle public BitSet bits(IndexReader reader) throws IOException {
48   
49  3 if (cache == null) {
50  3 cache = new WeakHashMap();
51    }
52   
53  3 synchronized (cache) { // check cache
54  3 BitSet cached = (BitSet) cache.get(reader);
55  3 if (cached != null) {
56  0 return cached;
57    }
58    }
59   
60  3 final BitSet bits = new BitSet(reader.maxDoc());
61   
62  3 new IndexSearcher(reader).search(query, new HitCollector() {
 
63  2 toggle public final void collect(int doc, float score) {
64  2 bits.set(doc); // set bit for hit
65    }
66    });
67   
68  3 synchronized (cache) { // update cache
69  3 cache.put(reader, bits);
70    }
71   
72  3 return bits;
73    }
74   
 
75  0 toggle public String toString() {
76  0 return "QueryFilter("+query+")";
77    }
78   
 
79  0 toggle public boolean equals(Object o) {
80  0 if (!(o instanceof QueryFilter)) return false;
81  0 return this.query.equals(((QueryFilter)o).query);
82    }
83   
 
84  1 toggle public int hashCode() {
85  1 return query.hashCode() ^ 0x923F64B9;
86    }
87    }