-
Notifications
You must be signed in to change notification settings - Fork 8
/
istate-query.go
256 lines (221 loc) · 7.38 KB
/
istate-query.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
/*
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"
"github.com/hyperledger/fabric/core/chaincode/shim"
"reflect"
)
// Syntax and Keywords
const (
eq = "eq"
neq = "neq"
gt = "gt"
lt = "lt"
gte = "gte"
lte = "lte"
cmplx = "cmplx"
or = "or"
and = "and"
seq = "^eq"
sneq = "^neq"
sgt = "^gt"
slt = "^lt"
sgte = "^gte"
slte = "^lte"
// scmplx = "^cmplx"
splitDot = "."
)
type encodedKVs struct {
eq map[string][]byte
neq map[string][]byte
gt map[string][]byte
lt map[string][]byte
gte map[string][]byte
lte map[string][]byte
cmplx map[string][]byte
seq map[string][]byte
sneq map[string][]byte
sgt map[string][]byte
slt map[string][]byte
sgte map[string][]byte
slte map[string][]byte
// scmplx map[string][]byte
}
type querys struct {
eq []map[string]interface{}
neq []map[string]interface{}
gt []map[string]interface{}
lt []map[string]interface{}
gte []map[string]interface{}
lte []map[string]interface{}
cmplx []map[string]interface{}
seq []map[string]interface{}
sneq []map[string]interface{}
sgt []map[string]interface{}
slt []map[string]interface{}
sgte []map[string]interface{}
slte []map[string]interface{}
// scmplx []map[string]interface{}
}
type fetchFuncType func(shim.ChaincodeStubInterface, string, string, bool) (map[string]map[string][]byte, Error)
type efficientKeyType struct {
enckey string
genericField string
fetchFunc fetchFuncType
relatedEncKV map[string][]byte
relatedQueryP *[]map[string]interface{}
i int
}
type safeKeyFunc struct {
encKey string
fetchFunc fetchFuncType
relatedEncKV map[string][]byte
relatedQueryP *[]map[string]interface{}
i int
}
type queryEnv struct {
ufetchedKVMap map[string][]byte
uindecesMap map[string]map[string][]byte
}
// Query function is used to perform Rich Queries over a state type in state db
// "," separated objects are considered "or" always
// queryString = [{"docType":"eq USERPROFILE_DOCTYPE", "doctor.whatever": "cmplx or(and(gt bla, lt bla),or(eq a, eq b))", "groups":["cmplx and(neq doctor, neq patient)"]}, {"docType":"eq USERPROFILE_DOCTYPE", "groups":["eq patient"]}]
func (iState *iState) Query(stub shim.ChaincodeStubInterface, queryString string, isInvoke bool) (finalResult interface{}, iStateErr Error) {
iStateLogger.Debugf("Inside Query")
defer iStateLogger.Debugf("Exiting Query")
qEnv := &queryEnv{}
initQueryEnv(qEnv)
var uQuery []map[string]interface{}
err := json.Unmarshal([]byte(queryString), &uQuery)
if err != nil {
iStateErr = newError(err, 3002)
return
}
filteredKeys := make([]map[string]map[string][]byte, len(uQuery), len(uQuery))
for i := 0; i < len(uQuery); i++ {
filteredKeys[i], iStateErr = iState.parseAndEvalSingle(stub, uQuery[i], isInvoke)
if iStateErr != nil {
return
}
}
combinedResults := orOperation(filteredKeys...)
finalResult = reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(iState.structRef)), len(combinedResults), len(combinedResults))
i := 0
for key := range combinedResults {
var uObj reflect.Value
uObj, iStateErr = iState.getuObj(stub, key)
if iStateErr != nil {
return
}
finalResult.(reflect.Value).Index(i).Set(uObj)
i++
}
finalResult = finalResult.(reflect.Value).Interface()
return
}
func (iState *iState) parseAndEvalSingle(stub shim.ChaincodeStubInterface, uQuery map[string]interface{}, forceFetchDB bool) (filteredKeys map[string]map[string][]byte, iStateErr Error) {
// Fields will be declared automatically and make not needed
querySet := querys{}
if len(uQuery) == 0 {
iStateErr = newError(nil, 3023)
return
}
for index, val := range uQuery {
if _, ok := val.(string); !ok {
iStateErr = newError(nil, 3003, reflect.TypeOf(val))
return
}
// Get keyword and val
var keyword, secondPart string
keyword, secondPart, iStateErr = getKeyWordAndVal(val.(string))
if iStateErr != nil {
return
}
// For cmplx, cannot convert to right type
var convertedVal interface{} = secondPart
if keyword != cmplx { //&& keyword != scmplx {
// Convert the string value to appropriate type
convertedVal, iStateErr = convertToRightType(index, secondPart, iState.jsonFieldKindMap, iState.mapKeyKindMap)
if iStateErr != nil {
return
}
}
newIndex := ""
var newVal interface{}
newIndex, newVal, iStateErr = iState.generateActualStructure(index, convertedVal)
if iStateErr != nil {
return
}
switch keyword {
case eq:
querySet.eq = addKeyWithoutOverLap(querySet.eq, newIndex, newVal)
case neq:
querySet.neq = addKeyWithoutOverLap(querySet.neq, newIndex, newVal)
case gt:
querySet.gt = addKeyWithoutOverLap(querySet.gt, newIndex, newVal)
case lt:
querySet.lt = addKeyWithoutOverLap(querySet.lt, newIndex, newVal)
case gte:
querySet.gte = addKeyWithoutOverLap(querySet.gte, newIndex, newVal)
case lte:
querySet.lte = addKeyWithoutOverLap(querySet.lte, newIndex, newVal)
case cmplx:
querySet.cmplx = addKeyWithoutOverLap(querySet.cmplx, newIndex, newVal)
case seq:
querySet.seq = addKeyWithoutOverLap(querySet.seq, newIndex, newVal)
case sneq:
querySet.sneq = addKeyWithoutOverLap(querySet.sneq, newIndex, newVal)
case sgt:
querySet.sgt = addKeyWithoutOverLap(querySet.sgt, newIndex, newVal)
case slt:
querySet.slt = addKeyWithoutOverLap(querySet.slt, newIndex, newVal)
case sgte:
querySet.sgte = addKeyWithoutOverLap(querySet.sgte, newIndex, newVal)
case slte:
querySet.slte = addKeyWithoutOverLap(querySet.slte, newIndex, newVal)
// case scmplx:
// querySet.scmplx = addKeyWithoutOverLap(querySet.scmplx, newIndex, newVal)
default:
iStateErr = newError(nil, 3005, keyword)
return
}
}
var bestKey string
var fetchFunc fetchFuncType
var queryEncodedKVset encodedKVs
bestKey, fetchFunc, queryEncodedKVset, iStateErr = iState.getBestEncodedKeyFunc(querySet)
if iStateErr != nil {
return
}
kindecesMap, iStateErr := fetchFunc(stub, removeStarFromKey(bestKey), "index", forceFetchDB)
evalAndFilterEq(stub, queryEncodedKVset.eq, kindecesMap)
evalAndFilterNeq(stub, queryEncodedKVset.neq, kindecesMap)
evalAndFilterGt(stub, queryEncodedKVset.gt, kindecesMap)
evalAndFilterLt(stub, queryEncodedKVset.lt, kindecesMap)
evalAndFilterGte(stub, queryEncodedKVset.gte, kindecesMap)
evalAndFilterLte(stub, queryEncodedKVset.lte, kindecesMap)
evalAndFilterSeq(stub, queryEncodedKVset.seq, kindecesMap)
evalAndFilterSneq(stub, queryEncodedKVset.sneq, kindecesMap)
evalAndFilterSgt(stub, queryEncodedKVset.sgt, kindecesMap)
evalAndFilterSlt(stub, queryEncodedKVset.slt, kindecesMap)
evalAndFilterSgte(stub, queryEncodedKVset.sgte, kindecesMap)
evalAndFilterSlte(stub, queryEncodedKVset.slte, kindecesMap)
iStateErr = iState.evalAndFilterCmplx(stub, queryEncodedKVset.cmplx, kindecesMap)
if iStateErr != nil {
return
}
// and operation between fields
filteredKeys = kindecesMap
return
}