Search in sources :

Example 1 with Bytes

use of io.datarouter.bytes.Bytes in project datarouter by hotpads.

the class DecodingBlockLoader method leafRange.

@Override
public Scanner<LeafBlock> leafRange(LeafBlockRange range) {
    BlockKey rangeBlockKey = range.rangeBlockKey();
    byte[] compressedRangeBytes = snapshotBlockStorageReader.getLeafBlock(paths, rangeBlockKey);
    return range.parse(compressedRangeBytes).map(// TODO let decompressors accept Bytes to avoid this mem copy?
    Bytes::toArray).map(blockDecompressor::leaf).map(blockDecoder::leaf);
}
Also used : Bytes(io.datarouter.bytes.Bytes) BlockKey(io.datarouter.filesystem.snapshot.block.BlockKey)

Example 2 with Bytes

use of io.datarouter.bytes.Bytes in project datarouter by hotpads.

the class BranchBlock method findChildBlockIndex.

default int findChildBlockIndex(byte[] searchKey) {
    var searchKeyRange = new Bytes(searchKey);
    int numKeys = numRecords();
    int low = 0;
    int high = numKeys - 1;
    while (low <= high) {
        int mid = (low + high) >>> 1;
        Bytes midVal = key(mid);
        int diff = midVal.compareTo(searchKeyRange);
        if (diff < 0) {
            low = mid + 1;
        } else if (diff > 0) {
            high = mid - 1;
        } else {
            return mid;
        }
    }
    return low;
}
Also used : Bytes(io.datarouter.bytes.Bytes)

Example 3 with Bytes

use of io.datarouter.bytes.Bytes in project datarouter by hotpads.

the class BaseSnapshotTests method testSearches.

@Test
public void testSearches() {
    if (!ENABLED_TESTS.contains(TestId.SEARCHES)) {
        return;
    }
    BlockLoader blockLoader = makeBlockLoader(useMemoryCache(), shareMemoryCache());
    var reader = new ScanningSnapshotReader(snapshotKey, exec, getNumThreads(), blockLoader, SCAN_NUM_BLOCKS);
    int step = 1000;
    int limit = 1000;
    Scanner.iterate(0, fromId -> fromId += step).advanceWhile(fromId -> fromId < sortedInputs.size() - limit).parallel(new ParallelScannerContext(scanExec, getNumThreads(), true)).forEach(fromId -> {
        var idReader = new SnapshotIdReader(snapshotKey, blockLoader);
        // known first key inclusive
        byte[] searchKey = idReader.getRecord(fromId).key;
        List<SnapshotLeafRecord> outputsInclusive = reader.scanLeafRecords(searchKey, true).limit(limit).list();
        for (int i = 0; i < limit; ++i) {
            Input input = sortedInputs.get(fromId + i);
            SnapshotLeafRecord output = outputsInclusive.get(i);
            Assert.assertEquals(fromId + i, output.id);
            Assert.assertEquals(new Bytes(input.entry.key()), new Bytes(output.key));
        }
        // known first key exclusive
        List<SnapshotLeafRecord> outputsExclusive = reader.scanLeafRecords(searchKey, false).limit(limit).list();
        for (int i = 0; i < limit; ++i) {
            // plus one because exclusive
            Input input = sortedInputs.get(fromId + i + 1);
            SnapshotLeafRecord output = outputsExclusive.get(i);
            Assert.assertEquals(input.id, output.id);
            Assert.assertEquals(new Bytes(input.entry.key()), new Bytes(output.key));
        }
        // fake first key (should act like exclusive)
        byte[] nonExistentKey = ByteTool.concat(searchKey, new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 });
        List<SnapshotLeafRecord> outputsNonExistentKey = reader.scanLeafRecords(nonExistentKey, true).limit(limit).list();
        for (int i = 0; i < limit; ++i) {
            // plus one because the first key didn't exist
            Input input = sortedInputs.get(fromId + i + 1);
            SnapshotLeafRecord output = outputsNonExistentKey.get(i);
            Assert.assertEquals(input.id, output.id);
            Assert.assertEquals(new Bytes(input.entry.key()), new Bytes(output.key));
        }
    });
}
Also used : ScanningSnapshotReader(io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader) IntStream(java.util.stream.IntStream) SnapshotRecord(io.datarouter.filesystem.snapshot.reader.record.SnapshotRecord) Scanner(io.datarouter.scanner.Scanner) Arrays(java.util.Arrays) SnapshotKeyReader(io.datarouter.filesystem.snapshot.reader.SnapshotKeyReader) ByteTool(io.datarouter.bytes.ByteTool) BlockKey(io.datarouter.filesystem.snapshot.block.BlockKey) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) LoggerFactory(org.slf4j.LoggerFactory) Test(org.testng.annotations.Test) Bytes(io.datarouter.bytes.Bytes) GzipBlockCompressor(io.datarouter.filesystem.snapshot.compress.GzipBlockCompressor) SnapshotWriterConfig(io.datarouter.filesystem.snapshot.writer.SnapshotWriterConfig) NumberFormatter(io.datarouter.util.number.NumberFormatter) SnapshotWriteResult(io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult) Assert(org.testng.Assert) ScanningSnapshotReader(io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader) BlockLoader(io.datarouter.filesystem.snapshot.reader.block.BlockLoader) DatarouterFilesystemModuleFactory(io.datarouter.filesystem.DatarouterFilesystemModuleFactory) SnapshotLeafRecord(io.datarouter.filesystem.snapshot.reader.record.SnapshotLeafRecord) SnapshotWriterConfigBuilder(io.datarouter.filesystem.snapshot.writer.SnapshotWriterConfigBuilder) ExecutorService(java.util.concurrent.ExecutorService) PhaseTimer(io.datarouter.util.timer.PhaseTimer) SnapshotIdReader(io.datarouter.filesystem.snapshot.reader.SnapshotIdReader) AfterClass(org.testng.annotations.AfterClass) Logger(org.slf4j.Logger) BeforeClass(org.testng.annotations.BeforeClass) SnapshotGroup(io.datarouter.filesystem.snapshot.group.SnapshotGroup) Set(java.util.Set) MemoryBlockCache(io.datarouter.filesystem.snapshot.cache.MemoryBlockCache) SnapshotEntry(io.datarouter.filesystem.snapshot.entry.SnapshotEntry) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Guice(org.testng.annotations.Guice) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Optional(java.util.Optional) SnapshotKey(io.datarouter.filesystem.snapshot.key.SnapshotKey) ListTool(io.datarouter.util.collection.ListTool) Require(io.datarouter.util.Require) Bytes(io.datarouter.bytes.Bytes) SnapshotLeafRecord(io.datarouter.filesystem.snapshot.reader.record.SnapshotLeafRecord) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) BlockLoader(io.datarouter.filesystem.snapshot.reader.block.BlockLoader) SnapshotIdReader(io.datarouter.filesystem.snapshot.reader.SnapshotIdReader) Test(org.testng.annotations.Test)

Example 4 with Bytes

use of io.datarouter.bytes.Bytes in project datarouter by hotpads.

the class FieldSetToolTests method testGetConcatenatedValueBytes.

@Test
public void testGetConcatenatedValueBytes() {
    List<Field<?>> fields = List.of(new IntegerField(new IntegerFieldKey("a"), 55), new StringField(new StringFieldKey("b"), "abc"), new StringField(new StringFieldKey("c"), "xyz"));
    int lengthWithout = 4 + 3 + 1 + 3;
    int lengthWith = lengthWithout + 1;
    Bytes withoutTrailingByte = new Bytes(FieldTool.getConcatenatedValueBytesUnterminated(fields));
    Bytes withTrailingByte = new Bytes(FieldTool.getConcatenatedValueBytes(fields));
    Assert.assertEquals(withoutTrailingByte.getLength(), lengthWithout);
    Assert.assertEquals(withTrailingByte.getLength(), lengthWith);
}
Also used : IntegerField(io.datarouter.model.field.imp.comparable.IntegerField) StringField(io.datarouter.model.field.imp.StringField) Bytes(io.datarouter.bytes.Bytes) StringFieldKey(io.datarouter.model.field.imp.StringFieldKey) IntegerFieldKey(io.datarouter.model.field.imp.comparable.IntegerFieldKey) StringField(io.datarouter.model.field.imp.StringField) IntegerField(io.datarouter.model.field.imp.comparable.IntegerField) Test(org.testng.annotations.Test)

Example 5 with Bytes

use of io.datarouter.bytes.Bytes in project datarouter by hotpads.

the class BaseSnapshotTests method testParialScans.

@Test
public void testParialScans() {
    if (!ENABLED_TESTS.contains(TestId.PARTIAL_SCANS)) {
        return;
    }
    BlockLoader blockLoader = makeBlockLoader(useMemoryCache(), shareMemoryCache());
    var reader = new ScanningSnapshotReader(snapshotKey, exec, getNumThreads(), blockLoader, SCAN_NUM_BLOCKS);
    int step = 1000;
    int limit = 1000;
    Scanner.iterate(0, fromId -> fromId += step).advanceWhile(fromId -> fromId < sortedInputs.size() - limit).parallel(new ParallelScannerContext(scanExec, getNumThreads(), true)).forEach(fromId -> {
        var timer = new PhaseTimer(fromId + "");
        List<SnapshotRecord> outputs = reader.scan(fromId).limit(limit).list();
        timer.add("got " + outputs.size());
        for (int i = 0; i < limit; ++i) {
            Input input = sortedInputs.get(fromId + i);
            SnapshotRecord output = outputs.get(i);
            Assert.assertEquals(fromId + i, output.id);
            Assert.assertEquals(new Bytes(input.entry.key()), new Bytes(output.key));
            for (int column = 0; column < input.entry.columnValues.length; ++column) {
                if (!SnapshotEntry.equalColumnValue(input.entry, output.entry(), column)) {
                    String message = String.format("%s, actual=%s, expected=%s", i, utf8(output.columnValues[column]), utf8(input.entry.columnValues[column]));
                    throw new RuntimeException(message);
                }
            }
        }
        timer.add("assert");
        logger.info("{}", timer);
    });
}
Also used : ScanningSnapshotReader(io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader) IntStream(java.util.stream.IntStream) SnapshotRecord(io.datarouter.filesystem.snapshot.reader.record.SnapshotRecord) Scanner(io.datarouter.scanner.Scanner) Arrays(java.util.Arrays) SnapshotKeyReader(io.datarouter.filesystem.snapshot.reader.SnapshotKeyReader) ByteTool(io.datarouter.bytes.ByteTool) BlockKey(io.datarouter.filesystem.snapshot.block.BlockKey) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) LoggerFactory(org.slf4j.LoggerFactory) Test(org.testng.annotations.Test) Bytes(io.datarouter.bytes.Bytes) GzipBlockCompressor(io.datarouter.filesystem.snapshot.compress.GzipBlockCompressor) SnapshotWriterConfig(io.datarouter.filesystem.snapshot.writer.SnapshotWriterConfig) NumberFormatter(io.datarouter.util.number.NumberFormatter) SnapshotWriteResult(io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult) Assert(org.testng.Assert) ScanningSnapshotReader(io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader) BlockLoader(io.datarouter.filesystem.snapshot.reader.block.BlockLoader) DatarouterFilesystemModuleFactory(io.datarouter.filesystem.DatarouterFilesystemModuleFactory) SnapshotLeafRecord(io.datarouter.filesystem.snapshot.reader.record.SnapshotLeafRecord) SnapshotWriterConfigBuilder(io.datarouter.filesystem.snapshot.writer.SnapshotWriterConfigBuilder) ExecutorService(java.util.concurrent.ExecutorService) PhaseTimer(io.datarouter.util.timer.PhaseTimer) SnapshotIdReader(io.datarouter.filesystem.snapshot.reader.SnapshotIdReader) AfterClass(org.testng.annotations.AfterClass) Logger(org.slf4j.Logger) BeforeClass(org.testng.annotations.BeforeClass) SnapshotGroup(io.datarouter.filesystem.snapshot.group.SnapshotGroup) Set(java.util.Set) MemoryBlockCache(io.datarouter.filesystem.snapshot.cache.MemoryBlockCache) SnapshotEntry(io.datarouter.filesystem.snapshot.entry.SnapshotEntry) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Guice(org.testng.annotations.Guice) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Optional(java.util.Optional) SnapshotKey(io.datarouter.filesystem.snapshot.key.SnapshotKey) ListTool(io.datarouter.util.collection.ListTool) Require(io.datarouter.util.Require) PhaseTimer(io.datarouter.util.timer.PhaseTimer) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) BlockLoader(io.datarouter.filesystem.snapshot.reader.block.BlockLoader) SnapshotRecord(io.datarouter.filesystem.snapshot.reader.record.SnapshotRecord) Bytes(io.datarouter.bytes.Bytes) Test(org.testng.annotations.Test)

Aggregations

Bytes (io.datarouter.bytes.Bytes)8 BlockKey (io.datarouter.filesystem.snapshot.block.BlockKey)5 SnapshotRecord (io.datarouter.filesystem.snapshot.reader.record.SnapshotRecord)4 ByteTool (io.datarouter.bytes.ByteTool)2 DatarouterFilesystemModuleFactory (io.datarouter.filesystem.DatarouterFilesystemModuleFactory)2 LeafBlock (io.datarouter.filesystem.snapshot.block.leaf.LeafBlock)2 ValueLocation (io.datarouter.filesystem.snapshot.block.leaf.LeafBlock.ValueLocation)2 ValueBlock (io.datarouter.filesystem.snapshot.block.value.ValueBlock)2 MemoryBlockCache (io.datarouter.filesystem.snapshot.cache.MemoryBlockCache)2 GzipBlockCompressor (io.datarouter.filesystem.snapshot.compress.GzipBlockCompressor)2 SnapshotEntry (io.datarouter.filesystem.snapshot.entry.SnapshotEntry)2 SnapshotGroup (io.datarouter.filesystem.snapshot.group.SnapshotGroup)2 SnapshotWriteResult (io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult)2 SnapshotKey (io.datarouter.filesystem.snapshot.key.SnapshotKey)2 ScanningSnapshotReader (io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader)2 SnapshotIdReader (io.datarouter.filesystem.snapshot.reader.SnapshotIdReader)2 SnapshotKeyReader (io.datarouter.filesystem.snapshot.reader.SnapshotKeyReader)2 BlockLoader (io.datarouter.filesystem.snapshot.reader.block.BlockLoader)2 SnapshotLeafRecord (io.datarouter.filesystem.snapshot.reader.record.SnapshotLeafRecord)2 SnapshotWriterConfig (io.datarouter.filesystem.snapshot.writer.SnapshotWriterConfig)2