1

I want id in my chaincode to be assigned by a ... chaincode. So it will be just numeric from 1 to 99999999. When I create a new record I want to assign it id, so i guess, I need to get last record and grab that id, so how can I grab only last record? I think only of is GetStateByRange and then iterating through all keys, but that doesn't sound very efficient.

NIck
  • 163
  • 1
  • 2
  • 12
  • Adding autogenerated ids that rely on the ledger is not a very good idea according to me. Doing this will centralize your dependency of generating an ID on a single key and you have to increment and persist it in the blockchain every time you require an ID. If you are running concurrent transactions you would run into Optimistic Locking failures causing an MVCC_READ_CONFLICT (https://stackoverflow.com/questions/45347439/mvcc-read-conflict-when-submitting-multiple-transactions-concurrently). Try using some random keys or atleast invert the dependency of ID generation in Client App. – arnabkaycee Dec 18 '17 at 11:59
  • Thanks for reply, I thought about random Id, but it may have collssions on big numbers and it will have even more collissions, won't it? In my app I don't really need id because I will index ledger by composite key, but i definitly need to have id, otherwise I won't be able to put smth into the ledger. – NIck Dec 19 '17 at 17:23
  • There are numerous libraries that will help you to generate unique IDs, even on nodes on a distributed environment. Just search for UUID libs for the environment you are using. – arnabkaycee Dec 20 '17 at 02:08

0 Answers0