Top Description Inners Constructors Methods
org.apache.derby.impl.sql.execute

public final Class DeferredConstraintsMemory

extends Object
Class Inheritance
Imports
java.sql.ResultSet, java.util.ArrayList, .Enumeration, .HashMap, .HashSet, .List, .Set, org.apache.derby.catalog.UUID, org.apache.derby.shared.common.error.StandardException, org.apache.derby.iapi.services.io.FormatableBitSet, org.apache.derby.iapi.sql.Activation, .PreparedStatement, org.apache.derby.iapi.sql.conn.LanguageConnectionContext, .SQLSessionContext, .StatementContext, org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor, .DataDictionary, .ForeignKeyConstraintDescriptor, .KeyConstraintDescriptor, .ReferencedKeyConstraintDescriptor, .SchemaDescriptor, .TableDescriptor, org.apache.derby.iapi.sql.execute.ExecRow, org.apache.derby.iapi.store.access.BackingStoreHashtable, .ScanController, .TransactionController, org.apache.derby.iapi.types.DataValueDescriptor, .RowLocation, .SQLRef, org.apache.derby.shared.common.reference.SQLState

This class provides support for deferrable constraints. When the constraint mode is deferred, any violation of the constraint should not be flagged until the constraint mode is switched back to immediate, which may happen by explicitly setting the constraint mode to immediate, or implicitly at commit time. It may also happen implicitly when returning from a stored procedure if the constraint mode is immediate in the caller context.

The approach taken in Derby to support deferred constraints is to make a note when the violation happens (at insert or update time), and then remember that violation until the mode switches back as described above. We note exactly which rows cause violations, so checking can happen as quickly as possible when we get there. The core mechanism used to remember the violations as well as the deferred checking is embodied in this class.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
DeferredConstraintsMemory.CheckInfo

Info needed for check constraints

private static class
private static class
DeferredConstraintsMemory.UniquePkInfo

Info needed for unique and primary key constraints

public abstract static class
DeferredConstraintsMemory.ValidationInfo

Class hierarchy carrying the information we need to validate some deferred constraint.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static void
public static Enumeration<Object>
getDeferredCheckConstraintLocations(Activation activation, UUID validatingBaseTableUUID)

private static BackingStoreHashtable
makeDeferredHashTable(final TransactionController tc, final int cols)

public static BackingStoreHashtable

Returns:

the hash table (for caching by client to minimize lookups)
rememberCheckViolations
(final LanguageConnectionContext
the language connection context
lcc
,
UUID
the id of the target table
basetableId
,
final String
the schema of the target table
schemaName
,
final String
the target table name
tableName
,
BackingStoreHashtable
client cached value
deferredCheckViolations
,
final List<UUID>
offending constraint(s)
violatingCheckConstraints
,
final RowLocation
the duplicate row to be saved in the hash table for later checking
offendingRow
,
DeferredConstraintsMemory.CheckInfo[]
OUT parameter: the allocated CheckInfo
result
)

Save the row location of an offending row (one or more check constraints were violated) in a hash table (which may spill to disk) for later checking, typically on transaction commit, or upon request.

public static BackingStoreHashtable

Returns:

the hash table (for caching by client to minimize lookups)
rememberDuplicate
(final LanguageConnectionContext
the language connection context
lcc
,
BackingStoreHashtable
client cached value
deferredRowsHashTable
,
UUID
the id of the unique or primary key constraint
constraintId
,
final DataValueDescriptor[]
the duplicate row to be saved in the hash table for later checking
insertRow
)

Save the contents of an constraint supporting index row in a hash table (which may spill to disk) for later checking, typically on transaction commit, or upon request.

public static BackingStoreHashtable

Returns:

value to cache
rememberFKViolation
(final LanguageConnectionContext
the language connection context
lcc
,
BackingStoreHashtable
cached client copy
deferredRowsHashTable
,
final UUID
the UUID of the foreign key constraint
fkId
,
final DataValueDescriptor[]
the row in the supporting index which contains the key which is not present in the referenced index.
indexRow
,
String
the schema of the table
schemaName
,
String
the table being modified that has a FK.
tableName
)

Make note of a violated foreign key constraint, i.e. the referenced key is not present

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DeferredConstraintsMemoryback to summary
public DeferredConstraintsMemory()

Method Detail

compressOrTruncateback to summary
public static void compressOrTruncate(LanguageConnectionContext lcc, UUID tableId, String tableName) throws StandardException
getDeferredCheckConstraintLocationsback to summary
public static Enumeration<Object> getDeferredCheckConstraintLocations(Activation activation, UUID validatingBaseTableUUID) throws StandardException
makeDeferredHashTableback to summary
private static BackingStoreHashtable makeDeferredHashTable(final TransactionController tc, final int cols) throws StandardException
rememberCheckViolationsback to summary
public static BackingStoreHashtable rememberCheckViolations(final LanguageConnectionContext lcc, UUID basetableId, final String schemaName, final String tableName, BackingStoreHashtable deferredCheckViolations, final List<UUID> violatingCheckConstraints, final RowLocation offendingRow, DeferredConstraintsMemory.CheckInfo[] result) throws StandardException

Save the row location of an offending row (one or more check constraints were violated) in a hash table (which may spill to disk) for later checking, typically on transaction commit, or upon request. The row locations are subject to invalidation, cf. CheckInfo#setInvalidatedRowLocations.

Parameters
lcc:LanguageConnectionContext

the language connection context

basetableId:UUID

the id of the target table

schemaName:String

the schema of the target table

tableName:String

the target table name

deferredCheckViolations:BackingStoreHashtable

client cached value

violatingCheckConstraints:List<UUID>

offending constraint(s)

offendingRow:RowLocation

the duplicate row to be saved in the hash table for later checking

result:DeferredConstraintsMemory.CheckInfo[]

OUT parameter: the allocated CheckInfo

Returns:BackingStoreHashtable

the hash table (for caching by client to minimize lookups)

Exceptions
StandardException:
standard error policy
rememberDuplicateback to summary
public static BackingStoreHashtable rememberDuplicate(final LanguageConnectionContext lcc, BackingStoreHashtable deferredRowsHashTable, UUID constraintId, final DataValueDescriptor[] insertRow) throws StandardException

Save the contents of an constraint supporting index row in a hash table (which may spill to disk) for later checking, typically on transaction commit, or upon request.

Parameters
lcc:LanguageConnectionContext

the language connection context

deferredRowsHashTable:BackingStoreHashtable

client cached value

constraintId:UUID

the id of the unique or primary key constraint

insertRow:DataValueDescriptor[]

the duplicate row to be saved in the hash table for later checking

Returns:BackingStoreHashtable

the hash table (for caching by client to minimize lookups)

Exceptions
StandardException:
standard error policy
rememberFKViolationback to summary
public static BackingStoreHashtable rememberFKViolation(final LanguageConnectionContext lcc, BackingStoreHashtable deferredRowsHashTable, final UUID fkId, final DataValueDescriptor[] indexRow, String schemaName, String tableName) throws StandardException

Make note of a violated foreign key constraint, i.e. the referenced key is not present

Parameters
lcc:LanguageConnectionContext

the language connection context

deferredRowsHashTable:BackingStoreHashtable

cached client copy

fkId:UUID

the UUID of the foreign key constraint

indexRow:DataValueDescriptor[]

the row in the supporting index which contains the key which is not present in the referenced index.

schemaName:String

the schema of the table

tableName:String

the table being modified that has a FK.

Returns:BackingStoreHashtable

value to cache

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

public Class DeferredConstraintsMemory.CheckInfo

extends ValidationInfo
Class Inheritance

Info needed for check constraints

Field Summary

Modifier and TypeField and Description
private List<UUID>
private boolean
private final String
private final String
Inherited from org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.ValidationInfo:
infoRows

Constructor Summary

AccessConstructor and Description
public
CheckInfo(final BackingStoreHashtable infoRows, final String schemaName, final String tableName, final List<UUID> culprits)

Method Summary

Modifier and TypeMethod and Description
public void
addCulprits(List<UUID> newCulprits)

public List<UUID>
public boolean
public void
public void
private void
validateCheck(final LanguageConnectionContext lcc, final UUID constraintId, final boolean rollbackOnError)

public final void
validateConstraint(LanguageConnectionContext
The language connection context
lcc
,
UUID
If not null, check only for this constraint. This is used when switching mode to immediate. If null, we check all check constraints, i.e. at commit or if we have SET CONSTRAINT ALL IMMEDIATE.
constraintId
,
boolean
If true and a violation occurs, throw and exception that will cause rollback.
rollbackOnError
)

Implements abstract org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.ValidationInfo.validateConstraint.

Validate one or more CHECK constraints on a table.

Field Detail

culpritsback to summary
private List<UUID> culprits
invalidatedDueToCompressback to summary
private boolean invalidatedDueToCompress
schemaNameback to summary
private final String schemaName
tableNameback to summary
private final String tableName

Constructor Detail

CheckInfoback to summary
public CheckInfo(final BackingStoreHashtable infoRows, final String schemaName, final String tableName, final List<UUID> culprits)

Method Detail

addCulpritsback to summary
public void addCulprits(List<UUID> newCulprits)
getCulprintsback to summary
public List<UUID> getCulprints()
isInvalidatedback to summary
public boolean isInvalidated()
possiblyValidateOnReturnback to summary
public void possiblyValidateOnReturn(LanguageConnectionContext lcc, SQLSessionContext nested, SQLSessionContext caller) throws StandardException

Implements abstract org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.ValidationInfo.possiblyValidateOnReturn.

Annotations
@Override
setInvalidatedRowLocationsback to summary
public void setInvalidatedRowLocations()
validateCheckback to summary
private void validateCheck(final LanguageConnectionContext lcc, final UUID constraintId, final boolean rollbackOnError) throws StandardException
validateConstraintback to summary
public final void validateConstraint(LanguageConnectionContext lcc, UUID constraintId, boolean rollbackOnError) throws StandardException

Implements abstract org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.ValidationInfo.validateConstraint.

Validate one or more CHECK constraints on a table.

Implementation Note

We remember violations for a row by storing its row location in a disk based hash table, similar to what we do for the index location for primary key and unique constraints. As far as which constraints caused which violations, constraints are only presently remembered as having caused "one or more violations", i.e. for any violating row, we do not know at checking time exactly which constraint caused a problem for that exact row. So, for any given constraint which was violated in the transaction we visit all rows which had one or more violations and check again. This could be improved upon by remembering for each violating row the exact set of constraint(s) that saw a violation. Still, this is much faster than a full table scan in most use cases. We use a special internal query option to achieve this. The row locations may not be usable if Derby does a compress or otherwise makes them invalid. In that case we resort to a full table scan.

Parameters
lcc:LanguageConnectionContext

The language connection context

constraintId:UUID

If not null, check only for this constraint. This is used when switching mode to immediate. If null, we check all check constraints, i.e. at commit or if we have SET CONSTRAINT ALL IMMEDIATE.

rollbackOnError:boolean

If true and a violation occurs, throw and exception that will cause rollback.

Annotations
@Override
Exceptions
StandardException:
Default error policy
See Also
ValidateCheckConstraintResultSet
org.apache.derby.impl.sql.execute back to summary

private Class DeferredConstraintsMemory.ForeignKeyInfo

extends ValidationInfo
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final UUID
fkId

the UUID of the foreign constraint

private final String
private final String
Inherited from org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.ValidationInfo:
infoRows

Constructor Summary

AccessConstructor and Description
public
ForeignKeyInfo(final BackingStoreHashtable infoRows, UUID fkId, String schemaName, String tableName)

Method Summary

Modifier and TypeMethod and Description
public UUID
public void
public final void
private void
validateForeignKey(LanguageConnectionContext lcc, boolean rollbackOnError)

Field Detail

fkIdback to summary
private final UUID fkId

the UUID of the foreign constraint

schemaNameback to summary
private final String schemaName
tableNameback to summary
private final String tableName

Constructor Detail

ForeignKeyInfoback to summary
public ForeignKeyInfo(final BackingStoreHashtable infoRows, UUID fkId, String schemaName, String tableName)

Method Detail

getFkIdback to summary
public UUID getFkId()
possiblyValidateOnReturnback to summary
public void possiblyValidateOnReturn(LanguageConnectionContext lcc, SQLSessionContext nested, SQLSessionContext caller) throws StandardException

Implements abstract org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.ValidationInfo.possiblyValidateOnReturn.

Annotations
@Override
validateConstraintback to summary
public final void validateConstraint(LanguageConnectionContext lcc, UUID constraintId, boolean rollbackOnError) throws StandardException

Implements abstract org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.ValidationInfo.validateConstraint.

Annotations
@Override
validateForeignKeyback to summary
private void validateForeignKey(LanguageConnectionContext lcc, boolean rollbackOnError) throws StandardException
org.apache.derby.impl.sql.execute back to summary

private Class DeferredConstraintsMemory.UniquePkInfo

extends ValidationInfo
Class Inheritance

Info needed for unique and primary key constraints

Field Summary

Modifier and TypeField and Description
private final UUID
Inherited from org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.ValidationInfo:
infoRows

Constructor Summary

AccessConstructor and Description
public
UniquePkInfo(BackingStoreHashtable infoRows, UUID constraintId)

Method Summary

Modifier and TypeMethod and Description
public final void
public final void
validateConstraint(LanguageConnectionContext
The language connection context
lcc
,
UUID
Not used by this constraint type
constraintId
,
boolean
true if we should roll back the transaction if we see a violation of the constraint
rollbackOnError
)

Implements abstract org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.ValidationInfo.validateConstraint.

Validate one primary key or unique constraint

private void
validateUniquePK(final LanguageConnectionContext lcc, final BackingStoreHashtable ht, final boolean rollbackOnError)

Field Detail

constraintIdback to summary
private final UUID constraintId

Constructor Detail

UniquePkInfoback to summary
public UniquePkInfo(BackingStoreHashtable infoRows, UUID constraintId)

Method Detail

possiblyValidateOnReturnback to summary
public final void possiblyValidateOnReturn(LanguageConnectionContext lcc, SQLSessionContext nested, SQLSessionContext caller) throws StandardException

Implements abstract org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.ValidationInfo.possiblyValidateOnReturn.

Annotations
@Override
validateConstraintback to summary
public final void validateConstraint(LanguageConnectionContext lcc, UUID constraintId, boolean rollbackOnError) throws StandardException

Implements abstract org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.ValidationInfo.validateConstraint.

Validate one primary key or unique constraint

Parameters
lcc:LanguageConnectionContext

The language connection context

constraintId:UUID

Not used by this constraint type

rollbackOnError:boolean

true if we should roll back the transaction if we see a violation of the constraint

Annotations
@Override
validateUniquePKback to summary
private void validateUniquePK(final LanguageConnectionContext lcc, final BackingStoreHashtable ht, final boolean rollbackOnError) throws StandardException
org.apache.derby.impl.sql.execute back to summary

public abstract Class DeferredConstraintsMemory.ValidationInfo

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.ValidationInfo
Known Direct Subclasses
org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.UniquePkInfo, org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.CheckInfo, org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.ForeignKeyInfo

Class hierarchy carrying the information we need to validate some deferred constraint. For unique and primary key constraints, we only need the index row. For check constraints we need the target table name, schema name and some other info also.

Field Summary

Modifier and TypeField and Description
public final BackingStoreHashtable

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public abstract void
public abstract void
validateConstraint(LanguageConnectionContext lcc, UUID constraintId, boolean rollbackOnError)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

infoRowsback to summary
public final BackingStoreHashtable infoRows

Constructor Detail

ValidationInfoback to summary
public ValidationInfo(final BackingStoreHashtable infoRows)

Method Detail

possiblyValidateOnReturnback to summary
public abstract void possiblyValidateOnReturn(LanguageConnectionContext lcc, SQLSessionContext nested, SQLSessionContext caller) throws StandardException
validateConstraintback to summary
public abstract void validateConstraint(LanguageConnectionContext lcc, UUID constraintId, boolean rollbackOnError) throws StandardException