/*
* 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 System.IO;
using Lucene.Net.Support;
using Double = Lucene.Net.Support.Double;
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;
using Single = Lucene.Net.Support.Single;
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 = 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 object ReaderKey { get; }
public abstract string FieldName { get; }
public abstract Type CacheType { get; }
public abstract object Custom { get; }
public abstract object Value { get; }
///
///
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(Value);
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#
}
/// The most recently estimated size of the value, null unless
/// estimateSize has been called.
///
public string EstimatedSize { get; protected internal set; }
public override System.String ToString()
{
var b = new System.Text.StringBuilder();
b.Append("'").Append(ReaderKey).Append("'=>");
b.Append("'").Append(FieldName).Append("',");
b.Append(CacheType).Append(",").Append(Custom);
b.Append("=>").Append(Value.GetType().FullName).Append("#");
b.Append(Value.GetHashCode());
System.String s = EstimatedSize;
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
public readonly static ByteParser DEFAULT_BYTE_PARSER;
/// The default parser for short values, which are encoded by
public readonly static ShortParser DEFAULT_SHORT_PARSER;
/// The default parser for int values, which are encoded by
public readonly static IntParser DEFAULT_INT_PARSER;
/// The default parser for float values, which are encoded by
public readonly static FloatParser DEFAULT_FLOAT_PARSER;
/// The default parser for long values, which are encoded by
public readonly static LongParser DEFAULT_LONG_PARSER;
/// The default parser for double values, which are encoded by
public readonly static DoubleParser DEFAULT_DOUBLE_PARSER;
/// A parser instance for int values encoded by , e.g. when indexed
/// via /.
///
public readonly static IntParser NUMERIC_UTILS_INT_PARSER;
/// A parser instance for float values encoded with , e.g. when indexed
/// via /.
///
public readonly static FloatParser NUMERIC_UTILS_FLOAT_PARSER;
/// A parser instance for long values encoded by , e.g. when indexed
/// via /.
///
public readonly static LongParser NUMERIC_UTILS_LONG_PARSER;
/// A parser instance for double values encoded with , e.g. when indexed
/// via /.
///
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 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 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);
/// 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();
///
/// Expert: drops all cache entries associated with this
/// reader. NOTE: this reader must precisely match the
/// reader that the cache entry is keyed on. If you pass a
/// top-level reader, it usually will have no effect as
/// Lucene now caches at the segment reader level.
///
void Purge(IndexReader r);
/// Gets or sets the InfoStream for this FieldCache.
/// If non-null, FieldCacheImpl will warn whenever
/// entries are created that are not sane according to
/// .
///
///
StreamWriter InfoStream { get; set; }
}
/// Marker interface as super-interface to all parsers. It
/// is used to specify a custom parser to .
///
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 , 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 , 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);
}
}