What version of Go are you using (go version
)?
$ go version
go version go1.14.2 linux/amd64
What operating system are you using?
$ uname -a
Linux machine1 5.7.0-1-amd64 #1 SMP Debian 5.7.6-1 (2020-06-24) x86_64 GNU/Linux
What version of Badger are you using?
github.com/dgraph-io/badger/v2@v2.0.3
Does this issue reproduce with the latest master?
Yes.
foo@machine1:/go/src/github.com/xinau/relmon$ go get github.com/dgraph-io/badger/v2@master
go: github.com/dgraph-io/badger/v2 master => v2.0.1-rc1.0.20200718025359-1ccf3a875dd7
go: downloading github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20200718025359-1ccf3a875dd7
go: downloading github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de
Steps to Reproduce the issue
package main
import (
"fmt"
"reflect"
"testing"
"github.com/dgraph-io/badger/v2"
)
func TestReverse(t *testing.T) {
db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true))
if err != nil {
t.Errorf("opening database: %s", err)
}
for _, test := range ([]struct{
key []byte
val []byte
}{
{[]byte("key1.0"), []byte("val1")},
{[]byte("key1.1"), []byte("val2")},
{[]byte("key2.2"), []byte("val1")},
{[]byte("key1.3"), []byte("val3")},
{[]byte("key2.4"), []byte("val2")},
}){
if err := db.Update(func(txn *badger.Txn) error {
ent := badger.NewEntry(test.key, test.val)
return txn.SetEntry(ent)
}); err != nil {
t.Errorf("adding value: %s", err)
}
}
for _, test := range ([]struct{
key []byte
want []byte
}{
{[]byte("key1"), []byte("val3")},
{[]byte("key2"), []byte("val2")},
}) {
if err := db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.Reverse = true
it := txn.NewIterator(opts)
defer it.Close()
it.Seek(test.key)
if !it.ValidForPrefix(test.key) {
return fmt.Errorf("key not valid for prefix")
}
item := it.Item()
item.Value(func(data []byte) error {
if !reflect.DeepEqual(data, test.want) {
t.Errorf("got %q want %q", data, test.want)
}
return nil
})
return nil
}); err != nil {
t.Errorf("getting value: %s", err)
}
}
}
What Badger options were set?
The DefaultOptions
with In-Memory where set.
What did you do?
Iterating in reverse over a list of keys and trying to get the last key for prefix as shown in the test case.
What did you expect to see?
The test case above to pass.
What did you see instead?
$ go test ./internal/store --count 1
--- FAIL: TestReverse (0.00s)
store_test.go:61: getting value: key not valid for prefix
store_test.go:61: getting value: key not valid for prefix
FAIL
FAIL example.com/main 0.005s
FAIL
Adiditonal notes?
When executing the same test with IterationOptions.Reverse set to false the test fails as expected with.
$ go test ./internal/store --count 1
--- FAIL: TestReverse (0.00s)
store_test.go:55: got "val1" want "val3"
store_test.go:55: got "val1" want "val2"
FAIL
FAIL example.com/main 0.005s
FAIL
P.s. This issue is probably me overlooking something obvious, but I could need some help or explanation to get the test to pass.
P.s.s I enjoy the work and tools you folks are providing and truly want to say thanks.