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

Add delete and iterator functionality to database

parent 584569ef
No related branches found
No related tags found
No related merge requests found
...@@ -112,6 +112,37 @@ func (this *databaseImpl) Get(key []byte) ([]byte, error) { ...@@ -112,6 +112,37 @@ func (this *databaseImpl) Get(key []byte) ([]byte, error) {
return result, err return result, err
} }
func (this *databaseImpl) Delete(key []byte) error {
err := this.db.Update(func(txn *badger.Txn) error {
err := txn.Delete(key)
return err
})
return err
}
func (this *databaseImpl) ForEach(consumer func(key []byte, value []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()
// 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)
if err != nil {
return err
}
consumer(key, value)
}
return nil
})
return err
}
func (this *databaseImpl) Close() error { func (this *databaseImpl) Close() error {
this.openLock.Lock() this.openLock.Lock()
defer this.openLock.Unlock() defer this.openLock.Unlock()
......
...@@ -3,7 +3,9 @@ package database ...@@ -3,7 +3,9 @@ package database
type Database interface { type Database interface {
Open() error Open() error
Set(key []byte, value []byte) error Set(key []byte, value []byte) error
Get(key []byte) ([]byte, error)
Contains(key []byte) (bool, error) Contains(key []byte) (bool, error)
Get(key []byte) ([]byte, error)
ForEach(func(key []byte, value []byte)) error
Delete(key []byte) error
Close() error Close() error
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment