Skip to content

Commit c62e07b

Browse files
committed
manifest: add btree.All
Add an All method that returns a Go iterator over the B-Tree's items.
1 parent 8dc3fce commit c62e07b

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

internal/manifest/btree.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
stdcmp "cmp"
1010
"fmt"
11+
"iter"
1112
"strings"
1213
"sync"
1314
"sync/atomic"
@@ -773,6 +774,20 @@ func (t *btree[M]) Insert(item M) error {
773774
return err
774775
}
775776

777+
// All returns an iterator over all the items in the tree.
778+
func (t *btree[M]) All() iter.Seq[M] {
779+
iter := iterator[M]{r: t.root, pos: -1, cmp: t.bcmp}
780+
iter.first()
781+
return func(yield func(M) bool) {
782+
for iter.valid() {
783+
if !yield(iter.cur()) {
784+
return
785+
}
786+
iter.next()
787+
}
788+
}
789+
}
790+
776791
// tableMetadataIter returns a new iterator over a B-Tree of *TableMetadata. It
777792
// is not safe to continue using an iterator after modifications are made to the
778793
// tree. If modifications are made, create a new iterator.

internal/manifest/btree_test.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,8 @@ func TestBTreeCloneConcurrentOperations(t *testing.T) {
462462
t.Logf("Starting equality checks on %d trees", len(trees))
463463
want := rang(0, cloneTestSize-1)
464464
for i, tree := range trees {
465-
if got := all(tree); !reflect.DeepEqual(strReprs(got), strReprs(want)) {
465+
got := slices.Collect(tree.All())
466+
if !reflect.DeepEqual(strReprs(got), strReprs(want)) {
466467
t.Errorf("tree %v mismatch", i)
467468
}
468469
}
@@ -489,7 +490,8 @@ func TestBTreeCloneConcurrentOperations(t *testing.T) {
489490
} else {
490491
wantpart = want
491492
}
492-
if got := all(tree); !reflect.DeepEqual(strReprs(got), strReprs(wantpart)) {
493+
got := slices.Collect(tree.All())
494+
if !reflect.DeepEqual(strReprs(got), strReprs(wantpart)) {
493495
t.Errorf("tree %v mismatch, want %#v got %#v", i, strReprs(wantpart), strReprs(got))
494496
}
495497
}
@@ -666,17 +668,6 @@ func strReprs(items []*TableMetadata) []string {
666668
return s
667669
}
668670

669-
// all extracts all items from a tree in order as a slice.
670-
func all(tr *btree[*TableMetadata]) (out []*TableMetadata) {
671-
it := tableMetadataIter(tr)
672-
it.first()
673-
for it.valid() {
674-
out = append(out, it.cur())
675-
it.next()
676-
}
677-
return out
678-
}
679-
680671
func forBenchmarkSizes(b *testing.B, f func(b *testing.B, count int)) {
681672
for _, count := range []int{16, 128, 1024, 8192, 65536} {
682673
b.Run(fmt.Sprintf("count=%d", count), func(b *testing.B) {

0 commit comments

Comments
 (0)