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();
}
}
Aggregations