/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using NumericTokenStream = Lucene.Net.Analysis.NumericTokenStream;
using NumericField = Lucene.Net.Documents.NumericField;
using IndexReader = Lucene.Net.Index.IndexReader;
using NumericUtils = Lucene.Net.Util.NumericUtils;
using RamUsageEstimator = Lucene.Net.Util.RamUsageEstimator;
namespace Lucene.Net.Search
{
/// Expert: Maintains caches of term values.
///
/// Created: May 19, 2004 11:13:14 AM
///
///
/// lucene 1.4
///
/// $Id: FieldCache.java 807841 2009-08-25 22:27:31Z markrmiller $
///
///
///
public sealed class CreationPlaceholder
{
internal System.Object value_Renamed;
}
/// Expert: Stores term text values and document ordering data.
public class StringIndex
{
public virtual int BinarySearchLookup(System.String key)
{
// this special case is the reason that Arrays.binarySearch() isn't useful.
if (key == null)
return 0;
int low = 1;
int high = lookup.Length - 1;
while (low <= high)
{
int mid = SupportClass.Number.URShift((low + high), 1);
int cmp = String.CompareOrdinal(lookup[mid], key);
if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return - (low + 1); // key not found.
}
/// All the term values, in natural order.
public System.String[] lookup;
/// For each document, an index into the lookup array.
public int[] order;
/// Creates one of these objects
public StringIndex(int[] values, System.String[] lookup)
{
this.order = values;
this.lookup = lookup;
}
}
/// EXPERT: A unique Identifier/Description for each item in the FieldCache.
/// Can be useful for logging/debugging.
///
/// EXPERIMENTAL API: This API is considered extremely advanced
/// and experimental. It may be removed or altered w/o warning in future
/// releases
/// of Lucene.
///
///
public abstract class CacheEntry
{
public abstract System.Object GetReaderKey();
public abstract System.String GetFieldName();
public abstract System.Type GetCacheType();
public abstract System.Object GetCustom();
public abstract System.Object GetValue();
private System.String size = null;
protected internal void SetEstimatedSize(System.String size)
{
this.size = size;
}
///
///
public virtual void EstimateSize()
{
EstimateSize(new RamUsageEstimator(false)); // doesn't check for interned
}
/// Computes (and stores) the estimated size of the cache Value
///
///
public virtual void EstimateSize(RamUsageEstimator ramCalc)
{
long size = ramCalc.EstimateRamUsage(GetValue());
SetEstimatedSize(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#
}
/// The most recently estimated size of the value, null unless
/// estimateSize has been called.
///
public System.String GetEstimatedSize()
{
return size;
}
public override System.String ToString()
{
System.Text.StringBuilder b = new System.Text.StringBuilder();
b.Append("'").Append(GetReaderKey()).Append("'=>");
b.Append("'").Append(GetFieldName()).Append("',");
b.Append(GetCacheType()).Append(",").Append(GetCustom());
b.Append("=>").Append(GetValue().GetType().FullName).Append("#");
b.Append(GetValue().GetHashCode());
System.String s = GetEstimatedSize();
if (null != s)
{
b.Append(" (size =~ ").Append(s).Append(')');
}
return b.ToString();
}
}
public struct FieldCache_Fields{
/// Indicator for StringIndex values in the cache.
// NOTE: the value assigned to this constant must not be
// the same as any of those in SortField!!
public readonly static int STRING_INDEX = - 1;
/// Expert: The cache used internally by sorting and range query classes.
public readonly static FieldCache DEFAULT;
/// The default parser for byte values, which are encoded by {@link Byte#toString(byte)}
public readonly static ByteParser DEFAULT_BYTE_PARSER;
/// The default parser for short values, which are encoded by {@link Short#toString(short)}
public readonly static ShortParser DEFAULT_SHORT_PARSER;
/// The default parser for int values, which are encoded by {@link Integer#toString(int)}
public readonly static IntParser DEFAULT_INT_PARSER;
/// The default parser for float values, which are encoded by {@link Float#toString(float)}
public readonly static FloatParser DEFAULT_FLOAT_PARSER;
/// The default parser for long values, which are encoded by {@link Long#toString(long)}
public readonly static LongParser DEFAULT_LONG_PARSER;
/// The default parser for double values, which are encoded by {@link Double#toString(double)}
public readonly static DoubleParser DEFAULT_DOUBLE_PARSER;
/// A parser instance for int values encoded by {@link NumericUtils#IntToPrefixCoded(int)}, e.g. when indexed
/// via {@link NumericField}/{@link NumericTokenStream}.
///
public readonly static IntParser NUMERIC_UTILS_INT_PARSER;
/// A parser instance for float values encoded with {@link NumericUtils}, e.g. when indexed
/// via {@link NumericField}/{@link NumericTokenStream}.
///
public readonly static FloatParser NUMERIC_UTILS_FLOAT_PARSER;
/// A parser instance for long values encoded by {@link NumericUtils#LongToPrefixCoded(long)}, e.g. when indexed
/// via {@link NumericField}/{@link NumericTokenStream}.
///
public readonly static LongParser NUMERIC_UTILS_LONG_PARSER;
/// A parser instance for double values encoded with {@link NumericUtils}, e.g. when indexed
/// via {@link NumericField}/{@link NumericTokenStream}.
///
public readonly static DoubleParser NUMERIC_UTILS_DOUBLE_PARSER;
static FieldCache_Fields()
{
DEFAULT = new FieldCacheImpl();
DEFAULT_BYTE_PARSER = new AnonymousClassByteParser();
DEFAULT_SHORT_PARSER = new AnonymousClassShortParser();
DEFAULT_INT_PARSER = new AnonymousClassIntParser();
DEFAULT_FLOAT_PARSER = new AnonymousClassFloatParser();
DEFAULT_LONG_PARSER = new AnonymousClassLongParser();
DEFAULT_DOUBLE_PARSER = new AnonymousClassDoubleParser();
NUMERIC_UTILS_INT_PARSER = new AnonymousClassIntParser1();
NUMERIC_UTILS_FLOAT_PARSER = new AnonymousClassFloatParser1();
NUMERIC_UTILS_LONG_PARSER = new AnonymousClassLongParser1();
NUMERIC_UTILS_DOUBLE_PARSER = new AnonymousClassDoubleParser1();
}
}
[Serializable]
class AnonymousClassByteParser : ByteParser
{
public virtual sbyte ParseByte(System.String value_Renamed)
{
return System.SByte.Parse(value_Renamed);
}
protected internal virtual System.Object ReadResolve()
{
return Lucene.Net.Search.FieldCache_Fields.DEFAULT_BYTE_PARSER;
}
public override System.String ToString()
{
return typeof(FieldCache).FullName + ".DEFAULT_BYTE_PARSER";
}
}
[Serializable]
class AnonymousClassShortParser : ShortParser
{
public virtual short ParseShort(System.String value_Renamed)
{
return System.Int16.Parse(value_Renamed);
}
protected internal virtual System.Object ReadResolve()
{
return Lucene.Net.Search.FieldCache_Fields.DEFAULT_SHORT_PARSER;
}
public override System.String ToString()
{
return typeof(FieldCache).FullName + ".DEFAULT_SHORT_PARSER";
}
}
[Serializable]
class AnonymousClassIntParser : IntParser
{
public virtual int ParseInt(System.String value_Renamed)
{
return System.Int32.Parse(value_Renamed);
}
protected internal virtual System.Object ReadResolve()
{
return Lucene.Net.Search.FieldCache_Fields.DEFAULT_INT_PARSER;
}
public override System.String ToString()
{
return typeof(FieldCache).FullName + ".DEFAULT_INT_PARSER";
}
}
[Serializable]
class AnonymousClassFloatParser : FloatParser
{
public virtual float ParseFloat(System.String value_Renamed)
{
try
{
return SupportClass.Single.Parse(value_Renamed);
}
catch (System.OverflowException)
{
return value_Renamed.StartsWith("-") ? float.PositiveInfinity : float.NegativeInfinity;
}
}
protected internal virtual System.Object ReadResolve()
{
return Lucene.Net.Search.FieldCache_Fields.DEFAULT_FLOAT_PARSER;
}
public override System.String ToString()
{
return typeof(FieldCache).FullName + ".DEFAULT_FLOAT_PARSER";
}
}
[Serializable]
class AnonymousClassLongParser : LongParser
{
public virtual long ParseLong(System.String value_Renamed)
{
return System.Int64.Parse(value_Renamed);
}
protected internal virtual System.Object ReadResolve()
{
return Lucene.Net.Search.FieldCache_Fields.DEFAULT_LONG_PARSER;
}
public override System.String ToString()
{
return typeof(FieldCache).FullName + ".DEFAULT_LONG_PARSER";
}
}
[Serializable]
class AnonymousClassDoubleParser : DoubleParser
{
public virtual double ParseDouble(System.String value_Renamed)
{
return SupportClass.Double.Parse(value_Renamed);
}
protected internal virtual System.Object ReadResolve()
{
return Lucene.Net.Search.FieldCache_Fields.DEFAULT_DOUBLE_PARSER;
}
public override System.String ToString()
{
return typeof(FieldCache).FullName + ".DEFAULT_DOUBLE_PARSER";
}
}
[Serializable]
class AnonymousClassIntParser1 : IntParser
{
public virtual int ParseInt(System.String val)
{
int shift = val[0] - NumericUtils.SHIFT_START_INT;
if (shift > 0 && shift <= 31)
throw new FieldCacheImpl.StopFillCacheException();
return NumericUtils.PrefixCodedToInt(val);
}
protected internal virtual System.Object ReadResolve()
{
return Lucene.Net.Search.FieldCache_Fields.NUMERIC_UTILS_INT_PARSER;
}
public override System.String ToString()
{
return typeof(FieldCache).FullName + ".NUMERIC_UTILS_INT_PARSER";
}
}
[Serializable]
class AnonymousClassFloatParser1 : FloatParser
{
public virtual float ParseFloat(System.String val)
{
int shift = val[0] - NumericUtils.SHIFT_START_INT;
if (shift > 0 && shift <= 31)
throw new FieldCacheImpl.StopFillCacheException();
return NumericUtils.SortableIntToFloat(NumericUtils.PrefixCodedToInt(val));
}
protected internal virtual System.Object ReadResolve()
{
return Lucene.Net.Search.FieldCache_Fields.NUMERIC_UTILS_FLOAT_PARSER;
}
public override System.String ToString()
{
return typeof(FieldCache).FullName + ".NUMERIC_UTILS_FLOAT_PARSER";
}
}
[Serializable]
class AnonymousClassLongParser1 : LongParser
{
public virtual long ParseLong(System.String val)
{
int shift = val[0] - NumericUtils.SHIFT_START_LONG;
if (shift > 0 && shift <= 63)
throw new FieldCacheImpl.StopFillCacheException();
return NumericUtils.PrefixCodedToLong(val);
}
protected internal virtual System.Object ReadResolve()
{
return Lucene.Net.Search.FieldCache_Fields.NUMERIC_UTILS_LONG_PARSER;
}
public override System.String ToString()
{
return typeof(FieldCache).FullName + ".NUMERIC_UTILS_LONG_PARSER";
}
}
[Serializable]
class AnonymousClassDoubleParser1 : DoubleParser
{
public virtual double ParseDouble(System.String val)
{
int shift = val[0] - NumericUtils.SHIFT_START_LONG;
if (shift > 0 && shift <= 63)
throw new FieldCacheImpl.StopFillCacheException();
return NumericUtils.SortableLongToDouble(NumericUtils.PrefixCodedToLong(val));
}
protected internal virtual System.Object ReadResolve()
{
return Lucene.Net.Search.FieldCache_Fields.NUMERIC_UTILS_DOUBLE_PARSER;
}
public override System.String ToString()
{
return typeof(FieldCache).FullName + ".NUMERIC_UTILS_DOUBLE_PARSER";
}
}
public interface FieldCache
{
/// Checks the internal cache for an appropriate entry, and if none is
/// found, reads the terms in field
as a single byte and returns an array
/// of size reader.maxDoc()
of the value each document
/// has in the given field.
///
/// Used to get field values.
///
/// Which field contains the single byte values.
///
/// The values in the given field for each document.
///
/// IOException If any error occurs.
sbyte[] GetBytes(IndexReader reader, System.String field);
/// Checks the internal cache for an appropriate entry, and if none is found,
/// reads the terms in field
as bytes and returns an array of
/// size reader.maxDoc()
of the value each document has in the
/// given field.
///
/// Used to get field values.
///
/// Which field contains the bytes.
///
/// Computes byte for string values.
///
/// The values in the given field for each document.
///
/// IOException If any error occurs.
sbyte[] GetBytes(IndexReader reader, System.String field, ByteParser parser);
/// Checks the internal cache for an appropriate entry, and if none is
/// found, reads the terms in field
as shorts and returns an array
/// of size reader.maxDoc()
of the value each document
/// has in the given field.
///
/// Used to get field values.
///
/// Which field contains the shorts.
///
/// The values in the given field for each document.
///
/// IOException If any error occurs.
short[] GetShorts(IndexReader reader, System.String field);
/// Checks the internal cache for an appropriate entry, and if none is found,
/// reads the terms in field
as shorts and returns an array of
/// size reader.maxDoc()
of the value each document has in the
/// given field.
///
/// Used to get field values.
///
/// Which field contains the shorts.
///
/// Computes short for string values.
///
/// The values in the given field for each document.
///
/// IOException If any error occurs.
short[] GetShorts(IndexReader reader, System.String field, ShortParser parser);
/// Checks the internal cache for an appropriate entry, and if none is
/// found, reads the terms in field
as integers and returns an array
/// of size reader.maxDoc()
of the value each document
/// has in the given field.
///
/// Used to get field values.
///
/// Which field contains the integers.
///
/// The values in the given field for each document.
///
/// IOException If any error occurs.
int[] GetInts(IndexReader reader, System.String field);
/// Checks the internal cache for an appropriate entry, and if none is found,
/// reads the terms in field
as integers and returns an array of
/// size reader.maxDoc()
of the value each document has in the
/// given field.
///
/// Used to get field values.
///
/// Which field contains the integers.
///
/// Computes integer for string values.
///
/// The values in the given field for each document.
///
/// IOException If any error occurs.
int[] GetInts(IndexReader reader, System.String field, IntParser parser);
/// Checks the internal cache for an appropriate entry, and if
/// none is found, reads the terms in field
as floats and returns an array
/// of size reader.maxDoc()
of the value each document
/// has in the given field.
///
/// Used to get field values.
///
/// Which field contains the floats.
///
/// The values in the given field for each document.
///
/// IOException If any error occurs.
float[] GetFloats(IndexReader reader, System.String field);
/// Checks the internal cache for an appropriate entry, and if
/// none is found, reads the terms in field
as floats and returns an array
/// of size reader.maxDoc()
of the value each document
/// has in the given field.
///
/// Used to get field values.
///
/// Which field contains the floats.
///
/// Computes float for string values.
///
/// The values in the given field for each document.
///
/// IOException If any error occurs.
float[] GetFloats(IndexReader reader, System.String field, FloatParser parser);
/// Checks the internal cache for an appropriate entry, and if none is
/// found, reads the terms in field
as longs and returns an array
/// of size reader.maxDoc()
of the value each document
/// has in the given field.
///
///
/// Used to get field values.
///
/// Which field contains the longs.
///
/// The values in the given field for each document.
///
/// java.io.IOException If any error occurs.
long[] GetLongs(IndexReader reader, System.String field);
/// Checks the internal cache for an appropriate entry, and if none is found,
/// reads the terms in field
as longs and returns an array of
/// size reader.maxDoc()
of the value each document has in the
/// given field.
///
///
/// Used to get field values.
///
/// Which field contains the longs.
///
/// Computes integer for string values.
///
/// The values in the given field for each document.
///
/// IOException If any error occurs.
long[] GetLongs(IndexReader reader, System.String field, LongParser parser);
/// Checks the internal cache for an appropriate entry, and if none is
/// found, reads the terms in field
as integers and returns an array
/// of size reader.maxDoc()
of the value each document
/// has in the given field.
///
///
/// Used to get field values.
///
/// Which field contains the doubles.
///
/// The values in the given field for each document.
///
/// IOException If any error occurs.
double[] GetDoubles(IndexReader reader, System.String field);
/// Checks the internal cache for an appropriate entry, and if none is found,
/// reads the terms in field
as doubles and returns an array of
/// size reader.maxDoc()
of the value each document has in the
/// given field.
///
///
/// Used to get field values.
///
/// Which field contains the doubles.
///
/// Computes integer for string values.
///
/// The values in the given field for each document.
///
/// IOException If any error occurs.
double[] GetDoubles(IndexReader reader, System.String field, DoubleParser parser);
/// Checks the internal cache for an appropriate entry, and if none
/// is found, reads the term values in field
and returns an array
/// of size reader.maxDoc()
containing the value each document
/// has in the given field.
///
/// Used to get field values.
///
/// Which field contains the strings.
///
/// The values in the given field for each document.
///
/// IOException If any error occurs.
System.String[] GetStrings(IndexReader reader, System.String field);
/// Checks the internal cache for an appropriate entry, and if none
/// is found reads the term values in field
and returns
/// an array of them in natural order, along with an array telling
/// which element in the term array each document uses.
///
/// Used to get field values.
///
/// Which field contains the strings.
///
/// Array of terms and index into the array for each document.
///
/// IOException If any error occurs.
StringIndex GetStringIndex(IndexReader reader, System.String field);
/// Checks the internal cache for an appropriate entry, and if
/// none is found reads field
to see if it contains integers, longs, floats
/// or strings, and then calls one of the other methods in this class to get the
/// values. For string values, a StringIndex is returned. After
/// calling this method, there is an entry in the cache for both
/// type AUTO
and the actual found type.
///
/// Used to get field values.
///
/// Which field contains the values.
///
/// int[], long[], float[] or StringIndex.
///
/// IOException If any error occurs.
/// Please specify the exact type, instead.
/// Especially, guessing does not work with the new
/// {@link NumericField} type.
///
[Obsolete("Please specify the exact type, instead. Especially, guessing does not work with the new NumericField type.")]
System.Object GetAuto(IndexReader reader, System.String field);
/// Checks the internal cache for an appropriate entry, and if none
/// is found reads the terms out of field
and calls the given SortComparator
/// to get the sort values. A hit in the cache will happen if reader
,
/// field
, and comparator
are the same (using equals()
)
/// as a previous call to this method.
///
/// Used to get field values.
///
/// Which field contains the values.
///
/// Used to convert terms into something to sort by.
///
/// Array of sort objects, one for each document.
///
/// IOException If any error occurs.
/// Please implement {@link
/// FieldComparatorSource} directly, instead.
///
[Obsolete("Please implement FieldComparatorSource directly, instead.")]
System.IComparable[] GetCustom(IndexReader reader, System.String field, SortComparator comparator);
/// EXPERT: Generates an array of CacheEntry objects representing all items
/// currently in the FieldCache.
///
/// NOTE: These CacheEntry objects maintain a strong refrence to the
/// Cached Values. Maintaining refrences to a CacheEntry the IndexReader
/// associated with it has garbage collected will prevent the Value itself
/// from being garbage collected when the Cache drops the WeakRefrence.
///
///
/// EXPERIMENTAL API: This API is considered extremely advanced
/// and experimental. It may be removed or altered w/o warning in future
/// releases
/// of Lucene.
///
///
CacheEntry[] GetCacheEntries();
///
/// EXPERT: Instructs the FieldCache to forcibly expunge all entries
/// from the underlying caches. This is intended only to be used for
/// test methods as a way to ensure a known base state of the Cache
/// (with out needing to rely on GC to free WeakReferences).
/// It should not be relied on for "Cache maintenance" in general
/// application code.
///
///
/// EXPERIMENTAL API: This API is considered extremely advanced
/// and experimental. It may be removed or altered w/o warning in future
/// releases
/// of Lucene.
///
///
void PurgeAllCaches();
/// If non-null, FieldCacheImpl will warn whenever
/// entries are created that are not sane according to
/// {@link Lucene.Net.Util.FieldCacheSanityChecker}.
///
void SetInfoStream(System.IO.StreamWriter stream);
/// counterpart of {@link #SetInfoStream(PrintStream)}
System.IO.StreamWriter GetInfoStream();
}
/// Marker interface as super-interface to all parsers. It
/// is used to specify a custom parser to {@link
/// SortField#SortField(String, FieldCache.Parser)}.
///
public interface Parser
{
}
/// Interface to parse bytes from document fields.
///
///
public interface ByteParser:Parser
{
/// Return a single Byte representation of this field's value.
sbyte ParseByte(System.String string_Renamed);
}
/// Interface to parse shorts from document fields.
///
///
public interface ShortParser:Parser
{
/// Return a short representation of this field's value.
short ParseShort(System.String string_Renamed);
}
/// Interface to parse ints from document fields.
///
///
public interface IntParser:Parser
{
/// Return an integer representation of this field's value.
int ParseInt(System.String string_Renamed);
}
/// Interface to parse floats from document fields.
///
///
public interface FloatParser:Parser
{
/// Return an float representation of this field's value.
float ParseFloat(System.String string_Renamed);
}
/// Interface to parse long from document fields.
///
///
/// Use {@link FieldCache.LongParser}, this will be removed in Lucene 3.0
///
[Obsolete("Use FieldCache.LongParser, this will be removed in Lucene 3.0")]
public interface LongParser:Parser
{
/// Return an long representation of this field's value.
long ParseLong(System.String string_Renamed);
}
/// Interface to parse doubles from document fields.
///
///
/// Use {@link FieldCache.DoubleParser}, this will be removed in Lucene 3.0
///
[Obsolete("Use FieldCache.DoubleParser, this will be removed in Lucene 3.0 ")]
public interface DoubleParser:Parser
{
/// Return an long representation of this field's value.
double ParseDouble(System.String string_Renamed);
}
}