/
istate-functions.go
557 lines (475 loc) · 15.3 KB
/
istate-functions.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
Copyright 2020 Motoreq Infotech Pvt Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package istate
import (
"encoding/json"
"fmt"
"github.com/bluele/gcache"
"github.com/hyperledger/fabric/core/chaincode/shim"
"hash/crc64"
"reflect"
"sync"
)
type iState struct {
mux sync.Mutex
structRef interface{}
fieldJSONIndexMap map[string]int
jsonFieldKindMap map[string]reflect.Kind
mapKeyKindMap map[string]reflect.Kind
depthKindMap map[string]reflect.Kind
primaryIndex int
docsCounter map[string]int
istateJSONMap map[string]string
CompactionSize int
// Cache
kvCache gcache.Cache
hashTable *crc64.Table
}
// Options is an input parameter for NewiState function.
// CacheSize determines the max no. of records allowed in memory.
// DefaultCompactionSize determines how many index records to compact at one go.
// Since Compaction process takes lot of time, it is useful to set a batchsize and
// Compaction can be run multiple times, where each time, n records gets compacted.
type Options struct {
CacheSize int
DefaultCompactionSize int
}
// NewiState function is used to create an iState interface for a struct type.
// Further CRUD operation on the struct type can be performed using the interface returned.
// It takes an empty struct value, Options as input and returns istate interface.
func NewiState(object interface{}, opt Options) (iStateInterface Interface, iStateErr Error) {
iStateLogger.Debugf("Inside NewiState")
defer iStateLogger.Debugf("Exiting NewiState")
filledRef := fillZeroValue(object)
// A map of JSON fieldname & it's position in the struct
fieldJSONIndexMap := make(map[string]int)
jsonFieldKindMap := make(map[string]reflect.Kind)
mapKeyKindMap := make(map[string]reflect.Kind)
depthKindMap := make(map[string]reflect.Kind)
iStateErr = generateFieldJSONIndexMap(filledRef, fieldJSONIndexMap)
if iStateErr != nil {
return
}
iStateErr = generatejsonFieldKindMap(filledRef, jsonFieldKindMap, mapKeyKindMap)
if iStateErr != nil {
return
}
iStateErr = generateDepthKindMap(filledRef, depthKindMap)
if iStateErr != nil {
return
}
var i int
i, iStateErr = getPrimaryFieldIndex(filledRef)
if iStateErr != nil {
return
}
var istateJSONMap map[string]string
istateJSONMap, iStateErr = generateistateJSONMap(object, fieldJSONIndexMap)
if iStateErr != nil {
return
}
docsCounter := make(map[string]int)
iStateIns := &iState{
structRef: filledRef,
fieldJSONIndexMap: fieldJSONIndexMap,
jsonFieldKindMap: jsonFieldKindMap,
mapKeyKindMap: mapKeyKindMap,
depthKindMap: depthKindMap,
primaryIndex: i,
docsCounter: docsCounter,
CompactionSize: opt.DefaultCompactionSize,
hashTable: crc64.MakeTable(crc64.ISO),
istateJSONMap: istateJSONMap,
}
// Cache
kvCache := gcache.New(opt.CacheSize).
ARC().
Build()
iStateIns.kvCache = kvCache
iStateInterface = iStateIns
return
}
// CreateState function is used to create a new state in the state db.
// It is a function in iState Interface and must be called via the Interface
// returned by NewiState function.
// It takes chaincode stub and the actual structure value as input params.
// Note: This function does not do state validations such as, checking whether state exists or not
// before performing the operation.
func (iState *iState) CreateState(stub shim.ChaincodeStubInterface, object interface{}) (iStateErr Error) {
iStateLogger.Debugf("Inside CreateState")
defer iStateLogger.Debugf("Exiting CreateState")
if reflect.TypeOf(object) != reflect.TypeOf(iState.structRef) {
iStateErr = newError(nil, 1014, reflect.TypeOf(iState.structRef), reflect.TypeOf(object))
return
}
// Find primary key
keyref := iState.getPrimaryKey(object)
mo, err := json.Marshal(object)
if err != nil {
iStateErr = newError(err, 1002)
return
}
var oMap map[string]interface{}
err = json.Unmarshal(mo, &oMap)
if err != nil {
iStateErr = newError(err, 1003)
return
}
hashString := iState.hash(mo)
encodedKeyValPairs, docsCounter, _, iStateErr := iState.encodeState(oMap, keyref, hashString)
if iStateErr != nil {
return
}
// Main key - value
encodedKeyValPairs[keyref] = mo
for key, val := range encodedKeyValPairs {
err = stub.PutState(key, val)
if err != nil {
iStateErr = newError(err, 1001)
return
}
}
for index, count := range docsCounter {
iState.incDocsCounter(index, count)
}
iStateErr = iState.setCache(keyref, object, mo, hashString)
if iStateErr != nil {
return
}
return nil
}
// ReadState function is used to read a state from state db.
// It is a function in iState Interface and must be called via the Interface
// returned by NewiState function.
// It takes chaincode stub and value of primary key as input params.
// It returns the actual structure value as an interface{}. The returned value can
// be type asserted to the actual struct type before using.
// Note: This function does not do state validations such as, checking whether state exists or not
// before performing the operation.
func (iState *iState) ReadState(stub shim.ChaincodeStubInterface, primaryKey interface{}) (uObj interface{}, iStateErr Error) {
iStateLogger.Debugf("Inside ReadState")
defer iStateLogger.Debugf("Exiting ReadState")
primaryKeyString := fmt.Sprintf("%v", primaryKey)
stateBytes, err := stub.GetState(primaryKeyString)
if err != nil {
iStateErr = newError(err, 1005)
}
if stateBytes == nil {
return
}
uObjReflect, iStateErr := iState.unmarshalToStruct(stateBytes)
if iStateErr != nil {
return
}
uObj = uObjReflect.Interface()
return
}
// UpdateState function is used to update a state in statedb.
// It is a function in iState Interface and must be called via the Interface
// returned by NewiState function.
// It takes chaincode stub and the actual structure value as input params.
// Note: This function does not do state validations such as, checking whether state exists or not
// before performing the operation.
func (iState *iState) UpdateState(stub shim.ChaincodeStubInterface, object interface{}) (iStateErr Error) {
iStateLogger.Debugf("Inside UpdateState")
defer iStateLogger.Debugf("Exiting UpdateState")
if reflect.TypeOf(object) != reflect.TypeOf(iState.structRef) {
iStateErr = newError(nil, 1015, reflect.TypeOf(iState.structRef), reflect.TypeOf(object))
return
}
// Find primary key
keyref := iState.getPrimaryKey(object)
stateBytes, err := stub.GetState(keyref)
if err != nil {
iStateErr = newError(err, 1017)
}
if stateBytes == nil {
return iState.CreateState(stub, object)
}
var source map[string]interface{}
err = json.Unmarshal(stateBytes, &source)
if err != nil {
iStateErr = newError(err, 1007)
return
}
mo, err := json.Marshal(object)
if err != nil {
iStateErr = newError(err, 1006)
return
}
var target map[string]interface{}
err = json.Unmarshal(mo, &target)
if err != nil {
iStateErr = newError(err, 1007)
return
}
fmt.Println("Source: ", source)
fmt.Println("Target: ", target)
appendOrModifyMap, deleteMap, iStateErr := iState.findDifference(source, target)
if iStateErr != nil {
return
}
if len(appendOrModifyMap) == 0 && len(deleteMap) == 0 {
iStateErr = newError(nil, 1004)
return
}
hashString := iState.hash(mo)
// Delete first, so that TestStruct_aMap_user1 (map key) does not get deleted.
// When updating same key of a map, the above key gets over-writted at first,
// then when deleting, the over-written key gets deleted.
deleteEncodedKeyValPairs, delDocsCounter, _, iStateErr := iState.encodeState(deleteMap, keyref, hashString)
if iStateErr != nil {
return
}
appendEncodedKeyValPairs, addDocsCounter, _, iStateErr := iState.encodeState(target, keyref, hashString)
if iStateErr != nil {
return
}
// Main key - value
appendEncodedKeyValPairs[keyref] = mo
for key := range deleteEncodedKeyValPairs {
err = stub.DelState(key)
if err != nil {
iStateErr = newError(err, 1008)
return
}
}
for key, val := range appendEncodedKeyValPairs {
err = stub.PutState(key, val)
if err != nil {
iStateErr = newError(err, 1009)
return
}
}
for index, count := range delDocsCounter {
iState.decDocsCounter(index, count)
}
for index, count := range addDocsCounter {
iState.incDocsCounter(index, count)
}
iStateErr = iState.setCache(keyref, object, mo, hashString)
if iStateErr != nil {
return
}
return nil
}
// PartialUpdateState function is used to update a state in statedb partially.
// It is a function in iState Interface and must be called via the Interface
// returned by NewiState function.
// It takes chaincode stub, state id / primary key and the partial structure value in the form of
// map[string]interface{} as input params.
// Note: This function does state validation, and returns error if state does not exist.
func (iState *iState) PartialUpdateState(stub shim.ChaincodeStubInterface, primaryKey interface{}, partialObject map[string]interface{}) (iStateErr Error) {
iStateLogger.Debugf("Inside PartialUpdateState")
defer iStateLogger.Debugf("Exiting PartialUpdateState")
// Find primary key
keyref := fmt.Sprintf("%v", primaryKey)
stateBytes, err := stub.GetState(keyref)
if err != nil {
iStateErr = newError(err, 1017)
}
if stateBytes == nil {
iStateErr = newError(nil, 1017, keyref)
return
}
var source map[string]interface{}
err = json.Unmarshal(stateBytes, &source)
if err != nil {
iStateErr = newError(err, 1007)
return
}
var target map[string]interface{}
// Unmarshalling existing state first
err = json.Unmarshal(stateBytes, &target)
if err != nil {
iStateErr = newError(err, 1007)
return
}
// Now editing partial fields
mo, err := json.Marshal(partialObject)
if err != nil {
iStateErr = newError(err, 1006)
return
}
err = json.Unmarshal(mo, &target)
if err != nil {
iStateErr = newError(err, 1007)
return
}
finalmo, err := json.Marshal(target)
if err != nil {
iStateErr = newError(err, 1006)
return
}
appendOrModifyMap, deleteMap, iStateErr := iState.findDifference(source, target)
if iStateErr != nil {
return
}
if len(appendOrModifyMap) == 0 && len(deleteMap) == 0 {
iStateErr = newError(nil, 1004)
return
}
hashString := iState.hash(finalmo)
// Delete first, so that TestStruct_aMap_user1 (map key) does not get deleted.
// When updating same key of a map, the above key gets over-writted at first,
// then when deleting, the over-written key gets deleted.
deleteEncodedKeyValPairs, delDocsCounter, _, iStateErr := iState.encodeState(deleteMap, keyref, hashString)
if iStateErr != nil {
return
}
appendEncodedKeyValPairs, addDocsCounter, _, iStateErr := iState.encodeState(target, keyref, hashString)
if iStateErr != nil {
return
}
// Main key - value
appendEncodedKeyValPairs[keyref] = finalmo
for key := range deleteEncodedKeyValPairs {
err = stub.DelState(key)
if err != nil {
iStateErr = newError(err, 1008)
return
}
}
for key, val := range appendEncodedKeyValPairs {
err = stub.PutState(key, val)
if err != nil {
iStateErr = newError(err, 1009)
return
}
}
for index, count := range delDocsCounter {
iState.decDocsCounter(index, count)
}
for index, count := range addDocsCounter {
iState.incDocsCounter(index, count)
}
// Getting updated object in perfect structure (not in map[string]interface{})
uObjReflect, iStateErr := iState.unmarshalToStruct(finalmo)
if iStateErr != nil {
return
}
uObj := uObjReflect.Interface()
iStateErr = iState.setCache(keyref, uObj, finalmo, hashString)
if iStateErr != nil {
return
}
return nil
}
// DeleteState function is used to delete a state from state db.
// It is a function in iState Interface and must be called via the Interface
// returned by NewiState function.
// It takes chaincode stub and value of primary key as input params.
// Note: This function does not do state validations such as, checking whether state exists or not
// before performing the operation.
func (iState *iState) DeleteState(stub shim.ChaincodeStubInterface, primaryKey interface{}) (iStateErr Error) {
iStateLogger.Debugf("Inside DeleteState")
defer iStateLogger.Debugf("Exiting DeleteState")
keyref := fmt.Sprintf("%v", primaryKey)
stateBytes, err := stub.GetState(keyref)
if err != nil {
iStateErr = newError(err, 1018)
}
if stateBytes == nil {
// If state does not exist, return silently
//iStateErr = newError(nil, 1013, keyref)
return
}
var source map[string]interface{}
err = json.Unmarshal(stateBytes, &source)
if err != nil {
iStateErr = newError(err, 1011)
return
}
// Delete first, so that TestStruct_aMap_user1 (map key) does not get deleted.
// When updating same key of a map, the above key gets over-writted at first,
// then when deleting, the over-written key gets deleted.
encodedKeyValPairs, delDocsCounter, _, iStateErr := iState.encodeState(source, keyref, "")
if iStateErr != nil {
return
}
// Main key - value
encodedKeyValPairs[keyref] = stateBytes
for key := range encodedKeyValPairs {
err = stub.DelState(key)
if err != nil {
iStateErr = newError(err, 1012)
return
}
}
for index, count := range delDocsCounter {
iState.decDocsCounter(index, count)
}
iState.removeCache(keyref)
return nil
}
//
func (iState *iState) CompactIndex(stub shim.ChaincodeStubInterface) (iStateErr Error) {
iStateLogger.Debugf("Inside CompactIndex")
defer iStateLogger.Debugf("Exiting CompactIndex")
uObj, iStateErr := convertObjToMap(iState.structRef)
if iStateErr != nil {
return
}
keyRef := ""
encodedKV, _, _, iStateErr := iState.encodeState(uObj, keyRef, "", 2) // separate key & value = 2, query = false
if iStateErr != nil {
return
}
compactedIndexMap := make(map[string]compactIndexV) // compacted index
for index := range encodedKV {
startKey := index
endKey := index + asciiLast
var kvMap map[string][]byte // original index
kvMap, iStateErr = getKeyByRange(stub, startKey, endKey, iState.CompactionSize)
if iStateErr != nil {
return
}
alreadyFetched := make(map[string]struct{})
for origIndexK, hashBytes := range kvMap {
compactIndex, keyRef := generateCIndexKey(origIndexK)
if compactIndex == "" {
continue
}
var cIndexVal compactIndexV
var oldCIndexVal compactIndexV
switch val, ok := compactedIndexMap[compactIndex]; !ok {
case true:
if _, ok := alreadyFetched[compactIndex]; !ok {
cIndexVal, iStateErr = fetchCompactIndex(stub, compactIndex)
alreadyFetched[compactIndex] = struct{}{}
oldCIndexVal = cIndexVal
}
default:
cIndexVal = val
}
if len(cIndexVal) == 0 {
cIndexVal = make(compactIndexV)
}
cIndexVal[keyRef] = string(hashBytes)
if !reflect.DeepEqual(oldCIndexVal, cIndexVal) {
compactedIndexMap[compactIndex] = cIndexVal
// Delete original index key
err := stub.DelState(origIndexK)
if err != nil {
iStateErr = newError(err, 1016)
return
}
}
}
}
iStateErr = putCompactIndex(stub, compactedIndexMap)
if iStateErr != nil {
return
}
return
}