sorted: make the in-memory implementation support Wiper

So an in-memory key/value store can be used as an index,
for the demo blob server on the website.
This commit is contained in:
Brad Fitzpatrick 2021-10-24 19:36:43 -07:00
parent 4fa9c80be0
commit e29e6dc98c
1 changed files with 12 additions and 2 deletions

View File

@ -31,10 +31,13 @@ import (
// NewMemoryKeyValue returns a KeyValue implementation that's backed only // NewMemoryKeyValue returns a KeyValue implementation that's backed only
// by memory. It's mostly useful for tests and development. // by memory. It's mostly useful for tests and development.
func NewMemoryKeyValue() KeyValue { func NewMemoryKeyValue() KeyValue {
db := memdb.New(comparer.DefaultComparer, 128) kv := new(memKeys)
return &memKeys{db: db} kv.Wipe()
return kv
} }
var _ Wiper = (*memKeys)(nil)
// memKeys is a naive in-memory implementation of KeyValue for test & development // memKeys is a naive in-memory implementation of KeyValue for test & development
// purposes only. // purposes only.
type memKeys struct { type memKeys struct {
@ -42,6 +45,13 @@ type memKeys struct {
db *memdb.DB db *memdb.DB
} }
func (s *memKeys) Wipe() error {
s.mu.Lock()
defer s.mu.Unlock()
s.db = memdb.New(comparer.DefaultComparer, 128)
return nil
}
// memIter converts from leveldb's iterator.Iterator interface, which // memIter converts from leveldb's iterator.Iterator interface, which
// operates on []byte, to Perkeep's index.Iterator, which operates // operates on []byte, to Perkeep's index.Iterator, which operates
// on string. // on string.