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

Add ForEachWithPrefix database function

parent 1b10d910
Branches
Tags
No related merge requests found
...@@ -100,10 +100,10 @@ func (this *prefixDb) Delete(key []byte) error { ...@@ -100,10 +100,10 @@ func (this *prefixDb) Delete(key []byte) error {
return err return err
} }
func (this *prefixDb) ForEach(consumer func([]byte, []byte)) error { func (this *prefixDb) forEach(prefix []byte, consumer func([]byte, []byte)) error {
err := this.db.View(func(txn *badger.Txn) error { err := this.db.View(func(txn *badger.Txn) error {
iteratorOptions := badger.DefaultIteratorOptions iteratorOptions := badger.DefaultIteratorOptions
iteratorOptions.Prefix = this.prefix // filter by prefix iteratorOptions.Prefix = prefix // filter by prefix
// create an iterator the default options // create an iterator the default options
it := txn.NewIterator(iteratorOptions) it := txn.NewIterator(iteratorOptions)
...@@ -124,3 +124,11 @@ func (this *prefixDb) ForEach(consumer func([]byte, []byte)) error { ...@@ -124,3 +124,11 @@ func (this *prefixDb) ForEach(consumer func([]byte, []byte)) error {
}) })
return err return err
} }
func (this *prefixDb) ForEachWithPrefix(prefix []byte, consumer func([]byte, []byte)) error {
return this.forEach(append(this.prefix, prefix...), consumer)
}
func (this *prefixDb) ForEach(consumer func([]byte, []byte)) error {
return this.forEach(this.prefix, consumer)
}
...@@ -7,6 +7,7 @@ type Database interface { ...@@ -7,6 +7,7 @@ type Database interface {
SetWithTTL(key []byte, value []byte, ttl time.Duration) error SetWithTTL(key []byte, value []byte, ttl time.Duration) error
Contains(key []byte) (bool, error) Contains(key []byte) (bool, error)
Get(key []byte) ([]byte, error) Get(key []byte) ([]byte, error)
ForEach(func(key []byte, value []byte)) error ForEach(consumer func(key []byte, value []byte)) error
ForEachWithPrefix(prefix []byte, consumer func(key []byte, value []byte)) error
Delete(key []byte) error Delete(key []byte) error
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment