use of org.apache.derby.catalog.UUID in project derby by apache.
the class SYSSEQUENCESRowFactory method makeRow.
/**
* Make a SYSSEQUENCES row
*
* @param td a sequence descriptor
* @param parent unused
* @return Row suitable for inserting into SYSSEQUENCES.
* @throws org.apache.derby.shared.common.error.StandardException
* thrown on failure
*/
public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) throws StandardException {
ExecRow row;
String oidString = null;
String sequenceName = null;
String schemaIdString = null;
TypeDescriptor typeDesc = null;
Long currentValue = null;
long startValue = 0;
long minimumValue = 0;
long maximumValue = 0;
long increment = 0;
boolean canCycle = false;
if (td != null) {
SequenceDescriptor sd = (SequenceDescriptor) td;
UUID oid = sd.getUUID();
oidString = oid.toString();
sequenceName = sd.getSequenceName();
UUID schemaId = sd.getSchemaId();
schemaIdString = schemaId.toString();
typeDesc = sd.getDataType().getCatalogType();
currentValue = sd.getCurrentValue();
startValue = sd.getStartValue();
minimumValue = sd.getMinimumValue();
maximumValue = sd.getMaximumValue();
increment = sd.getIncrement();
canCycle = sd.canCycle();
}
/* Build the row to insert */
row = getExecutionFactory().getValueRow(SYSSEQUENCES_COLUMN_COUNT);
/* 1st column is UUID */
row.setColumn(SYSSEQUENCES_SEQUENCEID, new SQLChar(oidString));
/* 2nd column is SEQUENCENAME */
row.setColumn(SYSSEQUENCES_SEQUENCENAME, new SQLVarchar(sequenceName));
/* 3nd column is SCHEMAID */
row.setColumn(SYSSEQUENCES_SCHEMAID, new SQLChar(schemaIdString));
/* 4th column is SEQUENCEDATATYPE */
row.setColumn(SYSSEQUENCES_SEQUENCEDATATYPE, new UserType(typeDesc));
/* 5th column is CURRENTVALUE */
SQLLongint curVal;
if (currentValue == null) {
curVal = new SQLLongint();
} else {
curVal = new SQLLongint(currentValue.longValue());
}
row.setColumn(SYSSEQUENCES_CURRENT_VALUE, curVal);
/* 6th column is STARTVALUE */
row.setColumn(SYSSEQUENCES_START_VALUE, new SQLLongint(startValue));
/* 7th column is MINIMUMVALUE */
row.setColumn(SYSSEQUENCES_MINIMUM_VALUE, new SQLLongint(minimumValue));
/* 8th column is MAXIMUMVALUE */
row.setColumn(SYSSEQUENCES_MAXIMUM_VALUE, new SQLLongint(maximumValue));
/* 9th column is INCREMENT */
row.setColumn(SYSSEQUENCES_INCREMENT, new SQLLongint(increment));
/* 10th column is CYCLEOPTION */
row.setColumn(SYSSEQUENCES_CYCLE_OPTION, new SQLChar(canCycle ? "Y" : "N"));
return row;
}
use of org.apache.derby.catalog.UUID in project derby by apache.
the class SQLBoolean method throwExceptionIfImmediateAndFalse.
public BooleanDataValue throwExceptionIfImmediateAndFalse(String sqlState, String tableName, String constraintName, Activation a, int savedUUIDIdx) throws StandardException {
if (!isNull() && (value == false)) {
final ExecPreparedStatement ps = a.getPreparedStatement();
final UUID constrId = (UUID) ps.getSavedObject(savedUUIDIdx);
final LanguageConnectionContext lcc = a.getLanguageConnectionContext();
final boolean isDeferred = lcc.isEffectivelyDeferred(lcc.getCurrentSQLSessionContext(a), constrId);
if (!isDeferred) {
throw StandardException.newException(sqlState, tableName, constraintName);
} else {
// Just return the false value and validate later,
// cf NoRowsResultSetImpl#evaluateCheckConstraints.
// and InsertResultSet#evaluateCheckConstraints
DMLWriteResultSet rs = (DMLWriteResultSet) a.getResultSet();
rs.rememberConstraint(constrId);
}
}
return this;
}
use of org.apache.derby.catalog.UUID in project derby by apache.
the class IndexStatisticsDaemonImpl method updateIndexStatsMinion.
/**
* Updates the index statistics for the given table and the specified
* indexes.
* <p>
* <strong>API note</strong>: Using {@code null} to update the statistics
* for all conglomerates is preferred over explicitly passing an array with
* all the conglomerates for the table. Doing so allows for some
* optimizations, and will cause a disposable statistics check to be
* performed.
*
* @param lcc language connection context used to perform the work
* @param td the table to update index stats for
* @param cds the conglomerates to update statistics for (non-index
* conglomerates will be ignored), {@code null} means all indexes
* @param asBackgroundTask whether the updates are done automatically as
* part of a background task or if explicitly invoked by the user
* @throws StandardException if something goes wrong
*/
private void updateIndexStatsMinion(LanguageConnectionContext lcc, TableDescriptor td, ConglomerateDescriptor[] cds, boolean asBackgroundTask) throws StandardException {
// can only properly identify disposable stats if cds == null,
// which means we are processing all indexes on the conglomerate.
final boolean identifyDisposableStats = (cds == null);
// Fetch descriptors if we're updating statistics for all indexes.
if (cds == null) {
cds = td.getConglomerateDescriptors();
}
// Extract/derive information from the table descriptor
long[] conglomerateNumber = new long[cds.length];
ExecIndexRow[] indexRow = new ExecIndexRow[cds.length];
TransactionController tc = lcc.getTransactionExecute();
ConglomerateController heapCC = tc.openConglomerate(td.getHeapConglomerateId(), false, 0, TransactionController.MODE_RECORD, asBackgroundTask ? TransactionController.ISOLATION_READ_UNCOMMITTED : TransactionController.ISOLATION_REPEATABLE_READ);
// create a list of indexes that should have statistics, by looking
// at all indexes on the conglomerate, and conditionally skipping
// unique single column indexes. This set is the "non disposable
// stat list".
UUID[] non_disposable_objectUUID = new UUID[cds.length];
try {
for (int i = 0; i < cds.length; i++) {
// Skip non-index conglomerates
if (!cds[i].isIndex()) {
conglomerateNumber[i] = -1;
continue;
}
IndexRowGenerator irg = cds[i].getIndexDescriptor();
// or we are running in soft-upgrade-mode on a pre 10.9 db.
if (skipDisposableStats) {
if (irg.isUnique() && irg.numberOfOrderedColumns() == 1) {
conglomerateNumber[i] = -1;
continue;
}
}
// at this point have found a stat for an existing
// index which is not a single column unique index, add it
// to the list of "non disposable stats"
conglomerateNumber[i] = cds[i].getConglomerateNumber();
non_disposable_objectUUID[i] = cds[i].getUUID();
indexRow[i] = irg.getNullIndexRow(td.getColumnDescriptorList(), heapCC.newRowLocationTemplate());
}
} finally {
heapCC.close();
}
if (identifyDisposableStats) {
// Note this loop is not controlled by the skipDisposableStats
// flag. The above loop controls if we drop single column unique
// index stats or not. In all cases we are going to drop
// stats with no associated index (orphaned stats).
List<StatisticsDescriptor> existingStats = td.getStatistics();
StatisticsDescriptor[] stats = (StatisticsDescriptor[]) existingStats.toArray(new StatisticsDescriptor[existingStats.size()]);
// those entries that don't have a matching conglomerate in the
for (int si = 0; si < stats.length; si++) {
UUID referencedIndex = stats[si].getReferenceID();
boolean isValid = false;
for (int ci = 0; ci < conglomerateNumber.length; ci++) {
if (referencedIndex.equals(non_disposable_objectUUID[ci])) {
isValid = true;
break;
}
}
// mechanism in case of another bug like DERBY-5681 in Derby.
if (!isValid) {
String msg = "dropping disposable statistics entry " + stats[si].getUUID() + " for index " + stats[si].getReferenceID() + " (cols=" + stats[si].getColumnCount() + ")";
logAlways(td, null, msg);
trace(1, msg + " on table " + stats[si].getTableUUID());
DataDictionary dd = lcc.getDataDictionary();
if (!lcc.dataDictionaryInWriteMode()) {
dd.startWriting(lcc);
}
dd.dropStatisticsDescriptors(td.getUUID(), stats[si].getReferenceID(), tc);
if (asBackgroundTask) {
lcc.internalCommit(true);
}
}
}
}
// [x][0] = conglomerate number, [x][1] = start time, [x][2] = stop time
long[][] scanTimes = new long[conglomerateNumber.length][3];
int sci = 0;
for (int indexNumber = 0; indexNumber < conglomerateNumber.length; indexNumber++) {
if (conglomerateNumber[indexNumber] == -1)
continue;
// Check if daemon has been disabled.
if (asBackgroundTask) {
if (isShuttingDown()) {
break;
}
}
scanTimes[sci][0] = conglomerateNumber[indexNumber];
scanTimes[sci][1] = System.currentTimeMillis();
// Subtract one for the RowLocation added for indexes.
int numCols = indexRow[indexNumber].nColumns() - 1;
long[] cardinality = new long[numCols];
KeyComparator cmp = new KeyComparator(indexRow[indexNumber]);
/* Read uncommitted, with record locking. Actually CS store may
not hold record locks */
GroupFetchScanController gsc = tc.openGroupFetchScan(conglomerateNumber[indexNumber], // hold
false, 0, // locking
TransactionController.MODE_RECORD, TransactionController.ISOLATION_READ_UNCOMMITTED, // scancolumnlist-- want everything.
null, // startkeyvalue-- start from the beginning.
null, 0, // qualifiers, none!
null, // stopkeyvalue,
null, 0);
try {
int rowsFetched = 0;
boolean giving_up_on_shutdown = false;
while ((rowsFetched = cmp.fetchRows(gsc)) > 0) {
// I/O that is processed as a convenient point.
if (asBackgroundTask) {
if (isShuttingDown()) {
giving_up_on_shutdown = true;
break;
}
}
for (int i = 0; i < rowsFetched; i++) {
int whichPositionChanged = cmp.compareWithPrevKey(i);
if (whichPositionChanged >= 0) {
for (int j = whichPositionChanged; j < numCols; j++) cardinality[j]++;
}
}
}
if (giving_up_on_shutdown)
break;
gsc.setEstimatedRowCount(cmp.getRowCount());
} finally // try
{
gsc.close();
gsc = null;
}
scanTimes[sci++][2] = System.currentTimeMillis();
// We have scanned the indexes, so let's give this a few attempts
// before giving up.
int retries = 0;
while (true) {
try {
writeUpdatedStats(lcc, td, non_disposable_objectUUID[indexNumber], cmp.getRowCount(), cardinality, asBackgroundTask);
break;
} catch (StandardException se) {
retries++;
if (se.isLockTimeout() && retries < 3) {
trace(2, "lock timeout when writing stats, retrying");
sleep(100 * retries);
} else {
// o too many lock timeouts
throw se;
}
}
}
}
log(asBackgroundTask, td, fmtScanTimes(scanTimes));
}
use of org.apache.derby.catalog.UUID in project derby by apache.
the class CompilerContextImpl method addRequiredUsagePriv.
/**
* @see CompilerContext#addRequiredUsagePriv
*/
public void addRequiredUsagePriv(PrivilegedSQLObject usableObject) {
if (requiredUsagePrivileges == null || usableObject == null) {
return;
}
UUID objectID = usableObject.getUUID();
String objectType = usableObject.getObjectTypeName();
if (requiredUsagePrivileges.get(objectID) == null) {
requiredUsagePrivileges.put(objectID, objectType);
}
}
use of org.apache.derby.catalog.UUID in project derby by apache.
the class CompilerContextImpl method getRequiredPermissionsList.
/**
* @return The list of required privileges.
*/
public List<StatementPermission> getRequiredPermissionsList() {
int size = 0;
if (requiredRoutinePrivileges != null) {
size += requiredRoutinePrivileges.size();
}
if (requiredUsagePrivileges != null) {
size += requiredUsagePrivileges.size();
}
if (requiredTablePrivileges != null) {
size += requiredTablePrivileges.size();
}
if (requiredSchemaPrivileges != null) {
size += requiredSchemaPrivileges.size();
}
if (requiredColumnPrivileges != null) {
size += requiredColumnPrivileges.size();
}
if (requiredRolePrivileges != null) {
size += requiredRolePrivileges.size();
}
ArrayList<StatementPermission> list = new ArrayList<StatementPermission>(size);
if (requiredRoutinePrivileges != null) {
for (Iterator<UUID> itr = requiredRoutinePrivileges.keySet().iterator(); itr.hasNext(); ) {
UUID routineUUID = itr.next();
list.add(new StatementRoutinePermission(routineUUID));
}
}
if (requiredUsagePrivileges != null) {
for (Iterator<UUID> itr = requiredUsagePrivileges.keySet().iterator(); itr.hasNext(); ) {
UUID objectID = itr.next();
list.add(new StatementGenericPermission(objectID, requiredUsagePrivileges.get(objectID), PermDescriptor.USAGE_PRIV));
}
}
if (requiredTablePrivileges != null) {
for (Iterator<StatementTablePermission> itr = requiredTablePrivileges.values().iterator(); itr.hasNext(); ) {
list.add(itr.next());
}
}
if (requiredSchemaPrivileges != null) {
for (Iterator<StatementSchemaPermission> itr = requiredSchemaPrivileges.values().iterator(); itr.hasNext(); ) {
list.add(itr.next());
}
}
if (requiredColumnPrivileges != null) {
for (Iterator<StatementColumnPermission> itr = requiredColumnPrivileges.values().iterator(); itr.hasNext(); ) {
list.add(itr.next());
}
}
if (requiredRolePrivileges != null) {
for (Iterator<StatementRolePermission> itr = requiredRolePrivileges.values().iterator(); itr.hasNext(); ) {
list.add(itr.next());
}
}
return list;
}
Aggregations