use of org.apache.derby.iapi.types.RowLocation in project derby by apache.
the class ConsistencyChecker method checkTable.
/**
* Check the named table, ensuring that all of its indexes are consistent
* with the base table.
* Use this
* method only within an SQL-J statement; do not call it directly.
* <P>When tables are consistent, the method returns true. Otherwise, the method throws an exception.
* <p>To check the consistency of a single table:
* <p><code>
* VALUES ConsistencyChecker::checkTable(<i>SchemaName</i>, <i>TableName</i>)</code></p>
* <P>For example, to check the consistency of the table <i>APP.Flights</i>:
* <p><code>
* VALUES ConsistencyChecker::checkTable('APP', 'FLIGHTS')</code></p>
* <p>To check the consistency of all of the tables in the 'APP' schema,
* stopping at the first failure:
*
* <P><code>SELECT tablename, ConsistencyChecker::checkTable(<br>
* 'APP', tablename)<br>
* FROM sys.sysschemas s, sys.systables t
* WHERE s.schemaname = 'APP' AND s.schemaid = t.schemaid</code>
*
* <p> To check the consistency of an entire database, stopping at the first failure:
*
* <p><code>SELECT schemaname, tablename,<br>
* ConsistencyChecker::checkTable(schemaname, tablename)<br>
* FROM sys.sysschemas s, sys.systables t<br>
* WHERE s.schemaid = t.schemaid</code>
*
* @param schemaName The schema name of the table.
* @param tableName The name of the table
*
* @return true, if the table is consistent, exception thrown if inconsistent
*
* @exception SQLException Thrown if some inconsistency
* is found, or if some unexpected
* exception is thrown..
*/
public static boolean checkTable(String schemaName, String tableName) throws SQLException {
DataDictionary dd;
TableDescriptor td;
long baseRowCount = -1;
TransactionController tc;
ConglomerateDescriptor heapCD;
ConglomerateDescriptor indexCD;
ExecRow baseRow;
ExecRow indexRow;
RowLocation rl = null;
RowLocation scanRL = null;
ScanController scan = null;
int[] baseColumnPositions;
int baseColumns = 0;
DataValueFactory dvf;
long indexRows;
ConglomerateController baseCC = null;
ConglomerateController indexCC = null;
SchemaDescriptor sd;
ConstraintDescriptor constraintDesc;
LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
tc = lcc.getTransactionExecute();
try {
// make sure that application code doesn't bypass security checks
// by calling this public entry point
SecurityUtil.authorize(Securable.CHECK_TABLE);
dd = lcc.getDataDictionary();
dvf = lcc.getDataValueFactory();
ExecutionFactory ef = lcc.getLanguageConnectionFactory().getExecutionFactory();
sd = dd.getSchemaDescriptor(schemaName, tc, true);
td = dd.getTableDescriptor(tableName, sd, tc);
if (td == null) {
throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND, schemaName + "." + tableName);
}
/* Skip views */
if (td.getTableType() == TableDescriptor.VIEW_TYPE) {
return true;
}
/* Open the heap for reading */
baseCC = tc.openConglomerate(td.getHeapConglomerateId(), false, 0, TransactionController.MODE_TABLE, TransactionController.ISOLATION_SERIALIZABLE);
/* Check the consistency of the heap */
baseCC.checkConsistency();
heapCD = td.getConglomerateDescriptor(td.getHeapConglomerateId());
/* Get a row template for the base table */
baseRow = ef.getValueRow(td.getNumberOfColumns());
/* Fill the row with nulls of the correct type */
ColumnDescriptorList cdl = td.getColumnDescriptorList();
int cdlSize = cdl.size();
for (int index = 0; index < cdlSize; index++) {
ColumnDescriptor cd = (ColumnDescriptor) cdl.elementAt(index);
baseRow.setColumn(cd.getPosition(), cd.getType().getNull());
}
/* Look at all the indexes on the table */
ConglomerateDescriptor[] cds = td.getConglomerateDescriptors();
for (int index = 0; index < cds.length; index++) {
indexCD = cds[index];
/* Skip the heap */
if (!indexCD.isIndex())
continue;
/* Check the internal consistency of the index */
indexCC = tc.openConglomerate(indexCD.getConglomerateNumber(), false, 0, TransactionController.MODE_TABLE, TransactionController.ISOLATION_SERIALIZABLE);
indexCC.checkConsistency();
indexCC.close();
indexCC = null;
if (indexCD.isConstraint()) {
constraintDesc = dd.getConstraintDescriptor(td, indexCD.getUUID());
if (constraintDesc == null) {
throw StandardException.newException(SQLState.LANG_OBJECT_NOT_FOUND, "CONSTRAINT for INDEX", indexCD.getConglomerateName());
}
}
/*
** Set the base row count when we get to the first index.
** We do this here, rather than outside the index loop, so
** we won't do the work of counting the rows in the base table
** if there are no indexes to check.
*/
if (baseRowCount < 0) {
scan = tc.openScan(heapCD.getConglomerateNumber(), // hold
false, // not forUpdate
0, TransactionController.MODE_TABLE, TransactionController.ISOLATION_SERIALIZABLE, RowUtil.EMPTY_ROW_BITSET, // startKeyValue
null, // not used with null start posn.
0, // qualifier
null, // stopKeyValue
null, // not used with null stop posn.
0);
/* Also, get the row location template for index rows */
rl = scan.newRowLocationTemplate();
scanRL = scan.newRowLocationTemplate();
for (baseRowCount = 0; scan.next(); baseRowCount++) ;
/* Empty statement */
scan.close();
scan = null;
}
baseColumnPositions = indexCD.getIndexDescriptor().baseColumnPositions();
baseColumns = baseColumnPositions.length;
FormatableBitSet indexColsBitSet = new FormatableBitSet();
for (int i = 0; i < baseColumns; i++) {
indexColsBitSet.grow(baseColumnPositions[i]);
indexColsBitSet.set(baseColumnPositions[i] - 1);
}
/* Get one row template for the index scan, and one for the fetch */
indexRow = ef.getValueRow(baseColumns + 1);
/* Fill the row with nulls of the correct type */
for (int column = 0; column < baseColumns; column++) {
/* Column positions in the data dictionary are one-based */
ColumnDescriptor cd = td.getColumnDescriptor(baseColumnPositions[column]);
indexRow.setColumn(column + 1, cd.getType().getNull());
}
/* Set the row location in the last column of the index row */
indexRow.setColumn(baseColumns + 1, rl);
/* Do a full scan of the index */
scan = tc.openScan(indexCD.getConglomerateNumber(), // hold
false, // not forUpdate
0, TransactionController.MODE_TABLE, TransactionController.ISOLATION_SERIALIZABLE, (FormatableBitSet) null, // startKeyValue
null, // not used with null start posn.
0, // qualifier
null, // stopKeyValue
null, // not used with null stop posn.
0);
DataValueDescriptor[] baseRowIndexOrder = new DataValueDescriptor[baseColumns];
DataValueDescriptor[] baseObjectArray = baseRow.getRowArray();
for (int i = 0; i < baseColumns; i++) {
baseRowIndexOrder[i] = baseObjectArray[baseColumnPositions[i] - 1];
}
/* Get the index rows and count them */
for (indexRows = 0; scan.fetchNext(indexRow.getRowArray()); indexRows++) {
/*
** Get the base row using the RowLocation in the index row,
** which is in the last column.
*/
RowLocation baseRL = (RowLocation) indexRow.getColumn(baseColumns + 1);
boolean base_row_exists = baseCC.fetch(baseRL, baseObjectArray, indexColsBitSet);
/* Throw exception if fetch() returns false */
if (!base_row_exists) {
String indexName = indexCD.getConglomerateName();
throw StandardException.newException(SQLState.LANG_INCONSISTENT_ROW_LOCATION, (schemaName + "." + tableName), indexName, baseRL.toString(), indexRow.toString());
}
/* Compare all the column values */
for (int column = 0; column < baseColumns; column++) {
DataValueDescriptor indexColumn = indexRow.getColumn(column + 1);
DataValueDescriptor baseColumn = baseRowIndexOrder[column];
/*
** With this form of compare(), null is considered equal
** to null.
*/
if (indexColumn.compare(baseColumn) != 0) {
ColumnDescriptor cd = td.getColumnDescriptor(baseColumnPositions[column]);
throw StandardException.newException(SQLState.LANG_INDEX_COLUMN_NOT_EQUAL, indexCD.getConglomerateName(), td.getSchemaName(), td.getName(), baseRL.toString(), cd.getColumnName(), indexColumn.toString(), baseColumn.toString(), indexRow.toString());
}
}
}
/* Clean up after the index scan */
scan.close();
scan = null;
/*
** The index is supposed to have the same number of rows as the
** base conglomerate.
*/
if (indexRows != baseRowCount) {
throw StandardException.newException(SQLState.LANG_INDEX_ROW_COUNT_MISMATCH, indexCD.getConglomerateName(), td.getSchemaName(), td.getName(), Long.toString(indexRows), Long.toString(baseRowCount));
}
}
/* check that all constraints have backing index */
ConstraintDescriptorList constraintDescList = dd.getConstraintDescriptors(td);
for (int index = 0; index < constraintDescList.size(); index++) {
constraintDesc = constraintDescList.elementAt(index);
if (constraintDesc.hasBackingIndex()) {
ConglomerateDescriptor conglomDesc;
conglomDesc = td.getConglomerateDescriptor(constraintDesc.getConglomerateId());
if (conglomDesc == null) {
throw StandardException.newException(SQLState.LANG_OBJECT_NOT_FOUND, "INDEX for CONSTRAINT", constraintDesc.getConstraintName());
}
}
}
} catch (StandardException se) {
throw PublicAPI.wrapStandardException(se);
} finally {
try {
/* Clean up before we leave */
if (baseCC != null) {
baseCC.close();
baseCC = null;
}
if (indexCC != null) {
indexCC.close();
indexCC = null;
}
if (scan != null) {
scan.close();
scan = null;
}
} catch (StandardException se) {
throw PublicAPI.wrapStandardException(se);
}
}
return true;
}
use of org.apache.derby.iapi.types.RowLocation in project derby by apache.
the class DataDictionaryImpl method getUUIDForCoreTable.
/**
* Get the UUID for the specified system table. Prior
* to Plato, system tables did not have canonical UUIDs, so
* we need to scan systables to get the UUID when we
* are updating the core tables.
*
* @param tableName Name of the table
* @param schemaUUID UUID of schema
* @param tc TransactionController to user
*
* @return UUID The UUID of the core table.
*
* @exception StandardException Thrown on failure
*/
private UUID getUUIDForCoreTable(String tableName, String schemaUUID, TransactionController tc) throws StandardException {
ConglomerateController heapCC;
ExecRow row;
DataValueDescriptor schemaIDOrderable;
DataValueDescriptor tableNameOrderable;
ScanController scanController;
TabInfoImpl ti = coreInfo[SYSTABLES_CORE_NUM];
SYSTABLESRowFactory rf = (SYSTABLESRowFactory) ti.getCatalogRowFactory();
// We only want the 1st column from the heap
row = exFactory.getValueRow(1);
/* Use tableNameOrderable and schemaIdOrderable in both start
* and stop position for scan.
*/
tableNameOrderable = new SQLVarchar(tableName);
schemaIDOrderable = new SQLChar(schemaUUID);
/* Set up the start/stop position for the scan */
ExecIndexRow keyRow = exFactory.getIndexableRow(2);
keyRow.setColumn(1, tableNameOrderable);
keyRow.setColumn(2, schemaIDOrderable);
heapCC = tc.openConglomerate(ti.getHeapConglomerate(), false, 0, TransactionController.MODE_RECORD, TransactionController.ISOLATION_REPEATABLE_READ);
ExecRow indexTemplateRow = rf.buildEmptyIndexRow(SYSTABLESRowFactory.SYSTABLES_INDEX1_ID, heapCC.newRowLocationTemplate());
/* Scan the index and go to the data pages for qualifying rows to
* build the column descriptor.
*/
scanController = tc.openScan(// conglomerate to open
ti.getIndexConglomerate(SYSTABLESRowFactory.SYSTABLES_INDEX1_ID), // don't hold open across commit
false, 0, TransactionController.MODE_RECORD, TransactionController.ISOLATION_REPEATABLE_READ, // all fields as objects
(FormatableBitSet) null, // start position - first row
keyRow.getRowArray(), // startSearchOperation
ScanController.GE, // scanQualifier,
(ScanQualifier[][]) null, // stop position - through last row
keyRow.getRowArray(), // stopSearchOperation
ScanController.GT);
/* OK to fetch into the template row,
* since we won't be doing a next.
*/
if (scanController.fetchNext(indexTemplateRow.getRowArray())) {
RowLocation baseRowLocation;
baseRowLocation = (RowLocation) indexTemplateRow.getColumn(indexTemplateRow.nColumns());
/* 1st column is TABLEID (UUID - char(36)) */
row.setColumn(SYSTABLESRowFactory.SYSTABLES_TABLEID, new SQLChar());
FormatableBitSet bi = new FormatableBitSet(1);
bi.set(0);
boolean base_row_exists = heapCC.fetch(baseRowLocation, row.getRowArray(), (FormatableBitSet) null);
if (SanityManager.DEBUG) {
// it can not be possible for heap row to disappear while
// holding scan cursor on index at ISOLATION_REPEATABLE_READ.
SanityManager.ASSERT(base_row_exists, "base row not found");
}
}
scanController.close();
heapCC.close();
return uuidFactory.recreateUUID(row.getColumn(1).toString());
}
use of org.apache.derby.iapi.types.RowLocation in project derby by apache.
the class DataDictionaryImpl method computeAutoincRowLocations.
/**
* @see DataDictionary#computeAutoincRowLocations
*/
public RowLocation[] computeAutoincRowLocations(TransactionController tc, TableDescriptor td) throws StandardException {
int size;
if (!(td.tableHasAutoincrement()))
return null;
size = td.getNumberOfColumns();
RowLocation[] rla = new RowLocation[size];
for (int i = 0; i < size; i++) {
ColumnDescriptor cd = td.getColumnDescriptor(i + 1);
if (cd.isAutoincrement())
rla[i] = computeRowLocation(tc, td, cd.getColumnName());
}
return rla;
}
use of org.apache.derby.iapi.types.RowLocation in project derby by apache.
the class DataDictionaryImpl method visitPermsByGrantee.
/**
* Scan <code>indexNo</code> index on a permission table
* <code>catalog</code>, looking for match(es) for the grantee column
* (given by granteeColnoInIndex for the catalog in question).
*
* The action argument can be either <code>EXISTS</code> or
* <code>DROP</code> (to check for existence, or to drop that row).
*
* There is no index on grantee column only on on any of the
* permissions tables, so we use the index which contain grantee
* and scan that, setting up a scan qualifier to match the
* grantee, then fetch the base row.
*
* If this proves too slow, we should add an index on grantee
* only.
*
* @param authId grantee to match against
* @param tc transaction controller
* @param catalog the underlying permission table to visit
* @param indexNo the number of the index by which to access the catalog
* @param granteeColnoInIndex the column number to match
* <code>authId</code> against
* @param action drop matching rows (<code>DROP</code>), or return
* <code>true</code> if there is a matching row
* (<code>EXISTS</code>)
*
* @return action=EXISTS: return {@code true} if there is a matching row
* else return {@code false}.
* @exception StandardException
*/
private boolean visitPermsByGrantee(String authId, TransactionController tc, int catalog, int indexNo, int granteeColnoInIndex, int action) throws StandardException {
TabInfoImpl ti = getNonCoreTI(catalog);
PermissionsCatalogRowFactory rf = (PermissionsCatalogRowFactory) ti.getCatalogRowFactory();
ConglomerateController heapCC = tc.openConglomerate(ti.getHeapConglomerate(), false, 0, TransactionController.MODE_RECORD, TransactionController.ISOLATION_REPEATABLE_READ);
DataValueDescriptor authIdOrderable = new SQLVarchar(authId);
ScanQualifier[][] scanQualifier = exFactory.getScanQualifier(1);
scanQualifier[0][0].setQualifier(granteeColnoInIndex - 1, /* to zero-based */
authIdOrderable, Orderable.ORDER_OP_EQUALS, false, false, false);
ScanController sc = tc.openScan(ti.getIndexConglomerate(indexNo), // don't hold open across commit
false, // for update
0, TransactionController.MODE_RECORD, TransactionController.ISOLATION_REPEATABLE_READ, // all fields as objects
(FormatableBitSet) null, // start position -
(DataValueDescriptor[]) null, // startSearchOperation - none
0, //
scanQualifier, // stop position -through last row
(DataValueDescriptor[]) null, // stopSearchOperation - none
0);
try {
ExecRow outRow = rf.makeEmptyRow();
ExecIndexRow indexRow = getIndexRowFromHeapRow(ti.getIndexRowGenerator(indexNo), heapCC.newRowLocationTemplate(), outRow);
while (sc.fetchNext(indexRow.getRowArray())) {
RowLocation baseRowLocation = (RowLocation) indexRow.getColumn(indexRow.nColumns());
boolean base_row_exists = heapCC.fetch(baseRowLocation, outRow.getRowArray(), (FormatableBitSet) null);
if (SanityManager.DEBUG) {
// it can not be possible for heap row to
// disappear while holding scan cursor on index at
// ISOLATION_REPEATABLE_READ.
SanityManager.ASSERT(base_row_exists, "base row doesn't exist");
}
if (action == DataDictionaryImpl.EXISTS) {
return true;
} else if (action == DataDictionaryImpl.DROP) {
PermissionsDescriptor perm = (PermissionsDescriptor) rf.buildDescriptor(outRow, (TupleDescriptor) null, this);
removePermEntryInCache(perm);
ti.deleteRow(tc, indexRow, indexNo);
}
}
} finally {
if (sc != null) {
sc.close();
}
if (heapCC != null) {
heapCC.close();
}
}
return false;
}
use of org.apache.derby.iapi.types.RowLocation in project derby by apache.
the class DataDictionaryImpl method bootstrapOneIndex.
private ConglomerateDescriptor bootstrapOneIndex(SchemaDescriptor sd, TransactionController tc, DataDescriptorGenerator ddg, TabInfoImpl ti, int indexNumber, long heapConglomerateNumber) throws StandardException {
boolean isUnique;
ConglomerateController cc;
ExecRow baseRow;
ExecIndexRow indexableRow;
int numColumns;
long conglomId;
RowLocation rl;
CatalogRowFactory rf = ti.getCatalogRowFactory();
IndexRowGenerator irg;
ConglomerateDescriptor conglomerateDescriptor;
initSystemIndexVariables(ti, indexNumber);
irg = ti.getIndexRowGenerator(indexNumber);
numColumns = ti.getIndexColumnCount(indexNumber);
/* Is the index unique */
isUnique = ti.isIndexUnique(indexNumber);
// create an index row template
indexableRow = irg.getIndexRowTemplate();
baseRow = rf.makeEmptyRowForCurrentVersion();
// Get a RowLocation template
cc = tc.openConglomerate(heapConglomerateNumber, false, 0, TransactionController.MODE_RECORD, TransactionController.ISOLATION_REPEATABLE_READ);
rl = cc.newRowLocationTemplate();
cc.close();
// Get an index row based on the base row
irg.getIndexRow(baseRow, rl, indexableRow, (FormatableBitSet) null);
// Describe the properties of the index to the store using Properties
// RESOLVE: The following properties assume a BTREE index.
Properties indexProperties = ti.getCreateIndexProperties(indexNumber);
// Tell it the conglomerate id of the base table
indexProperties.put("baseConglomerateId", Long.toString(heapConglomerateNumber));
// All indexes are unique because they contain the RowLocation.
// The number of uniqueness columns must include the RowLocation
// if the user did not specify a unique index.
indexProperties.put("nUniqueColumns", Integer.toString(isUnique ? numColumns : numColumns + 1));
// By convention, the row location column is the last column
indexProperties.put("rowLocationColumn", Integer.toString(numColumns));
// For now, all columns are key fields, including the RowLocation
indexProperties.put("nKeyFields", Integer.toString(numColumns + 1));
/* Create and add the conglomerate (index) */
conglomId = tc.createConglomerate(// we're requesting an index conglomerate
"BTREE", indexableRow.getRowArray(), // default sort order
null, // default collation id's for collumns in all system congloms
null, // default properties
indexProperties, // not temporary
TransactionController.IS_DEFAULT);
conglomerateDescriptor = ddg.newConglomerateDescriptor(conglomId, rf.getIndexName(indexNumber), true, irg, false, rf.getCanonicalIndexUUID(indexNumber), rf.getCanonicalTableUUID(), sd.getUUID());
ti.setIndexConglomerate(conglomerateDescriptor);
return conglomerateDescriptor;
}
Aggregations