public virtual void TestSingleWriterReader() { Counter bytesUsed = Util.Counter.NewCounter(); IntBlockPool pool = new IntBlockPool(new ByteTrackingAllocator(bytesUsed)); for (int j = 0; j < 2; j++) { IntBlockPool.SliceWriter writer = new IntBlockPool.SliceWriter(pool); int start = writer.StartNewSlice(); int num = AtLeast(100); for (int i = 0; i < num; i++) { writer.WriteInt(i); } int upto = writer.CurrentOffset; IntBlockPool.SliceReader reader = new IntBlockPool.SliceReader(pool); reader.Reset(start, upto); for (int i = 0; i < num; i++) { Assert.AreEqual(i, reader.ReadInt()); } Assert.IsTrue(reader.EndOfSlice()); if (Random().NextBoolean()) { pool.Reset(true, false); Assert.AreEqual(0, bytesUsed.Get()); } else { pool.Reset(true, true); Assert.AreEqual(IntBlockPool.INT_BLOCK_SIZE * RamUsageEstimator.NUM_BYTES_INT, bytesUsed.Get()); } } }
public virtual void TestMultipleWriterReader() { Counter bytesUsed = Util.Counter.NewCounter(); IntBlockPool pool = new IntBlockPool(new ByteTrackingAllocator(bytesUsed)); for (int j = 0; j < 2; j++) { IList <StartEndAndValues> holders = new List <StartEndAndValues>(); int num = AtLeast(4); for (int i = 0; i < num; i++) { holders.Add(new StartEndAndValues(Random().Next(1000))); } IntBlockPool.SliceWriter writer = new IntBlockPool.SliceWriter(pool); IntBlockPool.SliceReader reader = new IntBlockPool.SliceReader(pool); int numValuesToWrite = AtLeast(10000); for (int i = 0; i < numValuesToWrite; i++) { StartEndAndValues values = holders[Random().Next(holders.Count)]; if (values.ValueCount == 0) { values.Start = writer.StartNewSlice(); } else { writer.Reset(values.End); } writer.WriteInt(values.NextValue()); values.End = writer.CurrentOffset; if (Random().Next(5) == 0) { // pick one and reader the ints AssertReader(reader, holders[Random().Next(holders.Count)]); } } while (holders.Count > 0) { int randIndex = Random().Next(holders.Count); StartEndAndValues values = holders[randIndex]; holders.RemoveAt(randIndex); AssertReader(reader, values); } if (Random().NextBoolean()) { pool.Reset(true, false); Assert.AreEqual(0, bytesUsed.Get()); } else { pool.Reset(true, true); Assert.AreEqual(IntBlockPool.INT_BLOCK_SIZE * RamUsageEstimator.NUM_BYTES_INT, bytesUsed.Get()); } } }
/// <summary> /// Expert: This constructor accepts an upper limit for the number of bytes that should be reused if this instance is <see cref="Reset()"/>. /// </summary> /// <param name="storeOffsets"> <c>true</c> if offsets should be stored </param> /// <param name="maxReusedBytes"> the number of bytes that should remain in the internal memory pools after <see cref="Reset()"/> is called </param> internal MemoryIndex(bool storeOffsets, long maxReusedBytes) { this.storeOffsets = storeOffsets; this.bytesUsed = Counter.NewCounter(); int maxBufferedByteBlocks = (int)((maxReusedBytes / 2) / ByteBlockPool.BYTE_BLOCK_SIZE); int maxBufferedIntBlocks = (int)((maxReusedBytes - (maxBufferedByteBlocks * ByteBlockPool.BYTE_BLOCK_SIZE)) / (IntBlockPool.INT_BLOCK_SIZE * RamUsageEstimator.NUM_BYTES_INT)); Debug.Assert((maxBufferedByteBlocks * ByteBlockPool.BYTE_BLOCK_SIZE) + (maxBufferedIntBlocks * IntBlockPool.INT_BLOCK_SIZE * RamUsageEstimator.NUM_BYTES_INT) <= maxReusedBytes); byteBlockPool = new ByteBlockPool(new RecyclingByteBlockAllocator(ByteBlockPool.BYTE_BLOCK_SIZE, maxBufferedByteBlocks, bytesUsed)); intBlockPool = new IntBlockPool(new RecyclingIntBlockAllocator(IntBlockPool.INT_BLOCK_SIZE, maxBufferedIntBlocks, bytesUsed)); postingsWriter = new IntBlockPool.SliceWriter(intBlockPool); }