-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
62 lines (52 loc) · 1.12 KB
/
store.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
54
55
56
57
58
59
60
61
62
package keeper
import (
"github.com/ci123chain/ci123chain/pkg/abci/types"
)
type Store struct {
parent types.KVStore
prefix []byte
}
func NewStore(parent types.KVStore, prefix []byte) Store {
return Store{
parent: parent,
prefix: prefix,
}
}
// Implements KVStore
func (s Store) Set(key, value []byte) {
AssertValidKey(key)
AssertValidValue(value)
s.parent.Set(s.key(key), value)
return
}
// Implements KVStore
func (s Store) Get(key []byte) []byte {
res := s.parent.Get(s.key(key))
return res
}
// Implements KVStore
func (s Store) Has(key []byte) bool {
res := s.parent.Get(s.key(key))
return len(res) != 0
}
// Implements KVStore
func (s Store) Delete(key []byte) {
s.parent.Delete(s.key(key))
}
// Implements KVStore
func (s Store) Iterator(start, end []byte) types.Iterator{
return s.parent.Iterator(s.key(start), s.key(end))
}
func (s Store) key(key []byte) (res []byte) {
if key == nil {
panic("nil key on Store")
}
res = cloneAppend(s.prefix, key)
return
}
func cloneAppend(bz []byte, tail []byte) (res []byte) {
res = make([]byte, len(bz)+len(tail))
copy(res, bz)
copy(res[len(bz):], tail)
return
}