diff --git a/btree.go b/btree.go index 02cc600..9bea785 100644 --- a/btree.go +++ b/btree.go @@ -1091,28 +1091,30 @@ func (b *BTree) nget(x *Node, k []byte) ([]*Key, error) { keys := make([]*Key, 0) if x != nil { i := 0 - for i < len(x.Keys) && notEq(x.Keys[i].K, k) { - if !x.Leaf { - childBytes, err := b.Pager.GetPage(x.Children[i]) - if err != nil { - return nil, err - } - - child, err := decodeNode(childBytes) - if err != nil { - return nil, err - } - - childKeys, err := b.nget(child, k) - if err != nil { - return nil, err + for i < len(x.Keys) { + if notEq(x.Keys[i].K, k) { + if !x.Leaf { + childBytes, err := b.Pager.GetPage(x.Children[i]) + if err != nil { + return nil, err + } + + child, err := decodeNode(childBytes) + if err != nil { + return nil, err + } + + childKeys, err := b.nget(child, k) + if err != nil { + return nil, err + } + keys = append(keys, childKeys...) } - keys = append(keys, childKeys...) + keys = append(keys, x.Keys[i]) } - keys = append(keys, x.Keys[i]) i++ } - if !x.Leaf && i < len(x.Children) { + if !x.Leaf && i <= len(x.Children) { childBytes, err := b.Pager.GetPage(x.Children[i]) if err != nil { return nil, err @@ -1131,7 +1133,6 @@ func (b *BTree) nget(x *Node, k []byte) ([]*Key, error) { } } return keys, nil - } // InOrderTraversal returns all keys in the BTree in order diff --git a/btree_test.go b/btree_test.go index 12aa917..b92aacb 100644 --- a/btree_test.go +++ b/btree_test.go @@ -263,6 +263,10 @@ func TestBTree_NGet(t *testing.T) { return } + if len(keys) != 99 { + t.Fatalf("expected 99 keys, got %d", len(keys)) + } + for _, key := range keys { if string(key.K) == "key_50" { t.Fatalf("expected key not to be key_50")