Class SequenceUpdater
- All Implemented Interfaces:
Cacheable
- Direct Known Subclasses:
SequenceUpdater.BulkInsertUpdater
,SequenceUpdater.SyssequenceUpdater
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:
- It pre-allocates a range of values from a sequence so that we don't have to change the on-disk value every time we get the next value for a sequence.
- When updating the on-disk value, we use a subtransaction of the user's execution transaction. If the special transaction cannot do its work immediately, without waiting for a lock, then a TOO MUCH CONTENTION error is raised. It is believed that this can only happen if someone holds locks on SYSSEQUENCES, either via sequence DDL or a scan of the catalog. The TOO MUCH CONTENTION error tells the user to not scan SYSSEQUENCES directly, but to instead use the SYSCS_UTIL.SYSCS_PEEK_AT_SEQUENCE() if the user needs the current value of the sequence generator.
Here is the algorithm pursued when the caller asks for the next number in a sequence:
- We try to get the next number from a cache of pre-allocated numbers. The endpoint (last number in the pre-allocated range) was previously recorded in the catalog row which describes this sequence. If we are successful in getting the next number, we return it and all is well.
- Otherwise, we must allocate a new range by updating the catalog row. We should not be in contention with another connection because the update method is synchronized.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Implementation of SequenceUpdater for use with the bulk-insert optimization used by InsertResultSet.static final class
Specific implementation of SequenceUpdater for the sequences managed by SYSSEQUENCES. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected DataDictionaryImpl
protected SequenceGenerator
This is the object which allocates ranges of sequence valuesprotected String
This is the key used to lookup this generator in the cache. -
Constructor Summary
ConstructorsConstructorDescriptionNo-arg constructor to satisfy the Cacheable contractNormal constructor -
Method Summary
Modifier and TypeMethodDescriptionvoid
clean
(boolean forRemove) Clean the object.void
Put the object into the No Identity state.createIdentity
(Object key, Object createParameter) Create a new item.protected abstract SequenceGenerator
createSequenceGenerator
(TransactionController readOnlyTC) Initialize the sequence generator.getBulkInsertUpdater
(boolean restart) Get the SequenceUpdater used for the bulk-insert optimization in InsertResultSet.private static Context
getContextOrNull
(String contextID) Privileged lookup of a Context.private static ContextService
Privileged lookup of the ContextService.void
getCurrentValueAndAdvance
(NumberDataValue returnValue) Get the next sequence number managed by this generator and advance the number.Get the identity of this object.private static LanguageConnectionContext
getLCC()
boolean
isDirty()
Returns true of the object is dirty.private boolean
protected SequencePreallocator
Make a new range allocator (called when the generator is instantiated)private StandardException
missingAllocator
(String propertyName, String className, Exception e) Get the current value of the sequence generator without advancing it.void
Reset the sequence generator to a new start value.setIdentity
(Object key) Set the identity of the object.private StandardException
Create an exception to state that there is too much contention on the generator.private StandardException
Report an unimplemented featureboolean
updateCurrentValueOnDisk
(Long oldValue, Long newValue) Update the value on disk.protected abstract boolean
updateCurrentValueOnDisk
(TransactionController tc, Long oldValue, Long newValue, boolean wait) Update the sequence value on disk.
-
Field Details
-
_dd
-
_uuidString
This is the key used to lookup this generator in the cache. -
_sequenceGenerator
This is the object which allocates ranges of sequence values
-
-
Constructor Details
-
SequenceUpdater
public SequenceUpdater()No-arg constructor to satisfy the Cacheable contract -
SequenceUpdater
Normal constructor
-
-
Method Details
-
createSequenceGenerator
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.
- Throws:
StandardException
-
updateCurrentValueOnDisk
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
- The transaction to useoldValue
- Expected value on disk for this sequencenewValue
- The value to poke into the system table backing this sequencewait
- Whether to wait for a lock- Returns:
- Returns true if the value was successfully updated, false if we lost a race with another session.
- Throws:
StandardException
- May throw an exception if a lock can't be obtained.
-
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.
-
clean
Description copied from interface:Cacheable
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.- Specified by:
clean
in interfaceCacheable
- Throws:
StandardException
- Standard Derby error policy.
-
isDirty
public boolean isDirty()Description copied from interface:Cacheable
Returns true of the object is dirty. May be called when the object is kept or unkept.
MT - thread safe -
getIdentity
Description copied from interface:Cacheable
Get the identity of this object.
MT - thread safe.- Specified by:
getIdentity
in interfaceCacheable
-
clearIdentity
public void clearIdentity()Description copied from interface:Cacheable
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.- Specified by:
clearIdentity
in interfaceCacheable
-
createIdentity
Description copied from interface:Cacheable
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.- Specified by:
createIdentity
in interfaceCacheable
- Returns:
- an object reference if the object can take on the identity, null otherwise.
- Throws:
StandardException
- If forCreate is true and the object cannot be created.- See Also:
-
setIdentity
Description copied from interface:Cacheable
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.- Specified by:
setIdentity
in interfaceCacheable
- Returns:
- an object reference if the object can take on the identity, null otherwise.
- Throws:
StandardException
- Thrown on error- See Also:
-
reset
Reset the sequence generator to a new start value. This is used by the special bulk-insert optimization in InsertResultSet.
- Throws:
StandardException
-
getBulkInsertUpdater
public SequenceUpdater.BulkInsertUpdater getBulkInsertUpdater(boolean restart) throws StandardException Get the SequenceUpdater used for the bulk-insert optimization in InsertResultSet.
- Parameters:
restart
- True if the counter should be re-initialized to its start position.- Throws:
StandardException
-
getCurrentValueAndAdvance
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
- This value is stuffed with the new sequence number.- Throws:
StandardException
-
peekAtCurrentValue
Get the current value of the sequence generator without advancing it. May return null if the generator is exhausted.
- Throws:
StandardException
-
updateCurrentValueOnDisk
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:
- Returns true if the value was successfully updated, false if we lost a race with another session.
- Throws:
StandardException
-
makePreallocator
Make a new range allocator (called when the generator is instantiated)- Throws:
StandardException
-
missingAllocator
-
isNumber
-
getLCC
-
unimplementedFeature
Report an unimplemented feature -
getContextService
Privileged lookup of the ContextService. Must be private so that user code can't call this entry point. -
getContextOrNull
Privileged lookup of a Context. Must be private so that user code can't call this entry point.
-