diff --git a/src/btree/node.ts b/src/btree/node.ts index 421210d..4d7a396 100644 --- a/src/btree/node.ts +++ b/src/btree/node.ts @@ -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[], @@ -28,6 +30,8 @@ export class BTreeNode { dataFileResolver: RangeResolver, fileFormat: FileFormat, pageFieldType: FieldType, + tree: RangeResolver, + pageFieldWidth: number, ) { this.keys = keys; this.leafPointers = leafPointers; @@ -35,6 +39,8 @@ export class BTreeNode { this.dataFileResolver = dataFileResolver; this.fileFormat = fileFormat; this.pageFieldType = pageFieldType; + this.tree = tree; + this.pageFieldWidth = pageFieldWidth; } leaf(): boolean { @@ -56,6 +62,25 @@ export class BTreeNode { return this.internalPointers.length + this.leafPointers.length; } + async child(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); @@ -226,6 +251,8 @@ export class BTreeNode { dataFilePointer, fileFormat, pageFieldType, + resolver, + pageFieldWidth, ); await node.unmarshalBinary(bufferData, pageFieldWidth); diff --git a/src/btree/traversal.ts b/src/btree/traversal.ts index 24ffda2..7d359de 100644 --- a/src/btree/traversal.ts +++ b/src/btree/traversal.ts @@ -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.child( + this.records[i + 1].index, ); if (rolloverLeft) {