-
Notifications
You must be signed in to change notification settings - Fork 1
/
statedb.go
316 lines (203 loc) · 5.97 KB
/
statedb.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package statedb
import (
"fmt"
"sort"
"github.com/mcc-github/blockchain/core/common/ccprovider"
"github.com/mcc-github/blockchain/core/ledger/kvledger/txmgmt/version"
"github.com/mcc-github/blockchain/core/ledger/util"
)
type VersionedDBProvider interface {
GetDBHandle(id string) (VersionedDB, error)
Close()
}
type VersionedDB interface {
GetState(namespace string, key string) (*VersionedValue, error)
GetVersion(namespace string, key string) (*version.Height, error)
GetStateMultipleKeys(namespace string, keys []string) ([]*VersionedValue, error)
GetStateRangeScanIterator(namespace string, startKey string, endKey string) (ResultsIterator, error)
GetStateRangeScanIteratorWithMetadata(namespace string, startKey string, endKey string, metadata map[string]interface{}) (QueryResultsIterator, error)
ExecuteQuery(namespace, query string) (ResultsIterator, error)
ExecuteQueryWithMetadata(namespace, query string, metadata map[string]interface{}) (QueryResultsIterator, error)
ApplyUpdates(batch *UpdateBatch, height *version.Height) error
GetLatestSavePoint() (*version.Height, error)
ValidateKeyValue(key string, value []byte) error
BytesKeySupported() bool
Open() error
Close()
}
type BulkOptimizable interface {
LoadCommittedVersions(keys []*CompositeKey) error
GetCachedVersion(namespace, key string) (*version.Height, bool)
ClearCachedVersions()
}
type IndexCapable interface {
GetDBType() string
ProcessIndexesForChaincodeDeploy(namespace string, fileEntries []*ccprovider.TarFileEntry) error
}
type CompositeKey struct {
Namespace string
Key string
}
type VersionedValue struct {
Value []byte
Metadata []byte
Version *version.Height
}
func (vv *VersionedValue) IsDelete() bool {
return vv.Value == nil
}
type VersionedKV struct {
CompositeKey
VersionedValue
}
type ResultsIterator interface {
Next() (QueryResult, error)
Close()
}
type QueryResultsIterator interface {
ResultsIterator
GetBookmarkAndClose() string
}
type QueryResult interface{}
type nsUpdates struct {
M map[string]*VersionedValue
}
func newNsUpdates() *nsUpdates {
return &nsUpdates{make(map[string]*VersionedValue)}
}
type UpdateBatch struct {
ContainsPostOrderWrites bool
Updates map[string]*nsUpdates
}
func NewUpdateBatch() *UpdateBatch {
return &UpdateBatch{false, make(map[string]*nsUpdates)}
}
func (batch *UpdateBatch) Get(ns string, key string) *VersionedValue {
nsUpdates, ok := batch.Updates[ns]
if !ok {
return nil
}
vv, ok := nsUpdates.M[key]
if !ok {
return nil
}
return vv
}
func (batch *UpdateBatch) Put(ns string, key string, value []byte, version *version.Height) {
batch.PutValAndMetadata(ns, key, value, nil, version)
}
func (batch *UpdateBatch) PutValAndMetadata(ns string, key string, value []byte, metadata []byte, version *version.Height) {
if value == nil {
panic("Nil value not allowed. Instead call 'Delete' function")
}
batch.Update(ns, key, &VersionedValue{value, metadata, version})
}
func (batch *UpdateBatch) Delete(ns string, key string, version *version.Height) {
batch.Update(ns, key, &VersionedValue{nil, nil, version})
}
func (batch *UpdateBatch) Exists(ns string, key string) bool {
nsUpdates, ok := batch.Updates[ns]
if !ok {
return false
}
_, ok = nsUpdates.M[key]
return ok
}
func (batch *UpdateBatch) GetUpdatedNamespaces() []string {
namespaces := make([]string, len(batch.Updates))
i := 0
for ns := range batch.Updates {
namespaces[i] = ns
i++
}
return namespaces
}
func (batch *UpdateBatch) Update(ns string, key string, vv *VersionedValue) {
batch.getOrCreateNsUpdates(ns).M[key] = vv
}
func (batch *UpdateBatch) GetUpdates(ns string) map[string]*VersionedValue {
nsUpdates, ok := batch.Updates[ns]
if !ok {
return nil
}
return nsUpdates.M
}
func (batch *UpdateBatch) GetRangeScanIterator(ns string, startKey string, endKey string) QueryResultsIterator {
return newNsIterator(ns, startKey, endKey, batch)
}
func (batch *UpdateBatch) Merge(toMerge *UpdateBatch) {
batch.ContainsPostOrderWrites = batch.ContainsPostOrderWrites || toMerge.ContainsPostOrderWrites
for ns, nsUpdates := range toMerge.Updates {
for key, vv := range nsUpdates.M {
batch.Update(ns, key, vv)
}
}
}
func (batch *UpdateBatch) getOrCreateNsUpdates(ns string) *nsUpdates {
nsUpdates := batch.Updates[ns]
if nsUpdates == nil {
nsUpdates = newNsUpdates()
batch.Updates[ns] = nsUpdates
}
return nsUpdates
}
type nsIterator struct {
ns string
nsUpdates *nsUpdates
sortedKeys []string
nextIndex int
lastIndex int
}
func newNsIterator(ns string, startKey string, endKey string, batch *UpdateBatch) *nsIterator {
nsUpdates, ok := batch.Updates[ns]
if !ok {
return &nsIterator{}
}
sortedKeys := util.GetSortedKeys(nsUpdates.M)
var nextIndex int
var lastIndex int
if startKey == "" {
nextIndex = 0
} else {
nextIndex = sort.SearchStrings(sortedKeys, startKey)
}
if endKey == "" {
lastIndex = len(sortedKeys)
} else {
lastIndex = sort.SearchStrings(sortedKeys, endKey)
}
return &nsIterator{ns, nsUpdates, sortedKeys, nextIndex, lastIndex}
}
func (itr *nsIterator) Next() (QueryResult, error) {
if itr.nextIndex >= itr.lastIndex {
return nil, nil
}
key := itr.sortedKeys[itr.nextIndex]
vv := itr.nsUpdates.M[key]
itr.nextIndex++
return &VersionedKV{CompositeKey{itr.ns, key}, VersionedValue{vv.Value, vv.Metadata, vv.Version}}, nil
}
func (itr *nsIterator) Close() {
}
func (itr *nsIterator) GetBookmarkAndClose() string {
return ""
}
const optionLimit = "limit"
func ValidateRangeMetadata(metadata map[string]interface{}) error {
for key, keyVal := range metadata {
switch key {
case optionLimit:
if _, ok := keyVal.(int32); ok {
continue
}
return fmt.Errorf("Invalid entry, \"limit\" must be a int32")
default:
return fmt.Errorf("Invalid entry, option %s not recognized", key)
}
}
return nil
}