什么是高/低算法?算法

2023-09-10 22:22:40 作者:陪你做一场缠绵梦

什么是高/低算法?

我的 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.

推荐答案

其基本思想是,你有两个数字来弥补主键盘 - 高号和低号。客户端可以基本上增加了高序列,知道它可以然后安全地生成的previous高值与各种低值的整个范围键。

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 ...一千零二十三分之三十五是所有可用的。

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.

它可以是非常有用的(尤其奥姆斯),以便能够在客户端侧设置,而不是没有主键插入值,然后取它们拖回客户端的主键。除了别的,这意味着你可以很容易地使父/子关系,并有钥匙全部到位在此之前的任意的插入,这使得他们批处理简单。

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.