Search in sources :

Example 26 with UUID

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;
}
Also used : TypeDescriptor(org.apache.derby.catalog.TypeDescriptor) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) SQLLongint(org.apache.derby.iapi.types.SQLLongint) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) SQLChar(org.apache.derby.iapi.types.SQLChar) SequenceDescriptor(org.apache.derby.iapi.sql.dictionary.SequenceDescriptor) UUID(org.apache.derby.catalog.UUID) SQLVarchar(org.apache.derby.iapi.types.SQLVarchar) UserType(org.apache.derby.iapi.types.UserType)

Example 27 with UUID

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;
}
Also used : ExecPreparedStatement(org.apache.derby.iapi.sql.execute.ExecPreparedStatement) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) DMLWriteResultSet(org.apache.derby.impl.sql.execute.DMLWriteResultSet) UUID(org.apache.derby.catalog.UUID)

Example 28 with UUID

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));
}
Also used : StatisticsDescriptor(org.apache.derby.iapi.sql.dictionary.StatisticsDescriptor) ConglomerateController(org.apache.derby.iapi.store.access.ConglomerateController) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) GroupFetchScanController(org.apache.derby.iapi.store.access.GroupFetchScanController) ExecIndexRow(org.apache.derby.iapi.sql.execute.ExecIndexRow) StandardException(org.apache.derby.shared.common.error.StandardException) IndexRowGenerator(org.apache.derby.iapi.sql.dictionary.IndexRowGenerator) TransactionController(org.apache.derby.iapi.store.access.TransactionController) UUID(org.apache.derby.catalog.UUID)

Example 29 with UUID

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);
    }
}
Also used : UUID(org.apache.derby.catalog.UUID)

Example 30 with UUID

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;
}
Also used : ArrayList(java.util.ArrayList) StatementSchemaPermission(org.apache.derby.iapi.sql.dictionary.StatementSchemaPermission) StatementRoutinePermission(org.apache.derby.iapi.sql.dictionary.StatementRoutinePermission) StatementGenericPermission(org.apache.derby.iapi.sql.dictionary.StatementGenericPermission) StatementColumnPermission(org.apache.derby.iapi.sql.dictionary.StatementColumnPermission) StatementPermission(org.apache.derby.iapi.sql.dictionary.StatementPermission) UUID(org.apache.derby.catalog.UUID) StatementTablePermission(org.apache.derby.iapi.sql.dictionary.StatementTablePermission) StatementRolePermission(org.apache.derby.iapi.sql.dictionary.StatementRolePermission)

Aggregations

UUID (org.apache.derby.catalog.UUID)101 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)31 DataDescriptorGenerator (org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator)23 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)22 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)21 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)19 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)15 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)15 SQLChar (org.apache.derby.iapi.types.SQLChar)15 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)14 TransactionController (org.apache.derby.iapi.store.access.TransactionController)14 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)12 SQLVarchar (org.apache.derby.iapi.types.SQLVarchar)12 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)11 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)10 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)10 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)10 ArrayList (java.util.ArrayList)9 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)9 ExecIndexRow (org.apache.derby.iapi.sql.execute.ExecIndexRow)8