Hi Dan,

I definitely need short strings - 6-8 characters for the url. It is
for the url of e-cards that people send - they don't have to be secret
urls, but it would be nice if people couldn't easily guess other urls
and read other peoples cards, so just using the auto-incrementing id
isn't really an option :(

How likely is rand(36**8).to_s(36) to have a collision compared to
truncating UUIDTools::UUID.random_create?

I realise that with smaller strings the chances of collision are
larger. How do sites like disqus and bit.ly make their short urls?

Thanks again for all your help and ideas,

DAZ



On Mar 12, 8:00 pm, "Dan Kubb (dkubb)" <[email protected]> wrote:
> DAZ,
>
> > Thanks for the reply. I have a few follow-up questions:
> > UUIDs seem to be very long, I was looking for a short url of about 6-8
> > characters, is there any way of doing this? (I still quite like the
> > quick and dirty rand(36**8).to_s(36) method)
>
> You can always truncate the string it returns, and the UUID generation
> algorithm ensures the likelihood of collisions is low. Keep in mind
> that the smaller the string the chances of collision go up and there's
> no way around that.
>
> What you can do is setup a default proc that auto-generates the
> string, checks to see if it exists, and then writes it. There's a race
> condition there though, because something else could come along and
> write the string in between the time you checked and write. You can
> check to see if the various DataMapper locking libraries will help
> with this though.
>
> You're probably starting to understand why people use auto-
> incrementing columns and just let the database worry about these kinds
> of issues ;)
>
> > >   property :unique_value, UUID, :required => true, :unique =>
> > > true, :default => proc { UUIDTools::UUID.random_create }
> >   snip
> > >create the underlying column so it is unique in the datastore
> > > (assuming it's supported)
>
> > What do you mean by '(assuming it's supported)'?
>
> Some datastores don't support the concept of uniqueness. All databases
> do afaik, so if you're using a DB you should be ok.
>
> > Using this method, what would happen if UUIDTools::UUID.random_create
> > created a string that had already been used. Would it just fail to
> > create the resource or try to create a new UUID?
>
> The chances are exceedingly low that this would happen. So low I
> probably wouldn't worry about it -- billions to one, if not more. I
> haven't done the math to figure it out exactly though, I'm sure google
> will give you more insight.
>
> --
>
> Dan
> (dkubb)

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to