Search in sources :

Example 21 with RowLocation

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

the class ScrollInsensitiveResultSet method getNextRowFromSource.

/* Get the next row from the source ResultSet tree and insert into the hash table */
private ExecRow getNextRowFromSource() throws StandardException {
    ExecRow sourceRow = null;
    ExecRow result = null;
    /* Don't give back more rows than requested */
    if (maxRows > 0 && maxRows == positionInSource) {
        seenLast = true;
        lastPosition = positionInSource;
        afterLast = true;
        return null;
    }
    if (needsRepositioning) {
        positionInLastFetchedRow();
        needsRepositioning = false;
    }
    sourceRow = source.getNextRowCore();
    if (sourceRow != null) {
        seenFirst = true;
        beforeFirst = false;
        long beginTCTime = getCurrentTimeMillis();
        /* If this is the first row from the source then we create a new row
			 * for use when fetching from the hash table.
			 */
        if (resultRow == null) {
            resultRow = activation.getExecutionFactory().getValueRow(sourceRowWidth);
        }
        positionInSource++;
        currentPosition = positionInSource;
        RowLocation rowLoc = null;
        if (source.isForUpdate()) {
            rowLoc = ((CursorResultSet) source).getRowLocation();
        }
        addRowToHashTable(sourceRow, currentPosition, rowLoc, false);
    } else // Remember whether or not we're past the end of the table
    {
        if (!seenLast) {
            lastPosition = positionInSource;
        }
        seenLast = true;
        // Special case for empty table (afterLast is never true)
        if (positionInSource == 0) {
            afterLast = false;
        } else {
            afterLast = true;
            currentPosition = positionInSource + 1;
        }
    }
    return sourceRow;
}
Also used : ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) RowLocation(org.apache.derby.iapi.types.RowLocation)

Example 22 with RowLocation

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

the class B2I method create.

/*
	** Methods of B2I.
	*/
/**
 *	Create an empty secondary index b-tree, using the generic b-tree to do the
 *    generic part of the creation process.
 *
 *    This routine opens the newly created container, adds a single page, and
 *    makes this page the root by inserting a LeafControlRow onto this page
 *    at slot 0 and marking in that control row that the page is a root page.
 *
 *    The following properties are specific to the b-tree secondary index:
 *    <UL>
 *    <LI> "baseConglomerateId" (integer).  The conglomerate id of the base
 *    conglomerate is never actually accessed by the b-tree secondary
 *    index implementation, it only serves as a namespace for row locks.
 *    This property is required.
 *    <LI> "rowLocationColumn" (integer).  The zero-based index into the row which
 *    the b-tree secondary index will assume holds a @see RowLocation of
 *    the base row in the base conglomerate.  This value will be used
 *    for acquiring locks.  In this implementation RowLocationColumn must be
 *    the last key column.
 *    This property is required.
 *    </UL>
 *
 *    A secondary index i (a, b) on table t (a, b, c) would have rows
 *    which looked like (a, b, row_location).  baseConglomerateId is set to the
 *    conglomerate id of t.  rowLocationColumns is set to 2.  allowsDuplicates
 *    would be set to false, @see BTree#create.  To create a unique
 *    secondary index set uniquenessColumns to 2, this means that the btree
 *    code will compare the key values but not the row id when determing
 *    uniqueness.  To create a nonunique secondary index set uniquenessColumns
 *    to 3, this would mean that the uniqueness test would include the row
 *    location and since all row locations will be unique  all rows inserted
 *    into the index will be differentiated (at least) by row location.
 *
 *	@see BTree#create
 *
 *	@exception StandardException Standard exception policy.
 */
public void create(TransactionManager xact_manager, int segmentId, long input_conglomid, DataValueDescriptor[] template, ColumnOrdering[] columnOrder, int[] collationIds, Properties properties, int temporaryFlag) throws StandardException {
    String property_value = null;
    Transaction rawtran = xact_manager.getRawStoreXact();
    if (properties == null) {
        throw (StandardException.newException(SQLState.BTREE_PROPERTY_NOT_FOUND, PROPERTY_BASECONGLOMID));
    }
    // Get baseConglomerateId //
    property_value = properties.getProperty(PROPERTY_BASECONGLOMID);
    if (property_value == null) {
        throw (StandardException.newException(SQLState.BTREE_PROPERTY_NOT_FOUND, PROPERTY_BASECONGLOMID));
    }
    if (SanityManager.DEBUG) {
        if (property_value == null)
            SanityManager.THROWASSERT(PROPERTY_BASECONGLOMID + "property not passed to B2I.create()");
    }
    baseConglomerateId = Long.parseLong(property_value);
    // Get rowLocationColumn //
    property_value = properties.getProperty(PROPERTY_ROWLOCCOLUMN);
    if (SanityManager.DEBUG) {
        if (property_value == null)
            SanityManager.THROWASSERT(PROPERTY_ROWLOCCOLUMN + "property not passed to B2I.create()");
    }
    if (property_value == null) {
        throw (StandardException.newException(SQLState.BTREE_PROPERTY_NOT_FOUND, PROPERTY_BASECONGLOMID));
    }
    rowLocationColumn = Integer.parseInt(property_value);
    // comparing the columns in the index easier.
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(rowLocationColumn == template.length - 1, "rowLocationColumn is not the last column in the index");
        SanityManager.ASSERT(template[rowLocationColumn] instanceof RowLocation);
        // There must be at least one key column
        if (rowLocationColumn < 1)
            SanityManager.THROWASSERT("rowLocationColumn (" + rowLocationColumn + ") expected to be >= 1");
    }
    /* convert the sorting order information into a boolean array map.
		 * If the sorting order for the columns is not provided, we
		 * assign the default as Ascending Order.
		 * array length is equal to template length, because column order
		 * length changes whether it is unique or is non unique. store assumes
		 * template length arrays. So, we make template length array and make
		 * the last column as ascending instead of having lot of execeptions 
         * code.
		 */
    ascDescInfo = new boolean[template.length];
    for (int i = 0; i < ascDescInfo.length; i++) {
        if (columnOrder != null && i < columnOrder.length)
            ascDescInfo[i] = columnOrder[i].getIsAscending();
        else
            // default values - ascending order
            ascDescInfo[i] = true;
    }
    // get collation ids from input collation ids, store it in the
    // conglom state.
    collation_ids = ConglomerateUtil.createCollationIds(template.length, collationIds);
    hasCollatedTypes = hasCollatedColumns(collation_ids);
    // Do the generic part of creating the b-tree.
    super.create(rawtran, segmentId, input_conglomid, template, properties, getTypeFormatId(), temporaryFlag);
    // open the base conglomerate - to get the lock
    ConglomerateController base_cc = xact_manager.openConglomerate(baseConglomerateId, false, TransactionController.OPENMODE_FOR_LOCK_ONLY, TransactionController.MODE_TABLE, TransactionController.ISOLATION_SERIALIZABLE);
    OpenBTree open_btree = new OpenBTree();
    BTreeLockingPolicy b2i_locking_policy = new B2ITableLocking3(rawtran, TransactionController.MODE_TABLE, rawtran.newLockingPolicy(LockingPolicy.MODE_CONTAINER, TransactionController.ISOLATION_SERIALIZABLE, true), base_cc, open_btree);
    // The following call will "open" the new btree.  Create is
    // an interesting case.  What we really want is read only table lock
    // on the base conglomerate and update locks on the index.  For now
    // just get the update lock on the base table, this is done by the
    // lockTable() call made by base class.
    open_btree.init(// current user xact
    (TransactionManager) xact_manager, // current xact
    (TransactionManager) xact_manager, // have init open the container.
    (ContainerHandle) null, rawtran, false, (ContainerHandle.MODE_FORUPDATE), TransactionController.MODE_TABLE, // get table level lock.
    b2i_locking_policy, this, // no logical undo necessary, as
    (LogicalUndo) null, // rows will not move.
    (DynamicCompiledOpenConglomInfo) null);
    // Open the newly created container, and insert the first control row.
    LeafControlRow.initEmptyBtree(open_btree);
    open_btree.close();
    base_cc.close();
}
Also used : OpenBTree(org.apache.derby.impl.store.access.btree.OpenBTree) Transaction(org.apache.derby.iapi.store.raw.Transaction) ConglomerateController(org.apache.derby.iapi.store.access.ConglomerateController) RowLocation(org.apache.derby.iapi.types.RowLocation) BTreeLockingPolicy(org.apache.derby.impl.store.access.btree.BTreeLockingPolicy)

Example 23 with RowLocation

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

the class BTreeScan method initScanParams.

/**
 * Shared initialization code between init() and reopenScan().
 * <p>
 * Basically save away input parameters describing qualifications for
 * the scan, and do some error checking.
 *
 * @exception  StandardException  Standard exception policy.
 */
private void initScanParams(DataValueDescriptor[] startKeyValue, int startSearchOperator, Qualifier[][] qualifier, DataValueDescriptor[] stopKeyValue, int stopSearchOperator) throws StandardException {
    // startKeyValue init.
    this.init_startKeyValue = startKeyValue;
    if (RowUtil.isRowEmpty(this.init_startKeyValue))
        this.init_startKeyValue = null;
    // startSearchOperator init.
    this.init_startSearchOperator = startSearchOperator;
    // qualifier init.
    if ((qualifier != null) && (qualifier.length == 0))
        qualifier = null;
    this.init_qualifier = qualifier;
    // stopKeyValue init.
    this.init_stopKeyValue = stopKeyValue;
    if (RowUtil.isRowEmpty(this.init_stopKeyValue))
        this.init_stopKeyValue = null;
    // stopSearchOperator init.
    this.init_stopSearchOperator = stopSearchOperator;
    // reset the "current" position to starting condition.
    // RESOLVE (mmm) - "compile" this.
    scan_position = new BTreeRowPosition(this);
    scan_position.init();
    scan_position.current_lock_template = new DataValueDescriptor[this.init_template.length];
    scan_position.current_lock_template[this.init_template.length - 1] = scan_position.current_lock_row_loc = (RowLocation) init_template[init_template.length - 1].cloneValue(false);
    // scanColumnList.
    if (SanityManager.DEBUG) {
        if (init_scanColumnList != null) {
            // verify that all columns specified in qualifiers, start
            // and stop positions are specified in the scanColumnList.
            FormatableBitSet required_cols;
            if (qualifier != null)
                required_cols = RowUtil.getQualifierBitSet(qualifier);
            else
                required_cols = new FormatableBitSet(0);
            // add in start columns
            if (this.init_startKeyValue != null) {
                required_cols.grow(this.init_startKeyValue.length);
                for (int i = 0; i < this.init_startKeyValue.length; i++) required_cols.set(i);
            }
            if (this.init_stopKeyValue != null) {
                required_cols.grow(this.init_stopKeyValue.length);
                for (int i = 0; i < this.init_stopKeyValue.length; i++) required_cols.set(i);
            }
            FormatableBitSet required_cols_and_scan_list = (FormatableBitSet) required_cols.clone();
            required_cols_and_scan_list.and(init_scanColumnList);
            // FormatableBitSet equals requires the two FormatableBitSets to be of same
            // length.
            required_cols.grow(init_scanColumnList.size());
            if (!required_cols_and_scan_list.equals(required_cols)) {
                SanityManager.THROWASSERT("Some column specified in a Btree " + " qualifier/start/stop list is " + "not represented in the scanColumnList." + "\n:required_cols_and_scan_list = " + required_cols_and_scan_list + "\n;required_cols = " + required_cols + "\n;init_scanColumnList = " + init_scanColumnList);
            }
        }
    }
}
Also used : FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) RowLocation(org.apache.derby.iapi.types.RowLocation)

Example 24 with RowLocation

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

the class T_AccessFactory method holdCursor.

// test various flavors of commit
protected boolean holdCursor(TransactionController tc) throws StandardException, T_Fail {
    REPORT("(holdCursor)");
    // Create a conglomerates and an index on that conglomerate.
    long base_id = createAConglom(tc, 0, false);
    // Open it.
    ConglomerateController cc = tc.openConglomerate(base_id, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
    // insert 5 rows
    T_AccessRow r1 = null;
    SQLLongint c1 = null;
    for (int i = 1; i < 5; i++) {
        // Create a row.
        r1 = new T_AccessRow(1);
        c1 = new SQLLongint(i);
        r1.setCol(0, c1);
        // Get a location template
        RowLocation rowloc = cc.newRowLocationTemplate();
        // Insert the row and remember its location.
        cc.insertAndFetchLocation(r1.getRowArray(), rowloc);
    }
    // Create an index on the base table.
    long index_id = createBtree(tc, base_id, false);
    tc.commit();
    cc.close();
    tc.commit();
    // HEAP - test that scan is closed on commit of non-held cursor.
    // Open scan on the base table.
    ScanController base_scan = tc.openScan(base_id, // don't hold
    false, // for update
    TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, // all columns, all as objects
    (FormatableBitSet) null, // start position - first row in conglomerate
    null, // unused if start position is null.
    0, // qualifier - accept all rows
    null, // stop position - last row in conglomerate
    null, // unused if stop position is null.
    0);
    // Open scan on the index table.
    ScanController index_scan = tc.openScan(index_id, // don't hold
    false, // for update
    TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, // all columns, all as objects
    (FormatableBitSet) null, // start position - first row in conglomerate
    null, // unused if start position is null.
    0, // qualifier - accept all rows
    null, // stop position - last row in conglomerate
    null, // unused if stop position is null.
    0);
    testOpsBeforeFirstNext(base_scan, r1.getRowArray());
    testOpsBeforeFirstNext(index_scan, r1.getRowArray());
    base_scan.next();
    index_scan.next();
    base_scan.next();
    index_scan.next();
    base_scan.next();
    index_scan.next();
    base_scan.next();
    index_scan.next();
    base_scan.next();
    index_scan.next();
    base_scan.next();
    index_scan.next();
    base_scan.next();
    index_scan.next();
    // should be able call get and set even after next'ing through the rows.
    long row_count = base_scan.getEstimatedRowCount();
    base_scan.setEstimatedRowCount(10);
    row_count = base_scan.getEstimatedRowCount();
    // should be able call get and set even after next'ing through the rows.
    row_count = index_scan.getEstimatedRowCount();
    index_scan.setEstimatedRowCount(10);
    row_count = index_scan.getEstimatedRowCount();
    if (row_count != 10)
        throw T_Fail.testFailMsg("(holdCursor) some problem with get/set row count.");
    tc.commit();
    testOpsBeforeFirstNext(base_scan, r1.getRowArray());
    testOpsBeforeFirstNext(index_scan, r1.getRowArray());
    // see if commit closed the base_scan.
    if (base_scan.next())
        throw T_Fail.testFailMsg("(holdCursor) next() should return false, commit should close base_scan.");
    // see if commit closed the base_scan.
    if (index_scan.next())
        throw T_Fail.testFailMsg("(holdCursor) next() should return false, commit should close base_scan.");
    tc.commit();
    // Open another scan on the conglomerate.
    base_scan = tc.openScan(base_id, // hold cursor open across commit
    true, // for update
    TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, // all columns, all as objects
    (FormatableBitSet) null, // start position - first row in conglomerate
    null, // unused if start position is null.
    0, // qualifier - accept all rows
    null, // stop position - last row in conglomerate
    null, // unused if stop position is null.
    0);
    // Open scan on the index table.
    index_scan = tc.openScan(index_id, // don't hold
    true, // for update
    TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, // all columns, all as objects
    (FormatableBitSet) null, // start position - first row in conglomerate
    null, // unused if start position is null.
    0, // qualifier - accept all rows
    null, // stop position - last row in conglomerate
    null, // unused if stop position is null.
    0);
    tc.commit();
    testOpsBeforeFirstNext(base_scan, r1.getRowArray());
    testOpsBeforeFirstNext(index_scan, r1.getRowArray());
    // move cursor to be positioned on 0
    if (!base_scan.next())
        throw T_Fail.testFailMsg("(holdCursor) next() should not fail, commit should close hold scan.");
    // move cursor to be positioned on 0
    if (!index_scan.next())
        throw T_Fail.testFailMsg("(holdCursor) next() should not fail, commit should close hold scan.");
    // the 1st next should return the 1st row - ie. 0
    base_scan.fetch(r1.getRowArray());
    long key_value = ((SQLLongint) r1.getCol(0)).getLong();
    if (key_value != 0)
        throw T_Fail.testFailMsg("(holdCursor) 1st row is not 0.");
    index_scan.fetch(r1.getRowArray());
    key_value = ((SQLLongint) r1.getCol(0)).getLong();
    if (key_value != 0)
        throw T_Fail.testFailMsg("(holdCursor) 1st row is not 0.");
    // move cursor to be positioned on 1
    base_scan.next();
    index_scan.next();
    tc.commit();
    testOpsBeforeFirstNext(base_scan, r1.getRowArray());
    testOpsBeforeFirstNext(index_scan, r1.getRowArray());
    // should be able call get and set even after next'ing through the rows.
    row_count = base_scan.getEstimatedRowCount();
    base_scan.setEstimatedRowCount(5);
    row_count = base_scan.getEstimatedRowCount();
    // should be able call get and set even after next'ing through the rows.
    row_count = index_scan.getEstimatedRowCount();
    index_scan.setEstimatedRowCount(5);
    row_count = index_scan.getEstimatedRowCount();
    if (row_count != 5)
        throw T_Fail.testFailMsg("(holdCursor) some problem with get/set row count.");
    // move cursor to be positioned on 2
    if (!base_scan.next())
        throw T_Fail.testFailMsg("(holdCursor) next() should not fail, commit should close hold base_scan.");
    if (!index_scan.next())
        throw T_Fail.testFailMsg("(holdCursor) next() should not fail, commit should close hold base_scan.");
    // the 1st next should return the 1st row - ie. 0
    base_scan.fetch(r1.getRowArray());
    key_value = ((SQLLongint) r1.getCol(0)).getLong();
    if (key_value != 2)
        throw T_Fail.testFailMsg("(holdCursor) 1st row is not 0.");
    index_scan.fetch(r1.getRowArray());
    key_value = ((SQLLongint) r1.getCol(0)).getLong();
    if (key_value != 2)
        throw T_Fail.testFailMsg("(holdCursor) 1st row is not 0.");
    // move cursor to be positioned on 3
    base_scan.next();
    base_scan.delete();
    index_scan.next();
    index_scan.delete();
    // move cursor to be positioned on 4
    base_scan.next();
    index_scan.next();
    // move cursor past the end, thus closing it.
    base_scan.next();
    index_scan.next();
    tc.commit();
    // should be able call get and set even after next'ing through the rows.
    row_count = base_scan.getEstimatedRowCount();
    base_scan.setEstimatedRowCount(15);
    row_count = base_scan.getEstimatedRowCount();
    if (row_count != 15)
        throw T_Fail.testFailMsg("(holdCursor) some problem with get/set row count.");
    row_count = index_scan.getEstimatedRowCount();
    index_scan.setEstimatedRowCount(15);
    row_count = index_scan.getEstimatedRowCount();
    if (row_count != 15)
        throw T_Fail.testFailMsg("(holdCursor) some problem with get/set row count.");
    testOpsBeforeFirstNext(base_scan, r1.getRowArray());
    testOpsBeforeFirstNext(index_scan, r1.getRowArray());
    if (base_scan.next())
        throw T_Fail.testFailMsg("(holdCursor) next() should fail, the base_scan has been closed by progressing to end.");
    if (index_scan.next())
        throw T_Fail.testFailMsg("(holdCursor) next() should fail, the base_scan has been closed by progressing to end.");
    tc.commit();
    base_scan.close();
    index_scan.close();
    REPORT("(holdCursor) succeeded");
    return true;
}
Also used : SQLLongint(org.apache.derby.iapi.types.SQLLongint) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) RowLocation(org.apache.derby.iapi.types.RowLocation) SQLLongint(org.apache.derby.iapi.types.SQLLongint)

Example 25 with RowLocation

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

the class T_AccessFactory method deletetest.

// Insert a single row with a single column containing
// the first argument integer, delete it, make sure subsequent
// delete, replace, and replace a single column return false.
// 
protected boolean deletetest(TransactionController tc, long conglomid, int value1, int value2) throws StandardException, T_Fail {
    boolean ret_val;
    // Open the conglomerate.
    ConglomerateController cc = tc.openConglomerate(conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
    // Create a row.
    T_AccessRow r1 = new T_AccessRow(1);
    r1.setCol(0, new SQLInteger(value1));
    // Get a location template
    RowLocation rowloc = cc.newRowLocationTemplate();
    // Insert the row and remember its location.
    cc.insertAndFetchLocation(r1.getRowArray(), rowloc);
    // delete it.
    if (!cc.delete(rowloc)) {
        throw T_Fail.testFailMsg("(deleteTest) delete of row failed");
    }
    // subsequent replace, update a single column, and delete
    // should return false
    // update single column
    DataValueDescriptor[] update_row = new DataValueDescriptor[1];
    FormatableBitSet update_desc = new FormatableBitSet(1);
    update_desc.set(0);
    if (cc.replace(rowloc, update_row, update_desc)) {
        throw T_Fail.testFailMsg("(deleteTest) partial column row replace returned true on del row");
    }
    // update whole row.
    if (cc.replace(rowloc, r1.getRowArray(), (FormatableBitSet) null)) {
        throw T_Fail.testFailMsg("(deleteTest) update returned true on del row");
    }
    if (cc.delete(rowloc)) {
        throw T_Fail.testFailMsg("(deleteTest) delete returned true on del row");
    }
    // Close the conglomerate.
    cc.close();
    return true;
}
Also used : FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) RowLocation(org.apache.derby.iapi.types.RowLocation) SQLInteger(org.apache.derby.iapi.types.SQLInteger)

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