Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
FieldCache.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.IO;
20 using Lucene.Net.Support;
21 using Double = Lucene.Net.Support.Double;
22 using NumericTokenStream = Lucene.Net.Analysis.NumericTokenStream;
23 using NumericField = Lucene.Net.Documents.NumericField;
24 using IndexReader = Lucene.Net.Index.IndexReader;
25 using NumericUtils = Lucene.Net.Util.NumericUtils;
26 using RamUsageEstimator = Lucene.Net.Util.RamUsageEstimator;
27 using Single = Lucene.Net.Support.Single;
28 
29 namespace Lucene.Net.Search
30 {
31 
43  public sealed class CreationPlaceholder
44  {
45  internal System.Object value_Renamed;
46  }
48  public class StringIndex
49  {
50 
51  public virtual int BinarySearchLookup(System.String key)
52  {
53  // this special case is the reason that Arrays.binarySearch() isn't useful.
54  if (key == null)
55  return 0;
56 
57  int low = 1;
58  int high = lookup.Length - 1;
59 
60  while (low <= high)
61  {
62  int mid = Number.URShift((low + high), 1);
63  int cmp = String.CompareOrdinal(lookup[mid], key);
64 
65  if (cmp < 0)
66  low = mid + 1;
67  else if (cmp > 0)
68  high = mid - 1;
69  else
70  return mid; // key found
71  }
72  return -(low + 1); // key not found.
73  }
74 
76  public System.String[] lookup;
77 
79  public int[] order;
80 
82  public StringIndex(int[] values, System.String[] lookup)
83  {
84  this.order = values;
85  this.lookup = lookup;
86  }
87  }
97  public abstract class CacheEntry
98  {
99  public abstract object ReaderKey { get; }
100  public abstract string FieldName { get; }
101  public abstract Type CacheType { get; }
102  public abstract object Custom { get; }
103  public abstract object Value { get; }
104 
107  public virtual void EstimateSize()
108  {
109  EstimateSize(new RamUsageEstimator(false)); // doesn't check for interned
110  }
114  public virtual void EstimateSize(RamUsageEstimator ramCalc)
115  {
116  long size = ramCalc.EstimateRamUsage(Value);
117  EstimatedSize = RamUsageEstimator.HumanReadableUnits(size, new System.Globalization.NumberFormatInfo()); // {{Aroush-2.9}} in Java, the formater is set to "0.#", so we need to do the same in C#
118  }
119 
123  public string EstimatedSize { get; protected internal set; }
124 
125 
126  public override System.String ToString()
127  {
128  var b = new System.Text.StringBuilder();
129  b.Append("'").Append(ReaderKey).Append("'=>");
130  b.Append("'").Append(FieldName).Append("',");
131  b.Append(CacheType).Append(",").Append(Custom);
132  b.Append("=>").Append(Value.GetType().FullName).Append("#");
133  b.Append(Value.GetHashCode());
134 
135  System.String s = EstimatedSize;
136  if (null != s)
137  {
138  b.Append(" (size =~ ").Append(s).Append(')');
139  }
140 
141  return b.ToString();
142  }
143  }
144  public struct FieldCache_Fields
145  {
147  // NOTE: the value assigned to this constant must not be
148  // the same as any of those in SortField!!
149  public readonly static int STRING_INDEX = -1;
151  public readonly static FieldCache DEFAULT;
153  public readonly static ByteParser DEFAULT_BYTE_PARSER;
155  public readonly static ShortParser DEFAULT_SHORT_PARSER;
157  public readonly static IntParser DEFAULT_INT_PARSER;
159  public readonly static FloatParser DEFAULT_FLOAT_PARSER;
161  public readonly static LongParser DEFAULT_LONG_PARSER;
163  public readonly static DoubleParser DEFAULT_DOUBLE_PARSER;
167  public readonly static IntParser NUMERIC_UTILS_INT_PARSER;
171  public readonly static FloatParser NUMERIC_UTILS_FLOAT_PARSER;
175  public readonly static LongParser NUMERIC_UTILS_LONG_PARSER;
181  {
182  DEFAULT = new FieldCacheImpl();
183  DEFAULT_BYTE_PARSER = new AnonymousClassByteParser();
184  DEFAULT_SHORT_PARSER = new AnonymousClassShortParser();
185  DEFAULT_INT_PARSER = new AnonymousClassIntParser();
186  DEFAULT_FLOAT_PARSER = new AnonymousClassFloatParser();
187  DEFAULT_LONG_PARSER = new AnonymousClassLongParser();
188  DEFAULT_DOUBLE_PARSER = new AnonymousClassDoubleParser();
189  NUMERIC_UTILS_INT_PARSER = new AnonymousClassIntParser1();
190  NUMERIC_UTILS_FLOAT_PARSER = new AnonymousClassFloatParser1();
191  NUMERIC_UTILS_LONG_PARSER = new AnonymousClassLongParser1();
192  NUMERIC_UTILS_DOUBLE_PARSER = new AnonymousClassDoubleParser1();
193  }
194  }
195 
196  [Serializable]
198  {
199  public virtual sbyte ParseByte(System.String value_Renamed)
200  {
201  return System.SByte.Parse(value_Renamed);
202  }
203  protected internal virtual System.Object ReadResolve()
204  {
205  return Lucene.Net.Search.FieldCache_Fields.DEFAULT_BYTE_PARSER;
206  }
207  public override System.String ToString()
208  {
209  return typeof(FieldCache).FullName + ".DEFAULT_BYTE_PARSER";
210  }
211  }
212  [Serializable]
214  {
215  public virtual short ParseShort(System.String value_Renamed)
216  {
217  return System.Int16.Parse(value_Renamed);
218  }
219  protected internal virtual System.Object ReadResolve()
220  {
221  return Lucene.Net.Search.FieldCache_Fields.DEFAULT_SHORT_PARSER;
222  }
223  public override System.String ToString()
224  {
225  return typeof(FieldCache).FullName + ".DEFAULT_SHORT_PARSER";
226  }
227  }
228  [Serializable]
230  {
231  public virtual int ParseInt(System.String value_Renamed)
232  {
233  return System.Int32.Parse(value_Renamed);
234  }
235  protected internal virtual System.Object ReadResolve()
236  {
237  return Lucene.Net.Search.FieldCache_Fields.DEFAULT_INT_PARSER;
238  }
239  public override System.String ToString()
240  {
241  return typeof(FieldCache).FullName + ".DEFAULT_INT_PARSER";
242  }
243  }
244  [Serializable]
246  {
247  public virtual float ParseFloat(System.String value_Renamed)
248  {
249  try
250  {
251  return Single.Parse(value_Renamed);
252  }
253  catch (System.OverflowException)
254  {
255  return value_Renamed.StartsWith("-") ? float.PositiveInfinity : float.NegativeInfinity;
256  }
257  }
258  protected internal virtual System.Object ReadResolve()
259  {
260  return Lucene.Net.Search.FieldCache_Fields.DEFAULT_FLOAT_PARSER;
261  }
262  public override System.String ToString()
263  {
264  return typeof(FieldCache).FullName + ".DEFAULT_FLOAT_PARSER";
265  }
266  }
267  [Serializable]
269  {
270  public virtual long ParseLong(System.String value_Renamed)
271  {
272  return System.Int64.Parse(value_Renamed);
273  }
274  protected internal virtual System.Object ReadResolve()
275  {
276  return Lucene.Net.Search.FieldCache_Fields.DEFAULT_LONG_PARSER;
277  }
278  public override System.String ToString()
279  {
280  return typeof(FieldCache).FullName + ".DEFAULT_LONG_PARSER";
281  }
282  }
283  [Serializable]
285  {
286  public virtual double ParseDouble(System.String value_Renamed)
287  {
288  return Double.Parse(value_Renamed);
289  }
290  protected internal virtual System.Object ReadResolve()
291  {
292  return Lucene.Net.Search.FieldCache_Fields.DEFAULT_DOUBLE_PARSER;
293  }
294  public override System.String ToString()
295  {
296  return typeof(FieldCache).FullName + ".DEFAULT_DOUBLE_PARSER";
297  }
298  }
299  [Serializable]
301  {
302  public virtual int ParseInt(System.String val)
303  {
304  int shift = val[0] - NumericUtils.SHIFT_START_INT;
305  if (shift > 0 && shift <= 31)
306  throw new FieldCacheImpl.StopFillCacheException();
307  return NumericUtils.PrefixCodedToInt(val);
308  }
309  protected internal virtual System.Object ReadResolve()
310  {
311  return Lucene.Net.Search.FieldCache_Fields.NUMERIC_UTILS_INT_PARSER;
312  }
313  public override System.String ToString()
314  {
315  return typeof(FieldCache).FullName + ".NUMERIC_UTILS_INT_PARSER";
316  }
317  }
318  [Serializable]
320  {
321  public virtual float ParseFloat(System.String val)
322  {
323  int shift = val[0] - NumericUtils.SHIFT_START_INT;
324  if (shift > 0 && shift <= 31)
325  throw new FieldCacheImpl.StopFillCacheException();
326  return NumericUtils.SortableIntToFloat(NumericUtils.PrefixCodedToInt(val));
327  }
328  protected internal virtual System.Object ReadResolve()
329  {
330  return Lucene.Net.Search.FieldCache_Fields.NUMERIC_UTILS_FLOAT_PARSER;
331  }
332  public override System.String ToString()
333  {
334  return typeof(FieldCache).FullName + ".NUMERIC_UTILS_FLOAT_PARSER";
335  }
336  }
337  [Serializable]
339  {
340  public virtual long ParseLong(System.String val)
341  {
342  int shift = val[0] - NumericUtils.SHIFT_START_LONG;
343  if (shift > 0 && shift <= 63)
344  throw new FieldCacheImpl.StopFillCacheException();
345  return NumericUtils.PrefixCodedToLong(val);
346  }
347  protected internal virtual System.Object ReadResolve()
348  {
349  return Lucene.Net.Search.FieldCache_Fields.NUMERIC_UTILS_LONG_PARSER;
350  }
351  public override System.String ToString()
352  {
353  return typeof(FieldCache).FullName + ".NUMERIC_UTILS_LONG_PARSER";
354  }
355  }
356  [Serializable]
358  {
359  public virtual double ParseDouble(System.String val)
360  {
361  int shift = val[0] - NumericUtils.SHIFT_START_LONG;
362  if (shift > 0 && shift <= 63)
363  throw new FieldCacheImpl.StopFillCacheException();
364  return NumericUtils.SortableLongToDouble(NumericUtils.PrefixCodedToLong(val));
365  }
366  protected internal virtual System.Object ReadResolve()
367  {
368  return Lucene.Net.Search.FieldCache_Fields.NUMERIC_UTILS_DOUBLE_PARSER;
369  }
370  public override System.String ToString()
371  {
372  return typeof(FieldCache).FullName + ".NUMERIC_UTILS_DOUBLE_PARSER";
373  }
374  }
375 
376  public interface FieldCache
377  {
378 
391  sbyte[] GetBytes(IndexReader reader, System.String field);
392 
407  sbyte[] GetBytes(IndexReader reader, System.String field, ByteParser parser);
408 
421  short[] GetShorts(IndexReader reader, System.String field);
422 
437  short[] GetShorts(IndexReader reader, System.String field, ShortParser parser);
438 
451  int[] GetInts(IndexReader reader, System.String field);
452 
467  int[] GetInts(IndexReader reader, System.String field, IntParser parser);
468 
481  float[] GetFloats(IndexReader reader, System.String field);
482 
497  float[] GetFloats(IndexReader reader, System.String field, FloatParser parser);
498 
512  long[] GetLongs(IndexReader reader, System.String field);
513 
529  long[] GetLongs(IndexReader reader, System.String field, LongParser parser);
530 
531 
545  double[] GetDoubles(IndexReader reader, System.String field);
546 
562  double[] GetDoubles(IndexReader reader, System.String field, DoubleParser parser);
563 
576  System.String[] GetStrings(IndexReader reader, System.String field);
577 
590  StringIndex GetStringIndex(IndexReader reader, System.String field);
591 
607  CacheEntry[] GetCacheEntries();
608 
624  void PurgeAllCaches();
625 
633  void Purge(IndexReader r);
634 
641  StreamWriter InfoStream { get; set; }
642  }
643 
647  public interface Parser
648  {
649  }
650 
654  public interface ByteParser : Parser
655  {
657  sbyte ParseByte(System.String string_Renamed);
658  }
659 
663  public interface ShortParser : Parser
664  {
666  short ParseShort(System.String string_Renamed);
667  }
668 
672  public interface IntParser : Parser
673  {
675  int ParseInt(System.String string_Renamed);
676  }
677 
681  public interface FloatParser : Parser
682  {
684  float ParseFloat(System.String string_Renamed);
685  }
686 
692  public interface LongParser : Parser
693  {
695  long ParseLong(System.String string_Renamed);
696  }
697 
703  public interface DoubleParser : Parser
704  {
706  double ParseDouble(System.String string_Renamed);
707  }
708 }