-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseKVStore.go
381 lines (320 loc) · 8.49 KB
/
baseKVStore.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package store
import (
"bytes"
"fmt"
sdk "github.com/ci123chain/ci123chain/pkg/abci/types"
sdkerrors "github.com/ci123chain/ci123chain/pkg/abci/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/tmhash"
db "github.com/tendermint/tm-db"
"io"
"sort"
"sync"
)
type baseKVStore struct {
mtx sync.Mutex
cache map[string]cValue
parent KVStore
storeEvery int64
numRecent int64
key sdk.StoreKey
}
func NewBaseKVStore(parent KVStore, storeEvery, numRecent int64, key sdk.StoreKey) *baseKVStore {
return &baseKVStore{
cache: make(map[string]cValue),
parent: parent,
storeEvery: storeEvery,
numRecent: numRecent,
key: key,
}
}
func (ks *baseKVStore) SetPruning(pruning sdk.PruningStrategy) {
switch pruning {
case sdk.PruneEverything:
ks.numRecent = 0
ks.storeEvery = 0
case sdk.PruneNothing:
ks.storeEvery = 1
case sdk.PruneSyncable:
ks.numRecent = 100
ks.storeEvery = 10000
}
}
// Implements Store.
func (ks *baseKVStore) GetStoreType() StoreType {
return sdk.StoreTypeMulti
}
// Implements KVStore.
func (ks *baseKVStore) Get(key []byte) (value []byte) {
ks.mtx.Lock()
defer ks.mtx.Unlock()
ks.assertValidKey(key)
ckey := string(key)
cacheValue, ok := ks.cache[ckey]
if !ok {
value = ks.parent.Get(key)
ks.setCacheValue(key, value, false, false)
} else {
value = cacheValue.value
}
return value
}
// Implements KVStore.
func (ks *baseKVStore) Set(key []byte, value []byte) {
ks.mtx.Lock()
defer ks.mtx.Unlock()
ks.assertValidKey(key)
ks.assertValidValue(value)
ks.setCacheValue(key, value, false, true)
}
// Implements KVStore.
func (ks *baseKVStore) Has(key []byte) bool {
value := ks.Get(key)
return value != nil
}
// Implements KVStore.
func (ks *baseKVStore) Delete(key []byte) {
ks.mtx.Lock()
defer ks.mtx.Unlock()
ks.assertValidKey(key)
ks.setCacheValue(key, nil, true, true)
}
// Implements KVStore
func (ks *baseKVStore) Prefix(prefix []byte) KVStore {
return prefixStore{ks, prefix}
}
// Implements KVStore
func (ks *baseKVStore) Gas(meter GasMeter, config GasConfig) KVStore {
return NewGasKVStore(meter, config, ks)
}
// Implements CacheWrapper.
func (ks *baseKVStore) CacheWrap() CacheWrap {
return &cacheKVStore{
cache: make(map[string]cValue),
parent: ks,
}
}
// CacheWrapWithTrace implements the CacheWrapper interface.
func (ks *baseKVStore) CacheWrapWithTrace(w io.Writer, tc TraceContext) CacheWrap {
return nil
}
// Implements KVStore.
func (ks *baseKVStore) RemoteIterator(start, end []byte) Iterator {
return ks.iterator(start, end, true)
}
// Implements KVStore.
func (ks *baseKVStore) Iterator(start, end []byte) Iterator {
//cstart := ks.getCombineKey(start)
//cend := ks.getCombineKey(end)
return ks.iterator([]byte(start), []byte(end), true)
}
// Implements KVStore.
func (ks *baseKVStore) ReverseIterator(start, end []byte) Iterator {
//cstart := ks.getCombineKey(start)
//cend := ks.getCombineKey(end)
return ks.iterator([]byte(start), []byte(end), false)
}
func (ks *baseKVStore) iterator(start, end []byte, ascending bool) Iterator {
var parent, cache Iterator
cstart := start
cend := end
if ascending {
parent = ks.parent.Iterator(cstart, cend)
} else {
parent = ks.parent.ReverseIterator(cstart, cend)
}
items := ks.dirtyItems(ascending)
cache = newMemIterator(cstart, cend, items)
return newCacheMergeIterator(parent, cache, ascending)
}
// Constructs a slice of dirty items, to use w/ memIterator.
func (ks *baseKVStore) dirtyItems(ascending bool) []abci.EventAttribute {
items := make([]abci.EventAttribute, 0, len(ks.cache))
for key, cacheValue := range ks.cache {
if !cacheValue.dirty {
continue
}
items = append(items, abci.EventAttribute{Key: []byte(key), Value: cacheValue.value})
}
sort.Slice(items, func(i, j int) bool {
if ascending {
return bytes.Compare(items[i].Key, items[j].Key) < 0
}
return bytes.Compare(items[i].Key, items[j].Key) > 0
})
return items
}
// Implements CacheKVStore.
func (ks *baseKVStore) Write() {
return
}
func (ks *baseKVStore) assertValidKey(key []byte) {
if key == nil {
panic("types is nil")
}
}
func (ks *baseKVStore) assertValidValue(value []byte) {
if value == nil {
panic("value is nil")
}
}
// Only entrypoint to mutate ci.cache.
func (ks *baseKVStore) setCacheValue(key, value []byte, deleted bool, dirty bool) {
ks.cache[string(key)] = cValue{
value: value,
deleted: deleted,
dirty: dirty,
}
}
func (ks *baseKVStore) Commit() CommitID {
ks.mtx.Lock()
defer ks.mtx.Unlock()
// We need a copy of all of the keys.
// Not the best, but probably not a bottleneck depending.
keys := make([]string, 0, len(ks.cache))
for key, dbValue := range ks.cache {
if dbValue.dirty {
keys = append(keys, key)
}
}
sort.Strings(keys)
var valueBytes []cValue
// TODO: Consider allowing usage of Batch, which would allow the write to
// at least happen atomically.
for _, key := range keys {
cacheValue := ks.cache[key]
if cacheValue.deleted {
ks.parent.Delete([]byte(key))
} else if cacheValue.value == nil {
// Skip, it already doesn't exist in parent.
} else {
ks.parent.Set([]byte(key), cacheValue.value)
valueBytes = append(valueBytes, cacheValue)
}
}
// compute commit hash
bz, _ := cdc.MarshalBinaryLengthPrefixed(valueBytes)
hasher := tmhash.New()
_, err := hasher.Write(bz)
if err != nil {
panic(err)
}
hash := hasher.Sum(nil)
// Clear the cache
ks.cache = make(map[string]cValue)
version := getLatestVersion(ks.parent)
if version == 0 {
return CommitID{
Version: 1,
Hash: hash,
}
}else{
cInfo, err := getCommitInfo(ks.parent, version)
if err != nil {
panic(err)
}
return CommitID{
Version: cInfo.Version + 1,
Hash: hash,
}
}
}
func (ks *baseKVStore) BatchSet(batch db.Batch) {
ks.mtx.Lock()
defer ks.mtx.Unlock()
// We need a copy of all of the keys.
// Not the best, but probably not a bottleneck depending.
keys := make([]string, 0, len(ks.cache))
for key, dbValue := range ks.cache {
if dbValue.dirty {
keys = append(keys, key)
}
}
sort.Strings(keys)
var valueBytes []cValue
for _, key := range keys {
cacheValue := ks.cache[key]
if cacheValue.deleted {
ks.parent.Delete([]byte(key))
} else if cacheValue.value == nil {
// Skip, it already doesn't exist in parent.
} else {
batch.Set([]byte(key), cacheValue.value)
valueBytes = append(valueBytes, cacheValue)
}
}
// Clear the cache
ks.cache = make(map[string]cValue)
}
func (ks *baseKVStore) GetCache() map[string][]byte{
ks.mtx.Lock()
defer ks.mtx.Unlock()
// We need a copy of all of the keys.
// Not the best, but probably not a bottleneck depending.
keys := make([]string, 0, len(ks.cache))
for key, dbValue := range ks.cache {
if dbValue.dirty {
keys = append(keys, key)
}
}
sort.Strings(keys)
valueBytes := make(map[string][]byte)
for _, key := range keys {
cacheValue := ks.cache[key]
if cacheValue.deleted {
ks.parent.Delete([]byte(key))
} else if cacheValue.value == nil {
// Skip, it already doesn't exist in parent.
} else {
rawkey := "s/k:"+ks.key.Name()+"/" + key
valueBytes[rawkey] = cacheValue.value
}
}
ks.cache = make(map[string]cValue)
return valueBytes
}
func (ks *baseKVStore) LastCommitID() CommitID {
version := getLatestVersion(ks.parent)
cInfo, err := getCommitInfo(ks.parent, version)
if err != nil {
panic(err)
}
return cInfo.CommitID()
}
func (ks *baseKVStore) Latest(keys []string) KVStore {
return NewlatestStore(ks, keys)
}
func (ks *baseKVStore) Parent() KVStore {
return ks.parent
}
//-------------------------------------
//query
func (ks *baseKVStore) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
if len(req.Data) == 0 {
msg := "Query cannot be zero length"
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrParams, msg))
}
// store the height we chose in the response, with 0 being changed to the
// latest height
switch req.Path {
case "/key": // get by key
key := req.Data // data holds the key bytes
res.Key = key
value := ks.Get(key)
res.Value = value
case "/subspace":
var KVs []KVPair
subspace := req.Data
res.Key = subspace
iterator := sdk.KVStorePrefixIterator(ks, subspace)
for ; iterator.Valid(); iterator.Next() {
KVs = append(KVs, KVPair{Key: iterator.Key(), Value: iterator.Value()})
}
iterator.Close()
res.Value = cdc.MustMarshalBinaryLengthPrefixed(KVs)
default:
msg := fmt.Sprintf("Unexpected Query path: %v", req.Path)
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, msg))
}
return
}