Search in sources :

Example 26 with ByteArrayDataInput

use of org.apache.lucene.store.ByteArrayDataInput in project lucene-solr by apache.

the class TestPackedInts method testBlockPackedReaderWriter.

public void testBlockPackedReaderWriter() throws IOException {
    final int iters = atLeast(2);
    for (int iter = 0; iter < iters; ++iter) {
        final int blockSize = 1 << TestUtil.nextInt(random(), 6, 18);
        final int valueCount = random().nextInt(1 << 18);
        final long[] values = new long[valueCount];
        long minValue = 0;
        int bpv = 0;
        for (int i = 0; i < valueCount; ++i) {
            if (i % blockSize == 0) {
                minValue = rarely() ? random().nextInt(256) : rarely() ? -5 : random().nextLong();
                bpv = random().nextInt(65);
            }
            if (bpv == 0) {
                values[i] = minValue;
            } else if (bpv == 64) {
                values[i] = random().nextLong();
            } else {
                values[i] = minValue + TestUtil.nextLong(random(), 0, (1L << bpv) - 1);
            }
        }
        final Directory dir = newDirectory();
        final IndexOutput out = dir.createOutput("out.bin", IOContext.DEFAULT);
        final BlockPackedWriter writer = new BlockPackedWriter(out, blockSize);
        for (int i = 0; i < valueCount; ++i) {
            assertEquals(i, writer.ord());
            writer.add(values[i]);
        }
        assertEquals(valueCount, writer.ord());
        writer.finish();
        assertEquals(valueCount, writer.ord());
        final long fp = out.getFilePointer();
        out.close();
        IndexInput in1 = dir.openInput("out.bin", IOContext.DEFAULT);
        byte[] buf = new byte[(int) fp];
        in1.readBytes(buf, 0, (int) fp);
        in1.seek(0L);
        ByteArrayDataInput in2 = new ByteArrayDataInput(buf);
        final DataInput in = random().nextBoolean() ? in1 : in2;
        final BlockPackedReaderIterator it = new BlockPackedReaderIterator(in, PackedInts.VERSION_CURRENT, blockSize, valueCount);
        for (int i = 0; i < valueCount; ) {
            if (random().nextBoolean()) {
                assertEquals("" + i, values[i], it.next());
                ++i;
            } else {
                final LongsRef nextValues = it.next(TestUtil.nextInt(random(), 1, 1024));
                for (int j = 0; j < nextValues.length; ++j) {
                    assertEquals("" + (i + j), values[i + j], nextValues.longs[nextValues.offset + j]);
                }
                i += nextValues.length;
            }
            assertEquals(i, it.ord());
        }
        assertEquals(fp, in instanceof ByteArrayDataInput ? ((ByteArrayDataInput) in).getPosition() : ((IndexInput) in).getFilePointer());
        expectThrows(IOException.class, () -> {
            it.next();
        });
        if (in instanceof ByteArrayDataInput) {
            ((ByteArrayDataInput) in).setPosition(0);
        } else {
            ((IndexInput) in).seek(0L);
        }
        final BlockPackedReaderIterator it2 = new BlockPackedReaderIterator(in, PackedInts.VERSION_CURRENT, blockSize, valueCount);
        int i = 0;
        while (true) {
            final int skip = TestUtil.nextInt(random(), 0, valueCount - i);
            it2.skip(skip);
            i += skip;
            assertEquals(i, it2.ord());
            if (i == valueCount) {
                break;
            } else {
                assertEquals(values[i], it2.next());
                ++i;
            }
        }
        assertEquals(fp, in instanceof ByteArrayDataInput ? ((ByteArrayDataInput) in).getPosition() : ((IndexInput) in).getFilePointer());
        expectThrows(IOException.class, () -> {
            it2.skip(1);
        });
        in1.seek(0L);
        final BlockPackedReader reader = new BlockPackedReader(in1, PackedInts.VERSION_CURRENT, blockSize, valueCount, random().nextBoolean());
        assertEquals(in1.getFilePointer(), in1.length());
        for (i = 0; i < valueCount; ++i) {
            assertEquals("i=" + i, values[i], reader.get(i));
        }
        in1.close();
        dir.close();
    }
}
Also used : IndexOutput(org.apache.lucene.store.IndexOutput) ByteArrayDataInput(org.apache.lucene.store.ByteArrayDataInput) DataInput(org.apache.lucene.store.DataInput) ByteArrayDataInput(org.apache.lucene.store.ByteArrayDataInput) IndexInput(org.apache.lucene.store.IndexInput) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) LongsRef(org.apache.lucene.util.LongsRef)

Aggregations

ByteArrayDataInput (org.apache.lucene.store.ByteArrayDataInput)26 BytesRef (org.apache.lucene.util.BytesRef)16 ByteArrayDataOutput (org.apache.lucene.store.ByteArrayDataOutput)8 IntsRefBuilder (org.apache.lucene.util.IntsRefBuilder)5 IndexOutput (org.apache.lucene.store.IndexOutput)4 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 Random (java.util.Random)3 IntPoint (org.apache.lucene.document.IntPoint)3 CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)3 IntsRef (org.apache.lucene.util.IntsRef)3 OfflineSorter (org.apache.lucene.util.OfflineSorter)3 Pair (org.apache.lucene.util.fst.PairOutputs.Pair)3 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)2 TokenStreamToAutomaton (org.apache.lucene.analysis.TokenStreamToAutomaton)2 Directory (org.apache.lucene.store.Directory)2 IndexInput (org.apache.lucene.store.IndexInput)2 CharsRef (org.apache.lucene.util.CharsRef)2 LimitedFiniteStringsIterator (org.apache.lucene.util.automaton.LimitedFiniteStringsIterator)2