public virtual void TestEncodeDecode() { int iterations = RandomInts.NextIntBetween(Random(), 1, 1000); float AcceptableOverheadRatio = (float)Random().NextDouble(); int[] values = new int[(iterations - 1) * Lucene41PostingsFormat.BLOCK_SIZE + ForUtil.MAX_DATA_SIZE]; for (int i = 0; i < iterations; ++i) { int bpv = Random().Next(32); if (bpv == 0) { int value = RandomInts.NextIntBetween(Random(), 0, int.MaxValue); for (int j = 0; j < Lucene41PostingsFormat.BLOCK_SIZE; ++j) { values[i * Lucene41PostingsFormat.BLOCK_SIZE + j] = value; } } else { for (int j = 0; j < Lucene41PostingsFormat.BLOCK_SIZE; ++j) { values[i * Lucene41PostingsFormat.BLOCK_SIZE + j] = RandomInts.NextIntBetween(Random(), 0, (int)PackedInts.MaxValue(bpv)); } } } Directory d = new RAMDirectory(); long endPointer; { // encode IndexOutput @out = d.CreateOutput("test.bin", IOContext.DEFAULT); ForUtil forUtil = new ForUtil(AcceptableOverheadRatio, @out); for (int i = 0; i < iterations; ++i) { forUtil.WriteBlock(Arrays.CopyOfRange(values, i * Lucene41PostingsFormat.BLOCK_SIZE, values.Length), new byte[Lucene41.ForUtil.MAX_ENCODED_SIZE], @out); } endPointer = @out.FilePointer; @out.Dispose(); } { // decode IndexInput @in = d.OpenInput("test.bin", IOContext.READONCE); ForUtil forUtil = new ForUtil(@in); for (int i = 0; i < iterations; ++i) { if (Random().NextBoolean()) { forUtil.SkipBlock(@in); continue; } int[] restored = new int[Lucene41.ForUtil.MAX_DATA_SIZE]; forUtil.ReadBlock(@in, new byte[Lucene41.ForUtil.MAX_ENCODED_SIZE], restored); Assert.AreEqual(Arrays.CopyOfRange(values, i * Lucene41PostingsFormat.BLOCK_SIZE, (i + 1) * Lucene41PostingsFormat.BLOCK_SIZE), Arrays.CopyOf(restored, Lucene41PostingsFormat.BLOCK_SIZE)); } Assert.AreEqual(endPointer, @in.FilePointer); @in.Dispose(); } }
// public static boolean DEBUG = false; /// <summary> /// Sole constructor. </summary> public Lucene41PostingsReader(Directory dir, FieldInfos fieldInfos, SegmentInfo segmentInfo, IOContext ioContext, string segmentSuffix) { bool success = false; IndexInput docIn = null; IndexInput posIn = null; IndexInput payIn = null; try { docIn = dir.OpenInput(IndexFileNames.SegmentFileName(segmentInfo.Name, segmentSuffix, Lucene41PostingsFormat.DOC_EXTENSION), ioContext); Version = CodecUtil.CheckHeader(docIn, Lucene41PostingsWriter.DOC_CODEC, Lucene41PostingsWriter.VERSION_START, Lucene41PostingsWriter.VERSION_CURRENT); forUtil = new ForUtil(docIn); if (fieldInfos.HasProx()) { posIn = dir.OpenInput(IndexFileNames.SegmentFileName(segmentInfo.Name, segmentSuffix, Lucene41PostingsFormat.POS_EXTENSION), ioContext); CodecUtil.CheckHeader(posIn, Lucene41PostingsWriter.POS_CODEC, Version, Version); if (fieldInfos.HasPayloads() || fieldInfos.HasOffsets()) { payIn = dir.OpenInput(IndexFileNames.SegmentFileName(segmentInfo.Name, segmentSuffix, Lucene41PostingsFormat.PAY_EXTENSION), ioContext); CodecUtil.CheckHeader(payIn, Lucene41PostingsWriter.PAY_CODEC, Version, Version); } } this.DocIn = docIn; this.PosIn = posIn; this.PayIn = payIn; success = true; } finally { if (!success) { IOUtils.CloseWhileHandlingException(docIn, posIn, payIn); } } }
/// <summary> /// Creates a postings writer with the specified PackedInts overhead ratio </summary> // TODO: does this ctor even make sense? public Lucene41PostingsWriter(SegmentWriteState state, float acceptableOverheadRatio) : base() { DocOut = state.Directory.CreateOutput(IndexFileNames.SegmentFileName(state.SegmentInfo.Name, state.SegmentSuffix, Lucene41PostingsFormat.DOC_EXTENSION), state.Context); IndexOutput posOut = null; IndexOutput payOut = null; bool success = false; try { CodecUtil.WriteHeader(DocOut, DOC_CODEC, VERSION_CURRENT); ForUtil = new ForUtil(acceptableOverheadRatio, DocOut); if (state.FieldInfos.HasProx()) { PosDeltaBuffer = new int[ForUtil.MAX_DATA_SIZE]; posOut = state.Directory.CreateOutput(IndexFileNames.SegmentFileName(state.SegmentInfo.Name, state.SegmentSuffix, Lucene41PostingsFormat.POS_EXTENSION), state.Context); CodecUtil.WriteHeader(posOut, POS_CODEC, VERSION_CURRENT); if (state.FieldInfos.HasPayloads()) { PayloadBytes = new sbyte[128]; PayloadLengthBuffer = new int[ForUtil.MAX_DATA_SIZE]; } else { PayloadBytes = null; PayloadLengthBuffer = null; } if (state.FieldInfos.HasOffsets()) { OffsetStartDeltaBuffer = new int[ForUtil.MAX_DATA_SIZE]; OffsetLengthBuffer = new int[ForUtil.MAX_DATA_SIZE]; } else { OffsetStartDeltaBuffer = null; OffsetLengthBuffer = null; } if (state.FieldInfos.HasPayloads() || state.FieldInfos.HasOffsets()) { payOut = state.Directory.CreateOutput(IndexFileNames.SegmentFileName(state.SegmentInfo.Name, state.SegmentSuffix, Lucene41PostingsFormat.PAY_EXTENSION), state.Context); CodecUtil.WriteHeader(payOut, PAY_CODEC, VERSION_CURRENT); } } else { PosDeltaBuffer = null; PayloadLengthBuffer = null; OffsetStartDeltaBuffer = null; OffsetLengthBuffer = null; PayloadBytes = null; } this.PayOut = payOut; this.PosOut = posOut; success = true; } finally { if (!success) { IOUtils.CloseWhileHandlingException(DocOut, posOut, payOut); } } DocDeltaBuffer = new int[ForUtil.MAX_DATA_SIZE]; FreqBuffer = new int[ForUtil.MAX_DATA_SIZE]; // TODO: should we try skipping every 2/4 blocks...? SkipWriter = new Lucene41SkipWriter(MaxSkipLevels, Lucene41PostingsFormat.BLOCK_SIZE, state.SegmentInfo.DocCount, DocOut, posOut, payOut); Encoded = new sbyte[ForUtil.MAX_ENCODED_SIZE]; }
public virtual void TestEncodeDecode() { int iterations = RandomInts.RandomInt32Between(Random, 1, 1000); float AcceptableOverheadRatio = (float)Random.NextDouble(); int[] values = new int[(iterations - 1) * Lucene41PostingsFormat.BLOCK_SIZE + ForUtil.MAX_DATA_SIZE]; for (int i = 0; i < iterations; ++i) { int bpv = Random.Next(32); if (bpv == 0) { int value = RandomInts.RandomInt32Between(Random, 0, int.MaxValue); for (int j = 0; j < Lucene41PostingsFormat.BLOCK_SIZE; ++j) { values[i * Lucene41PostingsFormat.BLOCK_SIZE + j] = value; } } else { for (int j = 0; j < Lucene41PostingsFormat.BLOCK_SIZE; ++j) { values[i * Lucene41PostingsFormat.BLOCK_SIZE + j] = RandomInts.RandomInt32Between(Random, 0, (int)PackedInt32s.MaxValue(bpv)); } } } Directory d = new RAMDirectory(); long endPointer; { // encode IndexOutput @out = d.CreateOutput("test.bin", IOContext.DEFAULT); ForUtil forUtil = new ForUtil(AcceptableOverheadRatio, @out); for (int i = 0; i < iterations; ++i) { forUtil.WriteBlock(Arrays.CopyOfRange(values, i * Lucene41PostingsFormat.BLOCK_SIZE, values.Length), new byte[Lucene41.ForUtil.MAX_ENCODED_SIZE], @out); } endPointer = @out.Position; // LUCENENET specific: Renamed from getFilePointer() to match FileStream @out.Dispose(); } { // decode IndexInput @in = d.OpenInput("test.bin", IOContext.READ_ONCE); ForUtil forUtil = new ForUtil(@in); for (int i = 0; i < iterations; ++i) { if (Random.NextBoolean()) { forUtil.SkipBlock(@in); continue; } int[] restored = new int[Lucene41.ForUtil.MAX_DATA_SIZE]; forUtil.ReadBlock(@in, new byte[Lucene41.ForUtil.MAX_ENCODED_SIZE], restored); Assert.AreEqual(Arrays.CopyOfRange(values, i * Lucene41PostingsFormat.BLOCK_SIZE, (i + 1) * Lucene41PostingsFormat.BLOCK_SIZE), Arrays.CopyOf(restored, Lucene41PostingsFormat.BLOCK_SIZE)); } assertEquals(endPointer, @in.Position); // LUCENENET specific: Renamed from getFilePointer() to match FileStream @in.Dispose(); } }