net.sf.farrago.catalog
Class FarragoSequenceAccessor

java.lang.Object
  extended by org.eigenbase.util.CompoundClosableAllocation
      extended by net.sf.farrago.catalog.FarragoSequenceAccessor
All Implemented Interfaces:
ClosableAllocation, ClosableAllocationOwner

public class FarragoSequenceAccessor
extends CompoundClosableAllocation

A FarragoSequenceAccessor optimizes access to sequences. A sequence generates new values on a per-row basis. But a sequence is not updated after every row (because that would be very slow.) Instead, an accessor reserves a large cache of values which it quickly allocates.

The accessor synchronizes access so multiple clients can use the sequence at the same time. However this requires clients to obtain an accessor from the singleton method FarragoRepos.getSequenceAccessor()

To clean up properly after a statement is completed or the database is shutdown, unreserve() should be called to release unused values.

Due to the use of singleton sequence accessors, sequence accessors may exist for a long time.

Version:
$Id: //open/dev/farrago/src/net/sf/farrago/catalog/FarragoSequenceAccessor.java#11 $
Author:
John Pham

Field Summary
private  boolean ascending
           
private  boolean cycle
           
private  long increment
           
private  long lastReservedValue
           
private  long max
           
private static long MAX_RESERVATION_SIZE
           
private  long min
           
private  String mofId
           
static String NEXT_VALUE_METHOD_NAME
           
private  Long nextReservedValue
           
private  FarragoRepos repos
           
private  boolean reserved
           
 
Fields inherited from class org.eigenbase.util.CompoundClosableAllocation
allocations
 
Constructor Summary
protected FarragoSequenceAccessor(FarragoRepos repos, String sequenceMofId)
          Constructs a FarragoSequenceAccessor
 
Method Summary
 void alterSequence(FarragoSequenceOptions options, RelDataType dataType)
          Modifies a sequence and loads updated fields.
 void closeAllocation()
          Deallocates unused sequence values.
private  String getName()
          Returns the name of the sequence
 long getNext()
          Retrieves a value from the sequence, possibly reserving more values in the process.
private  FemSequenceGenerator getSequence()
          Retrieves the underlying sequence from the catalog
private  void loadSequence(FemSequenceGenerator sequence)
          Initializes the sequence accessor from a sequence.
private  void reserve()
          Reserves up to MAX_RESERVATION_SIZE values in the sequence.
private  void reserveInternal()
           
private  void unreserve()
          Returns values unused by the sequence accessor to the catalog
 
Methods inherited from class org.eigenbase.util.CompoundClosableAllocation
addAllocation, forgetAllocation, hasAllocations
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NEXT_VALUE_METHOD_NAME

public static String NEXT_VALUE_METHOD_NAME

MAX_RESERVATION_SIZE

private static long MAX_RESERVATION_SIZE

repos

private final FarragoRepos repos

mofId

private final String mofId

increment

private long increment

min

private long min

max

private long max

cycle

private boolean cycle

ascending

private boolean ascending

reserved

private boolean reserved

nextReservedValue

private Long nextReservedValue

lastReservedValue

private long lastReservedValue
Constructor Detail

FarragoSequenceAccessor

protected FarragoSequenceAccessor(FarragoRepos repos,
                                  String sequenceMofId)
Constructs a FarragoSequenceAccessor

Parameters:
repos - the farrago repository containing the sequence
sequenceMofId - the id of the sequence within the repository
Method Detail

loadSequence

private void loadSequence(FemSequenceGenerator sequence)
Initializes the sequence accessor from a sequence.

Parameters:
sequence - up to date sequence

closeAllocation

public void closeAllocation()
Deallocates unused sequence values.

Specified by:
closeAllocation in interface ClosableAllocation
Overrides:
closeAllocation in class CompoundClosableAllocation

getNext

public long getNext()
Retrieves a value from the sequence, possibly reserving more values in the process.

Returns:
the value retrieved
Throws:
EigenbaseException - if the sequence has no more values

alterSequence

public void alterSequence(FarragoSequenceOptions options,
                          RelDataType dataType)
Modifies a sequence and loads updated fields.

Parameters:
options - specifies fields to be modified
dataType - the data type of the sequence

reserve

private void reserve()
Reserves up to MAX_RESERVATION_SIZE values in the sequence. Updates the baseValue of a sequence in the catalog sequence to the first valid unreserved value.

If the reservation was successful, then nextReservedValue will be set to a non-null value.


reserveInternal

private void reserveInternal()

unreserve

private void unreserve()
Returns values unused by the sequence accessor to the catalog


getSequence

private FemSequenceGenerator getSequence()
Retrieves the underlying sequence from the catalog

Returns:
the underlying sequence, or null if the sequence was deleted

getName

private String getName()
Returns the name of the sequence