Example #1
0
 /// <summary>
 /// Creates a new <seealso cref="BytesRefArray"/> with a counter to track allocated bytes
 /// </summary>
 public BytesRefArray(Counter bytesUsed)
 {
     this.Pool = new ByteBlockPool(new ByteBlockPool.DirectTrackingAllocator(bytesUsed));
     Pool.NextBuffer();
     bytesUsed.AddAndGet(RamUsageEstimator.NUM_BYTES_ARRAY_HEADER + RamUsageEstimator.NUM_BYTES_INT);
     this.BytesUsed = bytesUsed;
 }
Example #2
0
 /// <summary>
 /// Creates a new <see cref="BytesRefArray"/> with a counter to track allocated bytes
 /// </summary>
 public BytesRefArray(Counter bytesUsed)
 {
     this.pool = new ByteBlockPool(new ByteBlockPool.DirectTrackingAllocator(bytesUsed));
     pool.NextBuffer();
     bytesUsed.AddAndGet(RamUsageEstimator.NUM_BYTES_ARRAY_HEADER + RamUsageEstimator.NUM_BYTES_INT32);
     this.bytesUsed = bytesUsed;
 }
Example #3
0
        public virtual void TestReadAndWrite()
        {
            Counter       bytesUsed = Util.Counter.NewCounter();
            ByteBlockPool pool      = new ByteBlockPool(new ByteBlockPool.DirectTrackingAllocator(bytesUsed));

            pool.NextBuffer();
            bool reuseFirst = Random.NextBoolean();

            for (int j = 0; j < 2; j++)
            {
                IList <BytesRef> list = new List <BytesRef>();
                int      maxLength    = AtLeast(500);
                int      numValues    = AtLeast(100);
                BytesRef @ref         = new BytesRef();
                for (int i = 0; i < numValues; i++)
                {
                    string value = TestUtil.RandomRealisticUnicodeString(Random, maxLength);
                    list.Add(new BytesRef(value));
                    @ref.CopyChars(value);
                    pool.Append(@ref);
                }
                // verify
                long position = 0;
                foreach (BytesRef expected in list)
                {
                    @ref.Grow(expected.Length);
                    @ref.Length = expected.Length;
                    pool.ReadBytes(position, @ref.Bytes, @ref.Offset, @ref.Length);
                    Assert.AreEqual(expected, @ref);
                    position += @ref.Length;
                }
                pool.Reset(Random.NextBoolean(), reuseFirst);
                if (reuseFirst)
                {
                    Assert.AreEqual(ByteBlockPool.BYTE_BLOCK_SIZE, bytesUsed.Get());
                }
                else
                {
                    Assert.AreEqual(0, bytesUsed.Get());
                    pool.NextBuffer(); // prepare for next iter
                }
            }
        }
Example #4
0
 public virtual void TestReadAndWrite()
 {
     Counter bytesUsed = Util.Counter.NewCounter();
     ByteBlockPool pool = new ByteBlockPool(new ByteBlockPool.DirectTrackingAllocator(bytesUsed));
     pool.NextBuffer();
     bool reuseFirst = Random().NextBoolean();
     for (int j = 0; j < 2; j++)
     {
         IList<BytesRef> list = new List<BytesRef>();
         int maxLength = AtLeast(500);
         int numValues = AtLeast(100);
         BytesRef @ref = new BytesRef();
         for (int i = 0; i < numValues; i++)
         {
             string value = TestUtil.RandomRealisticUnicodeString(Random(), maxLength);
             list.Add(new BytesRef(value));
             @ref.CopyChars(value);
             pool.Append(@ref);
         }
         // verify
         long position = 0;
         foreach (BytesRef expected in list)
         {
             @ref.Grow(expected.Length);
             @ref.Length = expected.Length;
             pool.ReadBytes(position, @ref.Bytes, @ref.Offset, @ref.Length);
             Assert.AreEqual(expected, @ref);
             position += @ref.Length;
         }
         pool.Reset(Random().NextBoolean(), reuseFirst);
         if (reuseFirst)
         {
             Assert.AreEqual(ByteBlockPool.BYTE_BLOCK_SIZE, bytesUsed.Get());
         }
         else
         {
             Assert.AreEqual(0, bytesUsed.Get());
             pool.NextBuffer(); // prepare for next iter
         }
     }
 }