What's the Hi/Lo algorithm?

前端 未结 5 1805
面向向阳花
面向向阳花 2020-11-22 06:00

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 goo

5条回答
  •  难免孤独
    2020-11-22 06:49

    Lo is a cached allocator that splits the keyspace into large chunks, typically based on some machine word size, rather than the meaningfully-sized ranges (eg obtaining 200 keys at a time) which a human might sensibly choose.

    Hi-Lo usage tends to waste large numbers of keys on server restart, and generate large human-unfriendly key values.

    Better than the Hi-Lo allocator, is the "Linear Chunk" allocator. This uses a similar table-based principle but allocates small, conveniently-sized chunks & generates nice human-friendly values.

    create table KEY_ALLOC (
        SEQ varchar(32) not null,
        NEXT bigint not null,
        primary key (SEQ)
    );
    

    To allocate the next, say, 200 keys (which are then held as a range in the server & used as needed):

    select NEXT from KEY_ALLOC where SEQ=?;
    update KEY_ALLOC set NEXT=(old value+200) where SEQ=? and NEXT=(old value);
    

    Providing you can commit this transaction (use retries to handle contention), you have allocated 200 keys & can dispense them as needed.

    With a chunk-size of just 20, this scheme is 10x faster than allocating from an Oracle sequence, and is 100% portable amongst all databases. Allocation performance is equivalent to hi-lo.

    Unlike Ambler's idea, it treats the keyspace as a contiguous linear numberline.

    This avoids the impetus for composite keys (which were never really a good idea) and avoids wasting entire lo-words when the server restarts. It generates "friendly", human-scale key values.

    Mr Ambler's idea, by comparison, allocates the high 16- or 32-bits, and generates large human-unfriendly key values as the hi-words increment.

    Comparison of allocated keys:

    Linear_Chunk       Hi_Lo
    100                65536
    101                65537
    102                65538
    .. server restart
    120                131072
    121                131073
    122                131073
    .. server restart
    140                196608
    

    Design-wise, his solution is fundamentally more complex on the number-line (composite keys, large hi_word products) than Linear_Chunk while achieving no comparative benefit.

    The Hi-Lo design arose early in OO mapping and persistence. These days persistence frameworks such as Hibernate offer simpler and better allocators as their default.

提交回复
热议问题