I need to implement composite keys in hyperledger so that I could have a unique key based on the attributes put into the ledger. The function CreateCompositeKey(object
In Hyperledger Fabric there is an example chaincode which shows how to use composite keys, check it out: Marbles
Basically it almost as you said:
key, err := stub.CreateCompositeKey(index, []string{key1, key2, key3})
// Skiped
stub.PutState(key, value)
The function simply creates a key by combining the attributes into a single string. Its application is where we need to store multiple instances of one type on the ledger. The keys of those instances will be constructed from a combination of attributes— for example, "Order" + ID, yielding ["Order1","Order2", ...]. This function comes in hand when you intend to search for assets based on components of the key in range queries.
The 'CreateCompositeKey' in SHIM construct a composite key (indeed, a unique key) based on a combination of several attributes.
The inverse function is SplitCompositeKey which splits the compositeKey into attributes.
func SplitCompositeKey(compositeKey string) (string, []string, error)
The 'TestTradeWorkflow_Agreement' function is this code is also useful in understanding the whole process:
For Java implementation it did not clearly come out in the documentation/ examples, did a little digging around and you can use 'compositeKey.toString()' as the composite key.
Example below:
final String compositeKey = stub.createCompositeKey("my-key-part-1", "my-key-part-2").toString();
stub.putStringState(compositeKey, myJSONString); // use this
stub.putState(compositeKey, myJSONString.getBytes()); // or this