Search in sources :

Example 1 with KeyHasher

use of org.apache.derby.iapi.store.access.KeyHasher in project derby by apache.

the class HashTableResultSet 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 result = null;
    DataValueDescriptor[] columns = null;
    beginTime = getCurrentTimeMillis();
    if (isOpen) {
        /* We use a do/while loop to ensure that we continue down
			 * the duplicate chain, if one exists, until we find a
			 * row that matches on all probe predicates (or the
			 * duplicate chain is exhausted.)
			 */
        do {
            if (firstNext) {
                firstNext = false;
                /* Hash key could be either a single column or multiple 
                     * columns.  If a single column, then it is the datavalue 
                     * wrapper, otherwise it is a KeyHasher.
					 */
                Object hashEntry;
                if (keyColumns.length == 1) {
                    hashEntry = ht.get(nextQualifiers[0][0].getOrderable());
                } else {
                    KeyHasher mh = new KeyHasher(keyColumns.length);
                    for (int index = 0; index < keyColumns.length; index++) {
                        // RESOLVE (mikem) - will need to change when we
                        // support OR's in qualifiers.
                        mh.setObject(index, nextQualifiers[0][index].getOrderable());
                    }
                    hashEntry = ht.get(mh);
                }
                if (hashEntry instanceof List) {
                    entryVector = (List) hashEntry;
                    entryVectorSize = entryVector.size();
                    columns = (DataValueDescriptor[]) entryVector.get(0);
                } else {
                    entryVector = null;
                    entryVectorSize = 0;
                    columns = (DataValueDescriptor[]) hashEntry;
                }
            } else if (numFetchedOnNext < entryVectorSize) {
                // We are walking a list and there are more rows left.
                columns = (DataValueDescriptor[]) entryVector.get(numFetchedOnNext);
            }
            if (columns != null) {
                if (SanityManager.DEBUG) {
                    // Columns is really a Storable[]
                    for (int i = 0; i < columns.length; i++) {
                        if (!(columns[i] instanceof Storable)) {
                            SanityManager.THROWASSERT("columns[" + i + "] expected to be Storable, not " + columns[i].getClass().getName());
                        }
                    }
                }
                // See if the entry satisfies all of the other qualifiers
                boolean qualifies = true;
                if (SanityManager.DEBUG) {
                    // we don't support 2 d qualifiers yet.
                    SanityManager.ASSERT(nextQualifiers.length == 1);
                }
                for (int index = 0; index < nextQualifiers[0].length; index++) {
                    Qualifier q = nextQualifiers[0][index];
                    qualifies = columns[q.getColumnId()].compare(q.getOperator(), q.getOrderable(), q.getOrderedNulls(), q.getUnknownRV());
                    if (q.negateCompareResult()) {
                        qualifies = !(qualifies);
                    }
                    // Stop if any predicate fails
                    if (!qualifies) {
                        break;
                    }
                }
                if (qualifies) {
                    for (int index = 0; index < columns.length; index++) {
                        nextCandidate.setColumn(index + 1, columns[index]);
                    }
                    result = doProjection(nextCandidate);
                } else {
                    result = null;
                }
                numFetchedOnNext++;
            } else {
                result = null;
            }
        } while (result == null && numFetchedOnNext < entryVectorSize);
    }
    setCurrentRow(result);
    nextTime += getElapsedMillis(beginTime);
    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 : KeyHasher(org.apache.derby.iapi.store.access.KeyHasher) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) List(java.util.List) Qualifier(org.apache.derby.iapi.store.access.Qualifier) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) Storable(org.apache.derby.iapi.services.io.Storable) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Example 2 with KeyHasher

use of org.apache.derby.iapi.store.access.KeyHasher in project derby by apache.

the class HashScanResultSet method getNextRowCore.

/**
 * Return the next row (if any) from the scan (if open).
 *
 * @exception StandardException thrown on failure to get next row
 */
public ExecRow getNextRowCore() throws StandardException {
    if (isXplainOnlyMode())
        return null;
    ExecRow result = null;
    DataValueDescriptor[] columns = null;
    beginTime = getCurrentTimeMillis();
    if (isOpen && hashtableBuilt) {
        /* We use a do/while loop to ensure that we continue down
			 * the duplicate chain, if one exists, until we find a
			 * row that matches on all probe predicates (or the
			 * duplicate chain is exhausted.)
			 */
        do {
            if (firstNext) {
                firstNext = false;
                /* Hash key could be either a single column or multiple columns.
					 * If a single column, then it is the datavalue wrapper, otherwise
					 * it is a KeyHasher.
					 */
                Object hashEntry;
                if (keyColumns.length == 1) {
                    hashEntry = hashtable.get(nextQualifiers[0][0].getOrderable());
                } else {
                    KeyHasher mh = new KeyHasher(keyColumns.length);
                    if (SanityManager.DEBUG) {
                        SanityManager.ASSERT(nextQualifiers.length == 1);
                    }
                    for (int index = 0; index < keyColumns.length; index++) {
                        // For hashing only use the AND qualifiers
                        // located in nextQualifiers[0][0...N], OR
                        // qualifiers are checked down a bit by calling
                        // qualifyRow on rows returned from hash.
                        DataValueDescriptor dvd = nextQualifiers[0][index].getOrderable();
                        if (dvd == null) {
                            mh = null;
                            break;
                        }
                        mh.setObject(index, nextQualifiers[0][index].getOrderable());
                    }
                    hashEntry = (mh == null) ? null : hashtable.get(mh);
                }
                if (hashEntry instanceof List) {
                    entryVector = (List) hashEntry;
                    entryVectorSize = entryVector.size();
                    columns = unpackHashValue(entryVector.get(0));
                } else {
                    entryVector = null;
                    entryVectorSize = 0;
                    columns = unpackHashValue(hashEntry);
                }
            } else if (numFetchedOnNext < entryVectorSize) {
                // We are walking a list and there are more rows left.
                columns = unpackHashValue(entryVector.get(numFetchedOnNext));
            }
            if (columns != null) {
                if (RowUtil.qualifyRow(columns, nextQualifiers)) {
                    setCompatRow(compactRow, columns);
                    rowsSeen++;
                    result = compactRow;
                } else {
                    result = null;
                }
                numFetchedOnNext++;
            } else {
                result = null;
            }
        } while (result == null && numFetchedOnNext < entryVectorSize);
    }
    setCurrentRow(result);
    nextTime += getElapsedMillis(beginTime);
    return result;
}
Also used : KeyHasher(org.apache.derby.iapi.store.access.KeyHasher) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) List(java.util.List) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor)

Aggregations

List (java.util.List)2 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)2 KeyHasher (org.apache.derby.iapi.store.access.KeyHasher)2 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)2 Storable (org.apache.derby.iapi.services.io.Storable)1 StatementContext (org.apache.derby.iapi.sql.conn.StatementContext)1 Qualifier (org.apache.derby.iapi.store.access.Qualifier)1