Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / CachingWrapperFilter.cs
blob6458f5730433694516acfec0a2c62d33715ea20b
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.
16 using System;
17 using System.Runtime.InteropServices;
18 using IndexReader = Lucene.Net.Index.IndexReader;
19 namespace Lucene.Net.Search
22 /// <summary> Wraps another filters result and caches it. The caching
23 /// behavior is like {@link QueryFilter}. The purpose is to allow
24 /// filters to simply filter, and then wrap with this class to add
25 /// caching, keeping the two concerns decoupled yet composable.
26 /// </summary>
27 [Serializable]
28 public class CachingWrapperFilter:Filter
30 private Filter filter;
32 /// <todo> What about serialization in RemoteSearchable? Caching won't work. </todo>
33 /// <summary> Should transient be removed?
34 /// </summary>
35 [NonSerialized]
36 private System.Collections.IDictionary cache;
38 /// <param name="filter">Filter to cache results of
39 /// </param>
40 public CachingWrapperFilter(Filter filter)
42 this.filter = filter;
45 public override System.Collections.BitArray Bits(IndexReader reader)
47 if (cache == null)
49 cache = new System.Collections.Hashtable();
52 lock (cache.SyncRoot)
54 // check cache
55 System.Collections.BitArray cached = (System.Collections.BitArray) cache[reader];
56 if (cached != null)
58 return cached;
62 System.Collections.BitArray bits = filter.Bits(reader);
64 lock (cache.SyncRoot)
66 // update cache
67 cache[reader] = bits;
70 return bits;
73 public override System.String ToString()
75 return "CachingWrapperFilter(" + filter + ")";