Skip to content

Fix: handle last root numChildren when NumDocuments is exact multiple of MinKeys - #6

Merged
kael-reviewer[bot] merged 1 commit into
kael-agentsfrom
fix/task-5-diskinit-last-root-numchildren
Apr 15, 2026
Merged

Fix: handle last root numChildren when NumDocuments is exact multiple of MinKeys#6
kael-reviewer[bot] merged 1 commit into
kael-agentsfrom
fix/task-5-diskinit-last-root-numchildren

Conversation

@kael-developer

Copy link
Copy Markdown

Problem

In diskInitBtree(), the last root node's numChildren is computed as:

node.numChildren = int(t.NumDocuments % int64(MinKeys))
if node.numChildren == 0 {
    node.numChildren = MinKeys
}

When NumDocuments is an exact multiple of MinKeys (e.g., 2 × MinKeys), the i+1 == t.NumRoots check applies the "partial last root" adjustment to a root that is actually full. While the == 0 guard sets numChildren = MinKeys, the underlying issue is that the partial-root logic should not apply at all in this case.

Fix

doclite/btree.go: Changed the condition from i+1 == t.NumRoots to i+1 == t.NumRoots && t.NumDocuments%int64(MinKeys) != 0, so the partial-root adjustment only applies when there genuinely is a partial last root.

doclite/btree_test.go: Added TestBtreeDiskInitExactMultiple that inserts exactly 2 × MinKeys documents and verifies:

  • All documents are findable via Find()
  • The last root's numChildren == MinKeys
  • After diskInitBtree() re-initialization, all documents remain findable and numChildren is correct

Testing

go test ./doclite/ -v -run TestBtreeDiskInitExactMultiple
go test ./doclite/ -v

@kael-reviewer
kael-reviewer Bot merged commit 2555336 into kael-agents Apr 15, 2026
@kael-reviewer
kael-reviewer Bot deleted the fix/task-5-diskinit-last-root-numchildren branch April 15, 2026 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants