Search in sources :

Example 31 with UUID

use of org.apache.derby.catalog.UUID in project derby by apache.

the class BasicDependencyManager method clearInMemoryDependency.

/**
 * Clear the specified in memory dependency.
 * This is useful for clean-up when an exception occurs.
 * (We clear all in-memory dependencies added in the current
 * StatementContext.)
 */
public synchronized void clearInMemoryDependency(Dependency dy) {
    final UUID deptId = dy.getDependent().getObjectID();
    final UUID provId = dy.getProviderKey();
    List deps = (List) dependents.get(deptId);
    // to ensure the dependency manager is consistent.
    if (!SanityManager.DEBUG) {
        // dependency has already been removed
        if (deps == null)
            return;
    }
    List provs = (List) providers.get(provId);
    if (SanityManager.DEBUG) {
        // if both are null then everything is OK
        if ((deps != null) || (provs != null)) {
            // ensure that the Dependency dy is either
            // in both lists or in neither. Even if dy
            // is out of the list we can have non-null
            // deps and provs here because other dependencies
            // with the the same providers or dependents can exist
            // 
            int depCount = 0;
            if (deps != null) {
                for (int ci = 0; ci < deps.size(); ci++) {
                    if (dy.equals(deps.get(ci)))
                        depCount++;
                }
            }
            int provCount = 0;
            if (provs != null) {
                for (int ci = 0; ci < provs.size(); ci++) {
                    if (dy.equals(provs.get(ci)))
                        provCount++;
                }
            }
            SanityManager.ASSERT(depCount == provCount, "Dependency count mismatch count in deps: " + depCount + ", count in provs " + provCount + ", dy.getDependent().getObjectID() = " + deptId + ", dy.getProvider().getObjectID() = " + provId);
        }
        // matches code that is protected by !DEBUG above
        if (deps == null)
            return;
    }
    // dependency has already been removed
    if (provs == null)
        return;
    deps.remove(dy);
    if (deps.isEmpty())
        dependents.remove(deptId);
    provs.remove(dy);
    if (provs.isEmpty())
        providers.remove(provId);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ProviderList(org.apache.derby.iapi.sql.depend.ProviderList) UUID(org.apache.derby.catalog.UUID)

Example 32 with UUID

use of org.apache.derby.catalog.UUID in project derby by apache.

the class BasicDependencyManager method addDependencyToTable.

// 
// class implementation
// 
/**
 * Add a new dependency to the specified table if it does not
 * already exist in that table.
 *
 * @return boolean		Whether or not the dependency get added.
 */
private boolean addDependencyToTable(Map<UUID, List<Dependency>> table, UUID key, Dependency dy) {
    List<Dependency> deps = table.get(key);
    if (deps == null) {
        deps = new ArrayList<Dependency>();
        deps.add(dy);
        table.put(key, deps);
    } else {
        /* Make sure that we're not adding a duplicate dependency */
        UUID provKey = dy.getProvider().getObjectID();
        UUID depKey = dy.getDependent().getObjectID();
        for (ListIterator<Dependency> depsIT = deps.listIterator(); depsIT.hasNext(); ) {
            Dependency curDY = depsIT.next();
            // 
            if (curDY.getProvider().getObjectID() == null) {
                continue;
            }
            if (curDY.getProvider().getObjectID().equals(provKey) && curDY.getDependent().getObjectID().equals(depKey)) {
                return false;
            }
        }
        deps.add(dy);
    }
    if (SanityManager.DEBUG) {
        if (SanityManager.DEBUG_ON("memoryLeakTrace")) {
            if (table.size() > 100)
                System.out.println("memoryLeakTrace:BasicDependencyManager:table " + table.size());
            if (deps.size() > 50)
                System.out.println("memoryLeakTrace:BasicDependencyManager:deps " + deps.size());
        }
    }
    return true;
}
Also used : Dependency(org.apache.derby.iapi.sql.depend.Dependency) UUID(org.apache.derby.catalog.UUID)

Example 33 with UUID

use of org.apache.derby.catalog.UUID in project derby by apache.

the class BasicDependencyManager method clearDependencies.

/**
 * @inheritDoc
 */
public void clearDependencies(LanguageConnectionContext lcc, Dependent d, TransactionController tc) throws StandardException {
    UUID id = d.getObjectID();
    // Remove all the stored dependencies.
    if (d.isPersistent()) {
        boolean wait = (tc == null);
        dd.dropDependentsStoredDependencies(id, ((wait) ? lcc.getTransactionExecute() : tc), wait);
    }
    // Now remove the in-memory dependencies, if any.
    synchronized (this) {
        List deps = (List) dependents.get(id);
        if (deps != null) {
            Iterator depsIter = deps.iterator();
            // the dependency from their lists
            while (depsIter.hasNext()) {
                Dependency dy = (Dependency) depsIter.next();
                clearProviderDependency(dy.getProviderKey(), dy);
            }
            dependents.remove(id);
        }
    }
}
Also used : ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ProviderList(org.apache.derby.iapi.sql.depend.ProviderList) Dependency(org.apache.derby.iapi.sql.depend.Dependency) UUID(org.apache.derby.catalog.UUID)

Example 34 with UUID

use of org.apache.derby.catalog.UUID in project derby by apache.

the class TableElementList method genColumnInfos.

/**
 * Fill in the ColumnInfo[] for this table element list.
 *
 * @param colInfos	The ColumnInfo[] to be filled in.
 *
 * @return int		The number of constraints in the create table.
 */
int genColumnInfos(ColumnInfo[] colInfos) throws StandardException {
    int numConstraints = 0;
    int size = size();
    for (int index = 0; index < size; index++) {
        if (elementAt(index).getElementType() == TableElementNode.AT_DROP_COLUMN) {
            String columnName = elementAt(index).getName();
            colInfos[index] = new ColumnInfo(columnName, td.getColumnDescriptor(columnName).getType(), null, null, null, null, null, ColumnInfo.DROP, 0, 0, false, 0);
            break;
        }
        if (!(elementAt(index) instanceof ColumnDefinitionNode)) {
            if (SanityManager.DEBUG) {
                SanityManager.ASSERT(elementAt(index) instanceof ConstraintDefinitionNode, "elementAt(index) expected to be instanceof " + "ConstraintDefinitionNode");
            }
            /* Remember how many constraints we've seen */
            numConstraints++;
            continue;
        }
        ColumnDefinitionNode coldef = (ColumnDefinitionNode) elementAt(index);
        // 
        // Generated columns may depend on functions mentioned in their
        // generation clauses.
        // 
        ProviderList apl = null;
        ProviderInfo[] providerInfos = null;
        if (coldef.hasGenerationClause()) {
            apl = coldef.getGenerationClauseNode().getAuxiliaryProviderList();
        }
        if (apl != null && apl.size() > 0) {
            DependencyManager dm = getDataDictionary().getDependencyManager();
            providerInfos = dm.getPersistentProviderInfos(apl);
        }
        colInfos[index - numConstraints] = new ColumnInfo(coldef.getColumnName(), coldef.getType(), coldef.getDefaultValue(), coldef.getDefaultInfo(), providerInfos, (UUID) null, coldef.getOldDefaultUUID(), coldef.getAction(), (coldef.isAutoincrementColumn() ? coldef.getAutoincrementStart() : 0), (coldef.isAutoincrementColumn() ? coldef.getAutoincrementIncrement() : 0), (coldef.isAutoincrementColumn() ? coldef.getAutoincrementCycle() : false), (coldef.isAutoincrementColumn() ? coldef.getAutoinc_create_or_modify_Start_Increment() : -1));
        /* Remember how many constraints that we've seen */
        if (coldef.hasConstraint()) {
            numConstraints++;
        }
    }
    return numConstraints;
}
Also used : ProviderList(org.apache.derby.iapi.sql.depend.ProviderList) ProviderInfo(org.apache.derby.iapi.sql.depend.ProviderInfo) ColumnInfo(org.apache.derby.impl.sql.execute.ColumnInfo) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) UUID(org.apache.derby.catalog.UUID)

Example 35 with UUID

use of org.apache.derby.catalog.UUID in project derby by apache.

the class CreateConstraintConstantAction method executeConstantAction.

// INTERFACE METHODS
/**
 *	This is the guts of the Execution-time logic for CREATE CONSTRAINT.
 *  <P>
 *  A constraint is represented as:
 *  <UL>
 *  <LI> ConstraintDescriptor.
 *  </UL>
 *  If a backing index is required then the index will
 *  be created through an CreateIndexConstantAction setup
 *  by the compiler.
 *  <BR>
 *  Dependencies are created as:
 *  <UL>
 *  <LI> ConstraintDescriptor depends on all the providers collected
 *  at compile time and passed into the constructor.
 *  <LI> For a FOREIGN KEY constraint ConstraintDescriptor depends
 *  on the ConstraintDescriptor for the referenced constraints
 *  and the privileges required to create the constraint.
 *  </UL>
 *
 *  @see ConstraintDescriptor
 *  @see CreateIndexConstantAction
 *	@see ConstantAction#executeConstantAction
 *
 * @exception StandardException		Thrown on failure
 */
public void executeConstantAction(Activation activation) throws StandardException {
    ConglomerateDescriptor conglomDesc = null;
    ConglomerateDescriptor[] conglomDescs = null;
    ConstraintDescriptor conDesc = null;
    TableDescriptor td = null;
    UUID indexId = null;
    String uniqueName;
    String backingIndexName;
    /* RESOLVE - blow off not null constraints for now (and probably for ever) */
    if (constraintType == DataDictionary.NOTNULL_CONSTRAINT) {
        return;
    }
    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    DependencyManager dm = dd.getDependencyManager();
    TransactionController tc = lcc.getTransactionExecute();
    cf = lcc.getLanguageConnectionFactory().getClassFactory();
    /*
		** Inform the data dictionary that we are about to write to it.
		** There are several calls to data dictionary "get" methods here
		** that might be done in "read" mode in the data dictionary, but
		** it seemed safer to do this whole operation in "write" mode.
		**
		** We tell the data dictionary we're done writing at the end of
		** the transaction.
		*/
    dd.startWriting(lcc);
    /* Table gets locked in AlterTableConstantAction */
    /*
		** If the schema descriptor is null, then
		** we must have just read ourselves in.  
		** So we will get the corresponding schema
		** descriptor from the data dictionary.
		*/
    SchemaDescriptor sd = dd.getSchemaDescriptor(schemaName, tc, true);
    /* Try to get the TableDescriptor from
		 * the Activation. We will go to the
		 * DD if not there. (It should always be
		 * there except when in a target.)
		 */
    td = activation.getDDLTableDescriptor();
    if (td == null) {
        /* tableId will be non-null if adding a
			 * constraint to an existing table.
			 */
        if (tableId != null) {
            td = dd.getTableDescriptor(tableId);
        } else {
            td = dd.getTableDescriptor(tableName, sd, tc);
        }
        if (td == null) {
            throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, tableName);
        }
        activation.setDDLTableDescriptor(td);
    }
    /* Generate the UUID for the backing index.  This will become the
		 * constraint's name, if no name was specified.
		 */
    UUIDFactory uuidFactory = dd.getUUIDFactory();
    UUID constrId = uuidFactory.createUUID();
    /* Create the index, if there's one for this constraint */
    if (indexAction != null) {
        if (indexAction.getIndexName() == null) {
            /* Set the index name */
            backingIndexName = uuidFactory.createUUID().toString();
            indexAction.setIndexName(backingIndexName);
        } else {
            backingIndexName = indexAction.getIndexName();
        }
        indexAction.setConstraintID(constrId);
        /* Create the index */
        indexAction.executeConstantAction(activation);
        /* Get the conglomerate descriptor for the backing index */
        conglomDescs = td.getConglomerateDescriptors();
        for (int index = 0; index < conglomDescs.length; index++) {
            conglomDesc = conglomDescs[index];
            /* Check for conglomerate being an index first, since
				 * name is null for heap.
				 */
            if (conglomDesc.isIndex() && backingIndexName.equals(conglomDesc.getConglomerateName())) {
                break;
            }
        }
        if (SanityManager.DEBUG) {
            SanityManager.ASSERT(conglomDesc != null, "conglomDesc is expected to be non-null after search for backing index");
            SanityManager.ASSERT(conglomDesc.isIndex(), "conglomDesc is expected to be indexable after search for backing index");
            SanityManager.ASSERT(conglomDesc.getConglomerateName().equals(backingIndexName), "conglomDesc name expected to be the same as backing index name after search for backing index");
        }
        indexId = conglomDesc.getUUID();
    }
    boolean[] defaults = new boolean[] { ConstraintDefinitionNode.DEFERRABLE_DEFAULT, ConstraintDefinitionNode.INITIALLY_DEFERRED_DEFAULT, ConstraintDefinitionNode.ENFORCED_DEFAULT };
    for (int i = 0; i < characteristics.length; i++) {
        if (characteristics[i] != defaults[i]) {
            dd.checkVersion(DataDictionary.DD_VERSION_DERBY_10_11, "DEFERRED CONSTRAINTS");
            if (constraintType == DataDictionary.NOTNULL_CONSTRAINT || !characteristics[2]) /* not enforced */
            {
                // Remove when feature DERBY-532 is completed
                if (!PropertyUtil.getSystemProperty("derby.constraintsTesting", "false").equals("true")) {
                    throw StandardException.newException(SQLState.NOT_IMPLEMENTED, "non-default constraint characteristics");
                }
            }
        }
    }
    /* Now, lets create the constraint descriptor */
    DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
    switch(constraintType) {
        case DataDictionary.PRIMARYKEY_CONSTRAINT:
            conDesc = ddg.newPrimaryKeyConstraintDescriptor(td, constraintName, // deferable,
            characteristics[0], // initiallyDeferred,
            characteristics[1], // int[],
            genColumnPositions(td, false), constrId, indexId, sd, characteristics[2], // referenceCount
            0);
            dd.addConstraintDescriptor(conDesc, tc);
            break;
        case DataDictionary.UNIQUE_CONSTRAINT:
            conDesc = ddg.newUniqueConstraintDescriptor(td, constraintName, // deferable,
            characteristics[0], // initiallyDeferred,
            characteristics[1], // int[],
            genColumnPositions(td, false), constrId, indexId, sd, characteristics[2], // referenceCount
            0);
            dd.addConstraintDescriptor(conDesc, tc);
            break;
        case DataDictionary.CHECK_CONSTRAINT:
            conDesc = ddg.newCheckConstraintDescriptor(td, constraintName, // deferable,
            characteristics[0], // initiallyDeferred,
            characteristics[1], constrId, constraintText, // int[],
            new ReferencedColumnsDescriptorImpl(genColumnPositions(td, false)), sd, characteristics[2]);
            dd.addConstraintDescriptor(conDesc, tc);
            storeConstraintDependenciesOnPrivileges(activation, conDesc, null, providerInfo);
            break;
        case DataDictionary.FOREIGNKEY_CONSTRAINT:
            ReferencedKeyConstraintDescriptor referencedConstraint = DDUtils.locateReferencedConstraint(dd, td, constraintName, columnNames, otherConstraintInfo);
            DDUtils.validateReferentialActions(dd, td, constraintName, otherConstraintInfo, columnNames);
            conDesc = ddg.newForeignKeyConstraintDescriptor(td, constraintName, // deferable,
            characteristics[0], // initiallyDeferred,
            characteristics[1], // int[],
            genColumnPositions(td, false), constrId, indexId, sd, referencedConstraint, characteristics[2], otherConstraintInfo.getReferentialActionDeleteRule(), otherConstraintInfo.getReferentialActionUpdateRule());
            // try to create the constraint first, because it
            // is expensive to do the bulk check, find obvious
            // errors first
            dd.addConstraintDescriptor(conDesc, tc);
            /* No need to do check if we're creating a 
				 * table.
				 */
            if ((!forCreateTable) && dd.activeConstraint(conDesc)) {
                validateFKConstraint(activation, tc, dd, (ForeignKeyConstraintDescriptor) conDesc, referencedConstraint, ((CreateIndexConstantAction) indexAction).getIndexTemplateRow());
            }
            /* Create stored dependency on the referenced constraint */
            dm.addDependency(conDesc, referencedConstraint, lcc.getContextManager());
            // store constraint's dependency on REFERENCES privileges in the dependeny system
            storeConstraintDependenciesOnPrivileges(activation, conDesc, referencedConstraint.getTableId(), providerInfo);
            break;
        case DataDictionary.MODIFY_CONSTRAINT:
            throw StandardException.newException(SQLState.NOT_IMPLEMENTED, "ALTER CONSTRAINT");
        default:
            if (SanityManager.DEBUG) {
                SanityManager.THROWASSERT("contraintType (" + constraintType + ") has unexpected value");
            }
            break;
    }
    /* Create stored dependencies for each provider */
    if (providerInfo != null) {
        for (int ix = 0; ix < providerInfo.length; ix++) {
            Provider provider = null;
            /* We should always be able to find the Provider */
            provider = (Provider) providerInfo[ix].getDependableFinder().getDependable(dd, providerInfo[ix].getObjectId());
            dm.addDependency(conDesc, provider, lcc.getContextManager());
        }
    }
    /* Finally, invalidate off of the table descriptor(s)
		 * to ensure that any dependent statements get
		 * re-compiled.
		 */
    if (!forCreateTable) {
        dm.invalidateFor(td, DependencyManager.CREATE_CONSTRAINT, lcc);
    }
    if (constraintType == DataDictionary.FOREIGNKEY_CONSTRAINT) {
        if (SanityManager.DEBUG) {
            SanityManager.ASSERT(conDesc != null, "conDesc expected to be non-null");
            if (!(conDesc instanceof ForeignKeyConstraintDescriptor)) {
                SanityManager.THROWASSERT("conDesc expected to be instance of ForeignKeyConstraintDescriptor, not " + conDesc.getClass().getName());
            }
        }
        dm.invalidateFor(((ForeignKeyConstraintDescriptor) conDesc).getReferencedConstraint().getTableDescriptor(), DependencyManager.CREATE_CONSTRAINT, lcc);
    }
    this.constraintId = constrId;
}
Also used : SchemaDescriptor(org.apache.derby.iapi.sql.dictionary.SchemaDescriptor) UUIDFactory(org.apache.derby.iapi.services.uuid.UUIDFactory) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) ReferencedColumnsDescriptorImpl(org.apache.derby.catalog.types.ReferencedColumnsDescriptorImpl) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor) ForeignKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor) Provider(org.apache.derby.iapi.sql.depend.Provider) DataDescriptorGenerator(org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) ForeignKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor) ConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) UUID(org.apache.derby.catalog.UUID) TransactionController(org.apache.derby.iapi.store.access.TransactionController)

Aggregations

UUID (org.apache.derby.catalog.UUID)101 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)31 DataDescriptorGenerator (org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator)23 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)22 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)21 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)19 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)15 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)15 SQLChar (org.apache.derby.iapi.types.SQLChar)15 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)14 TransactionController (org.apache.derby.iapi.store.access.TransactionController)14 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)12 SQLVarchar (org.apache.derby.iapi.types.SQLVarchar)12 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)11 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)10 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)10 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)10 ArrayList (java.util.ArrayList)9 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)9 ExecIndexRow (org.apache.derby.iapi.sql.execute.ExecIndexRow)8