-
Notifications
You must be signed in to change notification settings - Fork 1
/
stateleveldb.go
297 lines (252 loc) · 9 KB
/
stateleveldb.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package stateleveldb
import (
"bytes"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
"github.com/pkg/errors"
"github.com/syndtr/goleveldb/leveldb/iterator"
)
var logger = flogging.MustGetLogger("stateleveldb")
var compositeKeySep = []byte{0x00}
var lastKeyIndicator = byte(0x01)
var savePointKey = []byte{0x00}
// VersionedDBProvider implements interface VersionedDBProvider
type VersionedDBProvider struct {
dbProvider *leveldbhelper.Provider
}
// NewVersionedDBProvider instantiates VersionedDBProvider
func NewVersionedDBProvider() *VersionedDBProvider {
dbPath := ledgerconfig.GetStateLevelDBPath()
logger.Debugf("constructing VersionedDBProvider dbPath=%s", dbPath)
dbProvider := leveldbhelper.NewProvider(&leveldbhelper.Conf{DBPath: dbPath})
return &VersionedDBProvider{dbProvider}
}
// GetDBHandle gets the handle to a named database
func (provider *VersionedDBProvider) GetDBHandle(dbName string) (statedb.VersionedDB, error) {
return newVersionedDB(provider.dbProvider.GetDBHandle(dbName), dbName), nil
}
// Close closes the underlying db
func (provider *VersionedDBProvider) Close() {
provider.dbProvider.Close()
}
// VersionedDB implements VersionedDB interface
type versionedDB struct {
db *leveldbhelper.DBHandle
dbName string
}
// newVersionedDB constructs an instance of VersionedDB
func newVersionedDB(db *leveldbhelper.DBHandle, dbName string) *versionedDB {
return &versionedDB{db, dbName}
}
// Open implements method in VersionedDB interface
func (vdb *versionedDB) Open() error {
// do nothing because shared db is used
return nil
}
// Close implements method in VersionedDB interface
func (vdb *versionedDB) Close() {
// do nothing because shared db is used
}
// ValidateKeyValue implements method in VersionedDB interface
func (vdb *versionedDB) ValidateKeyValue(key string, value []byte) error {
return nil
}
// BytesKeySupported implements method in VersionedDB interface
func (vdb *versionedDB) BytesKeySupported() bool {
return true
}
// GetState implements method in VersionedDB interface
func (vdb *versionedDB) GetState(namespace string, key string) (*statedb.VersionedValue, error) {
logger.Debugf("GetState(). ns=%s, key=%s", namespace, key)
compositeKey := constructCompositeKey(namespace, key)
dbVal, err := vdb.db.Get(compositeKey)
// dbVal, err := vdb.db.SnapshotGet(compositeKey)
if err != nil {
return nil, err
}
if dbVal == nil {
return nil, nil
}
return decodeValue(dbVal)
}
func (vdb *versionedDB) RetrieveLatestSnapshot() uint64 {
return vdb.db.RetrieveLatestSnapshot()
}
func (vdb *versionedDB) ReleaseSnapshot(snapshot uint64) bool {
return vdb.db.ReleaseSnapshot(snapshot)
}
func (vdb *versionedDB) GetSnapshotState(snapshot uint64, namespace string, key string) (*statedb.VersionedValue, error) {
compositeKey := constructCompositeKey(namespace, key)
// dbVal, err := vdb.db.Get(compositeKey)
dbVal, err := vdb.db.SnapshotGet(snapshot, compositeKey)
if err != nil {
return nil, err
}
if dbVal == nil {
return nil, nil
}
return decodeValue(dbVal)
}
// GetVersion implements method in VersionedDB interface
func (vdb *versionedDB) GetVersion(namespace string, key string) (*version.Height, error) {
versionedValue, err := vdb.GetState(namespace, key)
if err != nil {
return nil, err
}
if versionedValue == nil {
return nil, nil
}
return versionedValue.Version, nil
}
// GetStateMultipleKeys implements method in VersionedDB interface
func (vdb *versionedDB) GetStateMultipleKeys(namespace string, keys []string) ([]*statedb.VersionedValue, error) {
vals := make([]*statedb.VersionedValue, len(keys))
for i, key := range keys {
val, err := vdb.GetState(namespace, key)
if err != nil {
return nil, err
}
vals[i] = val
}
return vals, nil
}
// GetStateRangeScanIterator implements method in VersionedDB interface
// startKey is inclusive
// endKey is exclusive
func (vdb *versionedDB) GetStateRangeScanIterator(namespace string, startKey string, endKey string) (statedb.ResultsIterator, error) {
return vdb.GetStateRangeScanIteratorWithMetadata(namespace, startKey, endKey, nil)
}
const optionLimit = "limit"
// GetStateRangeScanIteratorWithMetadata implements method in VersionedDB interface
func (vdb *versionedDB) GetStateRangeScanIteratorWithMetadata(namespace string, startKey string, endKey string, metadata map[string]interface{}) (statedb.QueryResultsIterator, error) {
requestedLimit := int32(0)
// if metadata is provided, validate and apply options
if metadata != nil {
//validate the metadata
err := statedb.ValidateRangeMetadata(metadata)
if err != nil {
return nil, err
}
if limitOption, ok := metadata[optionLimit]; ok {
requestedLimit = limitOption.(int32)
}
}
// Note: metadata is not used for the goleveldb implementation of the range query
compositeStartKey := constructCompositeKey(namespace, startKey)
compositeEndKey := constructCompositeKey(namespace, endKey)
if endKey == "" {
compositeEndKey[len(compositeEndKey)-1] = lastKeyIndicator
}
dbItr := vdb.db.GetIterator(compositeStartKey, compositeEndKey)
return newKVScanner(namespace, dbItr, requestedLimit), nil
}
// ExecuteQuery implements method in VersionedDB interface
func (vdb *versionedDB) ExecuteQuery(namespace, query string) (statedb.ResultsIterator, error) {
return nil, errors.New("ExecuteQuery not supported for leveldb")
}
// ExecuteQueryWithMetadata implements method in VersionedDB interface
func (vdb *versionedDB) ExecuteQueryWithMetadata(namespace, query string, metadata map[string]interface{}) (statedb.QueryResultsIterator, error) {
return nil, errors.New("ExecuteQueryWithMetadata not supported for leveldb")
}
// ApplyUpdates implements method in VersionedDB interface
func (vdb *versionedDB) ApplyUpdates(batch *statedb.UpdateBatch, height *version.Height) error {
dbBatch := leveldbhelper.NewUpdateBatch()
namespaces := batch.GetUpdatedNamespaces()
batchSize := 0
for _, ns := range namespaces {
updates := batch.GetUpdates(ns)
for k, vv := range updates {
compositeKey := constructCompositeKey(ns, k)
logger.Debugf("Channel [%s]: Applying key(string)=[%s] key(bytes)=[%#v]", vdb.dbName, string(compositeKey), compositeKey)
if vv.Value == nil {
dbBatch.Delete(compositeKey)
} else {
encodedVal, err := encodeValue(vv)
if err != nil {
return err
}
dbBatch.Put(compositeKey, encodedVal)
batchSize += len(compositeKey) + len(encodedVal)
}
}
}
dbBatch.Put(savePointKey, height.ToBytes())
logger.Debugf("Block %d: Applied batch size: %d", height.BlockNum, batchSize)
// Setting snyc to true as a precaution, false may be an ok optimization after further testing.
// if err := vdb.db.WriteBatch(dbBatch, true); err != nil {
if err := vdb.db.SnapshotWriteBatch(dbBatch, height.BlockNum, true); err != nil {
return err
}
return nil
}
// GetLatestSavePoint implements method in VersionedDB interface
func (vdb *versionedDB) GetLatestSavePoint() (*version.Height, error) {
versionBytes, err := vdb.db.Get(savePointKey)
if err != nil {
return nil, err
}
if versionBytes == nil {
return nil, nil
}
version, _ := version.NewHeightFromBytes(versionBytes)
return version, nil
}
func constructCompositeKey(ns string, key string) []byte {
return append(append([]byte(ns), compositeKeySep...), []byte(key)...)
}
func splitCompositeKey(compositeKey []byte) (string, string) {
split := bytes.SplitN(compositeKey, compositeKeySep, 2)
return string(split[0]), string(split[1])
}
type kvScanner struct {
namespace string
dbItr iterator.Iterator
requestedLimit int32
totalRecordsReturned int32
}
func newKVScanner(namespace string, dbItr iterator.Iterator, requestedLimit int32) *kvScanner {
return &kvScanner{namespace, dbItr, requestedLimit, 0}
}
func (scanner *kvScanner) Next() (statedb.QueryResult, error) {
if scanner.requestedLimit > 0 && scanner.totalRecordsReturned >= scanner.requestedLimit {
return nil, nil
}
if !scanner.dbItr.Next() {
return nil, nil
}
dbKey := scanner.dbItr.Key()
dbVal := scanner.dbItr.Value()
dbValCopy := make([]byte, len(dbVal))
copy(dbValCopy, dbVal)
_, key := splitCompositeKey(dbKey)
vv, err := decodeValue(dbValCopy)
if err != nil {
return nil, err
}
scanner.totalRecordsReturned++
return &statedb.VersionedKV{
CompositeKey: statedb.CompositeKey{Namespace: scanner.namespace, Key: key},
// TODO remove dereferrencing below by changing the type of the field
// `VersionedValue` in `statedb.VersionedKV` to a pointer
VersionedValue: *vv}, nil
}
func (scanner *kvScanner) Close() {
scanner.dbItr.Release()
}
func (scanner *kvScanner) GetBookmarkAndClose() string {
retval := ""
if scanner.dbItr.Next() {
dbKey := scanner.dbItr.Key()
_, key := splitCompositeKey(dbKey)
retval = key
}
scanner.Close()
return retval
}