Race condition, url shortener algorithm/Rails/MongoDB/MongoMapper

169 views
Skip to first unread message

Pablo Cantero

unread,
Dec 30, 2010, 12:05:09 PM12/30/10
to MongoMapper
Hello

I created a url shortener algorithm with Ruby + MongoMapper

It's a simple url shortener algorithm with max 3 digits
http://pablocantero.com/###

Where each # can be [a-z] or [A-Z] or [0-9]

For this algorithm, I need to persist four attributes on MongoDB
(through
MongoMapper)


class ShortenerData
include MongoMapper::Document
VALUES = ('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a
key :col_a, Integer
key :col_b, Integer
key :col_c, Integer
key :index, Integer
end

I created another class to manage ShortenerData and to generate the
unique
identifier

class Shortener
include Singleton

def get_unique
unique = nil
@shortener_data.reload
# some operations that can increment the attributes col_a,
col_b,
col_c and index
# ...
@shortener_data.save
unique
end
end

The Shortener usage

Shortener.instance.get_unique

My doubt is how can I make get_unique synchronized, my app will be
deployed on heroku, concurrent requests can call
Shortener.instance.get_unique

Brian Hempel

unread,
Dec 30, 2010, 1:14:20 PM12/30/10
to mongo...@googlegroups.com
Pablo, I honestly just wrote something like this yesterday, you inspired me to blog about it: http://ihswebdesign.com/blog/autoincrement-in-mongodb-with-ruby/

With that Incrementor class all you have to do is translate the integer counter value into [a-zA-Z0-9] by converting to base 62 and using the low "digits".

Brian Hempel

> --
> You received this message because you are subscribed to the Google
> Groups "MongoMapper" group.
> For more options, visit this group at
> http://groups.google.com/group/mongomapper?hl=en?hl=en

Pablo Cantero

unread,
Dec 30, 2010, 6:45:45 PM12/30/10
to MongoMapper
Hello Brian

Thanks for your reply!

Inspired in your blog post I create a ruby gem to auto-increment with
MongoMapper

https://github.com/phstc/mongomapper_id2

Can you help to improve this gem? Adding tests, testing..

Pablo Cantero

John Nunemaker

unread,
Dec 31, 2010, 12:08:30 AM12/31/10
to mongo...@googlegroups.com
Pablo,
Why store an extra field? Why not just set _id as an Integer and use that as the id?

Pablo Cantero

unread,
Dec 31, 2010, 8:52:57 AM12/31/10
to MongoMapper
Hello John,

I created an extra field to preserve the original behavior from
MongoDB

But after your message I red the documentation of the _id
http://www.mongodb.org/display/DOCS/Object+IDs

"Users are welcome to use their own conventions for creating ids; the
_id value may be of any type, other than arrays, so long as it is a
unique. Arrays are not allowed because they are Multikeys."

I think there is no problem to change the '_id' to use auto
incremented id

You think that is better to use the original "_id" instead of "id2"?

Can I change mongomapper_id2 and sent it as pull request to the
MongoMapper on GitHub?

Best Regards,
Pablo

Pablo Cantero

unread,
Dec 31, 2010, 1:08:44 PM12/31/10
to MongoMapper
I committed a few changes on https://github.com/phstc/mongomapper_id2

Now, I'm working on a pull request to send this plugin for mongomapper

I will use id instead of id2! :)

On Dec 31, 11:52 am, Pablo Cantero <pabloh...@gmail.com> wrote:
> Hello John,
>
> I created an extra field to preserve the original behavior from
> MongoDB
>
> But after your message I red the documentation of the _idhttp://www.mongodb.org/display/DOCS/Object+IDs

John Nunemaker

unread,
Jan 1, 2011, 4:53:16 PM1/1/11
to mongo...@googlegroups.com
This kind of functionality doesn't need to be in MongoMapper itself. Better for it to stay as a plugin. Also, Ara Howard has already created functionality similar to this.


It was created before find and modify was a method in the ruby driver, but should work fine for generating sequenced numbers for ids.

Pablo Cantero

unread,
Jan 1, 2011, 8:16:15 PM1/1/11
to MongoMapper
Hello John

First: Congrats for your code. I saw MongoMapper's code to create the
pull request! It's very good and clean! I learned a lot! ;)

I totally agree about Base62. But with auto incremented, I don't know,
I think that's a good feature for MongoMapper. It's optional,
available only if "auto_increment!" is added in the Document (IMHO)

I will remove Base62 encode from mongomapper_id2 to keep it clean

On Jan 1, 7:53 pm, John Nunemaker <nunema...@gmail.com> wrote:
> This kind of functionality doesn't need to be in MongoMapperitself. Better
> for it to stay as a plugin. Also, Ara Howard has already created
> functionality similar to this.
>
> http://drawohara.com/post/476248094/monotonic-sequences-in-mongo-mapper
>
> It was created before find and modify was a method in the ruby driver, but
> should work fine for generating sequenced numbers for ids.
>
>
>
>
>
>
>
> On Fri, Dec 31, 2010 at 1:08 PM, Pablo Cantero <pabloh...@gmail.com> wrote:
> > I committed a few changes onhttps://github.com/phstc/mongomapper_id2

John Nunemaker

unread,
Jan 3, 2011, 9:55:06 AM1/3/11
to mongo...@googlegroups.com
Hey,
For the most part I try to keep only things in core that everyone will use, especially going forward. Stuff like this is perfect for plugins.
Reply all
Reply to author
Forward
0 new messages