Search in sources :

Example 66 with ConglomerateDescriptor

use of org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor in project derby by apache.

the class IndexStatisticsDaemonImpl method writeUpdatedStats.

/**
 * Writes updated statistics for the specified index to the data dictionary.
 *
 * @param lcc connection context to use to perform the work
 * @param td the base table
 * @param index the index of the base table
 * @param numRows number of rows in the base table
 * @param cardinality the number of unique values in the index (per number
 *      of leading columns)
 * @param asBackgroundTask whether the update is done automatically as
 *      part of a background task or if explicitly invoked by the user
 * @throws StandardException if updating the data dictionary fails
 */
private void writeUpdatedStats(LanguageConnectionContext lcc, TableDescriptor td, UUID index, long numRows, long[] cardinality, boolean asBackgroundTask) throws StandardException {
    TransactionController tc = lcc.getTransactionExecute();
    trace(1, "writing new stats (xid=" + tc.getTransactionIdString() + ")");
    UUID table = td.getUUID();
    DataDictionary dd = lcc.getDataDictionary();
    UUIDFactory uf = dd.getUUIDFactory();
    // Update the heap row count estimate.
    setHeapRowEstimate(tc, td.getHeapConglomerateId(), numRows);
    // Drop existing index statistics for this index.
    if (!lcc.dataDictionaryInWriteMode()) {
        dd.startWriting(lcc);
    }
    dd.dropStatisticsDescriptors(table, index, tc);
    // invalidation control flag
    boolean conglomerateGone = false;
    // Don't write statistics if the table is empty.
    if (numRows == 0) {
        trace(2, "empty table, no stats written");
    } else {
        // Construct and add the statistics entries.
        for (int i = 0; i < cardinality.length; i++) {
            StatisticsDescriptor statDesc = new StatisticsDescriptor(dd, uf.createUUID(), index, table, "I", new StatisticsImpl(numRows, cardinality[i]), i + 1);
            dd.addDescriptor(statDesc, null, DataDictionary.SYSSTATISTICS_CATALOG_NUM, true, tc);
        }
        // Log some information.
        ConglomerateDescriptor cd = dd.getConglomerateDescriptor(index);
        log(asBackgroundTask, td, "wrote stats for index " + (cd == null ? "n/a" : cd.getDescriptorName()) + " (" + index + "): rows=" + numRows + ", card=" + cardToStr(cardinality));
        // for non-existent indexes in SYSSTATISTICS.
        if (asBackgroundTask && cd == null) {
            log(asBackgroundTask, td, "rolled back index stats because index has been dropped");
            lcc.internalRollback();
        }
        conglomerateGone = (cd == null);
    }
    if (!conglomerateGone) {
        // Invalidate statments accessing the given table.
        invalidateStatements(lcc, td, asBackgroundTask);
    }
    // Only commit tx as we go if running as background task.
    if (asBackgroundTask) {
        lcc.internalCommit(true);
    }
}
Also used : StatisticsImpl(org.apache.derby.catalog.types.StatisticsImpl) StatisticsDescriptor(org.apache.derby.iapi.sql.dictionary.StatisticsDescriptor) UUIDFactory(org.apache.derby.iapi.services.uuid.UUIDFactory) TransactionController(org.apache.derby.iapi.store.access.TransactionController) UUID(org.apache.derby.catalog.UUID) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) 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