Skip to content
Snippets Groups Projects
Commit 3ce85fff authored by Wolfgang Welz's avatar Wolfgang Welz
Browse files

Reduce number of allocations when iterating through the db

parent db0dad7e
Branches
Tags
No related merge requests found
......@@ -120,23 +120,26 @@ func (this *databaseImpl) Delete(key []byte) error {
return err
}
func (this *databaseImpl) ForEach(consumer func(key []byte, value []byte)) error {
func (this *databaseImpl) ForEach(consumer func([]byte, []byte)) error {
err := this.db.View(func(txn *badger.Txn) error {
// create an iterator the default options
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
// avoid allocations by reusing the value buffer
var value []byte
// loop through every key-value-pair and call the function
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
key := item.Key()
value, err := item.ValueCopy(nil)
var err error
value, err = item.ValueCopy(value)
if err != nil {
return err
}
consumer(key, value)
consumer(item.Key(), value)
}
return nil
})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment