strategy to create 4 bytes unique Id in java -


our java application has 4 bytes size restriction hold unique id.

we forced implement strategy create unique ids 4 bytes in size.

does 1 know strategy create it

yes, start random 32 bit integer , increment it.

anything else demanding scale (e.g. if have 1 billion created ids , need randomly generate new one, have have 1 billion entry table check existence inside... ouch!).

but if absolutely has random , unique, 2 strategies can take are:

1) have big hashset of every id used far , check existence in set whenever generate new random id. if is, discard , try again.

2) store randomly used ids in database, , select see if newly generated random id exists. if does, discard , try again.

if unique id larger, use guid (also known uuid), generated large enough , in such way you'll never see 2 guids have same value ever, anywhere, without needing check.

for guids/uuids in java, see http://docs.oracle.com/javase/7/docs/api/java/util/uuid.html


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -