Top Description Fields Constructors Methods
org.apache.derby.impl.sql.catalog

public Class SequenceGenerator

extends Object
Class Inheritance
Imports
org.apache.derby.catalog.SequencePreallocator, org.apache.derby.shared.common.error.StandardException, org.apache.derby.shared.common.reference.SQLState

This is a generic machine for pre-allocating ranges of sequence numbers in order to improve concurrency. The public methods are synchronized and should be brief. The caller of the methods in this class is responsible for updating values on disk when the generator is exhausted or when it needs to allocate a new range of values.

The most used method in this class is getCurrentValueAndAdvance(). This method returns the next number in the range managed by the sequence generator. This method will raise an exception if the sequence generator is exhausted. Otherwise getCurrentValueAndAdvance() hands back a tuple of return values:

( status, currentValue, lastAllocatedValue, numberOfValuesAllocated )

The status field takes the following values:

It may happen that getCurrentValueAndAdvance() tells its caller to allocate a new range of sequence numbers in the system catalog. If the caller successfully allocates a new range, the caller should call allocateNewRange() to tell the generator to update its internal memory of that range.

The peekAtCurrentValue() method is provided so that unused, pre-allocated values can be flushed when the sequence generator is being discarded. The caller updates the catalog with the value returned by peekAtCurrentValue(). The peekAtCurrentValue() method is also called by the syscs_peek_at_sequence() function which users should call rather than try to scan the underlying catalog themselves.

Field Summary

Modifier and TypeField and Description
private final boolean
private long
private final long
private boolean
private final long
private final long
private final SequencePreallocator
private long
private final long
private final String
private final String
private final boolean
public static final int
public static final int
public static final int
public static final int
public static final int
private static final int
PREALLOCATION_THRESHHOLD

If pre-allocation drops below this level, then we need to grab another chunk of numbers

public static final int
public static final int
public static final int
public static final int

Constructor Summary

AccessConstructor and Description
public
SequenceGenerator(Long currentValue, boolean canCycle, long increment, long maxValue, long minValue, long restartValue, String schemaName, String sequenceName, SequencePreallocator sequencePreallocator)

Normal constructor

Method Summary

Modifier and TypeMethod and Description
private void
advanceValue(long[]
Array of return values to fill in: see CVAA_* constants
retval
)

Advance the sequence generator.

public synchronized void
allocateNewRange(long expectedCurrentValue, long numberOfAllocatedValues)

Allocate a new range.

public synchronized SequenceGenerator
clone(boolean
True if the clone should be reset to start at the beginning instead of at the current value.
restart
)

Clone this sequence generator.

public synchronized SequenceGenerator
clone(Long
New value to start with.
newStartValue
)

Clone this sequence generator.

private void
computeNewAllocation(long
INPUT Used to compute how many values need to be allocated
oldCurrentValue
,
long[]
OUTPUT Array of values to fill in (see CVAA_* constants)
retval
)

Compute the number of values to allocate.

private int
computePreAllocationCount()

This method returns the number of values to pre-allocate when we grab a new chunk of values.

private long
computeRemainingValues(long oldCurrentValue)

Get the number of values remaining until we bump against an endpoint of the legal range of values.

public synchronized long[]

Returns:

Returns an array of longs indexed by the CVAA_* constants.
getCurrentValueAndAdvance
()

Get the next sequence number managed by this generator and advance the number.

public synchronized String
getName()

Get the name of this sequence generator.

public synchronized String
getSchemaName()

Get the name of the schema of this sequence generator.

private void
markExhausted(long[] retval)

Mark the generator as exhausted.

private boolean
overflowed(long originalValue, long incrementedValue)

Return true if an overflow/underflow occurred.

public synchronized Long
peekAtCurrentValue()

Peek at the current value of the sequence generator without advancing the generator.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_CAN_CYCLEback to summary
private final boolean _CAN_CYCLE
_currentValueback to summary
private long _currentValue
_INCREMENTback to summary
private final long _INCREMENT
_isExhaustedback to summary
private boolean _isExhausted
_MAX_VALUEback to summary
private final long _MAX_VALUE
_MIN_VALUEback to summary
private final long _MIN_VALUE
_PREALLOCATORback to summary
private final SequencePreallocator _PREALLOCATOR
_remainingPreallocatedValuesback to summary
private long _remainingPreallocatedValues
_RESTART_VALUEback to summary
private final long _RESTART_VALUE
_SCHEMA_NAMEback to summary
private final String _SCHEMA_NAME
_SEQUENCE_NAMEback to summary
private final String _SEQUENCE_NAME
_STEP_INCREASESback to summary
private final boolean _STEP_INCREASES
CVAA_CURRENT_VALUEback to summary
public static final int CVAA_CURRENT_VALUE
CVAA_LAST_ALLOCATED_VALUEback to summary
public static final int CVAA_LAST_ALLOCATED_VALUE
CVAA_LENGTHback to summary
public static final int CVAA_LENGTH
CVAA_NUMBER_OF_VALUES_ALLOCATEDback to summary
public static final int CVAA_NUMBER_OF_VALUES_ALLOCATED
CVAA_STATUSback to summary
public static final int CVAA_STATUS
PREALLOCATION_THRESHHOLDback to summary
private static final int PREALLOCATION_THRESHHOLD

If pre-allocation drops below this level, then we need to grab another chunk of numbers

RET_ALLOCATE_NEW_VALUESback to summary
public static final int RET_ALLOCATE_NEW_VALUES
RET_I_AM_CONFUSEDback to summary
public static final int RET_I_AM_CONFUSED
RET_MARK_EXHAUSTEDback to summary
public static final int RET_MARK_EXHAUSTED
RET_OKback to summary
public static final int RET_OK

Constructor Detail

SequenceGeneratorback to summary
public SequenceGenerator(Long currentValue, boolean canCycle, long increment, long maxValue, long minValue, long restartValue, String schemaName, String sequenceName, SequencePreallocator sequencePreallocator)

Normal constructor

Method Detail

advanceValueback to summary
private void advanceValue(long[] retval) throws StandardException

Advance the sequence generator. Pre-allocate a range of new values if necessary.

Parameters
retval:long[]

Array of return values to fill in: see CVAA_* constants

allocateNewRangeback to summary
public synchronized void allocateNewRange(long expectedCurrentValue, long numberOfAllocatedValues)

Allocate a new range. Is a NOP if the current value is not what we expected. See the class header comment for more information on how this method is used.

cloneback to summary
public synchronized SequenceGenerator clone(boolean restart)

Clone this sequence generator. This method supports the special bulk-insert optimization in InsertResultSet.

Parameters
restart:boolean

True if the clone should be reset to start at the beginning instead of at the current value.

cloneback to summary
public synchronized SequenceGenerator clone(Long newStartValue)

Clone this sequence generator. This method supports the special bulk-insert optimization in InsertResultSet.

Parameters
newStartValue:Long

New value to start with.

computeNewAllocationback to summary
private void computeNewAllocation(long oldCurrentValue, long[] retval) throws StandardException

Compute the number of values to allocate. The range may wrap around.

Parameters
oldCurrentValue:long

INPUT Used to compute how many values need to be allocated

retval:long[]

OUTPUT Array of values to fill in (see CVAA_* constants)

Exceptions
StandardException:
if any error occurs.
computePreAllocationCountback to summary
private int computePreAllocationCount()

This method returns the number of values to pre-allocate when we grab a new chunk of values. This is a bit of defensive coding to cover the case when the sequence's parameters are absurdly large.

computeRemainingValuesback to summary
private long computeRemainingValues(long oldCurrentValue)

Get the number of values remaining until we bump against an endpoint of the legal range of values. This is a positive number and so may understate the number of remaining values if the datatype is BIGINT.

getCurrentValueAndAdvanceback to summary
public synchronized long[] getCurrentValueAndAdvance() throws StandardException

Get the next sequence number managed by this generator and advance the number. Could raise an exception if the legal range is exhausted and wrap-around is not allowed--that is, if NO CYCLE was specified when the sequence was defined. See the class header comment for a description of how this method operates.

Returns:long[]

Returns an array of longs indexed by the CVAA_* constants.

getNameback to summary
public synchronized String getName()

Get the name of this sequence generator. Technically, this doesn't need to be synchronized. But it is simpler to just maintain a rule that all public methods should be synchronized.

getSchemaNameback to summary
public synchronized String getSchemaName()

Get the name of the schema of this sequence generator. Technically, this doesn't need to be synchronized. But it is simpler to just maintain a rule that all public methods should be synchronized.

markExhaustedback to summary
private void markExhausted(long[] retval)

Mark the generator as exhausted.

overflowedback to summary
private boolean overflowed(long originalValue, long incrementedValue)

Return true if an overflow/underflow occurred. This happens if the originalValue and incrementedValue have opposite sign. Overflow also occurs if the incrementedValue falls outside the range of the sequence.

peekAtCurrentValueback to summary
public synchronized Long peekAtCurrentValue()

Peek at the current value of the sequence generator without advancing the generator. Returns null if the generator is exhausted.