use of io.datarouter.scanner.Ref in project datarouter by hotpads.
the class WordTests method testValueBlockV1.
@Test
public void testValueBlockV1() {
Supplier<ValueBlockV1Encoder> encoderSupplier = ValueBlockV1Encoder::new;
Ref<ValueBlockV1Encoder> encoder = new Ref<>(encoderSupplier.get());
int blockSize = 4096;
List<String> inputs = WordDataset.scanWords(getClass().getSimpleName() + "-testValueBlockV1").list();
List<byte[]> blocks = Scanner.of(inputs).map(str -> str.getBytes(StandardCharsets.UTF_8)).map(value -> new SnapshotEntry(EmptyArray.BYTE, EmptyArray.BYTE, new byte[][] { value })).concat(value -> {
encoder.get().add(value, 0);
if (encoder.get().numBytes() >= blockSize) {
byte[] block = encoder.get().encode().concat();
encoder.set(encoderSupplier.get());
return Scanner.of(block);
}
return Scanner.empty();
}).list();
if (encoder.get().numRecords() > 0) {
blocks.add(encoder.get().encode().concat());
}
logger.warn("encoded {} value blocks", blocks.size());
List<String> outputs = Scanner.of(blocks).map(ValueBlockV1::new).concat(ValueBlock::valueCopies).map(bytes -> new String(bytes, StandardCharsets.UTF_8)).list();
Require.equals(outputs.size(), inputs.size());
for (int i = 0; i < inputs.size(); ++i) {
if (ObjectTool.notEquals(outputs.get(i), inputs.get(i))) {
logger.warn("actual=[{}] expected=[{}]", outputs.get(i), inputs.get(i));
String message = String.format("actual=[%s] does not equal expected=[%s]", CsvIntByteStringCodec.INSTANCE.encode(outputs.get(i).getBytes(StandardCharsets.UTF_8)), CsvIntByteStringCodec.INSTANCE.encode(inputs.get(i).getBytes(StandardCharsets.UTF_8)));
throw new IllegalArgumentException(message);
}
}
}
use of io.datarouter.scanner.Ref in project datarouter by hotpads.
the class WordTests method testLeafBlockV1.
@Test
public void testLeafBlockV1() {
Supplier<LeafBlockV1Encoder> encoderSupplier = () -> new LeafBlockV1Encoder(32 * 1024);
Ref<LeafBlockV1Encoder> encoder = new Ref<>(encoderSupplier.get());
int blockSize = 4096;
String valuePrefix = "val_";
List<SnapshotEntry> inputs = WordDataset.scanWords(getClass().getSimpleName() + "-testLeafBlockV1").map(word -> {
byte[] keyBytes = word.getBytes(StandardCharsets.UTF_8);
byte[] valueBytes = ByteTool.concat(valuePrefix.getBytes(StandardCharsets.UTF_8), keyBytes);
return new SnapshotEntry(keyBytes, valueBytes, ByteTool.EMPTY_ARRAY_2);
}).list();
var keyId = new AtomicLong();
List<byte[]> blocks = Scanner.of(inputs).concat(entry -> {
// TODO use real value block references
encoder.get().add(0, keyId.getAndIncrement(), entry, new int[] { 0 }, new int[] { 0 });
if (encoder.get().numBytes() >= blockSize) {
var fileIdsAndEndings = new FileIdsAndEndings[] { new FileIdsAndEndings(new int[] { 0 }, new int[] { 0 }) };
byte[] block = encoder.get().encode(fileIdsAndEndings).concat();
encoder.set(encoderSupplier.get());
return Scanner.of(block);
}
return Scanner.empty();
}).list();
if (encoder.get().numRecords() > 0) {
var fileIdsAndEndings = new FileIdsAndEndings[] { new FileIdsAndEndings(new int[] { 0 }, new int[] { 0 }) };
blocks.add(encoder.get().encode(fileIdsAndEndings).concat());
}
logger.warn("encoded {} key blocks", blocks.size());
List<SnapshotLeafRecord> outputs = Scanner.of(blocks).map(LeafBlockV1::new).concat(LeafBlock::leafRecords).list();
Require.equals(outputs.size(), inputs.size());
for (int i = 0; i < outputs.size(); ++i) {
if (!Arrays.equals(outputs.get(i).key, inputs.get(i).key())) {
logger.warn("actual=[{}] expected=[{}]", outputs.get(i), inputs.get(i));
String message = String.format("key: actual=[%s] does not equal expected=[%s]", CsvIntByteStringCodec.INSTANCE.encode(outputs.get(i).key), CsvIntByteStringCodec.INSTANCE.encode(inputs.get(i).key()));
throw new IllegalArgumentException(message);
}
if (!Arrays.equals(outputs.get(i).value, inputs.get(i).value())) {
logger.warn("actual=[{}] expected=[{}]", outputs.get(i), inputs.get(i));
String message = String.format("value: actual=[%s] does not equal expected=[%s]", CsvIntByteStringCodec.INSTANCE.encode(outputs.get(i).value), CsvIntByteStringCodec.INSTANCE.encode(inputs.get(i).value()));
throw new IllegalArgumentException(message);
}
}
}
Aggregations