Skip to content

Commit

Permalink
Merge "FAB-2085: Chaincode explicitly manages indices"
Browse files Browse the repository at this point in the history
  • Loading branch information
Srinivasan Muralidharan authored and Gerrit Code Review committed Feb 8, 2017
2 parents 9011c66 + 7ef83d0 commit 0db4b6c
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions examples/chaincode/go/marbles02/marbles_chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,46 @@ func (t *SimpleChaincode) readMarble(stub shim.ChaincodeStubInterface, args []st
// delete - remove a marble key/value pair from state
// ==================================================
func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var jsonResp string
var marbleJSON marble
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}

marbleName := args[0]
err := stub.DelState(marbleName) //remove the marble from chaincode state

// to maintain the color~name index, we need to read the marble first and get its color
valAsbytes, err := stub.GetState(marbleName) //get the marble from chaincode state
if err != nil {
jsonResp = "{\"Error\":\"Failed to get state for " + marbleName + "\"}"
return shim.Error(jsonResp)
} else if valAsbytes == nil {
jsonResp = "{\"Error\":\"Marble does not exist: " + marbleName + "\"}"
return shim.Error(jsonResp)
}

err = json.Unmarshal([]byte(jsonResp), &marbleJSON)
if err != nil {
jsonResp = "{\"Error\":\"Failed to decode JSON of: " + marbleName + "\"}"
return shim.Error(jsonResp)
}

err = stub.DelState(marbleName) //remove the marble from chaincode state
if err != nil {
return shim.Error("Failed to delete state:" + err.Error())
}

// maintain the index
indexName := "color~name"
colorNameIndexKey, err := stub.CreateCompositeKey(indexName, []string{marbleJSON.Color, marbleJSON.Name})
if err != nil {
return shim.Error(err.Error())
}

// Delete index entry to state.
err = stub.DelState(colorNameIndexKey)
if err != nil {
return shim.Error("Failed to delete state:" + err.Error())
}
return shim.Success(nil)
}

Expand Down

0 comments on commit 0db4b6c

Please sign in to comment.