📄 fieldsreader.cs
字号:
else
{
f = new Field(fi.name, fieldsStream.ReadString(), store, index, termVector);
f.SetOmitNorms(fi.omitNorms);
}
doc.Add(f);
}
}
// Add the size of field as a byte[] containing the 4 bytes of the integer byte size (high order byte first; char = 2 bytes)
// Read just the size -- caller must skip the field content to continue reading fields
// Return the size in bytes or chars, depending on field type
private int AddFieldSize(Document doc, FieldInfo fi, bool binary, bool compressed)
{
int size = fieldsStream.ReadVInt(), bytesize = binary || compressed ? size : 2 * size;
byte[] sizebytes = new byte[4];
sizebytes[0] = (byte) (SupportClass.Number.URShift(bytesize, 24));
sizebytes[1] = (byte) (SupportClass.Number.URShift(bytesize, 16));
sizebytes[2] = (byte) (SupportClass.Number.URShift(bytesize, 8));
sizebytes[3] = (byte) bytesize;
doc.Add(new Field(fi.name, sizebytes, Field.Store.YES));
return size;
}
private Field.TermVector GetTermVectorType(FieldInfo fi)
{
Field.TermVector termVector = null;
if (fi.storeTermVector)
{
if (fi.storeOffsetWithTermVector)
{
if (fi.storePositionWithTermVector)
{
termVector = Field.TermVector.WITH_POSITIONS_OFFSETS;
}
else
{
termVector = Field.TermVector.WITH_OFFSETS;
}
}
else if (fi.storePositionWithTermVector)
{
termVector = Field.TermVector.WITH_POSITIONS;
}
else
{
termVector = Field.TermVector.YES;
}
}
else
{
termVector = Field.TermVector.NO;
}
return termVector;
}
private Field.Index GetIndexType(FieldInfo fi, bool tokenize)
{
Field.Index index;
if (fi.isIndexed && tokenize)
index = Field.Index.TOKENIZED;
else if (fi.isIndexed && !tokenize)
index = Field.Index.UN_TOKENIZED;
else
index = Field.Index.NO;
return index;
}
/// <summary> A Lazy implementation of Fieldable that differs loading of fields until asked for, instead of when the Document is
/// loaded.
/// </summary>
[Serializable]
private class LazyField:AbstractField, Fieldable
{
private void InitBlock(FieldsReader enclosingInstance)
{
this.enclosingInstance = enclosingInstance;
}
private FieldsReader enclosingInstance;
public FieldsReader Enclosing_Instance
{
get
{
return enclosingInstance;
}
}
private int toRead;
private long pointer;
public LazyField(FieldsReader enclosingInstance, System.String name, Field.Store store, int toRead, long pointer):base(name, store, Field.Index.NO, Field.TermVector.NO)
{
InitBlock(enclosingInstance);
this.toRead = toRead;
this.pointer = pointer;
lazy = true;
}
public LazyField(FieldsReader enclosingInstance, System.String name, Field.Store store, Field.Index index, Field.TermVector termVector, int toRead, long pointer):base(name, store, index, termVector)
{
InitBlock(enclosingInstance);
this.toRead = toRead;
this.pointer = pointer;
lazy = true;
}
private IndexInput GetFieldStream()
{
IndexInput localFieldsStream = (IndexInput) System.Threading.Thread.GetData(Enclosing_Instance.fieldsStreamTL);
if (localFieldsStream == null)
{
localFieldsStream = (IndexInput) Enclosing_Instance.cloneableFieldsStream.Clone();
System.Threading.Thread.SetData(Enclosing_Instance.fieldsStreamTL, localFieldsStream);
}
return localFieldsStream;
}
/// <summary> The value of the field in Binary, or null. If null, the Reader or
/// String value is used. Exactly one of stringValue(), readerValue() and
/// binaryValue() must be set.
/// </summary>
public override byte[] BinaryValue()
{
if (fieldsData == null)
{
byte[] b = new byte[toRead];
IndexInput localFieldsStream = GetFieldStream();
//Throw this IO Exception since IndexREader.document does so anyway, so probably not that big of a change for people
//since they are already handling this exception when getting the document
try
{
localFieldsStream.Seek(pointer);
localFieldsStream.ReadBytes(b, 0, b.Length);
if (isCompressed == true)
{
fieldsData = Enclosing_Instance.Uncompress(b);
}
else
{
fieldsData = b;
}
}
catch (System.IO.IOException e)
{
throw new FieldReaderException(e);
}
}
return fieldsData is byte[] ? (byte[]) fieldsData : null;
}
/// <summary> The value of the field as a Reader, or null. If null, the String value
/// or binary value is used. Exactly one of stringValue(), readerValue(),
/// and binaryValue() must be set.
/// </summary>
public override System.IO.TextReader ReaderValue()
{
return fieldsData is System.IO.TextReader ? (System.IO.TextReader) fieldsData : null;
}
/// <summary> The value of the field as a String, or null. If null, the Reader value
/// or binary value is used. Exactly one of stringValue(), readerValue(), and
/// binaryValue() must be set.
/// </summary>
public override System.String StringValue()
{
if (fieldsData == null)
{
IndexInput localFieldsStream = GetFieldStream();
try
{
localFieldsStream.Seek(pointer);
if (isCompressed)
{
byte[] b = new byte[toRead];
localFieldsStream.ReadBytes(b, 0, b.Length);
fieldsData = System.Text.Encoding.GetEncoding("UTF-8").GetString(Enclosing_Instance.Uncompress(b));
}
else
{
//read in chars b/c we already know the length we need to read
char[] chars = new char[toRead];
localFieldsStream.ReadChars(chars, 0, toRead);
fieldsData = new System.String(chars);
}
}
catch (System.IO.IOException e)
{
throw new FieldReaderException(e);
}
}
return fieldsData is System.String ? (System.String) fieldsData : null;
}
public long GetPointer()
{
return pointer;
}
public void SetPointer(long pointer)
{
this.pointer = pointer;
}
public int GetToRead()
{
return toRead;
}
public void SetToRead(int toRead)
{
this.toRead = toRead;
}
}
private byte[] Uncompress(byte[] input)
{
return SupportClass.CompressionSupport.Uncompress(input);
}
// Instances of this class hold field properties and data
// for merge
[Serializable]
public sealed class FieldForMerge : AbstractField
{
public override System.String StringValue()
{
return (System.String) this.fieldsData;
}
public override System.IO.TextReader ReaderValue()
{
// not needed for merge
return null;
}
public override byte[] BinaryValue()
{
return (byte[]) this.fieldsData;
}
public FieldForMerge(System.Object value_Renamed, FieldInfo fi, bool binary, bool compressed, bool tokenize)
{
this.isStored = true;
this.fieldsData = value_Renamed;
this.isCompressed = compressed;
this.isBinary = binary;
this.isTokenized = tokenize;
this.name = String.Intern(fi.name);
this.isIndexed = fi.isIndexed;
this.omitNorms = fi.omitNorms;
this.storeOffsetWithTermVector = fi.storeOffsetWithTermVector;
this.storePositionWithTermVector = fi.storePositionWithTermVector;
this.storeTermVector = fi.storeTermVector;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -