Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
CachingSpanFilter.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 
20 using System.Runtime.InteropServices;
21 using IndexReader = Lucene.Net.Index.IndexReader;
22 
23 namespace Lucene.Net.Search
24 {
25 
26  /// <summary> Wraps another SpanFilter's result and caches it. The purpose is to allow
27  /// filters to simply filter, and then wrap with this class to add caching.
28  /// </summary>
29  [Serializable]
31  {
32  private SpanFilter filter;
33 
34  /// <summary> A transient Filter cache (internal because of test)</summary>
35  [NonSerialized]
36  internal CachingWrapperFilter.FilterCache<SpanFilterResult> cache;
37 
38  /// <summary>
39  /// New deletions always result in a cache miss, by default
40  /// (<see cref="CachingWrapperFilter.DeletesMode.RECACHE" />.
41  /// <param name="filter">Filter to cache results of
42  /// </param>
43  /// </summary>
44  public CachingSpanFilter(SpanFilter filter): this(filter, CachingWrapperFilter.DeletesMode.RECACHE)
45  {
46 
47  }
48 
49  /// <summary>New deletions always result in a cache miss, specify the <paramref name="deletesMode"/></summary>
50  /// <param name="filter">Filter to cache results of</param>
51  /// <param name="deletesMode">See <see cref="CachingWrapperFilter.DeletesMode" /></param>
53  {
54  this.filter = filter;
55  if (deletesMode == CachingWrapperFilter.DeletesMode.DYNAMIC)
56  {
57  throw new System.ArgumentException("DeletesMode.DYNAMIC is not supported");
58  }
59  this.cache = new AnonymousFilterCache(deletesMode);
60  }
61 
62  class AnonymousFilterCache : CachingWrapperFilter.FilterCache<SpanFilterResult>
63  {
64  public AnonymousFilterCache(CachingWrapperFilter.DeletesMode deletesMode) : base(deletesMode)
65  {
66  }
67 
68  protected override SpanFilterResult MergeDeletes(IndexReader reader, SpanFilterResult docIdSet)
69  {
70  throw new System.ArgumentException("DeletesMode.DYNAMIC is not supported");
71  }
72  }
73 
74  public override DocIdSet GetDocIdSet(IndexReader reader)
75  {
76  SpanFilterResult result = GetCachedResult(reader);
77  return result != null?result.DocIdSet:null;
78  }
79 
80  // for testing
81  public int hitCount, missCount;
82 
83  private SpanFilterResult GetCachedResult(IndexReader reader)
84  {
85  object coreKey = reader.FieldCacheKey;
86  object delCoreKey = reader.HasDeletions ? reader.DeletesCacheKey : coreKey;
87 
88  SpanFilterResult result = cache.Get(reader, coreKey, delCoreKey);
89  if (result != null) {
90  hitCount++;
91  return result;
92  }
93 
94  missCount++;
95  result = filter.BitSpans(reader);
96 
97  cache.Put(coreKey, delCoreKey, result);
98  return result;
99  }
100 
101 
102  public override SpanFilterResult BitSpans(IndexReader reader)
103  {
104  return GetCachedResult(reader);
105  }
106 
107  public override System.String ToString()
108  {
109  return "CachingSpanFilter(" + filter + ")";
110  }
111 
112  public override bool Equals(System.Object o)
113  {
114  if (!(o is CachingSpanFilter))
115  return false;
116  return this.filter.Equals(((CachingSpanFilter) o).filter);
117  }
118 
119  public override int GetHashCode()
120  {
121  return filter.GetHashCode() ^ 0x1117BF25;
122  }
123  }
124 }