views:

64

answers:

2

If you've used GoToMeeting, that's the type of ID I want. I'd like it to be random so that it obfuscates the number of items being tracked and short, so that it's easy to reference manually; UUIDs are way too long. I'd like to avoid hitting persistent storage merely for performance reasons, but I can't think of any other way to avoid collisions. Is 9 digits enough to do something time-based?

In response to questions:

  • I'm building a ticket-tracking application. This ID would be used as the primary key for a table, but it would be needed before the record is persisted which would result in an extra database call that I'd like to avoid if possible.

  • I'd like to keep it at a 9 digit int. I consider a UUID to be too long because people are going to have to reference the ID manually (via email, phone, etc.).

  • I'm thinking of using the time of generation somehow. Since time is always ticking on forward, it would continually limit the set of potential IDs, excluding those that had already been generated.

+1  A: 

One way is to take a unique number or string (like a random UUID) then calculate a fixed-length digest (such as MD5 or SHA-1) and/or encode it in a higher base (like base64) to shorten it further.

Tore A.
Would something as simple as Math.abs(java.util.UUID.randomUUID().toString().hashCode()), in Java work. That will usually give you a multi-digit number, without letters. You could output it in hex if you wanted some letters, but it would be shorter. It's not guaranteed to be unique, but it seems reasonable to have another UUID / generated value be the DB key and use this as a secondary identifier. Your app could handle collisions this way too by presenting the user with more data (like the object name) and they could pick the right one.
AngerClown
A: 

Git does something similar where it generates a sha numbers for commits (and other events) and then the user can references the numbers manually in order to lookup those commits. The trick they used is that the user doesn't have to enter the whole string in order to find the correct event, they simply have to enter a long enough string that it doesn't collide with any other commit currently in the repository. In general this only require 5 or so hex digits for relatively large repositories.

Thomas Sidoti