Skip to content

Commit

Permalink
Add createResponse function for building paginated QueryResponse
Browse files Browse the repository at this point in the history
This commit introduces the createResponse function in handler.go, which constructs a QueryResponse for range queries. It handles pagination and includes error handling for payload marshaling, ensuring proper cleanup of the query context in case of errors.

Signed-off-by: David VIEJO <[email protected]>
  • Loading branch information
dviejokfs committed Dec 17, 2024
1 parent e8139fc commit 8cae2b2
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions core/chaincode/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1320,3 +1320,33 @@ func (s State) String() string {
return "UNKNOWN"
}
}

// createResponse builds a QueryResponse for range queries. If the query is paginated,
// it will include bookmark information in the response.
func createResponse(txContext *TransactionContext, iter commonledger.ResultsIterator, isPaginated bool, totalReturnLimit int32) (*pb.ChaincodeMessage, error) {
// Use the QueryResponseBuilder to construct the response
payload, err := txContext.Handler.QueryResponseBuilder.BuildQueryResponse(txContext, iter, txContext.QueryIterator.GetID(), isPaginated, totalReturnLimit)

Check failure on line 1328 in core/chaincode/handler.go

View workflow job for this annotation

GitHub Actions / Basic Checks

txContext.Handler undefined (type *TransactionContext has no field or method Handler)

Check failure on line 1328 in core/chaincode/handler.go

View workflow job for this annotation

GitHub Actions / Basic Checks

txContext.QueryIterator undefined (type *TransactionContext has no field or method QueryIterator)

Check failure on line 1328 in core/chaincode/handler.go

View workflow job for this annotation

GitHub Actions / Basic Checks

txContext.Handler undefined (type *TransactionContext has no field or method Handler)

Check failure on line 1328 in core/chaincode/handler.go

View workflow job for this annotation

GitHub Actions / Basic Checks

txContext.QueryIterator undefined (type *TransactionContext has no field or method QueryIterator)
if err != nil {
txContext.CleanupQueryContext(txContext.QueryIterator.GetID())

Check failure on line 1330 in core/chaincode/handler.go

View workflow job for this annotation

GitHub Actions / Basic Checks

txContext.QueryIterator undefined (type *TransactionContext has no field or method QueryIterator)

Check failure on line 1330 in core/chaincode/handler.go

View workflow job for this annotation

GitHub Actions / Basic Checks

txContext.QueryIterator undefined (type *TransactionContext has no field or method QueryIterator)
return nil, errors.WithStack(err)
}
if payload == nil {
txContext.CleanupQueryContext(txContext.QueryIterator.GetID())

Check failure on line 1334 in core/chaincode/handler.go

View workflow job for this annotation

GitHub Actions / Basic Checks

txContext.QueryIterator undefined (type *TransactionContext has no field or method QueryIterator)

Check failure on line 1334 in core/chaincode/handler.go

View workflow job for this annotation

GitHub Actions / Basic Checks

txContext.QueryIterator undefined (type *TransactionContext has no field or method QueryIterator)
return nil, errors.New("marshal failed: proto: Marshal called with nil")
}

// Marshal the payload into bytes
payloadBytes, err := proto.Marshal(payload)
if err != nil {
txContext.CleanupQueryContext(txContext.QueryIterator.GetID())

Check failure on line 1341 in core/chaincode/handler.go

View workflow job for this annotation

GitHub Actions / Basic Checks

txContext.QueryIterator undefined (type *TransactionContext has no field or method QueryIterator)
return nil, errors.Wrap(err, "marshal failed")
}

// Create and return the chaincode message response
return &pb.ChaincodeMessage{
Type: pb.ChaincodeMessage_RESPONSE,
Payload: payloadBytes,
Txid: txContext.TxID,

Check failure on line 1349 in core/chaincode/handler.go

View workflow job for this annotation

GitHub Actions / Basic Checks

txContext.TxID undefined (type *TransactionContext has no field or method TxID)
ChannelId: txContext.ChannelID,
}, nil
}

0 comments on commit 8cae2b2

Please sign in to comment.