Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cache root node + memptr #314

Merged
merged 3 commits into from
May 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/btree/btree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export class BTree {
private readonly pageFieldType: FieldType;
private readonly pageFieldWidth: number;

private rootNodeCache: Promise<BTreeNode> | null = null;
private rootPointerCache: Promise<MemoryPointer> | null = null;

// insight attributes below

private readonly entries: number;
Expand Down Expand Up @@ -48,27 +51,25 @@ export class BTree {
}

async root(): Promise<RootResponse> {
const mp = await this.meta.root();

if (!mp || mp.length === 0) {
return {
rootNode: null,
pointer: mp,
};
if (!this.rootNodeCache || !this.rootPointerCache) {
this.rootPointerCache = this.meta.root();
this.rootNodeCache = this.rootPointerCache.then(ptr => this.readNode(ptr));
}

const root = await this.readNode(mp);
if (!root) {
return {
rootNode: null,
pointer: mp,
};
}
const rootNode = await this.rootNodeCache;
const pointer = await this.rootPointerCache;

if (!rootNode) {
return {
rootNode: null,
pointer,
}
}

return {
rootNode: root,
pointer: mp,
};
return {
rootNode,
pointer
}
}

async readNode(ptr: MemoryPointer): Promise<BTreeNode> {
Expand Down
Loading