@@ -17,14 +17,11 @@ limitations under the License.
17
17
package qscc
18
18
19
19
import (
20
- "bytes"
21
20
"fmt"
22
21
"strconv"
23
22
24
23
"github.com/op/go-logging"
25
- "github.com/spf13/viper"
26
24
27
- commonledger "github.com/hyperledger/fabric/common/ledger"
28
25
"github.com/hyperledger/fabric/core/chaincode/shim"
29
26
"github.com/hyperledger/fabric/core/ledger"
30
27
"github.com/hyperledger/fabric/core/peer"
@@ -37,7 +34,6 @@ import (
37
34
// - GetBlockByNumber returns a block
38
35
// - GetBlockByHash returns a block
39
36
// - GetTransactionByID returns a transaction
40
- // - GetQueryResult returns result of a freeform query
41
37
type LedgerQuerier struct {
42
38
}
43
39
@@ -49,7 +45,6 @@ const (
49
45
GetBlockByNumber string = "GetBlockByNumber"
50
46
GetBlockByHash string = "GetBlockByHash"
51
47
GetTransactionByID string = "GetTransactionByID"
52
- GetQueryResult string = "GetQueryResult"
53
48
)
54
49
55
50
// Init is called once per chain when the chain is created.
@@ -68,11 +63,6 @@ func (e *LedgerQuerier) Init(stub shim.ChaincodeStubInterface) pb.Response {
68
63
// # GetBlockByNumber: Return the block specified by block number in args[2]
69
64
// # GetBlockByHash: Return the block specified by block hash in args[2]
70
65
// # GetTransactionByID: Return the transaction specified by ID in args[2]
71
- // # GetQueryResult: Return the result of executing the specified native
72
- // query string in args[2]. Note that this only works if plugged in database
73
- // supports it. The result is a JSON array in a byte array. Note that error
74
- // may be returned together with a valid partial result as error might occur
75
- // during accummulating records from the ledger
76
66
func (e * LedgerQuerier ) Invoke (stub shim.ChaincodeStubInterface ) pb.Response {
77
67
args := stub .GetArgs ()
78
68
@@ -97,8 +87,6 @@ func (e *LedgerQuerier) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
97
87
// TODO: Handle ACL
98
88
99
89
switch fname {
100
- case GetQueryResult :
101
- return getQueryResult (targetLedger , args [2 ])
102
90
case GetTransactionByID :
103
91
return getTransactionByID (targetLedger , args [2 ])
104
92
case GetBlockByNumber :
@@ -112,89 +100,6 @@ func (e *LedgerQuerier) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
112
100
return shim .Error (fmt .Sprintf ("Requested function %s not found." , fname ))
113
101
}
114
102
115
- // Execute the specified query string
116
- func getQueryResult (vledger ledger.PeerLedger , query []byte ) (res pb.Response ) {
117
- if query == nil {
118
- return shim .Error ("Query string must not be nil." )
119
- }
120
- qstring := string (query )
121
- var qexe ledger.QueryExecutor
122
- var ri commonledger.ResultsIterator
123
- var err error
124
-
125
- // We install a recover() to gain control in 2 cases
126
- // 1) bytes.Buffer panics, which happens when out of memory
127
- // This is a safety measure beyond the config limit variable
128
- // 2) plugin db driver might panic
129
- // We recover by stopping the query and return the panic error.
130
- defer func () {
131
- if panicValue := recover (); panicValue != nil {
132
- if qscclogger .IsEnabledFor (logging .DEBUG ) {
133
- qscclogger .Debugf ("Recovering panic: %s" , panicValue )
134
- }
135
- res = shim .Error (fmt .Sprintf ("Error recovery: %s" , panicValue ))
136
- }
137
- }()
138
-
139
- if qexe , err = vledger .NewQueryExecutor (); err != nil {
140
- return shim .Error (err .Error ())
141
- }
142
- if ri , err = qexe .ExecuteQuery (qstring ); err != nil {
143
- return shim .Error (err .Error ())
144
- }
145
- defer ri .Close ()
146
-
147
- limit := viper .GetInt ("ledger.state.couchDBConfig.queryLimit" )
148
-
149
- // buffer is a JSON array containing QueryRecords
150
- var buffer bytes.Buffer
151
- buffer .WriteString ("[" )
152
-
153
- var qresult commonledger.QueryResult
154
- bArrayMemberAlreadyWritten := false
155
- qresult , err = ri .Next ()
156
- for r := 0 ; qresult != nil && err == nil && r < limit ; r ++ {
157
- if qr , ok := qresult .(* ledger.QueryRecord ); ok {
158
- // Add a comma before array members, suppress it for the first array member
159
- if bArrayMemberAlreadyWritten == true {
160
- buffer .WriteString ("," )
161
- }
162
- collectRecord (& buffer , qr )
163
- bArrayMemberAlreadyWritten = true
164
- }
165
- qresult , err = ri .Next ()
166
- }
167
-
168
- buffer .WriteString ("]" )
169
-
170
- // Return what we have accummulated
171
- ret := buffer .Bytes ()
172
- return shim .Success (ret )
173
- }
174
-
175
- // Append QueryRecord into buffer as a JSON record of the form {namespace, key, record}
176
- // type QueryRecord struct {
177
- // Namespace string
178
- // Key string
179
- // Record []byte
180
- // }
181
- func collectRecord (buffer * bytes.Buffer , rec * ledger.QueryRecord ) {
182
- buffer .WriteString ("{\" Namespace\" :" )
183
- buffer .WriteString ("\" " )
184
- buffer .WriteString (rec .Namespace )
185
- buffer .WriteString ("\" " )
186
-
187
- buffer .WriteString (", \" Key\" :" )
188
- buffer .WriteString ("\" " )
189
- buffer .WriteString (rec .Key )
190
- buffer .WriteString ("\" " )
191
-
192
- buffer .WriteString (", \" Record\" :" )
193
- // Record is a JSON object, so we write as-is
194
- buffer .WriteString (string (rec .Record ))
195
- buffer .WriteString ("}" )
196
- }
197
-
198
103
func getTransactionByID (vledger ledger.PeerLedger , tid []byte ) pb.Response {
199
104
if tid == nil {
200
105
return shim .Error ("Transaction ID must not be nil." )
0 commit comments