I have some very simple code that’s not doing what I expect. Probably a newbie mistake.
Here’s the pseudo-code version:
h.DB, err := badger.Open(opts)
err = h.DB.Update(func(txn *badger.Txn) error {
e := badger.NewEntry("x:1", "1")
_ = txn.SetEntry(e)
e = badger.NewEntry("x:2", "2")
_ = txn.SetEntry(e)
} // All good
// Let's check
err = h.DB.View(func(txn *badger.Txn) error {
v, err := txn.Get("x:1")
assert(v == "1") // Success!
v, err = txn.Get("x:2")
assert(v == "2") // Success!
return err
})
err := h.DB.Update(func(txn *badger.Txn) error {
return txn.Delete("x:1")
})
assert(err == nil) // All good!
// Lookup of "x:1" should fail
err = h.DB.View(func(txn *badger.Txn) error {
_, err := txn.Get(key)
assert(err == "Key not found") // As expected!
return err
})
// Now, iterate over the key prefix
err := h.DB.View(func(txn *badger.Txn) error {
iter := txn.NewIterator(badger.DefaultIteratorOptions)
defer iter.Close()
prefix := "x:"
for iter.Seek(prefix); iter.ValidForPrefix(prefix); iter.Next() {
item := iter.Item()
if !item.IsDeletedOrExpired() {
fmt.Printf(item.Key) // prints "x:1" ... WTF?
}
}
If need be I can turn this into working code (or non-working code, so to speak) to demonstrate the problem but I’m hoping someone can just tell me what I’m doing wrong here!