forked from ThienLK1/fabric-v2.2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_response_generator.go
92 lines (74 loc) · 3.2 KB
/
query_response_generator.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chaincode
import (
"github.com/golang/protobuf/proto"
commonledger "github.com/hyperledger/fabric/common/ledger"
pb "github.com/hyperledger/fabric/protos/peer"
)
type QueryResponseGenerator struct {
MaxResultLimit int
}
// BuildQueryResponse takes an iterator and fetch state to construct QueryResponse
func (q *QueryResponseGenerator) BuildQueryResponse(txContext *TransactionContext, iter commonledger.ResultsIterator,
iterID string, isPaginated bool, totalReturnLimit int32) (*pb.QueryResponse, error) {
pendingQueryResults := txContext.GetPendingQueryResult(iterID)
totalReturnCount := txContext.GetTotalReturnCount(iterID)
for {
// if the total count has been reached, return the result and prevent the Next() being called
if *totalReturnCount >= totalReturnLimit {
return createQueryResponse(txContext, iterID, isPaginated, pendingQueryResults, *totalReturnCount)
}
queryResult, err := iter.Next()
switch {
case err != nil:
chaincodeLogger.Errorf("Failed to get query result from iterator")
txContext.CleanupQueryContext(iterID)
return nil, err
case queryResult == nil:
return createQueryResponse(txContext, iterID, isPaginated, pendingQueryResults, *totalReturnCount)
case !isPaginated && pendingQueryResults.Size() == q.MaxResultLimit:
// if explicit pagination is not used
// if the max number of results is queued up, cut batch, then add current result to pending batch
// MaxResultLimit is for batching between chaincode shim and handler
// MaxResultLimit does not limit the records returned to the client
batch := pendingQueryResults.Cut()
if err := pendingQueryResults.Add(queryResult); err != nil {
txContext.CleanupQueryContext(iterID)
return nil, err
}
*totalReturnCount++
return &pb.QueryResponse{Results: batch, HasMore: true, Id: iterID}, nil
default:
if err := pendingQueryResults.Add(queryResult); err != nil {
txContext.CleanupQueryContext(iterID)
return nil, err
}
*totalReturnCount++
}
}
}
func createQueryResponse(txContext *TransactionContext, iterID string, isPaginated bool, pendingQueryResults *PendingQueryResult, totalReturnCount int32) (*pb.QueryResponse, error) {
batch := pendingQueryResults.Cut()
if isPaginated {
// when explicit pagination is enabled, return the batch with the responseMetadata
bookmark := txContext.CleanupQueryContextWithBookmark(iterID)
responseMetadata := createResponseMetadata(totalReturnCount, bookmark)
responseMetadataBytes, err := proto.Marshal(responseMetadata)
if err != nil {
return nil, err
}
return &pb.QueryResponse{Results: batch, HasMore: false, Id: iterID, Metadata: responseMetadataBytes}, nil
}
// if explicit pagination is not used, then the end of the resultset has been reached, return the batch
txContext.CleanupQueryContext(iterID)
return &pb.QueryResponse{Results: batch, HasMore: false, Id: iterID}, nil
}
func createResponseMetadata(returnCount int32, bookmark string) *pb.QueryResponseMetadata {
responseMetadata := &pb.QueryResponseMetadata{}
responseMetadata.Bookmark = bookmark
responseMetadata.FetchedRecordsCount = int32(returnCount)
return responseMetadata
}