QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / beagled / Lucene.Net / Search / CachingWrapperFilter.cs
blob455ffc2bb27ccfd8928bc425e183bed657a37626
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
18 using System.Runtime.InteropServices;
19 using IndexReader = Lucene.Net.Index.IndexReader;
21 namespace Lucene.Net.Search
24 /// <summary> Wraps another filter's result and caches it. The caching
25 /// behavior is like {@link QueryFilter}. The purpose is to allow
26 /// filters to simply filter, and then wrap with this class to add
27 /// caching, keeping the two concerns decoupled yet composable.
28 /// </summary>
29 [Serializable]
30 public class CachingWrapperFilter:Filter
32 private Filter filter;
34 /// <todo> What about serialization in RemoteSearchable? Caching won't work. </todo>
35 /// <summary> Should transient be removed?
36 /// </summary>
37 [NonSerialized]
38 private System.Collections.IDictionary cache;
40 /// <param name="filter">Filter to cache results of
41 /// </param>
42 public CachingWrapperFilter(Filter filter)
44 this.filter = filter;
47 public override System.Collections.BitArray Bits(IndexReader reader)
49 if (cache == null)
51 cache = new System.Collections.Hashtable();
54 lock (cache.SyncRoot)
56 // check cache
57 System.Collections.BitArray cached = (System.Collections.BitArray) cache[reader];
58 if (cached != null)
60 return cached;
64 System.Collections.BitArray bits = filter.Bits(reader);
66 lock (cache.SyncRoot)
68 // update cache
69 cache[reader] = bits;
72 return bits;
75 public override System.String ToString()
77 return "CachingWrapperFilter(" + filter + ")";
80 public override bool Equals(System.Object o)
82 if (!(o is CachingWrapperFilter))
83 return false;
84 return this.filter.Equals(((CachingWrapperFilter) o).filter);
87 public override int GetHashCode()
89 return filter.GetHashCode() ^ 0x1117BF25;