Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Hi/Lo algorithm?

What's the Hi/Lo algorithm?

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.

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

like image 887
DiegoCofre Avatar asked Nov 11 '08 20:11

DiegoCofre


People also ask

What is HiLo strategy?

The HiLo Activator study is a trend-following indicator introduced as part of the Gann Swing trading strategy. In addition to indicating the current trend direction, this can be used as both entry signal and trailing stop.

What is a hi-lo?

Hi-Lo (High Low), a card game where players guess if a certain face-up card is higher or lower in value than a certain face-down card.

How does hibernate sequence generator work?

SEQUENCE Generation. To use a sequence-based id, Hibernate provides the SequenceStyleGenerator class. This generator uses sequences if our database supports them. It switches to table generation if they aren't supported.

What is hibernate generator?

A generator class is used to generate an ID for an object, which is going to be inserted in the database as a primary key. The ID is a unique identifier for the objects of the persistent class. We can use any generator classes in our application as per our requirements.


2 Answers

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.

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.

like image 159
Jon Skeet Avatar answered Sep 28 '22 18:09

Jon Skeet


In addition to Jon's answer:

It is used to be able to work disconnected. A client can then ask the server for a hi number and create objects increasing the lo number itself. It does not need to contact the server until the lo range is used up.

like image 43
Stephan Eggermont Avatar answered Sep 28 '22 17:09

Stephan Eggermont