问题
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(objectType string, attributes []string)(string,error)
takes in objectType and attributes string. I couldnt find any examples of this online, how are the relevant attributes to be made into the composite key passed and in what way is the output given?
So the way Composite keys should be used is make a key first and then push it to the blockchain with PutState(key string, value []byte) error
where the hey in PutState is the output of CreateCompositeKey? If not, then how are composite keys to be used?
Similarly in
GetStateByPartialCompositeKey(objectType string, keys []string) (StateQueryIteratorInterface, error)
How are the keys we want to make queries by passed to the function? And what are the output data types "StateQueryIteratorInterface" and "HistoryQueryIteratorInterface"?
I am fairly new to programming and have no prior knowledge of databases so am getting confused with really basic things. I'd really appreciate some help!
回答1:
In Hyperledger Fabric there are two sample chaincodes which shows how to use composite keys:
- Marbles
- Map
Basically it almost as you said:
key, err := stub.CreateCompositeKey(index, []string{key1, key2, key3})
// Skiped
stub.PutState(key, value)
回答2:
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:
来源:https://stackoverflow.com/questions/44918611/composite-key-functions-in-hyperledger