什么是 Hi/Lo 算法? [英] What's the Hi/Lo algorithm?

查看:60
本文介绍了什么是 Hi/Lo 算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是 Hi/Lo 算法?

What's the Hi/Lo algorithm?

我在 NHibernate 文档中找到了这个(这是一种生成唯一键的方法,第 5.1.4.2 节),但我还没有找到关于它是如何工作的很好的解释.

I've found this in the NHibernate documentation (it's one method to generate unique keys, section 5.1.4.2), but I haven't found a good explanation of how it works.

我知道 Nhibernate 处理它,我不需要知道内部,但我只是好奇.

I know that Nhibernate handles it, and I don't need to know the inside, but I'm just curious.

推荐答案

基本思想是你有两个数字来组成一个主键——一个高"数字和一个低"数字.客户端基本上可以增加高"序列,知道然后它可以安全地从先前高"值的整个范围和各种低"值生成密钥.

The basic idea is that you have two numbers to make up a primary key- a "high" number and a "low" number. A client can basically increment the "high" sequence, knowing that it can then safely generate keys from the entire range of the previous "high" value with the variety of "low" values.

例如,假设您有一个当前值为 35 的高"序列,而低"数字在 0-1023 的范围内.然后客户端可以将序列增加到 36(其他客户端在使用 35 时能够生成密钥)并且知道密钥 35/0、35/1、35/2、35/3...35/1023 是全部可用.

For instance, supposing you have a "high" sequence with a current value of 35, and the "low" number is in the range 0-1023. Then the client can increment the sequence to 36 (for other clients to be able to generate keys while it's using 35) and know that keys 35/0, 35/1, 35/2, 35/3... 35/1023 are all available.

能够在客户端设置主键非常有用(尤其是对于 ORM),而不是插入没有主键的值,然后将它们取回客户端.除此之外,这意味着您可以轻松地建立父/子关系并在执行任何插入之前将所有键都准备好,这使得批处理更简单.

It can be very useful (particularly with ORMs) to be able to set the primary keys on the client side, instead of inserting values without primary keys and then fetching them back onto the client. Aside from anything else, it means you can easily make parent/child relationships and have the keys all in place before you do any inserts, which makes batching them simpler.

这篇关于什么是 Hi/Lo 算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆