Search in sources :

Example 1 with IntBlockPool

use of org.apache.lucene.util.IntBlockPool in project lucene-solr by apache.

the class TestIntBlockPool method testMultipleWriterReader.

public void testMultipleWriterReader() {
    Counter bytesUsed = Counter.newCounter();
    IntBlockPool pool = new IntBlockPool(new ByteTrackingAllocator(bytesUsed));
    for (int j = 0; j < 2; j++) {
        List<StartEndAndValues> holders = new ArrayList<>();
        int num = atLeast(4);
        for (int i = 0; i < num; i++) {
            holders.add(new StartEndAndValues(random().nextInt(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.get(random().nextInt(holders.size()));
            if (values.valueCount == 0) {
                values.start = writer.startNewSlice();
            } else {
                writer.reset(values.end);
            }
            writer.writeInt(values.nextValue());
            values.end = writer.getCurrentOffset();
            if (random().nextInt(5) == 0) {
                // pick one and reader the ints
                assertReader(reader, holders.get(random().nextInt(holders.size())));
            }
        }
        while (!holders.isEmpty()) {
            StartEndAndValues values = holders.remove(random().nextInt(holders.size()));
            assertReader(reader, values);
        }
        if (random().nextBoolean()) {
            pool.reset(true, false);
            assertEquals(0, bytesUsed.get());
        } else {
            pool.reset(true, true);
            assertEquals(IntBlockPool.INT_BLOCK_SIZE * Integer.BYTES, bytesUsed.get());
        }
    }
}
Also used : Counter(org.apache.lucene.util.Counter) ArrayList(java.util.ArrayList) IntBlockPool(org.apache.lucene.util.IntBlockPool)

Example 2 with IntBlockPool

use of org.apache.lucene.util.IntBlockPool in project lucene-solr by apache.

the class TestIntBlockPool method testSingleWriterReader.

public void testSingleWriterReader() {
    Counter bytesUsed = 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.getCurrentOffset();
        IntBlockPool.SliceReader reader = new IntBlockPool.SliceReader(pool);
        reader.reset(start, upto);
        for (int i = 0; i < num; i++) {
            assertEquals(i, reader.readInt());
        }
        assertTrue(reader.endOfSlice());
        if (random().nextBoolean()) {
            pool.reset(true, false);
            assertEquals(0, bytesUsed.get());
        } else {
            pool.reset(true, true);
            assertEquals(IntBlockPool.INT_BLOCK_SIZE * Integer.BYTES, bytesUsed.get());
        }
    }
}
Also used : Counter(org.apache.lucene.util.Counter) IntBlockPool(org.apache.lucene.util.IntBlockPool)

Aggregations

Counter (org.apache.lucene.util.Counter)2 IntBlockPool (org.apache.lucene.util.IntBlockPool)2 ArrayList (java.util.ArrayList)1