use of org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor in project derby by apache.
the class AlterTableConstantAction method modifyColumnConstraint.
/**
* Workhorse for modifying column level constraints.
* Right now it is restricted to modifying a null constraint to a not null
* constraint.
*/
private void modifyColumnConstraint(String colName, boolean nullability) throws StandardException {
ColumnDescriptor columnDescriptor = td.getColumnDescriptor(colName);
// Get the type and change the nullability
DataTypeDescriptor dataType = columnDescriptor.getType().getNullabilityType(nullability);
// check if there are any unique constraints to update
ConstraintDescriptorList cdl = dd.getConstraintDescriptors(td);
int columnPostion = columnDescriptor.getPosition();
for (int i = 0; i < cdl.size(); i++) {
ConstraintDescriptor cd = cdl.elementAt(i);
if (cd.getConstraintType() == DataDictionary.UNIQUE_CONSTRAINT) {
ColumnDescriptorList columns = cd.getColumnDescriptors();
for (int count = 0; count < columns.size(); count++) {
if (columns.elementAt(count).getPosition() != columnPostion)
break;
// get backing index
ConglomerateDescriptor desc = td.getConglomerateDescriptor(cd.getConglomerateId());
// not null ie is backed by unique index
if (!(desc.getIndexDescriptor().isUnique() || desc.getIndexDescriptor().hasDeferrableChecking())) {
break;
}
// replace backing index with a unique when not null index.
recreateUniqueConstraintBackingIndexAsUniqueWhenNotNull(desc, td, activation, lcc);
}
}
}
ColumnDescriptor newColumnDescriptor = new ColumnDescriptor(colName, columnDescriptor.getPosition(), dataType, columnDescriptor.getDefaultValue(), columnDescriptor.getDefaultInfo(), td, columnDescriptor.getDefaultUUID(), columnDescriptor.getAutoincStart(), columnDescriptor.getAutoincInc(), columnDescriptor.getAutoincCycle());
// Update the ColumnDescriptor with new default info
dd.dropColumnDescriptor(td.getUUID(), colName, tc);
dd.addDescriptor(newColumnDescriptor, td, DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
}
use of org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor in project derby by apache.
the class AlterTableConstantAction method dropStatistics.
/**
* Drop statistics of either all the indexes on the table or only one
* specific index depending on what user has requested.
*
* @throws StandardException
*/
private void dropStatistics() throws StandardException {
td = dd.getTableDescriptor(tableId);
dd.startWriting(lcc);
dm.invalidateFor(td, DependencyManager.UPDATE_STATISTICS, lcc);
if (dropStatisticsAll) {
dd.dropStatisticsDescriptors(td.getUUID(), null, tc);
} else {
ConglomerateDescriptor cd = dd.getConglomerateDescriptor(indexNameForStatistics, sd, false);
dd.dropStatisticsDescriptors(td.getUUID(), cd.getUUID(), tc);
}
}
use of org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor in project derby by apache.
the class AlterTableConstantAction method updateIndex.
private void updateIndex(long newHeapConglom, DataDictionary dd, int index, long[] newIndexCongloms) throws StandardException {
Properties properties = new Properties();
// Get the ConglomerateDescriptor for the index
ConglomerateDescriptor cd = td.getConglomerateDescriptor(indexConglomerateNumbers[index]);
// Build the properties list for the new conglomerate
ConglomerateController indexCC = tc.openConglomerate(indexConglomerateNumbers[index], false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_TABLE, TransactionController.ISOLATION_SERIALIZABLE);
// Get the properties on the old index
indexCC.getInternalTablePropertySet(properties);
/* Create the properties that language supplies when creating the
* the index. (The store doesn't preserve these.)
*/
int indexRowLength = indexRows[index].nColumns();
properties.put("baseConglomerateId", Long.toString(newHeapConglom));
if (cd.getIndexDescriptor().isUnique()) {
properties.put("nUniqueColumns", Integer.toString(indexRowLength - 1));
} else {
properties.put("nUniqueColumns", Integer.toString(indexRowLength));
}
if (cd.getIndexDescriptor().isUniqueWithDuplicateNulls() && !cd.getIndexDescriptor().hasDeferrableChecking()) {
properties.put("uniqueWithDuplicateNulls", Boolean.toString(true));
}
properties.put("rowLocationColumn", Integer.toString(indexRowLength - 1));
properties.put("nKeyFields", Integer.toString(indexRowLength));
indexCC.close();
// We can finally drain the sorter and rebuild the index
// Populate the index.
RowLocationRetRowSource cCount;
boolean statisticsExist = false;
if (!truncateTable) {
sorters[index].completedInserts();
sorters[index] = null;
if (td.statisticsExist(cd)) {
cCount = new CardinalityCounter(tc.openSortRowSource(sortIds[index]));
statisticsExist = true;
} else {
cCount = new CardinalityCounter(tc.openSortRowSource(sortIds[index]));
}
newIndexCongloms[index] = tc.createAndLoadConglomerate("BTREE", indexRows[index].getRowArray(), ordering[index], collation[index], properties, TransactionController.IS_DEFAULT, cCount, (long[]) null);
// is not empty.
if (statisticsExist)
dd.dropStatisticsDescriptors(td.getUUID(), cd.getUUID(), tc);
long numRows;
if ((numRows = ((CardinalityCounter) cCount).getRowCount()) > 0) {
long[] c = ((CardinalityCounter) cCount).getCardinality();
for (int i = 0; i < c.length; i++) {
StatisticsDescriptor statDesc = new StatisticsDescriptor(dd, dd.getUUIDFactory().createUUID(), cd.getUUID(), td.getUUID(), "I", new StatisticsImpl(numRows, c[i]), i + 1);
dd.addDescriptor(statDesc, // no parent descriptor
null, DataDictionary.SYSSTATISTICS_CATALOG_NUM, // no error on duplicate.
true, tc);
}
}
} else {
newIndexCongloms[index] = tc.createConglomerate("BTREE", indexRows[index].getRowArray(), ordering[index], collation[index], properties, TransactionController.IS_DEFAULT);
// rowscount is zero and existing statistic will be invalid.
if (td.statisticsExist(cd))
dd.dropStatisticsDescriptors(td.getUUID(), cd.getUUID(), tc);
}
/* Update the DataDictionary
*
* Update sys.sysconglomerates with new conglomerate #, we need to
* update all (if any) duplicate index entries sharing this same
* conglomerate.
*/
dd.updateConglomerateDescriptor(td.getConglomerateDescriptors(indexConglomerateNumbers[index]), newIndexCongloms[index], tc);
// Drop the old conglomerate
tc.dropConglomerate(indexConglomerateNumbers[index]);
}
use of org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor in project derby by apache.
the class UpdateNode method makeConstantAction.
/**
* Compile constants that Execution will use
*
* @exception StandardException Thrown on failure
*/
@Override
public ConstantAction makeConstantAction() throws StandardException {
/*
** Updates are also deferred if they update a column in the index
** used to scan the table being updated.
*/
if (!deferred && !inMatchingClause()) {
ConglomerateDescriptor updateCD = targetTable.getTrulyTheBestAccessPath().getConglomerateDescriptor();
if (updateCD != null && updateCD.isIndex()) {
int[] baseColumns = updateCD.getIndexDescriptor().baseColumnPositions();
if (resultSet.getResultColumns().updateOverlaps(baseColumns)) {
deferred = true;
}
}
}
if (null == targetTableDescriptor) {
/* Return constant action for VTI
* NOTE: ConstantAction responsible for preserving instantiated
* VTIs for in-memory queries and for only preserving VTIs
* that implement Serializable for SPSs.
*/
return getGenericConstantActionFactory().getUpdatableVTIConstantAction(DeferModification.UPDATE_STATEMENT, deferred, changedColumnIds);
}
int lckMode = inMatchingClause() ? TransactionController.MODE_RECORD : resultSet.updateTargetLockMode();
long heapConglomId = targetTableDescriptor.getHeapConglomerateId();
TransactionController tc = getLanguageConnectionContext().getTransactionCompile();
StaticCompiledOpenConglomInfo[] indexSCOCIs = new StaticCompiledOpenConglomInfo[indexConglomerateNumbers.length];
for (int index = 0; index < indexSCOCIs.length; index++) {
indexSCOCIs[index] = tc.getStaticCompiledConglomInfo(indexConglomerateNumbers[index]);
}
/*
** Do table locking if the table's lock granularity is
** set to table.
*/
if (targetTableDescriptor.getLockGranularity() == TableDescriptor.TABLE_LOCK_GRANULARITY) {
lckMode = TransactionController.MODE_TABLE;
}
return getGenericConstantActionFactory().getUpdateConstantAction(targetTableDescriptor, tc.getStaticCompiledConglomInfo(heapConglomId), indicesToMaintain, indexConglomerateNumbers, indexSCOCIs, indexNames, deferred, targetTableDescriptor.getUUID(), lckMode, false, changedColumnIds, null, null, getFKInfo(), getTriggerInfo(), (readColsBitSet == null) ? (FormatableBitSet) null : new FormatableBitSet(readColsBitSet), getReadColMap(targetTableDescriptor.getNumberOfColumns(), readColsBitSet), resultColumnList.getStreamStorableColIds(targetTableDescriptor.getNumberOfColumns()), (readColsBitSet == null) ? targetTableDescriptor.getNumberOfColumns() : readColsBitSet.getNumBitsSet(), positionedUpdate, resultSet.isOneRowResultSet(), autoincRowLocation, inMatchingClause(), identitySequenceUUIDString);
}
use of org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor in project derby by apache.
the class RenameConstantAction method execGutsRenameIndex.
// do necessary work for rename index at execute time.
private void execGutsRenameIndex(TableDescriptor td, Activation activation) throws StandardException {
LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
DataDictionary dd = lcc.getDataDictionary();
DependencyManager dm = dd.getDependencyManager();
TransactionController tc = lcc.getTransactionExecute();
// for indexes, we only invalidate sps, rest we ignore(ie views)
dm.invalidateFor(td, DependencyManager.RENAME_INDEX, lcc);
ConglomerateDescriptor conglomerateDescriptor = dd.getConglomerateDescriptor(oldObjectName, sd, true);
if (conglomerateDescriptor == null)
throw StandardException.newException(SQLState.LANG_INDEX_NOT_FOUND_DURING_EXECUTION, oldObjectName);
/* Drop the index descriptor */
dd.dropConglomerateDescriptor(conglomerateDescriptor, tc);
// Change the index name of the index descriptor
conglomerateDescriptor.setConglomerateName(newObjectName);
// add the index descriptor with new name
dd.addDescriptor(conglomerateDescriptor, sd, DataDictionary.SYSCONGLOMERATES_CATALOG_NUM, false, tc);
}
Aggregations