Skip to content

Commit

Permalink
feat: move child resolution to bptree node
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlymatthew committed May 29, 2024
1 parent 70b2443 commit 6866b95
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/btree/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export class BTreeNode {
private readonly dataFileResolver: RangeResolver;
private readonly fileFormat: FileFormat;
private readonly pageFieldType: FieldType;
private readonly tree: RangeResolver;
private readonly pageFieldWidth: number;

constructor(
keys: ReferencedValue[],
Expand All @@ -28,13 +30,17 @@ export class BTreeNode {
dataFileResolver: RangeResolver,
fileFormat: FileFormat,
pageFieldType: FieldType,
tree: RangeResolver,
pageFieldWidth: number,
) {
this.keys = keys;
this.leafPointers = leafPointers;
this.internalPointers = internalPointers;
this.dataFileResolver = dataFileResolver;
this.fileFormat = fileFormat;
this.pageFieldType = pageFieldType;
this.tree = tree;
this.pageFieldWidth = pageFieldWidth;
}

leaf(): boolean {
Expand All @@ -56,6 +62,25 @@ export class BTreeNode {
return this.internalPointers.length + this.leafPointers.length;
}

async readChildNode(index: number) {
const childPointer = this.pointer(index);

const { node, bytesRead } = await BTreeNode.fromMemoryPointer(
childPointer,
this.tree,
this.dataFileResolver,
this.fileFormat,
this.pageFieldType,
this.pageFieldWidth,
);

if (!bytesRead) {
throw new Error("bytes read do not line up");
}

return node;
}

async unmarshalBinary(buffer: ArrayBuffer, pageFieldWidth: number) {
let dataView = new DataView(buffer);
let size = dataView.getUint32(0, true);
Expand Down Expand Up @@ -226,6 +251,8 @@ export class BTreeNode {
dataFilePointer,
fileFormat,
pageFieldType,
resolver,
pageFieldWidth,
);

await node.unmarshalBinary(bufferData, pageFieldWidth);
Expand Down
4 changes: 2 additions & 2 deletions src/btree/traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export class TraversalIterator {
return false;
}
// propagate the rollover
this.records[i].node = await this.tree.readNode(
this.records[i + 1].node.pointer(this.records[i + 1].index),
this.records[i].node = await this.records[i + 1].node.readChildNode(
this.records[i + 1].index,
);

if (rolloverLeft) {
Expand Down

0 comments on commit 6866b95

Please sign in to comment.