-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstateIterator.go
50 lines (46 loc) · 1.41 KB
/
stateIterator.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
package golang
import (
. "github.com/davidkhala/goutils"
"github.com/hyperledger/fabric-chaincode-go/shim"
)
func parse100States(iterator shim.StateQueryIteratorInterface, filter func(StateKV) bool) ([]StateKV, string) {
defer PanicError(iterator.Close())
var kvs []StateKV
var index = 0
var lastKey = ""
for iterator.HasNext() {
if index < 99 {
kv, err := iterator.Next()
PanicError(err)
lastKey = kv.Key
var stateKV = StateKV{kv.Namespace, kv.Key, string(kv.Value)}
if filter == nil || filter(stateKV) {
kvs = append(kvs, stateKV)
}
index++
} else {
return kvs, lastKey
}
}
return kvs, ""
}
func (cc CommonChaincode) WorldStatesPrivate(collection, startKey string, filter func(StateKV) bool) []StateKV {
var keysIterator shim.StateQueryIteratorInterface
keysIterator = cc.GetPrivateDataByRange(collection, startKey, "")
var states, bookmark = parse100States(keysIterator, filter)
if bookmark != "" {
return append(states, cc.WorldStatesPrivate(collection, bookmark, filter)[1:]...)
} else {
return states
}
}
func (cc CommonChaincode) WorldStates(startKey string, filter func(StateKV) bool) []StateKV {
var keysIterator shim.StateQueryIteratorInterface
keysIterator = cc.GetStateByRange(startKey, "")
var states, bookmark = parse100States(keysIterator, filter)
if bookmark != "" {
return append(states, cc.WorldStates(bookmark, filter)[1:]...)
} else {
return states
}
}