使用 NoRM for MongoDB 中的 HiLoIdGenerator 创建唯一标识符
我一直在使用 NoRM 附带的 HiLoIdGenerator (http://normproject.org/);我想用它来生成一个唯一标识符,我可以将其用作我的博客文章的 SLUG。目前,我使用 ObjectId 来唯一标识 MongoDB 中的文档,但由于它类似于 GUID,并且在 URL 中看起来不太好,所以我更喜欢使用类似 www.myblog.com/posts/1243 和这就是我决定使用 HiLoIdGenerator 的原因。
我想在客户端生成我的 HiLo id,我在 Stuart harris 的博客上阅读 http://red-badger.com/Blog/post/A-simple-IRepository3cT3e-implementation-for-MongoDB-and-NoRM.aspx NoRM 的新 HiLo Id 生成器还可以通过向客户端会话分配一系列整数来实现这一点,这些整数可以不受惩罚地使用(其他客户端将使用不同的范围),但是当我打开 HiLoIdGenerator 时,它说使用 HILO 算法生成新标识值的 HiLoIdGenerator 类。您的项目中只能使用该类的一个实例。
我真的有三个问题:
1)如果我的应用程序中有多个 HiLoIdGenerator 实例(假设我的服务类中有一个实例,为每个新文档调用GenerateId),我实际上可以保证我的所有 id 都是唯一的吗?鉴于 HiLoIdGenerator 类的代码表明应用程序中应该只有该类的单个实例?
2)HiLoIdGenerator构造函数接受一个容量参数,我想知道它的作用,我传递了0并且所有生成的Id都是相同的,然后我传递了1个新的HiLoIdGenerator(1),Id从1开始并且是加 1;我不太明白它的作用,但我假设它与生成器可以生成的一系列潜在值有关,但我不确定,但我想这样做。有人可以解释一下这个论点吗?
3)我想我理解HiLo算法的目的,如下所述什么是Hi/Lo算法? 但我不明白的是,我是否可以拥有两个 MongoDB 实例,以及两个不同的应用程序,每个应用程序查看不同的 MongoDB 实例,但都包含相同的集合类型,生成的 id 是否是全局唯一的,即,我可以像使用 GUID 一样使用它们吗?还是它们在给定的 MongoDB 实例中只是唯一的,因此无法在以后将两个集合合并到单个 MongoDB 实例中?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅此处了解如何生成单调递增的 id:
http://www.mongodb.org/display/DOCS/原子+操作#AtomicOperations-%22InsertifNotPresent%22
See here for how to produce monotonically increasing ids:
http://www.mongodb.org/display/DOCS/Atomic+Operations#AtomicOperations-%22InsertifNotPresent%22
是的,它们将是唯一的,每个客户端(HiLoGenerator)都会请求一系列可以分配的 lo,但只有当它们都使用相同的容量时,它们才会是唯一的
是的,
容量是客户端可以不受惩罚地分配的 Id 数量,同样,如果您有不同的容量对于客户端,您有可能创建非唯一值,如果您使用单调递增的 Id,则您只分配单个顺序值,您不需要 HiLo 算法,您只需要一个包含可以的值的位置增量并分配给一个新实体,请参阅 dm 的答案以了解此实现
是的,只要两个客户端都使用保存 Hi 值的相同集合,并且只要两个客户端使用相同的容量来生成 lo
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
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
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