Skip to content

Commit

Permalink
Fixes memiterator
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Sep 17, 2024
1 parent 081b90e commit 2b30bd8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
5 changes: 4 additions & 1 deletion server/v2/stf/branch/changeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type memIterator struct {
// The `valid` field of the iterator indicates whether the iterator is positioned at a valid key.
// The `start` and `end` fields of the iterator store the start and end keys respectively.
func newMemIterator(start, end []byte, tree *btree.BTreeG[item], ascending bool) *memIterator {
iter := tree.Iter()
iter := tree.Copy().Iter()
var valid bool
if ascending {
if start != nil {
Expand Down Expand Up @@ -207,6 +207,9 @@ func (mi *memIterator) keyInRange(key []byte) bool {
if !mi.ascending && mi.start != nil && bytes.Compare(key, mi.start) < 0 {
return false
}
if !mi.ascending && mi.end != nil && bytes.Compare(key, mi.end) >= 0 {
return false
}
return true
}

Expand Down
61 changes: 60 additions & 1 deletion server/v2/stf/branch/changeset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
)

func Test_memIterator(t *testing.T) {
func TestMemIteratorWithWriteToRebalance(t *testing.T) {
t.Run("iter is invalid after close", func(t *testing.T) {
cs := newChangeSet()
for i := byte(0); i < 32; i++ {
Expand All @@ -26,3 +26,62 @@ func Test_memIterator(t *testing.T) {
}
})
}

func TestKeyInRange(t *testing.T) {
specs := map[string]struct {
mi *memIterator
src []byte
exp bool
}{
"equal start": {
mi: &memIterator{ascending: true, start: []byte{0}, end: []byte{2}},
src: []byte{0},
exp: true,
},
"equal end": {
mi: &memIterator{ascending: true, start: []byte{0}, end: []byte{2}},
src: []byte{2},
exp: false,
},
"between": {
mi: &memIterator{ascending: true, start: []byte{0}, end: []byte{2}},
src: []byte{1},
exp: true,
},
"equal start - open end": {
mi: &memIterator{ascending: true, start: []byte{0}},
src: []byte{0},
exp: true,
},
"greater start - open end": {
mi: &memIterator{ascending: true, start: []byte{0}},
src: []byte{2},
exp: true,
},
"equal end - open start": {
mi: &memIterator{ascending: true, end: []byte{2}},
src: []byte{2},
exp: false,
},
"smaller end - open start": {
mi: &memIterator{ascending: true, end: []byte{2}},
src: []byte{1},
exp: true,
},
}
for name, spec := range specs {
for _, asc := range []bool{true, false} {
order := "asc_"
if !asc {
order = "desc_"
}
t.Run(order+name, func(t *testing.T) {
spec.mi.ascending = asc
got := spec.mi.keyInRange(spec.src)
if spec.exp != got {
t.Errorf("expected %v, got %v", spec.exp, got)
}
})
}
}
}

0 comments on commit 2b30bd8

Please sign in to comment.