0

What is the safe way to generate a ObjectId with String represnetation that will not collide in a distributed environment like Google App Engine (GAE) using Java?

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
xkm
  • 271
  • 2
  • 4
  • 11
  • 3
    Is there anything wrong with the autogenerated ids? Those would be the safest ones... If you're trying to implement a url shortener - maybe you could get some ideas from here https://stackoverflow.com/questions/742013/how-to-code-a-url-shortener or do a `md5(url)`, may be with a shorter hash algorithm like `crc32` (which will eventually collide though). – Mihail Russu Jan 30 '15 at 09:27

2 Answers2

1

How about using java UUID? You can do a quick check for collision by fetching the newly generated ID.

tj-recess
  • 1,781
  • 11
  • 15
  • 1
    Technically this risks a race condition in a distributed, eventually-consistent environment like the datastore (low probability, but...). – Alex Martelli Jan 30 '15 at 17:54
  • @AlexMartelli, it is only eventually consistent on global queries. Lookups are guaranteed to be strongly consistent, as is our handling of things like 'INSERT'; that is, 2 different processes can never encounter a race condition with INSERT that results in both being successful with the same key. UUIDs by their nature should also prevent you having to handle INSERT collisions, although by all means do handle it for events such as broken PRNGs, etc. – Dan McGrath Nov 15 '16 at 23:12
0

Do you need the actual key to be a string or do you just need a string representation of it under some circumstances? This answer might be relevant in the latter case: How to convert an integer to the shortest url-safe string in Python?

Community
  • 1
  • 1
kblomster
  • 91
  • 2