Class HiLoOptimizer

java.lang.Object
org.hibernate.id.enhanced.AbstractOptimizer
org.hibernate.id.enhanced.HiLoOptimizer
All Implemented Interfaces:
Optimizer

public class HiLoOptimizer extends AbstractOptimizer
Optimizer which applies a 'hilo' algorithm in memory to achieve optimization.

A 'hilo' algorithm is simply a means for a single value stored in the database to represent a "bucket" of possible, contiguous values. The database value identifies which particular bucket we are on.

This database value must be paired with another value that defines the size of the bucket; the number of possible values available. The incrementSize serves this purpose. The naming here is meant more for consistency in that this value serves the same purpose as the increment supplied to the PooledOptimizer.

The general algorithms used to determine the bucket is:

  1. upperLimit = (databaseValue * incrementSize) + 1
  2. lowerLimit = upperLimit - incrementSize

As an example, consider a case with incrementSize of 20. Initially, the database holds 1:

  1. upperLimit = (1 * 20) + 1 = 21
  2. lowerLimit = 21 - 20 = 1

From there we increment the value from lowerLimit until we reach the upperLimit, at which point we would define a new bucket. The database now contains 2, though incrementSize remains unchanged:

  1. upperLimit = (2 * 20) + 1 = 41
  2. lowerLimit = 41 - 20 = 21
And so on...

Note, 'value' always (after init) holds the next value to return

  • Constructor Details

    • HiLoOptimizer

      public HiLoOptimizer(Class<?> returnClass, int incrementSize)
      Constructs a HiLoOptimizer
      Parameters:
      returnClass - The Java type of the values to be generated
      incrementSize - The increment size.
  • Method Details

    • generate

      public Serializable generate(AccessCallback callback)
      Description copied from interface: Optimizer
      Generate an identifier value accounting for this specific optimization.
      Parameters:
      callback - Callback to access the underlying value source.
      Returns:
      The generated identifier value.
    • getLastSourceValue

      public IntegralDataTypeHolder getLastSourceValue()
      Description copied from interface: Optimizer
      A common means to access the last value obtained from the underlying source. This is intended for testing purposes, since accessing the underlying database source directly is much more difficult.
      Returns:
      The last value we obtained from the underlying source; null indicates we have not yet consulted with the source.
    • applyIncrementSizeToSourceValues

      public boolean applyIncrementSizeToSourceValues()
      Description copied from interface: Optimizer
      Are increments to be applied to the values stored in the underlying value source?
      Returns:
      True if the values in the source are to be incremented according to the defined increment size; false otherwise, in which case the increment is totally an in memory construct.
    • getLastValue

      public IntegralDataTypeHolder getLastValue()
      Getter for property 'lastValue'.

      Exposure intended for testing purposes.

      Returns:
      Value for property 'lastValue'.
    • getHiValue

      public IntegralDataTypeHolder getHiValue()
      Getter for property 'upperLimit'.

      Exposure intended for testing purposes.

      Returns:
      Value for property 'upperLimit'.