-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
iavlstore.go
345 lines (283 loc) · 7.27 KB
/
iavlstore.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
package store
import (
"fmt"
"sync"
abci "github.com/tendermint/abci/types"
"github.com/tendermint/iavl"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
defaultIAVLCacheSize = 10000
defaultIAVLNumHistory = 1<<53 - 1 // DEPRECATED
)
func LoadIAVLStore(db dbm.DB, id CommitID) (CommitStore, error) {
tree := iavl.NewVersionedTree(db, defaultIAVLCacheSize)
_, err := tree.LoadVersion(id.Version)
if err != nil {
return nil, err
}
store := newIAVLStore(tree, defaultIAVLNumHistory)
return store, nil
}
//----------------------------------------
var _ KVStore = (*iavlStore)(nil)
var _ CommitStore = (*iavlStore)(nil)
var _ Queryable = (*iavlStore)(nil)
// iavlStore Implements KVStore and CommitStore.
type iavlStore struct {
// The underlying tree.
tree *iavl.VersionedTree
// How many old versions we hold onto.
// A value of 0 means keep all history.
numHistory int64
}
// CONTRACT: tree should be fully loaded.
func newIAVLStore(tree *iavl.VersionedTree, numHistory int64) *iavlStore {
st := &iavlStore{
tree: tree,
numHistory: numHistory,
}
return st
}
// Implements Committer.
func (st *iavlStore) Commit() CommitID {
// Save a new version.
hash, version, err := st.tree.SaveVersion()
if err != nil {
// TODO: Do we want to extend Commit to allow returning errors?
panic(err)
}
// Release an old version of history
if st.numHistory > 0 && (st.numHistory < st.tree.Version64()) {
toRelease := version - st.numHistory
st.tree.DeleteVersion(toRelease)
}
return CommitID{
Version: version,
Hash: hash,
}
}
// Implements Committer.
func (st *iavlStore) LastCommitID() CommitID {
return CommitID{
Version: st.tree.Version64(),
Hash: st.tree.Hash(),
}
}
// Implements Store.
func (st *iavlStore) GetStoreType() StoreType {
return sdk.StoreTypeIAVL
}
// Implements Store.
func (st *iavlStore) CacheWrap() CacheWrap {
return NewCacheKVStore(st)
}
// Implements KVStore.
func (st *iavlStore) Set(key, value []byte) {
st.tree.Set(key, value)
}
// Implements KVStore.
func (st *iavlStore) Get(key []byte) (value []byte) {
_, v := st.tree.Get(key)
return v
}
// Implements KVStore.
func (st *iavlStore) Has(key []byte) (exists bool) {
return st.tree.Has(key)
}
// Implements KVStore.
func (st *iavlStore) Delete(key []byte) {
st.tree.Remove(key)
}
// Implements KVStore.
func (st *iavlStore) Iterator(start, end []byte) Iterator {
return newIAVLIterator(st.tree.Tree(), start, end, true)
}
func (st *iavlStore) Subspace(prefix []byte) Iterator {
end := make([]byte, len(prefix))
copy(end, prefix)
end[len(end)-1]++
return st.Iterator(prefix, end)
}
// Implements IterKVStore.
func (st *iavlStore) ReverseIterator(start, end []byte) Iterator {
return newIAVLIterator(st.tree.Tree(), start, end, false)
}
// Query implements ABCI interface, allows queries
//
// by default we will return from (latest height -1),
// as we will have merkle proofs immediately (header height = data height + 1)
// If latest-1 is not present, use latest (which must be present)
// if you care to have the latest data to see a tx results, you must
// explicitly set the height you want to see
func (st *iavlStore) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
if len(req.Data) == 0 {
msg := "Query cannot be zero length"
return sdk.ErrTxDecode(msg).QueryResult()
}
tree := st.tree
height := req.Height
if height == 0 {
latest := tree.Version64()
if tree.VersionExists(latest - 1) {
height = latest - 1
} else {
height = latest
}
}
// store the height we chose in the response
res.Height = height
switch req.Path {
case "/store", "/key": // Get by key
key := req.Data // Data holds the key bytes
res.Key = key
if req.Prove {
value, proof, err := tree.GetVersionedWithProof(key, height)
if err != nil {
res.Log = err.Error()
break
}
res.Value = value
res.Proof = proof.Bytes()
} else {
_, res.Value = tree.GetVersioned(key, height)
}
default:
msg := fmt.Sprintf("Unexpected Query path: %v", req.Path)
return sdk.ErrUnknownRequest(msg).QueryResult()
}
return
}
//----------------------------------------
// Implements Iterator.
type iavlIterator struct {
// Underlying store
tree *iavl.Tree
// Domain
start, end []byte
// Iteration order
ascending bool
// Channel to push iteration values.
iterCh chan cmn.KVPair
// Close this to release goroutine.
quitCh chan struct{}
// Close this to signal that state is initialized.
initCh chan struct{}
//----------------------------------------
// What follows are mutable state.
mtx sync.Mutex
invalid bool // True once, true forever
key []byte // The current key
value []byte // The current value
}
var _ Iterator = (*iavlIterator)(nil)
// newIAVLIterator will create a new iavlIterator.
// CONTRACT: Caller must release the iavlIterator, as each one creates a new
// goroutine.
func newIAVLIterator(tree *iavl.Tree, start, end []byte, ascending bool) *iavlIterator {
iter := &iavlIterator{
tree: tree,
start: cp(start),
end: cp(end),
ascending: ascending,
iterCh: make(chan cmn.KVPair, 0), // Set capacity > 0?
quitCh: make(chan struct{}),
initCh: make(chan struct{}),
}
go iter.iterateRoutine()
go iter.initRoutine()
return iter
}
// Run this to funnel items from the tree to iterCh.
func (iter *iavlIterator) iterateRoutine() {
iter.tree.IterateRange(
iter.start, iter.end, iter.ascending,
func(key, value []byte) bool {
select {
case <-iter.quitCh:
return true // done with iteration.
case iter.iterCh <- cmn.KVPair{key, value}:
return false // yay.
}
},
)
close(iter.iterCh) // done.
}
// Run this to fetch the first item.
func (iter *iavlIterator) initRoutine() {
iter.receiveNext()
close(iter.initCh)
}
// Implements Iterator.
func (iter *iavlIterator) Domain() (start, end []byte) {
return iter.start, iter.end
}
// Implements Iterator.
func (iter *iavlIterator) Valid() bool {
iter.waitInit()
iter.mtx.Lock()
defer iter.mtx.Unlock()
return !iter.invalid
}
// Implements Iterator.
func (iter *iavlIterator) Next() {
iter.waitInit()
iter.mtx.Lock()
defer iter.mtx.Unlock()
iter.assertIsValid()
iter.receiveNext()
}
// Implements Iterator.
func (iter *iavlIterator) Key() []byte {
iter.waitInit()
iter.mtx.Lock()
defer iter.mtx.Unlock()
iter.assertIsValid()
return iter.key
}
// Implements Iterator.
func (iter *iavlIterator) Value() []byte {
iter.waitInit()
iter.mtx.Lock()
defer iter.mtx.Unlock()
iter.assertIsValid()
return iter.value
}
// Implements Iterator.
func (iter *iavlIterator) Close() {
close(iter.quitCh)
}
//----------------------------------------
func (iter *iavlIterator) setNext(key, value []byte) {
iter.assertIsValid()
iter.key = key
iter.value = value
}
func (iter *iavlIterator) setInvalid() {
iter.assertIsValid()
iter.invalid = true
}
func (iter *iavlIterator) waitInit() {
<-iter.initCh
}
func (iter *iavlIterator) receiveNext() {
kvPair, ok := <-iter.iterCh
if ok {
iter.setNext(kvPair.Key, kvPair.Value)
} else {
iter.setInvalid()
}
}
func (iter *iavlIterator) assertIsValid() {
if iter.invalid {
panic("invalid iterator")
}
}
//----------------------------------------
func cp(bz []byte) (ret []byte) {
ret = make([]byte, len(bz))
copy(ret, bz)
return ret
}