19 using System.Collections;
20 using System.Collections.Generic;
21 using Lucene.Net.Support;
24 using Lucene.Net.Util;
26 namespace Lucene.Net.Search
35 protected internal Filter filter;
60 internal FilterCache<DocIdSet> cache;
63 abstract internal class FilterCache<T> where T : class
70 volatile IDictionary<Object, T> cache;
72 private DeletesMode deletesMode;
74 public FilterCache(DeletesMode deletesMode)
76 this.deletesMode = deletesMode;
79 public T Get(
IndexReader reader,
object coreKey,
object delCoreKey)
87 cache =
new WeakDictionary<object, T>();
90 if (deletesMode == DeletesMode.IGNORE)
93 value = cache[coreKey];
95 else if (deletesMode == DeletesMode.RECACHE)
98 value = cache[delCoreKey];
103 System.Diagnostics.Debug.Assert(deletesMode == DeletesMode.DYNAMIC);
106 value = cache[delCoreKey];
112 value = cache[coreKey];
113 if (value != null && reader.HasDeletions)
115 value = MergeDeletes(reader, value);
124 protected abstract T MergeDeletes(
IndexReader reader, T value);
126 public void Put(
object coreKey,
object delCoreKey, T value)
130 if (deletesMode == DeletesMode.IGNORE)
132 cache[coreKey] = value;
134 else if (deletesMode == DeletesMode.RECACHE)
136 cache[delCoreKey] = value;
140 cache[coreKey] = value;
141 cache[delCoreKey] = value;
172 this.filter = filter;
173 cache =
new AnonymousFilterCache(deletesMode);
187 class AnonymousFilterCache : FilterCache<DocIdSet>
196 public override bool Match(
int docid)
198 return !r.IsDeleted(docid);
202 public AnonymousFilterCache(DeletesMode deletesMode) : base(deletesMode)
207 return new AnonymousFilteredDocIdSet(docIdSet, reader);
217 if (docIdSet == null)
222 else if (docIdSet.IsCacheable) {
240 object coreKey = reader.FieldCacheKey;
241 object delCoreKey = reader.HasDeletions ? reader.DeletesCacheKey : coreKey;
243 DocIdSet docIdSet = cache.Get(reader, coreKey, delCoreKey);
245 if (docIdSet != null)
252 docIdSet = DocIdSetToCache(filter.GetDocIdSet(reader), reader);
254 if (docIdSet != null)
256 cache.Put(coreKey, delCoreKey, docIdSet);
262 public override System.String ToString()
264 return "CachingWrapperFilter(" + filter +
")";
267 public override bool Equals(System.Object o)
271 return this.filter.Equals(((CachingWrapperFilter) o).filter);
274 public override int GetHashCode()
276 return filter.GetHashCode() ^ 0x1117BF25;