Search in sources :

Example 6 with SortObserver

use of org.apache.derby.iapi.store.access.SortObserver in project derby by apache.

the class InsertResultSet method setUpAllSorts.

/**
 * Set up to update all of the indexes on a table when doing a bulk insert
 * on an empty table.
 *
 * @exception StandardException					thrown on error
 */
private void setUpAllSorts(ExecRow sourceRow, RowLocation rl) throws StandardException {
    int numIndexes = constants.irgs.length;
    int numColumns = td.getNumberOfColumns();
    ordering = new ColumnOrdering[numIndexes][];
    collation = new int[numIndexes][];
    needToDropSort = new boolean[numIndexes];
    sortIds = new long[numIndexes];
    rowSources = new RowLocationRetRowSource[numIndexes];
    // indexedCols is 1-based
    indexedCols = new FormatableBitSet(numColumns + 1);
    /* For each index, build a single index row, collation templage, 
         * and a sorter. 
         */
    for (int index = 0; index < numIndexes; index++) {
        // Update the bit map of indexed columns
        int[] keyColumns = constants.irgs[index].baseColumnPositions();
        for (int i2 = 0; i2 < keyColumns.length; i2++) {
            // indexedCols is 1-based
            indexedCols.set(keyColumns[i2]);
        }
        // create a single index row template for each index
        indexRows[index] = constants.irgs[index].getIndexRowTemplate();
        // Get an index row based on the base row
        // (This call is only necessary here because we need to
        // pass a template to the sorter.)
        constants.irgs[index].getIndexRow(sourceRow, rl, indexRows[index], (FormatableBitSet) null);
        /* For non-unique indexes, we order by all columns + the RID.
			 * For unique indexes, we just order by the columns.
			 * We create a unique index observer for unique indexes
			 * so that we can catch duplicate key
			 */
        // Get the ConglomerateDescriptor for the index
        ConglomerateDescriptor cd = td.getConglomerateDescriptor(constants.indexCIDS[index]);
        int[] baseColumnPositions = constants.irgs[index].baseColumnPositions();
        boolean[] isAscending = constants.irgs[index].isAscending();
        int numColumnOrderings;
        SortObserver sortObserver;
        /* We can only reuse the wrappers when doing an
			 * external sort if there is only 1 index.  Otherwise,
			 * we could get in a situation where 1 sort reuses a
			 * wrapper that is still in use in another sort.
			 */
        boolean reuseWrappers = (numIndexes == 1);
        final IndexRowGenerator indDes = cd.getIndexDescriptor();
        Properties sortProperties = null;
        String indexOrConstraintName = cd.getConglomerateName();
        boolean deferred = false;
        boolean deferrable = false;
        UUID uniqueDeferrableConstraintId = null;
        if (cd.isConstraint()) {
            // so, the index is backing up a constraint
            ConstraintDescriptor conDesc = dd.getConstraintDescriptor(td, cd.getUUID());
            indexOrConstraintName = conDesc.getConstraintName();
            deferred = lcc.isEffectivelyDeferred(lcc.getCurrentSQLSessionContext(activation), conDesc.getUUID());
            deferrable = conDesc.deferrable();
            uniqueDeferrableConstraintId = conDesc.getUUID();
        }
        if (indDes.isUnique() || indDes.isUniqueDeferrable()) {
            numColumnOrderings = indDes.isUnique() ? baseColumnPositions.length : baseColumnPositions.length + 1;
            sortObserver = new UniqueIndexSortObserver(lcc, uniqueDeferrableConstraintId, // don't clone rows
            false, deferrable, deferred, indexOrConstraintName, indexRows[index], reuseWrappers, td.getName());
        } else if (indDes.isUniqueWithDuplicateNulls()) {
            numColumnOrderings = baseColumnPositions.length + 1;
            // tell transaction controller to use the unique with
            // duplicate nulls sorter, when making createSort() call.
            sortProperties = new Properties();
            sortProperties.put(AccessFactoryGlobals.IMPL_TYPE, AccessFactoryGlobals.SORT_UNIQUEWITHDUPLICATENULLS_EXTERNAL);
            // use sort operator which treats nulls unequal
            sortObserver = new UniqueWithDuplicateNullsIndexSortObserver(lcc, uniqueDeferrableConstraintId, true, deferrable, deferred, indexOrConstraintName, indexRows[index], true, td.getName());
        } else {
            numColumnOrderings = baseColumnPositions.length + 1;
            sortObserver = new BasicSortObserver(false, false, indexRows[index], reuseWrappers);
        }
        ordering[index] = new ColumnOrdering[numColumnOrderings];
        for (int ii = 0; ii < isAscending.length; ii++) {
            ordering[index][ii] = new IndexColumnOrder(ii, isAscending[ii]);
        }
        if (numColumnOrderings > isAscending.length) {
            ordering[index][isAscending.length] = new IndexColumnOrder(isAscending.length);
        }
        // set collation templates for later index creation
        // call (createAndLoadConglomerate())
        collation[index] = constants.irgs[index].getColumnCollationIds(td.getColumnDescriptorList());
        // create the sorters
        sortIds[index] = tc.createSort(sortProperties, indexRows[index].getRowArrayClone(), ordering[index], sortObserver, // not in order
        false, // est rows
        (int) sourceResultSet.getEstimatedRowCount(), // est row size, -1 means no idea
        -1);
        needToDropSort[index] = true;
    }
    sorters = new SortController[numIndexes];
    // Open the sorts
    for (int index = 0; index < numIndexes; index++) {
        sorters[index] = tc.openSort(sortIds[index]);
        needToDropSort[index] = true;
    }
}
Also used : Properties(java.util.Properties) LanguageProperties(org.apache.derby.iapi.sql.LanguageProperties) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor) SortObserver(org.apache.derby.iapi.store.access.SortObserver) IndexRowGenerator(org.apache.derby.iapi.sql.dictionary.IndexRowGenerator) ConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) UUID(org.apache.derby.catalog.UUID)

Aggregations

Properties (java.util.Properties)6 SortObserver (org.apache.derby.iapi.store.access.SortObserver)6 ExecIndexRow (org.apache.derby.iapi.sql.execute.ExecIndexRow)4 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)4 UUID (org.apache.derby.catalog.UUID)3 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)3 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)3 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)3 IndexRowGenerator (org.apache.derby.iapi.sql.dictionary.IndexRowGenerator)3 SortController (org.apache.derby.iapi.store.access.SortController)3 LanguageProperties (org.apache.derby.iapi.sql.LanguageProperties)2 ColumnOrdering (org.apache.derby.iapi.store.access.ColumnOrdering)2 ConglomerateController (org.apache.derby.iapi.store.access.ConglomerateController)2 TransactionController (org.apache.derby.iapi.store.access.TransactionController)2 StatisticsImpl (org.apache.derby.catalog.types.StatisticsImpl)1 ClassFactory (org.apache.derby.iapi.services.loader.ClassFactory)1 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)1 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)1 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)1 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)1