Please see the code snipplet below. I want to iterate over all documents in the database. Then I do something with the value in the handlemessage()
function. So far so good. The last part is deleting the processed keys. This -for some reason- does not work ALL THE TIME. If I run the code multiple times, then eventually the database will be empty. But I don’t understand why the delete does not work all the time. (I don’t get an error by the way).
Is there some kind of race condition?
err := badgerDB.Update(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
k := item.Key()
err := item.Value(func(v []byte) error {
if err := handleMessage(k, v); err != nil {
log.Fatal(err)
}
return nil
})
if err := txn.Delete(k); err != nil {
log.Fatal(err)
}
if err != nil {
return err
}
}
return nil
})