Skip to content

Commit

Permalink
fix: fix reverse iterator broken by seek
Browse files Browse the repository at this point in the history
  • Loading branch information
harshil-goel committed Sep 12, 2024
1 parent 02d7531 commit ef88e2b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
32 changes: 32 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,38 @@ func runBadgerTest(t *testing.T, opts *Options, test func(t *testing.T, db *DB))
test(t, db)
}

func TestReverseIterator(t *testing.T) {
runBadgerTest(t, nil, func(t *testing.T, db *DB) {
key := make([]byte, 6)
db.Update(func(txn *Txn) error {

Check failure on line 170 in db_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `db.Update` is not checked (errcheck)
binary.BigEndian.PutUint16(key, 5)
binary.BigEndian.PutUint32(key[2:], 1)
txn.Set(key, []byte("value1"))

Check failure on line 173 in db_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `txn.Set` is not checked (errcheck)

binary.BigEndian.PutUint32(key[2:], 2)
txn.Set(key, []byte("value2"))

Check failure on line 176 in db_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `txn.Set` is not checked (errcheck)
return nil
})

db.View(func(txn *Txn) error {

Check failure on line 180 in db_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `db.View` is not checked (errcheck)
searchBuffer := make([]byte, 3)
binary.BigEndian.PutUint16(searchBuffer, 5)
searchBuffer[2] = 0xFF

iteratorOptions := DefaultIteratorOptions
iteratorOptions.Reverse = true
iteratorOptions.PrefetchValues = false
iteratorOptions.Prefix = searchBuffer
it := txn.NewIterator(iteratorOptions)
defer it.Close()

it.Rewind()
require.Equal(t, it.Item().Key(), key)
return nil
})
})
}

func TestWrite(t *testing.T) {
runBadgerTest(t, nil, func(t *testing.T, db *DB) {
for i := 0; i < 100; i++ {
Expand Down
10 changes: 5 additions & 5 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ func (it *Iterator) Next() {

// Set next item to current
it.item = it.data.pop()
for it.iitr.Valid() && hasPrefix(it.iitr, it.opt.Prefix) {
for it.iitr.Valid() && (!it.opt.Reverse && hasPrefix(it)) {
if it.parseItem() {
// parseItem calls one extra next.
// This is used to deal with the complexity of reverse iteration.
Expand Down Expand Up @@ -725,9 +725,9 @@ func (it *Iterator) fill(item *Item) {
}
}

func hasPrefix(it y.Iterator, prefix []byte) bool {
if len(prefix) > 0 {
return bytes.HasPrefix(y.ParseKey(it.Key()), prefix)
func hasPrefix(it *Iterator) bool {
if !it.opt.Reverse && len(it.opt.Prefix) > 0 {
return bytes.HasPrefix(y.ParseKey(it.iitr.Key()), it.opt.Prefix)
}
return true
}
Expand All @@ -741,7 +741,7 @@ func (it *Iterator) prefetch() {
i := it.iitr
var count int
it.item = nil
for i.Valid() && hasPrefix(i, it.opt.Prefix) {
for i.Valid() && hasPrefix(it) {
if !it.parseItem() {
continue
}
Expand Down

0 comments on commit ef88e2b

Please sign in to comment.