public override void Init(IndexOutput termsOut) { CodecUtil.WriteHeader(termsOut, Lucene40PostingsReader.TERMS_CODEC, Lucene40PostingsReader.VERSION_CURRENT); termsOut.WriteInt32(skipInterval); // write skipInterval termsOut.WriteInt32(maxSkipLevels); // write maxSkipLevels termsOut.WriteInt32(skipMinimum); // write skipMinimum }
public override void Init(IndexOutput termsOut) { CodecUtil.WriteHeader(termsOut, CODEC, VERSION_CURRENT); // TODO: -- just ask skipper to "start" here termsOut.WriteInt32(skipInterval); // write skipInterval termsOut.WriteInt32(maxSkipLevels); // write maxSkipLevels termsOut.WriteInt32(skipMinimum); // write skipMinimum }
[ExceptionToNetNumericConvention] // LUCENENET: Private API, keeping as-is private void AddIntsField(FieldInfo field, IndexOutput output, IEnumerable <long?> values) { field.PutAttribute(legacyKey, LegacyDocValuesType.FIXED_INTS_32.ToString()); CodecUtil.WriteHeader(output, Lucene40DocValuesFormat.INTS_CODEC_NAME, Lucene40DocValuesFormat.INTS_VERSION_CURRENT); output.WriteInt32(4); // size foreach (long?n in values) { output.WriteInt32((int)n.GetValueOrDefault()); } }
protected override void Dispose(bool disposing) { if (disposing) { _delegateFieldsConsumer.Dispose(); // Now we are done accumulating values for these fields var nonSaturatedBlooms = new List <KeyValuePair <FieldInfo, FuzzySet> >(); foreach (var entry in _bloomFilters) { var bloomFilter = entry.Value; if (!outerInstance._bloomFilterFactory.IsSaturated(bloomFilter, entry.Key)) { nonSaturatedBlooms.Add(entry); } } var bloomFileName = IndexFileNames.SegmentFileName( _state.SegmentInfo.Name, _state.SegmentSuffix, BLOOM_EXTENSION); IndexOutput bloomOutput = null; try { bloomOutput = _state.Directory.CreateOutput(bloomFileName, _state.Context); CodecUtil.WriteHeader(bloomOutput, /*BLOOM_CODEC_NAME*/ outerInstance.Name, VERSION_CURRENT); // remember the name of the postings format we will delegate to bloomOutput.WriteString(outerInstance._delegatePostingsFormat.Name); // First field in the output file is the number of fields+blooms saved bloomOutput.WriteInt32(nonSaturatedBlooms.Count); foreach (var entry in nonSaturatedBlooms) { var fieldInfo = entry.Key; var bloomFilter = entry.Value; bloomOutput.WriteInt32(fieldInfo.Number); SaveAppropriatelySizedBloomFilter(bloomOutput, bloomFilter, fieldInfo); } CodecUtil.WriteFooter(bloomOutput); } finally { IOUtils.Dispose(bloomOutput); } //We are done with large bitsets so no need to keep them hanging around _bloomFilters.Clear(); } }
public PreFlexRWStoredFieldsWriter(Directory directory, string segment, IOContext context) { Debug.Assert(directory != null); this.directory = directory; this.segment = segment; bool success = false; try { fieldsStream = directory.CreateOutput(IndexFileNames.SegmentFileName(segment, "", Lucene3xStoredFieldsReader.FIELDS_EXTENSION), context); indexStream = directory.CreateOutput(IndexFileNames.SegmentFileName(segment, "", Lucene3xStoredFieldsReader.FIELDS_INDEX_EXTENSION), context); fieldsStream.WriteInt32(Lucene3xStoredFieldsReader.FORMAT_CURRENT); indexStream.WriteInt32(Lucene3xStoredFieldsReader.FORMAT_CURRENT); success = true; } finally { if (!success) { Abort(); } } }
/// <summary> /// Writes this vector to the file <paramref name="name"/> in Directory /// <paramref name="d"/>, in a format that can be read by the constructor /// <see cref="BitVector(Directory, string, IOContext)"/>. /// </summary> public void Write(Directory d, string name, IOContext context) { if (Debugging.AssertsEnabled) { Debugging.Assert(!(d is CompoundFileDirectory)); } IndexOutput output = d.CreateOutput(name, context); try { output.WriteInt32(-2); CodecUtil.WriteHeader(output, CODEC, VERSION_CURRENT); if (IsSparse) { // sparse bit-set more efficiently saved as d-gaps. WriteClearedDgaps(output); } else { WriteBits(output); } CodecUtil.WriteFooter(output); if (Debugging.AssertsEnabled) { Debugging.Assert(VerifyCount()); } } finally { IOUtils.Dispose(output); } }
public PreFlexRWTermVectorsWriter(Directory directory, string segment, IOContext context) { this.directory = directory; this.segment = segment; bool success = false; try { // Open files for TermVector storage tvx = directory.CreateOutput(IndexFileNames.SegmentFileName(segment, "", Lucene3xTermVectorsReader.VECTORS_INDEX_EXTENSION), context); tvx.WriteInt32(Lucene3xTermVectorsReader.FORMAT_CURRENT); tvd = directory.CreateOutput(IndexFileNames.SegmentFileName(segment, "", Lucene3xTermVectorsReader.VECTORS_DOCUMENTS_EXTENSION), context); tvd.WriteInt32(Lucene3xTermVectorsReader.FORMAT_CURRENT); tvf = directory.CreateOutput(IndexFileNames.SegmentFileName(segment, "", Lucene3xTermVectorsReader.VECTORS_FIELDS_EXTENSION), context); tvf.WriteInt32(Lucene3xTermVectorsReader.FORMAT_CURRENT); success = true; } finally { if (!success) { Abort(); } } }
private void AddFixedSortedBytesField(FieldInfo field, IndexOutput data, IndexOutput index, IEnumerable <BytesRef> values, IEnumerable <long?> docToOrd, int length) { field.PutAttribute(legacyKey, LegacyDocValuesType.BYTES_FIXED_SORTED.ToString()); CodecUtil.WriteHeader(data, Lucene40DocValuesFormat.BYTES_FIXED_SORTED_CODEC_NAME_DAT, Lucene40DocValuesFormat.BYTES_FIXED_SORTED_VERSION_CURRENT); CodecUtil.WriteHeader(index, Lucene40DocValuesFormat.BYTES_FIXED_SORTED_CODEC_NAME_IDX, Lucene40DocValuesFormat.BYTES_FIXED_SORTED_VERSION_CURRENT); /* values */ data.WriteInt32(length); int valueCount = 0; foreach (BytesRef v in values) { data.WriteBytes(v.Bytes, v.Offset, v.Length); valueCount++; } /* ordinals */ index.WriteInt32(valueCount); int maxDoc = state.SegmentInfo.DocCount; Debug.Assert(valueCount > 0); PackedInt32s.Writer w = PackedInt32s.GetWriter(index, maxDoc, PackedInt32s.BitsRequired(valueCount - 1), PackedInt32s.DEFAULT); foreach (long n in docToOrd) { w.Add((long)n); } w.Finish(); }
/// <summary> /// Write as a d-gaps list. </summary> private void WriteClearedDgaps(IndexOutput output) { output.WriteInt32(-1); // mark using d-gaps output.WriteInt32(Length); // write size output.WriteInt32(Count()); // write count int last = 0; int numCleared = Length - Count(); for (int i = 0; i < bits.Length && numCleared > 0; i++) { if (bits[i] != 0xff) { output.WriteVInt32(i - last); output.WriteByte(bits[i]); last = i; numCleared -= (8 - BitUtil.BitCount(bits[i])); Debug.Assert(numCleared >= 0 || (i == (bits.Length - 1) && numCleared == -(8 - (size & 7)))); } } }
private void Initialize(Directory directory, string segment, FieldInfos fis, int interval, bool isi) { indexInterval = interval; fieldInfos = fis; isIndex = isi; output = directory.CreateOutput(IndexFileNames.SegmentFileName(segment, "", (isIndex ? Lucene3xPostingsFormat.TERMS_INDEX_EXTENSION : Lucene3xPostingsFormat.TERMS_EXTENSION)), IOContext.DEFAULT); bool success = false; try { output.WriteInt32(FORMAT_CURRENT); // write format output.WriteInt64(0); // leave space for size output.WriteInt32(indexInterval); // write indexInterval output.WriteInt32(skipInterval); // write skipInterval output.WriteInt32(maxSkipLevels); // write maxSkipLevels if (Debugging.AssertsEnabled) { Debugging.Assert(InitUTF16Results()); } success = true; } finally { if (!success) { IOUtils.DisposeWhileHandlingException(output); try { directory.DeleteFile(IndexFileNames.SegmentFileName(segment, "", (isIndex ? Lucene3xPostingsFormat.TERMS_INDEX_EXTENSION : Lucene3xPostingsFormat.TERMS_EXTENSION))); } #pragma warning disable 168 catch (IOException ignored) #pragma warning restore 168 { } } } }
private void AddFixedDerefBytesField(FieldInfo field, IndexOutput data, IndexOutput index, IEnumerable <BytesRef> values, int length) { field.PutAttribute(legacyKey, LegacyDocValuesType.BYTES_FIXED_DEREF.ToString()); CodecUtil.WriteHeader(data, Lucene40DocValuesFormat.BYTES_FIXED_DEREF_CODEC_NAME_DAT, Lucene40DocValuesFormat.BYTES_FIXED_DEREF_VERSION_CURRENT); CodecUtil.WriteHeader(index, Lucene40DocValuesFormat.BYTES_FIXED_DEREF_CODEC_NAME_IDX, Lucene40DocValuesFormat.BYTES_FIXED_DEREF_VERSION_CURRENT); // deduplicate JCG.SortedSet <BytesRef> dictionary = new JCG.SortedSet <BytesRef>(); foreach (BytesRef v in values) { dictionary.Add(v == null ? new BytesRef() : BytesRef.DeepCopyOf(v)); } /* values */ data.WriteInt32(length); foreach (BytesRef v in dictionary) { data.WriteBytes(v.Bytes, v.Offset, v.Length); } /* ordinals */ int valueCount = dictionary.Count; if (Debugging.AssertsEnabled) { Debugging.Assert(valueCount > 0); } index.WriteInt32(valueCount); int maxDoc = state.SegmentInfo.DocCount; PackedInt32s.Writer w = PackedInt32s.GetWriter(index, maxDoc, PackedInt32s.BitsRequired(valueCount - 1), PackedInt32s.DEFAULT); BytesRef brefDummy; foreach (BytesRef v in values) { brefDummy = v; if (v == null) { brefDummy = new BytesRef(); } //int ord = dictionary.HeadSet(brefDummy).Size(); int ord = dictionary.Count(@ref => @ref.CompareTo(brefDummy) < 0); w.Add(ord); } w.Finish(); }
private void AddFixedStraightBytesField(FieldInfo field, IndexOutput output, IEnumerable <BytesRef> values, int length) { field.PutAttribute(legacyKey, LegacyDocValuesType.BYTES_FIXED_STRAIGHT.ToString()); CodecUtil.WriteHeader(output, Lucene40DocValuesFormat.BYTES_FIXED_STRAIGHT_CODEC_NAME, Lucene40DocValuesFormat.BYTES_FIXED_STRAIGHT_VERSION_CURRENT); output.WriteInt32(length); foreach (BytesRef v in values) { if (v != null) { output.WriteBytes(v.Bytes, v.Offset, v.Length); } } }
public virtual void TestAppend() { Directory newDir = NewDirectory(); CompoundFileDirectory csw = new CompoundFileDirectory(newDir, "d.cfs", NewIOContext(Random), true); int size = 5 + Random.Next(128); for (int j = 0; j < 2; j++) { IndexOutput os = csw.CreateOutput("seg_" + j + "_foo.txt", NewIOContext(Random)); for (int i = 0; i < size; i++) { os.WriteInt32(i * j); } os.Dispose(); string[] listAll = newDir.ListAll(); Assert.AreEqual(1, listAll.Length); Assert.AreEqual("d.cfs", listAll[0]); } CreateSequenceFile(Dir, "d1", (sbyte)0, 15); Dir.Copy(csw, "d1", "d1", NewIOContext(Random)); string[] listAll_ = newDir.ListAll(); Assert.AreEqual(1, listAll_.Length); Assert.AreEqual("d.cfs", listAll_[0]); csw.Dispose(); CompoundFileDirectory csr = new CompoundFileDirectory(newDir, "d.cfs", NewIOContext(Random), false); for (int j = 0; j < 2; j++) { IndexInput openInput = csr.OpenInput("seg_" + j + "_foo.txt", NewIOContext(Random)); Assert.AreEqual(size * 4, openInput.Length); for (int i = 0; i < size; i++) { Assert.AreEqual(i * j, openInput.ReadInt32()); } openInput.Dispose(); } IndexInput expected = Dir.OpenInput("d1", NewIOContext(Random)); IndexInput actual = csr.OpenInput("d1", NewIOContext(Random)); AssertSameStreams("d1", expected, actual); AssertSameSeekBehavior("d1", expected, actual); expected.Dispose(); actual.Dispose(); csr.Dispose(); newDir.Dispose(); }
public override Int32IndexOutput CreateOutput(Directory dir, string fileName, IOContext context) { IndexOutput output = dir.CreateOutput(fileName, context); bool success = false; try { output.WriteInt32(baseBlockSize); VariableInt32BlockIndexOutput ret = new VariableInt32BlockIndexOutputAnonymousClass(output, baseBlockSize); success = true; return(ret); } finally { if (!success) { IOUtils.DisposeWhileHandlingException(output); } } }
public virtual void TestAppendTwice() { Directory newDir = NewDirectory(); CompoundFileDirectory csw = new CompoundFileDirectory(newDir, "d.cfs", NewIOContext(Random), true); CreateSequenceFile(newDir, "d1", (sbyte)0, 15); IndexOutput @out = csw.CreateOutput("d.xyz", NewIOContext(Random)); @out.WriteInt32(0); @out.Dispose(); Assert.AreEqual(1, csw.ListAll().Length); Assert.AreEqual("d.xyz", csw.ListAll()[0]); csw.Dispose(); CompoundFileDirectory cfr = new CompoundFileDirectory(newDir, "d.cfs", NewIOContext(Random), false); Assert.AreEqual(1, cfr.ListAll().Length); Assert.AreEqual("d.xyz", cfr.ListAll()[0]); cfr.Dispose(); newDir.Dispose(); }
//private readonly FieldInfos fieldInfos; // unread // LUCENENET: Not used public FixedGapTermsIndexWriter(SegmentWriteState state) { string indexFileName = IndexFileNames.SegmentFileName(state.SegmentInfo.Name, state.SegmentSuffix, TERMS_INDEX_EXTENSION); termIndexInterval = state.TermIndexInterval; m_output = state.Directory.CreateOutput(indexFileName, state.Context); bool success = false; try { //fieldInfos = state.FieldInfos; // LUCENENET: Not used WriteHeader(m_output); m_output.WriteInt32(termIndexInterval); success = true; } finally { if (!success) { IOUtils.DisposeWhileHandlingException(m_output); } } }
public virtual void TestReadNestedCFP() { Directory newDir = NewDirectory(); CompoundFileDirectory csw = new CompoundFileDirectory(newDir, "d.cfs", NewIOContext(Random), true); CompoundFileDirectory nested = new CompoundFileDirectory(newDir, "b.cfs", NewIOContext(Random), true); IndexOutput @out = nested.CreateOutput("b.xyz", NewIOContext(Random)); IndexOutput out1 = nested.CreateOutput("b_1.xyz", NewIOContext(Random)); @out.WriteInt32(0); out1.WriteInt32(1); @out.Dispose(); out1.Dispose(); nested.Dispose(); newDir.Copy(csw, "b.cfs", "b.cfs", NewIOContext(Random)); newDir.Copy(csw, "b.cfe", "b.cfe", NewIOContext(Random)); newDir.DeleteFile("b.cfs"); newDir.DeleteFile("b.cfe"); csw.Dispose(); Assert.AreEqual(2, newDir.ListAll().Length); csw = new CompoundFileDirectory(newDir, "d.cfs", NewIOContext(Random), false); Assert.AreEqual(2, csw.ListAll().Length); nested = new CompoundFileDirectory(csw, "b.cfs", NewIOContext(Random), false); Assert.AreEqual(2, nested.ListAll().Length); IndexInput openInput = nested.OpenInput("b.xyz", NewIOContext(Random)); Assert.AreEqual(0, openInput.ReadInt32()); openInput.Dispose(); openInput = nested.OpenInput("b_1.xyz", NewIOContext(Random)); Assert.AreEqual(1, openInput.ReadInt32()); openInput.Dispose(); nested.Dispose(); csw.Dispose(); newDir.Dispose(); }
/// <summary> /// Save a single segment's info. </summary> public override void Write(Directory dir, SegmentInfo si, FieldInfos fis, IOContext ioContext) { string fileName = IndexFileNames.SegmentFileName(si.Name, "", Lucene40SegmentInfoFormat.SI_EXTENSION); si.AddFile(fileName); IndexOutput output = dir.CreateOutput(fileName, ioContext); bool success = false; try { CodecUtil.WriteHeader(output, Lucene40SegmentInfoFormat.CODEC_NAME, Lucene40SegmentInfoFormat.VERSION_CURRENT); // Write the Lucene version that created this segment, since 3.1 output.WriteString(si.Version); output.WriteInt32(si.DocCount); output.WriteByte((byte)(sbyte)(si.UseCompoundFile ? SegmentInfo.YES : SegmentInfo.NO)); output.WriteStringStringMap(si.Diagnostics); output.WriteStringStringMap(Collections.EmptyMap <string, string>()); output.WriteStringSet(si.GetFiles()); success = true; } finally { if (!success) { IOUtils.DisposeWhileHandlingException(output); si.Dir.DeleteFile(fileName); } else { output.Dispose(); } } }
public virtual void TestDoubleClose() { Directory newDir = NewDirectory(); CompoundFileDirectory csw = new CompoundFileDirectory(newDir, "d.cfs", NewIOContext(Random), true); IndexOutput @out = csw.CreateOutput("d.xyz", NewIOContext(Random)); @out.WriteInt32(0); @out.Dispose(); csw.Dispose(); // close a second time - must have no effect according to IDisposable csw.Dispose(); csw = new CompoundFileDirectory(newDir, "d.cfs", NewIOContext(Random), false); IndexInput openInput = csw.OpenInput("d.xyz", NewIOContext(Random)); Assert.AreEqual(0, openInput.ReadInt32()); openInput.Dispose(); csw.Dispose(); // close a second time - must have no effect according to IDisposable csw.Dispose(); newDir.Dispose(); }
public override void Write(Directory directory, string segmentName, string segmentSuffix, FieldInfos infos, IOContext context) { string fileName = IndexFileNames.SegmentFileName(segmentName, "", FIELD_INFOS_EXTENSION); IndexOutput output = directory.CreateOutput(fileName, context); bool success = false; try { output.WriteVInt32(FORMAT_PREFLEX_RW); output.WriteVInt32(infos.Count); foreach (FieldInfo fi in infos) { sbyte bits = 0x0; if (fi.HasVectors) { bits |= STORE_TERMVECTOR; } if (fi.OmitsNorms) { bits |= OMIT_NORMS; } if (fi.HasPayloads) { bits |= STORE_PAYLOADS; } if (fi.IsIndexed) { bits |= IS_INDEXED; if (Debugging.AssertsEnabled) { Debugging.Assert(fi.IndexOptions == IndexOptions.DOCS_AND_FREQS_AND_POSITIONS || !fi.HasPayloads); } if (fi.IndexOptions == IndexOptions.DOCS_ONLY) { bits |= OMIT_TERM_FREQ_AND_POSITIONS; } else if (fi.IndexOptions == IndexOptions.DOCS_AND_FREQS) { bits |= OMIT_POSITIONS; } } output.WriteString(fi.Name); /* * we need to write the field number since IW tries * to stabelize the field numbers across segments so the * FI ordinal is not necessarily equivalent to the field number */ output.WriteInt32(fi.Number); output.WriteByte((byte)bits); if (fi.IsIndexed && !fi.OmitsNorms) { // to allow null norm types we need to indicate if norms are written // only in RW case output.WriteByte((byte)(fi.NormType == Index.DocValuesType.NONE ? 0 : 1)); } if (Debugging.AssertsEnabled) { Debugging.Assert(fi.Attributes is null); // not used or supported } } success = true; } finally { if (success) { output.Dispose(); } else { IOUtils.DisposeWhileHandlingException(output); } } }
private void FlushOffsets(int[] fieldNums) { bool hasOffsets = false; long[] sumPos = new long[fieldNums.Length]; long[] sumOffsets = new long[fieldNums.Length]; foreach (DocData dd in pendingDocs) { foreach (FieldData fd in dd.fields) { hasOffsets |= fd.hasOffsets; if (fd.hasOffsets && fd.hasPositions) { int fieldNumOff = Array.BinarySearch(fieldNums, fd.fieldNum); int pos = 0; for (int i = 0; i < fd.numTerms; ++i) { int previousPos = 0; int previousOff = 0; for (int j = 0; j < fd.freqs[i]; ++j) { int position = positionsBuf[fd.posStart + pos]; int startOffset = startOffsetsBuf[fd.offStart + pos]; sumPos[fieldNumOff] += position - previousPos; sumOffsets[fieldNumOff] += startOffset - previousOff; previousPos = position; previousOff = startOffset; ++pos; } } if (Debugging.AssertsEnabled) { Debugging.Assert(pos == fd.totalPositions); } } } } if (!hasOffsets) { // nothing to do return; } float[] charsPerTerm = new float[fieldNums.Length]; for (int i = 0; i < fieldNums.Length; ++i) { charsPerTerm[i] = (sumPos[i] <= 0 || sumOffsets[i] <= 0) ? 0 : (float)((double)sumOffsets[i] / sumPos[i]); } // start offsets for (int i = 0; i < fieldNums.Length; ++i) { vectorsStream.WriteInt32(J2N.BitConversion.SingleToRawInt32Bits(charsPerTerm[i])); } writer.Reset(vectorsStream); foreach (DocData dd in pendingDocs) { foreach (FieldData fd in dd.fields) { if ((fd.flags & OFFSETS) != 0) { int fieldNumOff = Array.BinarySearch(fieldNums, fd.fieldNum); float cpt = charsPerTerm[fieldNumOff]; int pos = 0; for (int i = 0; i < fd.numTerms; ++i) { int previousPos = 0; int previousOff = 0; for (int j = 0; j < fd.freqs[i]; ++j) { int position = fd.hasPositions ? positionsBuf[fd.posStart + pos] : 0; int startOffset = startOffsetsBuf[fd.offStart + pos]; writer.Add(startOffset - previousOff - (int)(cpt * (position - previousPos))); previousPos = position; previousOff = startOffset; ++pos; } } } } } writer.Finish(); // lengths writer.Reset(vectorsStream); foreach (DocData dd in pendingDocs) { foreach (FieldData fd in dd.fields) { if ((fd.flags & OFFSETS) != 0) { int pos = 0; for (int i = 0; i < fd.numTerms; ++i) { for (int j = 0; j < fd.freqs[i]; ++j) { writer.Add(lengthsBuf[fd.offStart + pos++] - fd.prefixLengths[i] - fd.suffixLengths[i]); } } if (Debugging.AssertsEnabled) { Debugging.Assert(pos == fd.totalPositions); } } } } writer.Finish(); }
/// <summary> /// Writes a codec footer, which records both a checksum /// algorithm ID and a checksum. This footer can /// be parsed and validated with /// <see cref="CheckFooter(ChecksumIndexInput)"/>. /// <para/> /// CodecFooter --> Magic,AlgorithmID,Checksum /// <list type="bullet"> /// <item><description>Magic --> Uint32 (<see cref="DataOutput.WriteInt32(int)"/>). this /// identifies the start of the footer. It is always <see cref="FOOTER_MAGIC"/>.</description></item> /// <item><description>AlgorithmID --> Uint32 (<see cref="DataOutput.WriteInt32(int)"/>). this /// indicates the checksum algorithm used. Currently this is always 0, /// for zlib-crc32.</description></item> /// <item><description>Checksum --> Uint32 (<see cref="DataOutput.WriteInt64(long)"/>). The /// actual checksum value for all previous bytes in the stream, including /// the bytes from Magic and AlgorithmID.</description></item> /// </list> /// </summary> /// <param name="out"> Output stream </param> /// <exception cref="IOException"> If there is an I/O error writing to the underlying medium. </exception> public static void WriteFooter(IndexOutput @out) { @out.WriteInt32(FOOTER_MAGIC); @out.WriteInt32(0); @out.WriteInt64(@out.Checksum); }
public override void WriteField(FieldInfo info, IIndexableField field) { fieldsStream.WriteVInt32(info.Number); int bits = 0; BytesRef bytes; string @string; // TODO: maybe a field should serialize itself? // this way we don't bake into indexer all these // specific encodings for different fields? and apps // can customize... // LUCENENET specific - To avoid boxing/unboxing, we don't // call GetNumericValue(). Instead, we check the field.NumericType and then // call the appropriate conversion method. if (field.NumericType != NumericFieldType.NONE) { switch (field.NumericType) { case NumericFieldType.BYTE: case NumericFieldType.INT16: case NumericFieldType.INT32: bits |= FIELD_IS_NUMERIC_INT; break; case NumericFieldType.INT64: bits |= FIELD_IS_NUMERIC_LONG; break; case NumericFieldType.SINGLE: bits |= FIELD_IS_NUMERIC_FLOAT; break; case NumericFieldType.DOUBLE: bits |= FIELD_IS_NUMERIC_DOUBLE; break; default: throw new ArgumentException("cannot store numeric type " + field.NumericType); } @string = null; bytes = null; } else { bytes = field.GetBinaryValue(); if (bytes != null) { bits |= FIELD_IS_BINARY; @string = null; } else { @string = field.GetStringValue(); if (@string == null) { throw new ArgumentException("field " + field.Name + " is stored but does not have binaryValue, stringValue nor numericValue"); } } } fieldsStream.WriteByte((byte)(sbyte)bits); if (bytes != null) { fieldsStream.WriteVInt32(bytes.Length); fieldsStream.WriteBytes(bytes.Bytes, bytes.Offset, bytes.Length); } else if (@string != null) { fieldsStream.WriteString(field.GetStringValue()); } else { switch (field.NumericType) { case NumericFieldType.BYTE: case NumericFieldType.INT16: case NumericFieldType.INT32: fieldsStream.WriteInt32(field.GetInt32Value().Value); break; case NumericFieldType.INT64: fieldsStream.WriteInt64(field.GetInt64Value().Value); break; case NumericFieldType.SINGLE: fieldsStream.WriteInt32(BitConversion.SingleToInt32Bits(field.GetSingleValue().Value)); break; case NumericFieldType.DOUBLE: fieldsStream.WriteInt64(BitConversion.DoubleToInt64Bits(field.GetDoubleValue().Value)); break; default: throw new InvalidOperationException("Cannot get here"); } } }
public override void WriteField(FieldInfo info, IIndexableField field) { fieldsStream.WriteVInt32(info.Number); int bits = 0; BytesRef bytes; string @string; // TODO: maybe a field should serialize itself? // this way we don't bake into indexer all these // specific encodings for different fields? and apps // can customize... object number = (object)field.GetNumericValue(); if (number != null) { if (number is sbyte || number is short || number is int) { bits |= FIELD_IS_NUMERIC_INT; } else if (number is long) { bits |= FIELD_IS_NUMERIC_LONG; } else if (number is float) { bits |= FIELD_IS_NUMERIC_FLOAT; } else if (number is double) { bits |= FIELD_IS_NUMERIC_DOUBLE; } else { throw new System.ArgumentException("cannot store numeric type " + number.GetType()); } @string = null; bytes = null; } else { bytes = field.GetBinaryValue(); if (bytes != null) { bits |= FIELD_IS_BINARY; @string = null; } else { @string = field.GetStringValue(); if (@string == null) { throw new System.ArgumentException("field " + field.Name + " is stored but does not have binaryValue, stringValue nor numericValue"); } } } fieldsStream.WriteByte((byte)(sbyte)bits); if (bytes != null) { fieldsStream.WriteVInt32(bytes.Length); fieldsStream.WriteBytes(bytes.Bytes, bytes.Offset, bytes.Length); } else if (@string != null) { fieldsStream.WriteString(field.GetStringValue()); } else { if (number is sbyte || number is short || number is int) { fieldsStream.WriteInt32((int)number); } else if (number is long) { fieldsStream.WriteInt64((long)number); } else if (number is float) { fieldsStream.WriteInt32(Number.SingleToInt32Bits((float)number)); } else if (number is double) { fieldsStream.WriteInt64(BitConverter.DoubleToInt64Bits((double)number)); } else { throw new InvalidOperationException("Cannot get here"); } } }
private void WriteBits(IndexOutput output) { output.WriteInt32(Length); // write size output.WriteInt32(Count()); // write count output.WriteBytes(bits, bits.Length); }