Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions .changeset/btree-max-node-size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@convex-dev/aggregate": patch
---

Read a tree's node size once per write instead of looking up the tree again at
every level of the B-tree, which speeds up inserts and deletes. (#305)
26 changes: 14 additions & 12 deletions src/component/btree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function insertHandler(
) {
const tree = await getOrCreateTree(ctx.db, args, DEFAULT_MAX_NODE_SIZE, true);
const summand = args.summand ?? 0;
const pushUp = await insertIntoNode(ctx, args, tree.root, {
const pushUp = await insertIntoNode(ctx, tree.maxNodeSize, tree.root, {
k: args.key,
v: args.value,
s: summand,
Expand All @@ -93,7 +93,7 @@ export async function deleteHandler(
args: { key: Key; namespace?: Namespace; name?: string },
) {
const tree = await getOrCreateTree(ctx.db, args, DEFAULT_MAX_NODE_SIZE, true);
await deleteFromNode(ctx, args, tree.root, args.key);
await deleteFromNode(ctx, tree.maxNodeSize, tree.root, args.key);
const root = (await ctx.db.get("btreeNode", tree.root))!;
if (root.items.length === 0 && root.subtrees.length === 1) {
log(
Expand Down Expand Up @@ -217,11 +217,14 @@ async function MAX_NODE_SIZE(ctx: { db: DatabaseReader }, treeKey: TreeKey) {
}

async function MIN_NODE_SIZE(ctx: { db: DatabaseReader }, treeKey: TreeKey) {
const max = await MAX_NODE_SIZE(ctx, treeKey);
if (max % 2 !== 0 || max < 4) {
return minNodeSizeFor(await MAX_NODE_SIZE(ctx, treeKey));
}

function minNodeSizeFor(maxNodeSize: number) {
if (maxNodeSize % 2 !== 0 || maxNodeSize < 4) {
throw new Error("MAX_NODE_SIZE must be even and at least 4");
}
return max / 2;
return maxNodeSize / 2;
}

async function validateNode(
Expand Down Expand Up @@ -607,7 +610,7 @@ export const offsetUntil = query({

async function deleteFromNode(
ctx: { db: DatabaseWriter },
treeKey: TreeKey,
maxNodeSize: number,
node: Id<"btreeNode">,
key: Key,
): Promise<Item | null> {
Expand Down Expand Up @@ -658,7 +661,7 @@ async function deleteFromNode(
message: `key ${p(key)} not found in node ${n._id}`,
});
}
const deleted = await deleteFromNode(ctx, treeKey, n.subtrees[i], key);
const deleted = await deleteFromNode(ctx, maxNodeSize, n.subtrees[i], key);
if (!deleted) {
return null;
}
Expand All @@ -674,7 +677,7 @@ async function deleteFromNode(

// Now we need to check if the subtree at index i is too small
const deficientSubtree = (await ctx.db.get("btreeNode", n.subtrees[i]))!;
const minNodeSize = await MIN_NODE_SIZE(ctx, treeKey);
const minNodeSize = minNodeSizeFor(maxNodeSize);
if (deficientSubtree.items.length < minNodeSize) {
log(`deficient subtree ${deficientSubtree._id}`);
// If the subtree is too small, we need to rebalance
Expand Down Expand Up @@ -932,7 +935,7 @@ type PushUp = {

async function insertIntoNode(
ctx: { db: DatabaseWriter },
treeKey: TreeKey,
maxNodeSize: number,
node: Id<"btreeNode">,
item: Item,
): Promise<PushUp | null> {
Expand All @@ -951,7 +954,7 @@ async function insertIntoNode(
// insert key before index i
if (n.subtrees.length > 0) {
// insert into subtree
const pushUp = await insertIntoNode(ctx, treeKey, n.subtrees[i], item);
const pushUp = await insertIntoNode(ctx, maxNodeSize, n.subtrees[i], item);
if (pushUp) {
await ctx.db.patch("btreeNode", node, {
items: [...n.items.slice(0, i), pushUp.item, ...n.items.slice(i)],
Expand All @@ -976,8 +979,7 @@ async function insertIntoNode(
}

const newN = (await ctx.db.get("btreeNode", node))!;
const maxNodeSize = await MAX_NODE_SIZE(ctx, treeKey);
const minNodeSize = await MIN_NODE_SIZE(ctx, treeKey);
const minNodeSize = minNodeSizeFor(maxNodeSize);
if (newN.items.length > maxNodeSize) {
if (
newN.items.length !== maxNodeSize + 1 ||
Expand Down
Loading