I'm working on Lagom project and one of the requirement is to uniquely identify each user's document.
A document has an id that I should generate each time a user adds a document.
My idea is simple to guarantee the unicity of the ID:
I've implemented the DocumentEventProcessor extends ReadSideProcessor<DocumentEvent>
and DocumentRepository
to be able to query the database.
My problem is how to write the function that iteratively (recursively maybe) query the database until new random ID returns.
Maybe you may use the following approach:
assumptions:
- distributed system,
- 7 characters long ID,
- you can assign each node a unique value - hostId (maybe last digits from your host IP?)
what to do:
- have a per node Singleton which will generate the ID,
- calculate uniqueNumericID by concatenating the hostId and the current time in millis (you should store last used timestamp, in case you need more than one ID per millisecond, just add one to the last value until last timestamp >= current timestamp).
- convert this numeric value (decimal) to the number with base say 62 (use characters '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' and recurrently calculate modulo of the (rest of the) value, use the modulo as index of the character).
There should be more than enough space in your 7 characters long ID.