Skip to content

Commit 2a36b57

Browse files
committed
fix the boundary iteration case for badger
1 parent eea7a2c commit 2a36b57

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

storage/operation/badgerimpl/iterator.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ func (i *badgerIterator) Valid() bool {
5353
return false
5454
}
5555

56+
// if upper bound is nil, then there's no upper bound, so it's always valid
57+
if i.upperBound == nil {
58+
return true
59+
}
60+
5661
// check if the key is within the upperbound (exclusive)
5762
key := i.iter.Item().Key()
5863
// note: for the boundary case,

storage/operation/reads_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,58 @@ func TestIterateKeysInPrefixRange(t *testing.T) {
6161
})
6262
}
6363

64+
func TestIterationBoundary(t *testing.T) {
65+
dbtest.RunWithStorages(t, func(t *testing.T, r storage.Reader, withWriter dbtest.WithWriter) {
66+
// Define the prefix range
67+
prefixStart := []byte{0x01}
68+
prefixEnd := []byte{0xff}
69+
70+
// Create a range of keys around the prefix start/end values
71+
keys := [][]byte{
72+
{0x00},
73+
{0x00, 0x00},
74+
{0x00, 0xff},
75+
{0x01},
76+
{0x01, 0x00},
77+
{0x01, 0xff},
78+
{0x02},
79+
{0xff},
80+
{0xff, 0x00},
81+
{0xff, 0xff},
82+
}
83+
84+
expectedKeys := [][]byte{
85+
{0x01},
86+
{0x01, 0x00},
87+
{0x01, 0xff},
88+
{0x02},
89+
{0xff},
90+
{0xff, 0x00},
91+
{0xff, 0xff},
92+
}
93+
94+
// Insert the keys into the storage
95+
require.NoError(t, withWriter(func(writer storage.Writer) error {
96+
for _, key := range keys {
97+
value := []byte{0x00} // value are skipped, doesn't matter
98+
err := operation.Upsert(key, value)(writer)
99+
if err != nil {
100+
return err
101+
}
102+
}
103+
return nil
104+
}))
105+
106+
// Forward iteration and check boundaries
107+
var found [][]byte
108+
require.NoError(t, operation.IterateKeysInPrefixRange(prefixStart, prefixEnd, func(key []byte) error {
109+
found = append(found, key)
110+
return nil
111+
})(r), "should iterate forward without error")
112+
require.ElementsMatch(t, expectedKeys, found, "forward iteration should return the correct keys in range")
113+
})
114+
}
115+
64116
func TestTraverse(t *testing.T) {
65117
dbtest.RunWithStorages(t, func(t *testing.T, r storage.Reader, withWriter dbtest.WithWriter) {
66118
keyVals := map[[2]byte]uint64{

0 commit comments

Comments
 (0)