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
ShapeFieldCacheProvider.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 using System.Runtime.CompilerServices;
20 using Lucene.Net.Index;
21 using Spatial4n.Core.Shapes;
22 #if NET35
23 using Lucene.Net.Support;
24 #endif
25 
26 namespace Lucene.Net.Spatial.Util
27 {
28  /// <summary>
29  /// Provides access to a {@link ShapeFieldCache} for a given {@link AtomicReader}.
30  ///
31  /// If a Cache does not exist for the Reader, then it is built by iterating over
32  /// the all terms for a given field, reconstructing the Shape from them, and adding
33  /// them to the Cache.
34  /// </summary>
35  /// <typeparam name="T"></typeparam>
36  public abstract class ShapeFieldCacheProvider<T> where T : Shape
37  {
38  //private Logger log = Logger.getLogger(getClass().getName());
39 
40  // it may be a List<T> or T
41 #if !NET35
42  private readonly ConditionalWeakTable<IndexReader, ShapeFieldCache<T>> sidx =
43  new ConditionalWeakTable<IndexReader, ShapeFieldCache<T>>(); // WeakHashMap
44 #else
45  private readonly WeakDictionary<IndexReader, ShapeFieldCache<T>> sidx =
46  new WeakDictionary<IndexReader, ShapeFieldCache<T>>();
47 #endif
48 
49 
50  protected readonly int defaultSize;
51  protected readonly String shapeField;
52 
53  protected ShapeFieldCacheProvider(String shapeField, int defaultSize)
54  {
55  this.shapeField = shapeField;
56  this.defaultSize = defaultSize;
57  }
58 
59  protected abstract T ReadShape(/*BytesRef*/ Term term);
60 
61  private readonly object locker = new object();
62 
63  public ShapeFieldCache<T> GetCache(IndexReader reader)
64  {
65  lock (locker)
66  {
68  if (sidx.TryGetValue(reader, out idx) && idx != null)
69  {
70  return idx;
71  }
72 
73  //long startTime = System.CurrentTimeMillis();
74  //log.fine("Building Cache [" + reader.MaxDoc() + "]");
75 
76  idx = new ShapeFieldCache<T>(reader.MaxDoc, defaultSize);
77  var count = 0;
78  var tec = new TermsEnumCompatibility(reader, shapeField);
79 
80  var term = tec.Next();
81  while (term != null)
82  {
83  var shape = ReadShape(term);
84  if (shape != null)
85  {
86  var docs = reader.TermDocs(new Term(shapeField, tec.Term().Text));
87  while (docs.Next())
88  {
89  idx.Add(docs.Doc, shape);
90  count++;
91  }
92  }
93  term = tec.Next();
94  }
95 
96  sidx.Add(reader, idx);
97  tec.Close();
98 
99  //long elapsed = System.CurrentTimeMillis() - startTime;
100  //log.fine("Cached: [" + count + " in " + elapsed + "ms] " + idx);
101  return idx;
102  }
103  }
104  }
105 }