Search in sources :

Example 11 with RowLocation

use of org.apache.derby.iapi.types.RowLocation in project derby by apache.

the class BackingStoreHashtable method getEstimatedMemUsage.

/**
 * Take a value which will go into the hash table and return an estimate
 * of how much memory that value will consume. The hash value could
 * be either an array of columns or a LocatedRow.
 *
 * @param hashValue The object for which we want to know the memory usage.
 * @return A guess as to how much memory the current hash value will
 *  use.
 */
private long getEstimatedMemUsage(Object hashValue) {
    long rowMem = 0;
    DataValueDescriptor[] row = null;
    if (hashValue instanceof DataValueDescriptor[]) {
        row = (DataValueDescriptor[]) hashValue;
    } else {
        LocatedRow locatedRow = (LocatedRow) hashValue;
        row = locatedRow.columnValues();
        // account for the RowLocation size and class overhead
        RowLocation rowLocation = locatedRow.rowLocation();
        if (rowLocation != null) {
            rowMem += locatedRow.rowLocation().estimateMemoryUsage();
            rowMem += ClassSize.refSize;
        }
        // account for class overhead of the LocatedRow itself
        rowMem += ClassSize.refSize;
    }
    for (int i = 0; i < row.length; i++) {
        // account for the column's size and class overhead
        rowMem += row[i].estimateMemoryUsage();
        rowMem += ClassSize.refSize;
    }
    // account for the class overhead of the array itself
    rowMem += ClassSize.refSize;
    return rowMem;
}
Also used : LocatedRow(org.apache.derby.iapi.types.LocatedRow) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) RowLocation(org.apache.derby.iapi.types.RowLocation)

Example 12 with RowLocation

use of org.apache.derby.iapi.types.RowLocation in project derby by apache.

the class AlterTableConstantAction method compressTable.

/**
 * routine to process compress table or ALTER TABLE <t> DROP COLUMN <c>;
 * <p>
 * Uses class level variable "compressTable" to determine if processing
 * compress table or drop column:
 *     if (!compressTable)
 *         must be drop column.
 * <p>
 * Handles rebuilding of base conglomerate and all necessary indexes.
 */
private void compressTable() throws StandardException {
    long newHeapConglom;
    Properties properties = new Properties();
    RowLocation rl;
    if (SanityManager.DEBUG) {
        if (lockGranularity != '\0') {
            SanityManager.THROWASSERT("lockGranularity expected to be '\0', not " + lockGranularity);
        }
        SanityManager.ASSERT(!compressTable || columnInfo == null, "columnInfo expected to be null");
        SanityManager.ASSERT(constraintActions == null, "constraintActions expected to be null");
    }
    ExecRow emptyHeapRow = td.getEmptyExecRow();
    int[] collation_ids = td.getColumnCollationIds();
    compressHeapCC = tc.openConglomerate(td.getHeapConglomerateId(), false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_TABLE, TransactionController.ISOLATION_SERIALIZABLE);
    rl = compressHeapCC.newRowLocationTemplate();
    // Get the properties on the old heap
    compressHeapCC.getInternalTablePropertySet(properties);
    compressHeapCC.close();
    compressHeapCC = null;
    // Create an array to put base row template
    baseRow = new ExecRow[bulkFetchSize];
    baseRowArray = new DataValueDescriptor[bulkFetchSize][];
    validRow = new boolean[bulkFetchSize];
    /* Set up index info */
    getAffectedIndexes();
    // Get an array of RowLocation template
    compressRL = new RowLocation[bulkFetchSize];
    indexRows = new ExecIndexRow[numIndexes];
    if (!compressTable) {
        // must be a drop column, thus the number of columns in the
        // new template row and the collation template is one less.
        ExecRow newRow = activation.getExecutionFactory().getValueRow(emptyHeapRow.nColumns() - 1);
        int[] new_collation_ids = new int[collation_ids.length - 1];
        for (int i = 0; i < newRow.nColumns(); i++) {
            newRow.setColumn(i + 1, i < droppedColumnPosition - 1 ? emptyHeapRow.getColumn(i + 1) : emptyHeapRow.getColumn(i + 1 + 1));
            new_collation_ids[i] = collation_ids[(i < droppedColumnPosition - 1) ? i : (i + 1)];
        }
        emptyHeapRow = newRow;
        collation_ids = new_collation_ids;
    }
    setUpAllSorts(emptyHeapRow, rl);
    // Start by opening a full scan on the base table.
    openBulkFetchScan(td.getHeapConglomerateId());
    // Get the estimated row count for the sorters
    estimatedRowCount = compressHeapGSC.getEstimatedRowCount();
    // Create the array of base row template
    for (int i = 0; i < bulkFetchSize; i++) {
        // create a base row template
        baseRow[i] = td.getEmptyExecRow();
        baseRowArray[i] = baseRow[i].getRowArray();
        compressRL[i] = compressHeapGSC.newRowLocationTemplate();
    }
    newHeapConglom = tc.createAndLoadConglomerate("heap", emptyHeapRow.getRowArray(), // column sort order - not required for heap
    null, collation_ids, properties, TransactionController.IS_DEFAULT, this, (long[]) null);
    closeBulkFetchScan();
    // Set the "estimated" row count
    ScanController compressHeapSC = tc.openScan(newHeapConglom, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_TABLE, TransactionController.ISOLATION_SERIALIZABLE, (FormatableBitSet) null, (DataValueDescriptor[]) null, 0, (Qualifier[][]) null, (DataValueDescriptor[]) null, 0);
    compressHeapSC.setEstimatedRowCount(rowCount);
    compressHeapSC.close();
    // RESOLVE DJD CLEANUP
    compressHeapSC = null;
    /*
		** Inform the data dictionary that we are about to write to it.
		** There are several calls to data dictionary "get" methods here
		** that might be done in "read" mode in the data dictionary, but
		** it seemed safer to do this whole operation in "write" mode.
		**
		** We tell the data dictionary we're done writing at the end of
		** the transaction.
		*/
    dd.startWriting(lcc);
    // Update all indexes
    if (compressIRGs.length > 0) {
        updateAllIndexes(newHeapConglom, dd);
    }
    /* Update the DataDictionary
		 * RESOLVE - this will change in 1.4 because we will get
		 * back the same conglomerate number
		 */
    // Get the ConglomerateDescriptor for the heap
    long oldHeapConglom = td.getHeapConglomerateId();
    ConglomerateDescriptor cd = td.getConglomerateDescriptor(oldHeapConglom);
    // Update sys.sysconglomerates with new conglomerate #
    dd.updateConglomerateDescriptor(cd, newHeapConglom, tc);
    // Now that the updated information is available in the system tables,
    // we should invalidate all statements that use the old conglomerates
    dm.invalidateFor(td, DependencyManager.COMPRESS_TABLE, lcc);
    // Drop the old conglomerate
    tc.dropConglomerate(oldHeapConglom);
    cleanUp();
}
Also used : ScanController(org.apache.derby.iapi.store.access.ScanController) GroupFetchScanController(org.apache.derby.iapi.store.access.GroupFetchScanController) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) Properties(java.util.Properties) RowLocation(org.apache.derby.iapi.types.RowLocation) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)

Example 13 with RowLocation

use of org.apache.derby.iapi.types.RowLocation in project derby by apache.

the class AlterTableConstantAction method modifyColumnDefault.

/**
 * Workhorse for modifying the default value of a column.
 *
 * @param       ix 		the index of the column specfication in the ALTER
 *						statement-- currently we allow only one.
 * @exception	StandardException, thrown on error.
 */
private void modifyColumnDefault(int ix) throws StandardException {
    ColumnDescriptor columnDescriptor = td.getColumnDescriptor(columnInfo[ix].name);
    int columnPosition = columnDescriptor.getPosition();
    // Clean up after the old default, if non-null
    if (columnDescriptor.hasNonNullDefault()) {
        // Invalidate off of the old default
        DefaultDescriptor defaultDescriptor = new DefaultDescriptor(dd, columnInfo[ix].oldDefaultUUID, td.getUUID(), columnPosition);
        dm.invalidateFor(defaultDescriptor, DependencyManager.MODIFY_COLUMN_DEFAULT, lcc);
        // Drop any dependencies
        dm.clearDependencies(lcc, defaultDescriptor);
    }
    UUID defaultUUID = columnInfo[ix].newDefaultUUID;
    /* Generate a UUID for the default, if one exists
		 * and there is no default id yet.
		 */
    if (columnInfo[ix].defaultInfo != null && defaultUUID == null) {
        defaultUUID = dd.getUUIDFactory().createUUID();
    }
    /* Get a ColumnDescriptor reflecting the new default */
    columnDescriptor = new ColumnDescriptor(columnInfo[ix].name, columnPosition, columnInfo[ix].dataType, columnInfo[ix].defaultValue, columnInfo[ix].defaultInfo, td, defaultUUID, columnInfo[ix].autoincStart, columnInfo[ix].autoincInc, columnInfo[ix].autoinc_create_or_modify_Start_Increment, columnInfo[ix].autoincCycle);
    // Update the ColumnDescriptor with new default info
    dd.dropColumnDescriptor(td.getUUID(), columnInfo[ix].name, tc);
    dd.addDescriptor(columnDescriptor, td, DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
    if (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_INCREMENT) {
        // adding an autoincrement default-- calculate the maximum value
        // of the autoincrement column.
        long maxValue = getColumnMax(td, columnInfo[ix].name, columnInfo[ix].autoincInc);
        dd.setAutoincrementValue(tc, td.getUUID(), columnInfo[ix].name, maxValue, true);
    } else if (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_RESTART) {
        dd.setAutoincrementValue(tc, td.getUUID(), columnInfo[ix].name, columnInfo[ix].autoincStart, false);
    }
    if ((columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_INCREMENT) || (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_RESTART) || (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_CYCLE)) {
        // 
        if (dd.checkVersion(DataDictionary.DD_VERSION_DERBY_10_11, null)) {
            Long currentValue = null;
            // are just changing the increment. see DERBY-6579.
            if ((columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_INCREMENT) || (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_CYCLE)) {
                currentValue = dd.peekAtIdentity(td.getSchemaName(), td.getName());
            }
            if (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_CYCLE) {
                if (columnInfo[ix].autoincCycle) {
                    // ALTER TABLE ALTER COLUMN $columnName SET CYCLE
                    if (currentValue == null) {
                        // 
                        // If the current value is NULL, then the sequence generator
                        // is exhausted and it must have been a NO CYCLE generator,
                        // which we are changing to CYCLE.
                        // According to the 2016 SQL Standard, section 4.27.2
                        // (Operations involving sequence generators),
                        // the next value of the sequence generator should be the minimum value
                        // (for an ascending sequence generator) or the maximum value
                        // (for a descending sequence generator). See DERBY-6961.
                        // This logic will have to change in the future if we
                        // let users configure the maximum and minimum values of identity columns.
                        // 
                        int topOrBottom = (columnInfo[ix].autoincInc > 0) ? RANGE_BOTTOM : RANGE_TOP;
                        currentValue = getRangeBound(columnInfo[ix].dataType, topOrBottom);
                    }
                } else {
                    // ALTER TABLE ALTER COLUMN $columnName SET NO CYCLE
                    // 
                    // If we are just about to issue the rollover value,
                    // set it to NULL in order to prevent cycling.
                    int topOrBottom = (columnInfo[ix].autoincInc > 0) ? RANGE_BOTTOM : RANGE_TOP;
                    Long rolloverValue = getRangeBound(columnInfo[ix].dataType, topOrBottom);
                    if ((currentValue != null) && (currentValue.equals(rolloverValue))) {
                        currentValue = null;
                    }
                }
            }
            DropTableConstantAction.dropIdentitySequence(dd, td, activation);
            // recreate the sequence
            String sequenceName = TableDescriptor.makeSequenceName(td.getUUID());
            CreateSequenceConstantAction csca = CreateTableConstantAction.makeCSCA(columnInfo[ix], sequenceName);
            csca.executeConstantAction(activation);
            // reset the current value of the sequence generator as necessary
            if ((columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_INCREMENT) || (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_CYCLE)) {
                SequenceDescriptor sequence = dd.getSequenceDescriptor(dd.getSystemSchemaDescriptor(), sequenceName);
                RowLocation[] rowLocation = new RowLocation[1];
                SequenceDescriptor[] sequenceDescriptor = new SequenceDescriptor[1];
                dd.computeSequenceRowLocation(tc, sequence.getUUID().toString(), rowLocation, sequenceDescriptor);
                dd.updateCurrentSequenceValue(tc, rowLocation[0], true, null, currentValue);
            }
        }
    }
}
Also used : ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) DefaultDescriptor(org.apache.derby.iapi.sql.dictionary.DefaultDescriptor) UUID(org.apache.derby.catalog.UUID) SequenceDescriptor(org.apache.derby.iapi.sql.dictionary.SequenceDescriptor) RowLocation(org.apache.derby.iapi.types.RowLocation)

Example 14 with RowLocation

use of org.apache.derby.iapi.types.RowLocation in project derby by apache.

the class ProjectRestrictResultSet method getNextRowCore.

/**
 * Return the requested values computed
 * from the next row (if any) for which
 * the restriction evaluates to true.
 * <p>
 * restriction and projection parameters
 * are evaluated for each row.
 *
 * @exception StandardException thrown on failure.
 * @exception StandardException ResultSetNotOpen thrown if not yet open.
 *
 * @return the next row in the result
 */
public ExecRow getNextRowCore() throws StandardException {
    if (isXplainOnlyMode())
        return null;
    ExecRow candidateRow = null;
    ExecRow result = null;
    boolean restrict = false;
    DataValueDescriptor restrictBoolean;
    long beginRT = 0;
    /* Return null if open was short circuited by false constant expression */
    if (shortCircuitOpen) {
        return result;
    }
    beginTime = getCurrentTimeMillis();
    do {
        if (validatingCheckConstraint) {
            candidateRow = null;
            while (rowLocations.hasMoreElements() && candidateRow == null) {
                DataValueDescriptor[] row = (DataValueDescriptor[]) rowLocations.nextElement();
                RowLocation rl = (RowLocation) ((SQLRef) row[0]).getObject();
                ((ValidateCheckConstraintResultSet) source).positionScanAtRowLocation(rl);
                candidateRow = source.getNextRowCore();
            // if null (deleted), we move to next
            }
        } else {
            candidateRow = source.getNextRowCore();
        }
        if (candidateRow != null) {
            beginRT = getCurrentTimeMillis();
            /* If restriction is null, then all rows qualify */
            if (restriction == null) {
                restrict = true;
            } else {
                setCurrentRow(candidateRow);
                restrictBoolean = (DataValueDescriptor) restriction.invoke(activation);
                restrictionTime += getElapsedMillis(beginRT);
                // if the result is null, we make it false --
                // so the row won't be returned.
                restrict = ((!restrictBoolean.isNull()) && restrictBoolean.getBoolean());
                if (!restrict) {
                    rowsFiltered++;
                }
            }
            /* Update the run time statistics */
            rowsSeen++;
        }
    } while ((candidateRow != null) && (!restrict));
    if (candidateRow != null) {
        beginRT = getCurrentTimeMillis();
        result = doProjection(candidateRow);
        projectionTime += getElapsedMillis(beginRT);
    } else /* Clear the current row, if null */
    {
        clearCurrentRow();
    }
    currentRow = result;
    if (runTimeStatsOn) {
        if (!isTopResultSet) {
            /* This is simply for RunTimeStats */
            /* We first need to get the subquery tracking array via the StatementContext */
            StatementContext sc = activation.getLanguageConnectionContext().getStatementContext();
            subqueryTrackingArray = sc.getSubqueryTrackingArray();
        }
        nextTime += getElapsedMillis(beginTime);
    }
    return result;
}
Also used : ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) RowLocation(org.apache.derby.iapi.types.RowLocation) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Example 15 with RowLocation

use of org.apache.derby.iapi.types.RowLocation in project derby by apache.

the class GroupedAggregateResultSet method getRowLocation.

// /////////////////////////////////////////////////////////////////////////////
// 
// CursorResultSet interface
// 
// /////////////////////////////////////////////////////////////////////////////
/**
 * This result set has its row location from
 * the last fetch done. If the cursor is closed,
 * a null is returned.
 *
 * @see CursorResultSet
 *
 * @return the row location of the current cursor row.
 * @exception StandardException thrown on failure to get row location
 */
public RowLocation getRowLocation() throws StandardException {
    if (!isOpen)
        return null;
    // REVISIT: could we reuse the same rowlocation object
    // across several calls?
    RowLocation rl;
    rl = scanController.newRowLocationTemplate();
    scanController.fetchLocation(rl);
    return rl;
}
Also used : RowLocation(org.apache.derby.iapi.types.RowLocation)

Aggregations

RowLocation (org.apache.derby.iapi.types.RowLocation)89 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)54 ConglomerateController (org.apache.derby.iapi.store.access.ConglomerateController)40 SQLLongint (org.apache.derby.iapi.types.SQLLongint)35 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)32 ScanController (org.apache.derby.iapi.store.access.ScanController)29 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)27 Properties (java.util.Properties)16 SQLInteger (org.apache.derby.iapi.types.SQLInteger)13 ExecIndexRow (org.apache.derby.iapi.sql.execute.ExecIndexRow)12 StandardException (org.apache.derby.shared.common.error.StandardException)11 SQLChar (org.apache.derby.iapi.types.SQLChar)10 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)7 TransactionController (org.apache.derby.iapi.store.access.TransactionController)7 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)6 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)4 ColumnOrdering (org.apache.derby.iapi.store.access.ColumnOrdering)4 UUID (org.apache.derby.catalog.UUID)3 ContextManager (org.apache.derby.iapi.services.context.ContextManager)3 StreamStorable (org.apache.derby.iapi.services.io.StreamStorable)3