-
Notifications
You must be signed in to change notification settings - Fork 5
/
memdb.go
206 lines (170 loc) · 4.78 KB
/
memdb.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package block_stm
import (
"io"
"cosmossdk.io/store/cachekv"
"cosmossdk.io/store/tracekv"
storetypes "cosmossdk.io/store/types"
"github.com/tidwall/btree"
)
type (
MemDB = GMemDB[[]byte]
ObjMemDB = GMemDB[any]
)
var (
_ storetypes.KVStore = (*MemDB)(nil)
_ storetypes.ObjKVStore = (*ObjMemDB)(nil)
)
func NewMemDB() *MemDB {
return NewGMemDB(BytesIsZero, BytesLen)
}
func NewObjMemDB() *ObjMemDB {
return NewGMemDB(ObjIsZero, ObjLen)
}
type GMemDB[V any] struct {
btree.BTreeG[memdbItem[V]]
isZero func(V) bool
valueLen func(V) int
}
func NewGMemDB[V any](
isZero func(V) bool,
valueLen func(V) int,
) *GMemDB[V] {
return &GMemDB[V]{
BTreeG: *btree.NewBTreeG[memdbItem[V]](KeyItemLess),
isZero: isZero,
valueLen: valueLen,
}
}
// NewGMemDBNonConcurrent returns a new BTree which is not concurrency safe.
func NewGMemDBNonConcurrent[V any](
isZero func(V) bool,
valueLen func(V) int,
) *GMemDB[V] {
return &GMemDB[V]{
BTreeG: *btree.NewBTreeGOptions[memdbItem[V]](KeyItemLess, btree.Options{
NoLocks: true,
}),
isZero: isZero,
valueLen: valueLen,
}
}
func (db *GMemDB[V]) Scan(cb func(key Key, value V) bool) {
db.BTreeG.Scan(func(item memdbItem[V]) bool {
return cb(item.key, item.value)
})
}
func (db *GMemDB[V]) Get(key []byte) V {
item, ok := db.BTreeG.Get(memdbItem[V]{key: key})
if !ok {
var empty V
return empty
}
return item.value
}
func (db *GMemDB[V]) Has(key []byte) bool {
return !db.isZero(db.Get(key))
}
func (db *GMemDB[V]) Set(key []byte, value V) {
if db.isZero(value) {
panic("nil value not allowed")
}
db.BTreeG.Set(memdbItem[V]{key: key, value: value})
}
func (db *GMemDB[V]) Delete(key []byte) {
db.BTreeG.Delete(memdbItem[V]{key: key})
}
// When used as an overlay (e.g. WriteSet), it stores the `nil` value to represent deleted keys,
// so we return seperate bool value for found status.
func (db *GMemDB[V]) OverlayGet(key Key) (V, bool) {
item, ok := db.BTreeG.Get(memdbItem[V]{key: key})
if !ok {
var zero V
return zero, false
}
return item.value, true
}
// When used as an overlay (e.g. WriteSet), it stores the `nil` value to represent deleted keys,
func (db *GMemDB[V]) OverlaySet(key Key, value V) {
db.BTreeG.Set(memdbItem[V]{key: key, value: value})
}
func (db *GMemDB[V]) Iterator(start, end []byte) storetypes.GIterator[V] {
return db.iterator(start, end, true)
}
func (db *GMemDB[V]) ReverseIterator(start, end []byte) storetypes.GIterator[V] {
return db.iterator(start, end, false)
}
func (db *GMemDB[V]) iterator(start, end Key, ascending bool) storetypes.GIterator[V] {
return NewMemDBIterator(start, end, db.Iter(), ascending)
}
func (db *GMemDB[V]) GetStoreType() storetypes.StoreType {
return storetypes.StoreTypeIAVL
}
// CacheWrap implements types.KVStore.
func (db *GMemDB[V]) CacheWrap() storetypes.CacheWrap {
return cachekv.NewGStore(db, db.isZero, db.valueLen)
}
// CacheWrapWithTrace implements types.KVStore.
func (db *GMemDB[V]) CacheWrapWithTrace(w io.Writer, tc storetypes.TraceContext) storetypes.CacheWrap {
if store, ok := any(db).(*GMemDB[[]byte]); ok {
return cachekv.NewGStore(tracekv.NewStore(store, w, tc), store.isZero, store.valueLen)
}
return db.CacheWrap()
}
type MemDBIterator[V any] struct {
BTreeIteratorG[memdbItem[V]]
}
var _ storetypes.Iterator = (*MemDBIterator[[]byte])(nil)
func NewMemDBIterator[V any](start, end Key, iter btree.IterG[memdbItem[V]], ascending bool) *MemDBIterator[V] {
return &MemDBIterator[V]{*NewBTreeIteratorG(
memdbItem[V]{key: start},
memdbItem[V]{key: end},
iter,
ascending,
)}
}
func NewNoopIterator[V any](start, end Key, ascending bool) storetypes.GIterator[V] {
return &MemDBIterator[V]{BTreeIteratorG[memdbItem[V]]{
start: start,
end: end,
ascending: ascending,
valid: false,
}}
}
func (it *MemDBIterator[V]) Value() V {
return it.Item().value
}
type memdbItem[V any] struct {
key Key
value V
}
var _ KeyItem = memdbItem[[]byte]{}
func (item memdbItem[V]) GetKey() []byte {
return item.key
}
type MultiMemDB struct {
dbs map[storetypes.StoreKey]storetypes.Store
}
var _ MultiStore = (*MultiMemDB)(nil)
func NewMultiMemDB(stores map[storetypes.StoreKey]int) *MultiMemDB {
dbs := make(map[storetypes.StoreKey]storetypes.Store, len(stores))
for name := range stores {
switch name.(type) {
case *storetypes.ObjectStoreKey:
dbs[name] = NewObjMemDB()
default:
dbs[name] = NewMemDB()
}
}
return &MultiMemDB{
dbs: dbs,
}
}
func (mmdb *MultiMemDB) GetStore(store storetypes.StoreKey) storetypes.Store {
return mmdb.dbs[store]
}
func (mmdb *MultiMemDB) GetKVStore(store storetypes.StoreKey) storetypes.KVStore {
return mmdb.GetStore(store).(storetypes.KVStore)
}
func (mmdb *MultiMemDB) GetObjKVStore(store storetypes.StoreKey) storetypes.ObjKVStore {
return mmdb.GetStore(store).(storetypes.ObjKVStore)
}