Skip to content

Commit

Permalink
Merge "[FAB-2980] Replace gob with proto for QueryResult"
Browse files Browse the repository at this point in the history
  • Loading branch information
hacera-jonathan authored and Gerrit Code Review committed Apr 20, 2017
2 parents 041fce8 + 214489e commit bee763f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
13 changes: 1 addition & 12 deletions core/chaincode/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package chaincode

import (
"bytes"
"encoding/gob"
"fmt"
"io"
"sync"
Expand Down Expand Up @@ -761,16 +760,6 @@ func (handler *Handler) handleGetStateByRange(msg *pb.ChaincodeMessage) {
}()
}

func getBytes(qresult interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(qresult)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

//getQueryResponse takes an iterator and fetch state to construct QueryResponse
func getQueryResponse(handler *Handler, txContext *transactionContext, iter commonledger.ResultsIterator,
iterID string) (*pb.QueryResponse, error) {
Expand All @@ -790,7 +779,7 @@ func getQueryResponse(handler *Handler, txContext *transactionContext, iter comm
break
}
var resultBytes []byte
resultBytes, err = getBytes(queryResult)
resultBytes, err = proto.Marshal(queryResult.(proto.Message))
if err != nil {
return nil, err
}
Expand Down
22 changes: 12 additions & 10 deletions core/chaincode/shim/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ limitations under the License.
package shim

import (
"bytes"
"encoding/gob"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -491,11 +489,17 @@ func getStateByPartialCompositeKey(stub ChaincodeStubInterface, objectType strin

func (iter *StateQueryIterator) Next() (*queryresult.KV, error) {
result, err := next(iter.CommonIterator, STATE_QUERY_RESULT)
if err != nil {
return nil, err
}
return result.(*queryresult.KV), err
}

func (iter *HistoryQueryIterator) Next() (*queryresult.KeyModification, error) {
result, err := next(iter.CommonIterator, HISTORY_QUERY_RESULT)
if err != nil {
return nil, err
}
return result.(*queryresult.KeyModification), err
}

Expand All @@ -515,23 +519,21 @@ func (iter *CommonIterator) HasNext() bool {
func getResultFromBytes(queryResultBytes *pb.QueryResultBytes, iter *CommonIterator,
rType resultType) (commonledger.QueryResult, error) {

decoder := gob.NewDecoder(bytes.NewBuffer(queryResultBytes.ResultBytes))

if rType == STATE_QUERY_RESULT {
var stateQueryResult queryresult.KV
if err := decoder.Decode(&stateQueryResult); err != nil {
stateQueryResult := &queryresult.KV{}
if err := proto.Unmarshal(queryResultBytes.ResultBytes, stateQueryResult); err != nil {
return nil, err
}
iter.currentLoc++
return &stateQueryResult, nil
return stateQueryResult, nil

} else if rType == HISTORY_QUERY_RESULT {
var historyQueryResult queryresult.KeyModification
if err := decoder.Decode(&historyQueryResult); err != nil {
historyQueryResult := &queryresult.KeyModification{}
if err := proto.Unmarshal(queryResultBytes.ResultBytes, historyQueryResult); err != nil {
return nil, err
}
iter.currentLoc++
return &historyQueryResult, nil
return historyQueryResult, nil

}
return nil, errors.New("Wrong result type")
Expand Down

0 comments on commit bee763f

Please sign in to comment.