Thanks for all the tips.

I had thought of the random generator, given that's what the Rails  
Engine Substruct uses, but I was worry more about collisions as proven  
here:

http://thedailywtf.com/Articles/The-Quest-for-the-Unique-ID.aspx

The KC Ruby group had suggested SHA1 truncated as shown here: 
http://gist.github.com/99156 
. I'm thinking this is the solution I'm leaning towards...

Ryan


On Apr 21, 2009, at 10:21 AM, Nic Benders wrote:

On Apr 21, 2009, at 8:02 AM, Edward O'Connor wrote:

>
>> I'm currently working on a project where I need a generated unique
>> identifier for an order and to not use the traditional database
>> primary key of id[...]
>> The UUID gem's shortest id is 32 characters and seems a quite  
>> lengthy.
>
> Think of it like a URL shortening service. Take several field values,
> concatenate them, and hash the result. Then encode the result in
> something like base 36, because that's compact and also URL-safe. the
> result should only be a few characters long. Store in another column
> and everyone's happy.

+1 for the much under-utilized Base-36.  Here is how I solved this  
problem:

rand(2**64).to_s(36) #=>  "3ps8njpa0eqot"

This gives you a 64-bit random identifier in 13 characters.  A 64-bit  
space means that your odds of a collision are less than 0.1% as long  
as you are generating less than 190 million IDs.  (48 bits would get  
you that same collision rate at 750 thousand IDs.)


--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---

Reply via email to