From 6866b958c01204f3af55b05875c6f612bca4ce7e Mon Sep 17 00:00:00 2001 From: Matthew Kim <38759997+friendlymatthew@users.noreply.github.com> Date: Wed, 29 May 2024 11:01:27 -0400 Subject: [PATCH 1/2] feat: move child resolution to bptree node --- src/btree/node.ts | 27 +++++++++++++++++++++++++++ src/btree/traversal.ts | 4 ++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/btree/node.ts b/src/btree/node.ts index 421210d6..2e2dc57a 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 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); @@ -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 24ffda2c..056d748c 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.readChildNode( + this.records[i + 1].index, ); if (rolloverLeft) { From 51f43e75eea1489be2ea0352fd027cbcaa897923 Mon Sep 17 00:00:00 2001 From: Matthew Kim <38759997+friendlymatthew@users.noreply.github.com> Date: Wed, 29 May 2024 11:40:16 -0400 Subject: [PATCH 2/2] rename conventions --- src/btree/node.ts | 2 +- src/btree/traversal.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/btree/node.ts b/src/btree/node.ts index 2e2dc57a..4d7a3960 100644 --- a/src/btree/node.ts +++ b/src/btree/node.ts @@ -62,7 +62,7 @@ export class BTreeNode { return this.internalPointers.length + this.leafPointers.length; } - async readChildNode(index: number) { + async child(index: number) { const childPointer = this.pointer(index); const { node, bytesRead } = await BTreeNode.fromMemoryPointer( diff --git a/src/btree/traversal.ts b/src/btree/traversal.ts index 056d748c..7d359de1 100644 --- a/src/btree/traversal.ts +++ b/src/btree/traversal.ts @@ -59,7 +59,7 @@ export class TraversalIterator { return false; } // propagate the rollover - this.records[i].node = await this.records[i + 1].node.readChildNode( + this.records[i].node = await this.records[i + 1].node.child( this.records[i + 1].index, );