Skip to content

Instantly share code, notes, and snippets.

@jastisriradheshyam
Created April 17, 2018 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jastisriradheshyam/4bacec1c374c41c68c49f8729f489091 to your computer and use it in GitHub Desktop.
Save jastisriradheshyam/4bacec1c374c41c68c49f8729f489091 to your computer and use it in GitHub Desktop.
Get all the objects (marble) names in the ledger
//Shows all the marbles or objects in the ledger (blockchain)
//This is a marbles chaincode (hyperledger fabric samples) related code.
// Jasti Sri Radhe Shyam
func (t *SimpleChaincode) getMarblesByRangeAll(stub shim.ChaincodeStubInterface) pb.Response {
//Gets all the data with undefined range
resultsIterator, err := stub.GetStateByRange("", "")
if err != nil {
return shim.Error(err.Error())
}
defer resultsIterator.Close()
// buffer is a JSON array containing QueryResults
var buffer bytes.Buffer
buffer.WriteString("[")
bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
// Add a comma before array members, suppress it for the first array member
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
buffer.WriteString("\"")
buffer.WriteString(queryResponse.Key)
buffer.WriteString("\"")
bArrayMemberAlreadyWritten = true
}
buffer.WriteString("]")
fmt.Printf("- getMarblesByRange queryResult:\n%s\n", buffer.String())
return shim.Success(buffer.Bytes())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment