Reset a namespaced kv-store

This commit is contained in:
Jakob Borg
2015-05-12 17:37:47 +02:00
parent 83ea8dc577
commit 0b9441e1a4
2 changed files with 57 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ import (
"time"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/util"
)
// NamespacedKV is a simple key-value store using a specific namespace within
@@ -29,6 +30,27 @@ func NewNamespacedKV(db *leveldb.DB, prefix string) *NamespacedKV {
}
}
// Reset removes all entries in this namespace.
func (n *NamespacedKV) Reset() {
it := n.db.NewIterator(util.BytesPrefix(n.prefix), nil)
defer it.Release()
batch := new(leveldb.Batch)
for it.Next() {
batch.Delete(it.Key())
if batch.Len() > batchFlushSize {
if err := n.db.Write(batch, nil); err != nil {
panic(err)
}
batch.Reset()
}
}
if batch.Len() > 0 {
if err := n.db.Write(batch, nil); err != nil {
panic(err)
}
}
}
// PutInt64 stores a new int64. Any existing value (even if of another type)
// is overwritten.
func (n *NamespacedKV) PutInt64(key string, val int64) {