-
Notifications
You must be signed in to change notification settings - Fork 148
/
kv.go
53 lines (43 loc) · 1.09 KB
/
kv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package kv
import (
"fmt"
)
// Key represents a key in the KVStore, to avoid unnecessary conversions
// between string and []byte, we use string as key data type, but it does
// not necessarily have to be a valid UTF-8 string.
type Key string
func (k Key) Hex() string {
return fmt.Sprintf("kv.Key('%X')", k)
}
const EmptyPrefix = Key("")
func (k Key) HasPrefix(prefix Key) bool {
if len(prefix) > len(k) {
return false
}
return k[:len(prefix)] == prefix
}
// KVStore represents a key-value store
// where both keys and values are arbitrary byte slices.
type KVStore interface {
KVWriter
KVStoreReader
}
type KVReader interface {
// Get returns the value, or nil if not found
Get(key Key) []byte
Has(key Key) bool
}
type KVWriter interface {
Set(key Key, value []byte)
Del(key Key)
}
type KVIterator interface {
Iterate(prefix Key, f func(key Key, value []byte) bool)
IterateKeys(prefix Key, f func(key Key) bool)
IterateSorted(prefix Key, f func(key Key, value []byte) bool)
IterateKeysSorted(prefix Key, f func(key Key) bool)
}
type KVStoreReader interface {
KVReader
KVIterator
}