CatgirlIntelligenceAgency/code/libraries/btree
2023-03-07 17:14:32 +01:00
..
src Move all code to a code directory. 2023-03-07 17:14:32 +01:00
build.gradle Move all code to a code directory. 2023-03-07 17:14:32 +01:00
readme.md Move all code to a code directory. 2023-03-07 17:14:32 +01:00

BTree

This package contains a small library for creating and reading a static b-tree.

The b-trees are specified through a BTreeContext which contains information about the data and index layout.

The b-trees are written through a BTreeWriter and read with a BTreeReader.

Demo

BTreeContext ctx = new BTreeContext(
        4,  // num layers max
        1,  // entry size
        BTreeBlockSize.BS_4096); // page size

// Allocate a memory area to work in, see the array library for how to do this with files
LongArray array = LongArray.allocate(8192);

// Write a btree at offset 123 in the area
long[] items = new long[400];
BTreeWriter writer = new BTreeWriter(array, ctx);
final int offsetInFile = 123;

long btreeSize = writer.write(offsetInFile, items.length, slice -> {
    // here we *must* write items.length * entry.size words in slice
    // these items must be sorted!!

    for (int i = 0; i < items.length; i++) {
        slice.set(i, items[i]);
    }
});

// Read the BTree

BTreeReader reader = new BTreeReader(array, ctx, offsetInFile);
reader.findEntry(items[0]);