storage: add seek implementation
during testing found missing Seek() implementation for inmemorydb
This commit is contained in:
parent
8e75674f30
commit
55dfc0bbbc
2 changed files with 23 additions and 0 deletions
|
@ -2,6 +2,7 @@ package storage
|
|||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
|
@ -67,6 +68,12 @@ func (s *MemoryStore) PutBatch(batch Batch) error {
|
|||
|
||||
// Seek implements the Store interface.
|
||||
func (s *MemoryStore) Seek(key []byte, f func(k, v []byte)) {
|
||||
for k, v := range s.mem {
|
||||
if strings.Contains(k, hex.EncodeToString(key)) {
|
||||
decodeString, _ := hex.DecodeString(k)
|
||||
f(decodeString, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Batch implements the Batch interface and returns a compatible Batch.
|
||||
|
|
|
@ -59,3 +59,19 @@ func TestPutBatch(t *testing.T) {
|
|||
assert.Equal(t, value, newVal)
|
||||
require.NoError(t, s.Close())
|
||||
}
|
||||
|
||||
func TestMemoryStore_Seek(t *testing.T) {
|
||||
var (
|
||||
s = NewMemoryStore()
|
||||
key = []byte("sparse")
|
||||
value = []byte("rocks")
|
||||
)
|
||||
|
||||
if err := s.Put(key, value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
s.Seek(key, func(k, v []byte) {
|
||||
assert.Equal(t, value, v)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue