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);
}
}
Aggregations