3

I have been struggling a little with the HiLoIdGenerator that comes with NoRM (http://normproject.org/); I want to use it to generate a unique identifier that I can use as a SLUG for my blog posts. At present I use the ObjectId to uniquely identify a document within MongoDB, but as this is GUID-like and it doesn't look very good in a URL, I would prefer to have something like www.myblog.com/posts/1243 and so this is why I have decided to use the HiLoIdGenerator.

I would like to generate my HiLo id's on the client-side and I read on stuart harris' blog http://red-badger.com/Blog/post/A-simple-IRepository3cT3e-implementation-for-MongoDB-and-NoRM.aspx that NoRM's new HiLo Id generator also allows this by allocating a range of integers to the client session that can be used with impunity (other clients will be using a different range) but when i opened the HiLoIdGenerator it said that the HiLoIdGenerator Class that generates a new identity value using the HILO algorithm. Only one instance of this class should be used in your project.

I really have three questions:

1) if I had multiple instances of the HiLoIdGenerator in my application (say I had an instance in my service class that called GenerateId for every new document) could I actually guarantee that all of my id's would be unique, given that the code for the HiLoIdGenerator class says that there should only be a single instance of this class in an application?

2) the HiLoIdGenerator constructor takes a capacity argument, and I would like to know what it does, I passed 0 and all of the generated Id's were the same, I then passed in 1 new HiLoIdGenerator(1) the Id's began at 1 and were incremented by 1; I don't really understand what it does but I am presuming that it has something to do with a range of potential values that the generator can generate, but I am not sure, and I would like to be. Could someone please explain this argument?

3) I think I understand the aim of the HiLo algorithm as explained here What's the Hi/Lo algorithm? but what I don't understand is whether I can have two instances of MongoDB with two different applications each looking at a different instance of a MongoDB but both containing the same collection types, whether generated id's are globally unique, i.e., could I use them the way I would a GUID, or are they simply unique within a given instance of MongoDB, therefore precluding a merge of both collections into a single instance of MongoDB at a later date?

thanks

Community
  • 1
  • 1
nickbw
  • 463
  • 1
  • 4
  • 12
  • I think regarding my second question that capacity is simply a range of values that represents a Hi and each number within that range could be assigned as a lo before a new hi is requested; if i were only ever assigning a single id would it make sense to set the capacity to 1? – nickbw Dec 05 '10 at 23:19

2 Answers2

1

See here for how to produce monotonically increasing ids: http://www.mongodb.org/display/DOCS/Atomic+Operations#AtomicOperations-%22InsertifNotPresent%22

dm.
  • 1,982
  • 12
  • 7
  • what i would like to do is use the HiLoIdGenerator generator so that i can have the value available on the client side, before the document is saved in MongoDB, that insert if not present method seems to be a function that is executed on the db. cheers – nickbw Dec 09 '10 at 22:06
  • +1 as i am in reality using monotonically increasing ids, but incorrectly using the HiLoIdGenerator to do it, if you are actually only using the HiLoGenerator to generate monotonically increasing Ids it will work but it is overkill as you can just have a field in a seperate collection that you increment by 1 and use that, you do not need the overhead of the HiLo algorithm it should be used if you are assigning 2+ id's at a time, with the potential for multiple clients assigning these id's – nickbw Mar 31 '11 at 00:16
0
  1. Yes they would be unique, each client (HiLoGenerator) would request a range of lo's that could be allocated but they would only be unique if they both used the same capacity

  2. Capacity is the number of Id's that the client can assign with impunity, again if you have a different capacity amongst clients you have the potential to create non-unique values, if you are using monotonically increasing Id's you are only ever assigning a single sequential value, you do not need the HiLo algorithm, you just need a single place that contains a value that you can increment and assign to a new entity, see dm's answer for an implementation of this

  3. Yes as long as both clients are both using the same collection that holds the Hi value, and as long as both clients use the same capcity for generating the lo's

nickbw
  • 463
  • 1
  • 4
  • 12