Created
April 17, 2018 09:43
-
-
Save jastisriradheshyam/4bacec1c374c41c68c49f8729f489091 to your computer and use it in GitHub Desktop.
Get all the objects (marble) names in the ledger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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