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

public abstract Class SequenceUpdater

extends Object
implements Cacheable
Class Inheritance
All Implemented Interfaces
org.apache.derby.iapi.services.cache.Cacheable
Known Direct Subclasses
org.apache.derby.impl.sql.catalog.SequenceUpdater.SyssequenceUpdater, org.apache.derby.impl.sql.catalog.SequenceUpdater.BulkInsertUpdater
Imports
java.util.HashMap, org.apache.derby.catalog.SequencePreallocator, org.apache.derby.iapi.db.Database, org.apache.derby.shared.common.error.StandardException, org.apache.derby.shared.common.reference.Property, .SQLState, org.apache.derby.iapi.services.cache.Cacheable, .CacheManager, org.apache.derby.iapi.services.context.ContextManager, .Context, .ContextService, org.apache.derby.shared.common.i18n.MessageService, org.apache.derby.iapi.services.monitor.Monitor, org.apache.derby.iapi.services.property.PropertyUtil, org.apache.derby.shared.common.sanity.SanityManager, org.apache.derby.iapi.sql.conn.LanguageConnectionContext, org.apache.derby.iapi.sql.dictionary.BulkInsertCounter, .SchemaDescriptor, .SequenceDescriptor, org.apache.derby.iapi.store.access.AccessFactory, .TransactionController, org.apache.derby.iapi.types.NumberDataValue, .RowLocation

An object cached in the data dictionary which manages new values for sequences. Note that this class must be public and have a 0-arg constructor in order to satisfy the Cacheable contract.

This is the abstract superclass of specific implementations for specific sequences. For instance, one subclass handles the ANSI/ISO sequences stored in SYSSEQUENCES. Another subclass could handle the sequences stored in Derby's identity columns.

This class does a couple tricky things:

Here is the algorithm pursued when the caller asks for the next number in a sequence:

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
SequenceUpdater.BulkInsertUpdater

Implementation of SequenceUpdater for use with the bulk-insert optimization used by InsertResultSet.

public static class
SequenceUpdater.SyssequenceUpdater

Specific implementation of SequenceUpdater for the sequences managed by SYSSEQUENCES.

Field Summary

Modifier and TypeField and Description
protected DataDictionaryImpl
protected SequenceGenerator
_sequenceGenerator

This is the object which allocates ranges of sequence values

protected String
_uuidString

This is the key used to lookup this generator in the cache.

Constructor Summary

AccessConstructor and Description
public
SequenceUpdater()

No-arg constructor to satisfy the Cacheable contract

public
SequenceUpdater(DataDictionaryImpl dd)

Normal constructor

Method Summary

Modifier and TypeMethod and Description
public void
clean(boolean
True if the object will be removed
forRemove
)

Implements org.apache.derby.iapi.services.cache.Cacheable.clean.

Clean the object.

public void
clearIdentity()

Implements org.apache.derby.iapi.services.cache.Cacheable.clearIdentity.

Put the object into the No Identity state.

public Cacheable
createIdentity(Object
The handle on the cacheable
key
,
Object
Creation details
createParameter
)

Implements org.apache.derby.iapi.services.cache.Cacheable.createIdentity.

Create a new item.

protected abstract SequenceGenerator
createSequenceGenerator(TransactionController readOnlyTC)

Initialize the sequence generator.

public synchronized SequenceUpdater.BulkInsertUpdater
getBulkInsertUpdater(boolean
True if the counter should be re-initialized to its start position.
restart
)

Get the SequenceUpdater used for the bulk-insert optimization in InsertResultSet.

private static Context
getContextOrNull(final String contextID)

Must be private so that user code can't call this entry point.

private static ContextService
getContextService()

Must be private so that user code can't call this entry point.

public synchronized void
getCurrentValueAndAdvance(NumberDataValue
This value is stuffed with the new sequence number.
returnValue
)

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

public Object
getIdentity()

Implements org.apache.derby.iapi.services.cache.Cacheable.getIdentity.

Get the identity of this object.

private static LanguageConnectionContext
public boolean
isDirty()

Implements org.apache.derby.iapi.services.cache.Cacheable.isDirty.

Returns true if the object is dirty.

private boolean
protected SequencePreallocator
makePreallocator(TransactionController tc)

Make a new range allocator (called when the generator is instantiated)

private StandardException
missingAllocator(String propertyName, String className, Exception e)

public Long
peekAtCurrentValue()

Get the current value of the sequence generator without advancing it.

public synchronized void
reset(Long newValue)

Reset the sequence generator to a new start value.

public Cacheable
setIdentity(Object
The handle on the cacheable
key
)

Implements org.apache.derby.iapi.services.cache.Cacheable.setIdentity.

Set the identity of the object.

private StandardException
tooMuchContentionException()

Create an exception to state that there is too much contention on the generator.

private StandardException
unimplementedFeature()

Report an unimplemented feature

protected abstract boolean

Returns:

Returns true if the value was successfully updated, false if we lost a race with another session.
updateCurrentValueOnDisk
(TransactionController
The transaction to use
tc
,
Long
Expected value on disk for this sequence
oldValue
,
Long
The value to poke into the system table backing this sequence
newValue
,
boolean
Whether to wait for a lock
wait
)

Update the sequence value on disk.

public synchronized boolean

Returns:

Returns true if the value was successfully updated, false if we lost a race with another session.
updateCurrentValueOnDisk
(Long oldValue, Long newValue)

Update the value on disk.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_ddback to summary
protected DataDictionaryImpl _dd
_sequenceGeneratorback to summary
protected SequenceGenerator _sequenceGenerator

This is the object which allocates ranges of sequence values

_uuidStringback to summary
protected String _uuidString

This is the key used to lookup this generator in the cache.

Constructor Detail

SequenceUpdaterback to summary
public SequenceUpdater()

No-arg constructor to satisfy the Cacheable contract

SequenceUpdaterback to summary
public SequenceUpdater(DataDictionaryImpl dd)

Normal constructor

Method Detail

cleanback to summary
public void clean(boolean forRemove) throws StandardException

Implements org.apache.derby.iapi.services.cache.Cacheable.clean.

Doc from org.apache.derby.iapi.services.cache.Cacheable.clean.

Clean the object. It is up to the object to ensure synchronization of the isDirty() and clean() method calls.
If forRemove is true then the object is being removed due to an explict remove request, in this case the cache manager will have called this method regardless of the state of the isDirty()
If an exception is thrown the object must be left in the clean state.
MT - thread safe - Can be called at any time by the cache manager, it is the responsibility of the object implementing Cacheable to ensure any users of the object do not conflict with the clean call.

Parameters
forRemove:boolean

True if the object will be removed

Exceptions
StandardException:
Standard Derby error policy.
clearIdentityback to summary
public void clearIdentity()

Implements org.apache.derby.iapi.services.cache.Cacheable.clearIdentity.

Doc from org.apache.derby.iapi.services.cache.Cacheable.clearIdentity.

Put the object into the No Identity state.
MT - single thread required - Method must only be called be cache manager and the cache manager will guarantee only one thread can be calling it.

createIdentityback to summary
public Cacheable createIdentity(Object key, Object createParameter) throws StandardException

Implements org.apache.derby.iapi.services.cache.Cacheable.createIdentity.

Doc from org.apache.derby.iapi.services.cache.Cacheable.createIdentity.

Create a new item.

Create a new item and set the identity of the object to represent it. The object will be in the No Identity state, ie. it will have just been created or clearIdentity() was just called.
The object must copy the information out of key, not just store a reference to key if the key is not immutable. After this call the expression getIdentity().equals(key) must return true.

If the class of the object needs to change (e.g. to support a different format) then the object should create a new object, call its initParameter() with the parameters the original object was called with, set its identity and return a reference to it. The cache manager will discard the reference to the old object.
If an exception is thrown the object must be left in the no-identity state.
MT - single thread required - Method must only be called be cache manager and the cache manager will guarantee only one thread can be calling it.

Parameters
key:Object

The handle on the cacheable

createParameter:Object

Creation details

Returns:Cacheable

an object reference if the object can take on the identity, null otherwise.

Exceptions
StandardException:
If forCreate is true and the object cannot be created.
createSequenceGeneratorback to summary
protected abstract SequenceGenerator createSequenceGenerator(TransactionController readOnlyTC) throws StandardException

Initialize the sequence generator. Work is done inside a read-only subtransaction of the session's execution transaction.

getBulkInsertUpdaterback to summary
public synchronized SequenceUpdater.BulkInsertUpdater getBulkInsertUpdater(boolean restart) throws StandardException

Get the SequenceUpdater used for the bulk-insert optimization in InsertResultSet.

Parameters
restart:boolean

True if the counter should be re-initialized to its start position.

getContextOrNullback to summary
private static Context getContextOrNull(final String contextID)

Must be private so that user code can't call this entry point.

getContextServiceback to summary
private static ContextService getContextService()

Must be private so that user code can't call this entry point.

getCurrentValueAndAdvanceback to summary
public synchronized void getCurrentValueAndAdvance(NumberDataValue returnValue) 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. Only one thread at a time is allowed through here. We do not want a race between the two calls to the sequence generator: getCurrentValueAndAdvance() and allocateNewRange().

Parameters
returnValue:NumberDataValue

This value is stuffed with the new sequence number.

getIdentityback to summary
public Object getIdentity()

Implements org.apache.derby.iapi.services.cache.Cacheable.getIdentity.

Doc from org.apache.derby.iapi.services.cache.Cacheable.getIdentity.

Get the identity of this object.
MT - thread safe.

Returns:Object

the object identity

getLCCback to summary
private static LanguageConnectionContext getLCC()
isDirtyback to summary
public boolean isDirty()

Implements org.apache.derby.iapi.services.cache.Cacheable.isDirty.

Doc from org.apache.derby.iapi.services.cache.Cacheable.isDirty.

Returns true if the object is dirty. May be called when the object is kept or unkept.
MT - thread safe

Returns:boolean

true if the object is dirty

isNumberback to summary
private boolean isNumber(String text)
makePreallocatorback to summary
protected SequencePreallocator makePreallocator(TransactionController tc) throws StandardException

Make a new range allocator (called when the generator is instantiated)

missingAllocatorback to summary
private StandardException missingAllocator(String propertyName, String className, Exception e)
peekAtCurrentValueback to summary
public Long peekAtCurrentValue() throws StandardException

Get the current value of the sequence generator without advancing it. May return null if the generator is exhausted.

resetback to summary
public synchronized void reset(Long newValue) throws StandardException

Reset the sequence generator to a new start value. This is used by the special bulk-insert optimization in InsertResultSet.

setIdentityback to summary
public Cacheable setIdentity(Object key) throws StandardException

Implements org.apache.derby.iapi.services.cache.Cacheable.setIdentity.

Doc from org.apache.derby.iapi.services.cache.Cacheable.setIdentity.

Set the identity of the object.

Set the identity of the object to represent an item that already exists, e.g. an existing container. The object will be in the No Identity state, ie. it will have just been created or clearIdentity() was just called.
The object must copy the information out of key, not just store a reference to key. After this call the expression getIdentity().equals(key) must return true.
If the class of the object needs to change (e.g. to support a different format) then the object should create a new object, call its initParameter() with the parameters the original object was called with, set its identity and return a reference to it. The cache manager will discard the reference to the old object.
If an exception is thrown the object must be left in the no-identity state.
MT - single thread required - Method must only be called be cache manager and the cache manager will guarantee only one thread can be calling it.

Parameters
key:Object

The handle on the cacheable

Returns:Cacheable

an object reference if the object can take on the identity, null otherwise.

Exceptions
StandardException:
Thrown on error
See Also
Cacheable#setIdentity
tooMuchContentionExceptionback to summary
private StandardException tooMuchContentionException()

Create an exception to state that there is too much contention on the generator. For backward compatibility reasons, different messages are needed by sequences and identities. See DERBY-5426.

unimplementedFeatureback to summary
private StandardException unimplementedFeature()

Report an unimplemented feature

updateCurrentValueOnDiskback to summary
protected abstract boolean updateCurrentValueOnDisk(TransactionController tc, Long oldValue, Long newValue, boolean wait) throws StandardException

Update the sequence value on disk. This method does its work in a subtransaction of the user's execution transaction.

Parameters
tc:TransactionController

The transaction to use

oldValue:Long

Expected value on disk for this sequence

newValue:Long

The value to poke into the system table backing this sequence

wait:boolean

Whether to wait for a lock

Returns:boolean

Returns true if the value was successfully updated, false if we lost a race with another session.

Exceptions
StandardException:
May throw an exception if a lock can't be obtained.
updateCurrentValueOnDiskback to summary
public synchronized boolean updateCurrentValueOnDisk(Long oldValue, Long newValue) throws StandardException

Update the value on disk. Does its work in a subtransaction of the user's execution transaction. If that fails, raises a TOO MUCH CONTENTION exception.

Returns:boolean

Returns true if the value was successfully updated, false if we lost a race with another session.

org.apache.derby.impl.sql.catalog back to summary

public final Class SequenceUpdater.BulkInsertUpdater

extends SequenceUpdater
implements BulkInsertCounter
Class Inheritance
All Implemented Interfaces
org.apache.derby.iapi.sql.dictionary.BulkInsertCounter

Implementation of SequenceUpdater for use with the bulk-insert optimization used by InsertResultSet. This BulkInsertUpdater doesn't really write to disk. It is assumed that the BulkInsertUpdater will only be used by the bulk-insert code, where the user has exclusive write-access on the table whose identity column is backed by the original SequenceUpdater. At the end of bulk-insert, the current value of the BulkInsertUpdater is written to disk by other code.

Field Summary

Inherited from org.apache.derby.impl.sql.catalog.SequenceUpdater:
_dd_sequenceGenerator_uuidString

Constructor Summary

AccessConstructor and Description
public
public
BulkInsertUpdater(SequenceUpdater originalUpdater, boolean restart)

Method Summary

Modifier and TypeMethod and Description
protected SequenceGenerator
protected boolean
updateCurrentValueOnDisk(TransactionController
The transaction to use
tc
,
Long
Expected value on disk for this sequence
oldValue
,
Long
The value to poke into the system table backing this sequence
newValue
,
boolean
Whether to wait for a lock
wait
)

Implements abstract org.apache.derby.impl.sql.catalog.SequenceUpdater.updateCurrentValueOnDisk.

Update the sequence value on disk.

Inherited from org.apache.derby.impl.sql.catalog.SequenceUpdater:
cleanclearIdentitycreateIdentitygetBulkInsertUpdatergetCurrentValueAndAdvancegetIdentityisDirtymakePreallocatorpeekAtCurrentValueresetsetIdentityupdateCurrentValueOnDisk

Constructor Detail

BulkInsertUpdaterback to summary
public BulkInsertUpdater()
BulkInsertUpdaterback to summary
public BulkInsertUpdater(SequenceUpdater originalUpdater, boolean restart)

Method Detail

createSequenceGeneratorback to summary
protected SequenceGenerator createSequenceGenerator(TransactionController readOnlyTC) throws StandardException

Implements abstract org.apache.derby.impl.sql.catalog.SequenceUpdater.createSequenceGenerator.

Doc from org.apache.derby.impl.sql.catalog.SequenceUpdater.createSequenceGenerator.

Initialize the sequence generator. Work is done inside a read-only subtransaction of the session's execution transaction.

updateCurrentValueOnDiskback to summary
protected boolean updateCurrentValueOnDisk(TransactionController tc, Long oldValue, Long newValue, boolean wait) throws StandardException

Implements abstract org.apache.derby.impl.sql.catalog.SequenceUpdater.updateCurrentValueOnDisk.

Doc from org.apache.derby.impl.sql.catalog.SequenceUpdater.updateCurrentValueOnDisk.

Update the sequence value on disk. This method does its work in a subtransaction of the user's execution transaction.

Parameters
tc:TransactionController

The transaction to use

oldValue:Long

Expected value on disk for this sequence

newValue:Long

The value to poke into the system table backing this sequence

wait:boolean

Whether to wait for a lock

Returns:boolean

Returns true if the value was successfully updated, false if we lost a race with another session.

Exceptions
StandardException:
May throw an exception if a lock can't be obtained.
org.apache.derby.impl.sql.catalog back to summary

public final Class SequenceUpdater.SyssequenceUpdater

extends SequenceUpdater
Class Inheritance

Specific implementation of SequenceUpdater for the sequences managed by SYSSEQUENCES.

Field Summary

Modifier and TypeField and Description
private RowLocation
Inherited from org.apache.derby.impl.sql.catalog.SequenceUpdater:
_dd_sequenceGenerator_uuidString

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

Modifier and TypeMethod and Description
protected SequenceGenerator
protected boolean
updateCurrentValueOnDisk(TransactionController
The transaction to use
tc
,
Long
Expected value on disk for this sequence
oldValue
,
Long
The value to poke into the system table backing this sequence
newValue
,
boolean
Whether to wait for a lock
wait
)

Implements abstract org.apache.derby.impl.sql.catalog.SequenceUpdater.updateCurrentValueOnDisk.

Update the sequence value on disk.

Inherited from org.apache.derby.impl.sql.catalog.SequenceUpdater:
cleanclearIdentitycreateIdentitygetBulkInsertUpdatergetCurrentValueAndAdvancegetIdentityisDirtymakePreallocatorpeekAtCurrentValueresetsetIdentityupdateCurrentValueOnDisk

Field Detail

_sequenceRowLocationback to summary
private RowLocation _sequenceRowLocation

Constructor Detail

SyssequenceUpdaterback to summary
public SyssequenceUpdater()
SyssequenceUpdaterback to summary
public SyssequenceUpdater(DataDictionaryImpl dd)

Method Detail

createSequenceGeneratorback to summary
protected SequenceGenerator createSequenceGenerator(TransactionController readOnlyTC) throws StandardException

Implements abstract org.apache.derby.impl.sql.catalog.SequenceUpdater.createSequenceGenerator.

Doc from org.apache.derby.impl.sql.catalog.SequenceUpdater.createSequenceGenerator.

Initialize the sequence generator. Work is done inside a read-only subtransaction of the session's execution transaction.

updateCurrentValueOnDiskback to summary
protected boolean updateCurrentValueOnDisk(TransactionController tc, Long oldValue, Long newValue, boolean wait) throws StandardException

Implements abstract org.apache.derby.impl.sql.catalog.SequenceUpdater.updateCurrentValueOnDisk.

Doc from org.apache.derby.impl.sql.catalog.SequenceUpdater.updateCurrentValueOnDisk.

Update the sequence value on disk. This method does its work in a subtransaction of the user's execution transaction.

Parameters
tc:TransactionController

The transaction to use

oldValue:Long

Expected value on disk for this sequence

newValue:Long

The value to poke into the system table backing this sequence

wait:boolean

Whether to wait for a lock

Returns:boolean

Returns true if the value was successfully updated, false if we lost a race with another session.

Exceptions
StandardException:
May throw an exception if a lock can't be obtained.