use of org.apache.derby.iapi.sql.dictionary.StatisticsDescriptor in project derby by apache.
the class SYSSTATISTICSRowFactory method makeRow.
/**
* Make a SYSSTATISTICS row
*
* @return Row suitable for inserting into SYSSTATISTICS.
*
* @exception StandardException thrown on failure
*/
public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) throws StandardException {
String myID = null, referenceID = null, tableID = null;
String statName = null, colMap = null, statType = null;
Timestamp updateTime = null;
int columnCount = 0;
Statistics statisticsObject = null;
boolean validStat = false;
ExecRow row = getExecutionFactory().getValueRow(SYSSTATISTICS_COLUMN_COUNT);
if (td != null) {
StatisticsDescriptor statDesc = (StatisticsDescriptor) td;
myID = statDesc.getUUID().toString();
tableID = statDesc.getTableUUID().toString();
referenceID = statDesc.getReferenceID().toString();
updateTime = statDesc.getUpdateTimestamp();
statType = statDesc.getStatType();
validStat = statDesc.isValid();
statisticsObject = statDesc.getStatistic();
columnCount = statDesc.getColumnCount();
}
row.setColumn(1, new SQLChar(myID));
row.setColumn(2, new SQLChar(referenceID));
row.setColumn(3, new SQLChar(tableID));
row.setColumn(4, new SQLTimestamp(updateTime));
row.setColumn(5, new SQLChar(statType));
row.setColumn(6, new SQLBoolean(validStat));
row.setColumn(7, new SQLInteger(columnCount));
row.setColumn(8, new UserType(statisticsObject));
return row;
}
use of org.apache.derby.iapi.sql.dictionary.StatisticsDescriptor in project derby by apache.
the class InsertResultSet method updateAllIndexes.
/**
* Update all of the indexes on a table when doing a bulk insert
* on an empty table.
*
* @exception StandardException thrown on error
*/
private void updateAllIndexes(long newHeapConglom, InsertConstantAction constants, TableDescriptor td, DataDictionary dd, ExecRow fullTemplate) throws StandardException {
int numIndexes = constants.irgs.length;
/*
** If we didn't actually read in any rows, then
** we don't need to do anything, unless we were
** doing a replace.
*/
if (indexRows == null) {
if (bulkInsertReplace) {
emptyIndexes(newHeapConglom, constants, td, dd, fullTemplate);
}
return;
}
dd.dropStatisticsDescriptors(td.getUUID(), null, tc);
long[] newIndexCongloms = new long[numIndexes];
indexConversionTable = new Hashtable<Long, Long>(numIndexes);
// Populate each index
for (int index = 0; index < numIndexes; index++) {
ConglomerateController indexCC;
Properties properties = new Properties();
ConglomerateDescriptor cd;
// Get the ConglomerateDescriptor for the index
cd = td.getConglomerateDescriptor(constants.indexCIDS[index]);
// Build the properties list for the new conglomerate
indexCC = tc.openCompiledConglomerate(false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_TABLE, TransactionController.ISOLATION_SERIALIZABLE, constants.indexSCOCIs[index], indexDCOCIs[index]);
// 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
// RESOLVE - all indexes are btrees right now
// Populate the index.
sorters[index].completedInserts();
sorters[index] = null;
rowSources[index] = new CardinalityCounter(tc.openSortRowSource(sortIds[index]));
newIndexCongloms[index] = tc.createAndLoadConglomerate("BTREE", indexRows[index].getRowArray(), ordering[index], collation[index], properties, TransactionController.IS_DEFAULT, rowSources[index], (long[]) null);
CardinalityCounter cCount = (CardinalityCounter) rowSources[index];
long numRows;
if ((numRows = cCount.getRowCount()) > 0) {
long[] c = 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, null, DataDictionary.SYSSTATISTICS_CATALOG_NUM, true, tc);
}
}
/* Update the DataDictionary
* RESOLVE - this will change in 1.4 because we will get
* back the same conglomerate number
*
* Update sys.sysconglomerates with new conglomerate #, if the
* conglomerate is shared by duplicate indexes, all the descriptors
* for those indexes need to be updated with the new number.
*/
dd.updateConglomerateDescriptor(td.getConglomerateDescriptors(constants.indexCIDS[index]), newIndexCongloms[index], tc);
// Drop the old conglomerate
tc.dropConglomerate(constants.indexCIDS[index]);
indexConversionTable.put(constants.indexCIDS[index], newIndexCongloms[index]);
}
}
use of org.apache.derby.iapi.sql.dictionary.StatisticsDescriptor 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