Search in sources :

Example 21 with ConglomerateDescriptor

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);
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) CheckConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.CheckConstraintDescriptor) ForeignKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor) ConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) ConstraintDescriptorList(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)

Example 22 with ConglomerateDescriptor

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);
    }
}
Also used : ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)

Example 23 with ConglomerateDescriptor

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]);
}
Also used : StatisticsImpl(org.apache.derby.catalog.types.StatisticsImpl) StatisticsDescriptor(org.apache.derby.iapi.sql.dictionary.StatisticsDescriptor) ConglomerateController(org.apache.derby.iapi.store.access.ConglomerateController) Properties(java.util.Properties) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor) RowLocationRetRowSource(org.apache.derby.iapi.store.access.RowLocationRetRowSource)

Example 24 with ConglomerateDescriptor

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);
}
Also used : FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) StaticCompiledOpenConglomInfo(org.apache.derby.iapi.store.access.StaticCompiledOpenConglomInfo) TransactionController(org.apache.derby.iapi.store.access.TransactionController) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)

Example 25 with ConglomerateDescriptor

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);
}
Also used : LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) TransactionController(org.apache.derby.iapi.store.access.TransactionController) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)

Aggregations

ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)66 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)19 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)17 TransactionController (org.apache.derby.iapi.store.access.TransactionController)13 Properties (java.util.Properties)12 UUID (org.apache.derby.catalog.UUID)12 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)12 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)12 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)11 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)11 IndexRowGenerator (org.apache.derby.iapi.sql.dictionary.IndexRowGenerator)10 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)10 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)8 ConglomerateController (org.apache.derby.iapi.store.access.ConglomerateController)8 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)8 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)7 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)7 RowLocation (org.apache.derby.iapi.types.RowLocation)6 ArrayList (java.util.ArrayList)5 IndexDescriptor (org.apache.derby.catalog.IndexDescriptor)5