Snorlaxative
Snorlaxative

Reputation: 55

Making multiple entries with the same key name in Hyperledger

I was trying to write a smart contract that would use the same key name(the name of the person whose details I am storing) multiple times and want all of the entries made for that key-name to be output when querying for the name.
Is it possible on to do so on hyperledger? If yes, then how would you write the query function
If not could you recommend an alternate method to achieve the same result?
I am new to hyperledger and have no idea how to proceed considering I didnt see any examples of chaincode similar to this.

Upvotes: 1

Views: 686

Answers (1)

Artem Barger
Artem Barger

Reputation: 41232

What you need to do is to encode the value into JSON format and store it marshaled for the given key, for example you can define a struct with a slice, update/append slice each time with new value, marshal to byte array and then save into ledger.

Each update you read the byte array from the ledger unmarshal it back to struct, update with required information and save back with same key.


To retrieve history of changes you can use one of the methods from ChaincodeStubInterface

// Chaincode interface must be implemented by all chaincodes. The fabric runs
// the transactions by calling these functions as specified.
type ChaincodeStubInterface interface {


// Other methods omitted

    // GetHistoryForKey returns a history of key values across time.
    // For each historic key update, the historic value and associated
    // transaction id and timestamp are returned. The timestamp is the
    // timestamp provided by the client in the proposal header.
    // GetHistoryForKey requires peer configuration
    // core.ledger.history.enableHistoryDatabase to be true.
    // The query is NOT re-executed during validation phase, phantom reads are
    // not detected. That is, other committed transactions may have updated
    // the key concurrently, impacting the result set, and this would not be
    // detected at validation/commit time. Applications susceptible to this
    // should therefore not use GetHistoryForKey as part of transactions that
    // update ledger, and should limit use to read-only chaincode operations.
    GetHistoryForKey(key string) (HistoryQueryIteratorInterface, error)


}

Upvotes: 2

Related Questions