Search in sources :

Example 1 with Storable

use of org.apache.derby.iapi.services.io.Storable 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 Storable

use of org.apache.derby.iapi.services.io.Storable in project derby by apache.

the class T_RecoverBadLog method RTest1.

/*
	 * test recovery of test 1
	 */
void RTest1() throws T_Fail, StandardException {
    long cid = find(key(1, 1));
    if (cid < 0) {
        REPORT("bad log test1 not run");
        return;
    }
    int numcol = (int) find(key(1, 2));
    Transaction t = t_util.t_startTransaction();
    try {
        ContainerHandle c = t_util.t_openContainer(t, 0, cid, true);
        Page page = t_util.t_getPage(c, ContainerHandle.FIRST_PAGE_NUMBER);
        int optimisticNumcol = (int) ((RawStoreFactory.PAGE_SIZE_MINIMUM * 8) / (10 * 20));
        T_RawStoreRow bigrow = new T_RawStoreRow(optimisticNumcol);
        for (int i = 0; i < optimisticNumcol; i++) bigrow.setColumn(i, (String) null);
        page.fetchFromSlot((RecordHandle) null, 0, bigrow.getRow(), (FetchDescriptor) null, false);
        Storable column;
        // the original 20 char string
        String string1 = "01234567890123456789";
        for (int i = 0; i < numcol; i++) {
            column = bigrow.getStorableColumn(i);
            if (!(column.toString().equals(string1)))
                throw T_Fail.testFailMsg("Column " + i + " value incorrect, got :" + column.toString());
        }
        for (int i = numcol; i < optimisticNumcol; i++) {
            column = bigrow.getStorableColumn(i);
            if (!column.isNull())
                throw T_Fail.testFailMsg("Column " + i + " expect Null, got : " + column.toString());
        }
        REPORT("RTest1 passed");
    } finally {
        t_util.t_commit(t);
        t.close();
    }
}
Also used : Storable(org.apache.derby.iapi.services.io.Storable)

Example 3 with Storable

use of org.apache.derby.iapi.services.io.Storable in project derby by apache.

the class T_RecoverBadLog method RTest6.

/*
	 * test recovery of test 6
	 */
void RTest6() throws T_Fail, StandardException {
    long cid = find(key(6, 1));
    if (cid < 0) {
        REPORT("bad log test6 not run");
        return;
    }
    int numcol = (int) find(key(6, 2));
    Transaction t = t_util.t_startTransaction();
    try {
        ContainerHandle c = t_util.t_openContainer(t, 0, cid, true);
        Page page = t_util.t_getPage(c, ContainerHandle.FIRST_PAGE_NUMBER);
        int optimisticNumcol = (int) ((RawStoreFactory.PAGE_SIZE_MINIMUM * 8) / (10 * 20));
        T_RawStoreRow bigrow = new T_RawStoreRow(optimisticNumcol);
        for (int i = 0; i < optimisticNumcol; i++) bigrow.setColumn(i, (String) null);
        page.fetchFromSlot((RecordHandle) null, 0, bigrow.getRow(), (FetchDescriptor) null, false);
        Storable column;
        // the original 20 char string
        String string1 = "01234567890123456789";
        for (int i = 0; i < numcol; i++) {
            column = bigrow.getStorableColumn(i);
            if (!(column.toString().equals(string1)))
                throw T_Fail.testFailMsg("Column " + i + " value incorrect, got :" + column.toString());
        }
        for (int i = numcol; i < optimisticNumcol; i++) {
            column = bigrow.getStorableColumn(i);
            if (!column.isNull())
                throw T_Fail.testFailMsg("Column " + i + " expect Null, got : " + column.toString());
        }
        REPORT("RTest6 passed");
    } finally {
        t_util.t_commit(t);
        t.close();
    }
}
Also used : Storable(org.apache.derby.iapi.services.io.Storable)

Example 4 with Storable

use of org.apache.derby.iapi.services.io.Storable in project derby by apache.

the class StreamFileContainer method writeColumn.

private void writeColumn(Object column) throws StandardException, IOException {
    int fieldStatus = FIELD_STATUS;
    if (column == null) {
        // just write a non-existent header.
        fieldStatus = StoredFieldHeader.setNonexistent(fieldStatus);
        StoredFieldHeader.write(out, fieldStatus, 0, LARGE_SLOT_SIZE);
        return;
    }
    // if the column is a null column, write the field header now.
    if (column instanceof Storable) {
        Storable sColumn = (Storable) column;
        if (sColumn.isNull()) {
            fieldStatus = StoredFieldHeader.setNull(fieldStatus, true);
            StoredFieldHeader.write(out, fieldStatus, 0, LARGE_SLOT_SIZE);
            return;
        }
    }
    int beginPosition = out.getPosition();
    int fieldDataLength = 0;
    // write out the header, mostly to reserve the space
    StoredFieldHeader.write(out, fieldStatus, fieldDataLength, LARGE_SLOT_SIZE);
    if (column instanceof StreamStorable) {
        if (((StreamStorable) column).returnStream() != null) {
            column = (InputStream) ((StreamStorable) column).returnStream();
        }
    }
    if (column instanceof InputStream) {
        InputStream inColumn = (InputStream) column;
        // Set a reasonable buffer size.
        // To avoid extremely inefficient reads, and an infinite loop when
        // InputStream.available() returns zero, a lower limit is set on
        // the buffer size. To avoid using too much memory (especially in
        // multi-user environments) an upper limit is set as well.
        // The limits can be tuned, but note that using a too high default
        // or lower limit can put unnecessary pressure on the memory sub-
        // system and the GC process.
        int bufferLen = Math.min(Math.max(inColumn.available(), 64), 8192);
        byte[] bufData = new byte[bufferLen];
        do {
            int lenRead = inColumn.read(bufData);
            if (lenRead != -1) {
                fieldDataLength += lenRead;
                out.write(bufData, 0, lenRead);
            } else {
                break;
            }
        } while (true);
    } else if (column instanceof Storable) {
        Storable sColumn = (Storable) column;
        // write field data to the stream, we already handled the null case
        sColumn.writeExternal(logicalDataOut);
        fieldDataLength = out.getPosition() - beginPosition - FIELD_HEADER_SIZE;
    } else {
        // Serializable/Externalizable/Formattable
        // all look the same at this point.
        logicalDataOut.writeObject(column);
        fieldDataLength = out.getPosition() - beginPosition - FIELD_HEADER_SIZE;
    }
    // Now we go back to update the fieldDataLength in the field header
    int endPosition = out.getPosition();
    out.setPosition(beginPosition);
    StoredFieldHeader.write(out, fieldStatus, fieldDataLength, LARGE_SLOT_SIZE);
    // set position to the end of the field
    if (!StoredFieldHeader.isNull(fieldStatus))
        out.setPosition(endPosition);
}
Also used : FormatIdInputStream(org.apache.derby.iapi.services.io.FormatIdInputStream) BufferedInputStream(java.io.BufferedInputStream) LimitInputStream(org.apache.derby.iapi.services.io.LimitInputStream) InputStream(java.io.InputStream) StreamStorable(org.apache.derby.iapi.services.io.StreamStorable) Storable(org.apache.derby.iapi.services.io.Storable) StreamStorable(org.apache.derby.iapi.services.io.StreamStorable)

Example 5 with Storable

use of org.apache.derby.iapi.services.io.Storable in project derby by apache.

the class StreamFileContainer method fetchNext.

public boolean fetchNext(Object[] row) throws StandardException {
    boolean inUserCode = false;
    int columnId = 0;
    try {
        // Get the number of columns in the row.
        int numberFields = recordHeader.getNumberFields();
        int arrayPosition = 0;
        for (columnId = 0; columnId < numberFields; columnId++) {
            if (arrayPosition >= row.length)
                break;
            limitIn.clearLimit();
            // read the field header
            int fieldStatus = StoredFieldHeader.readStatus(logicalDataIn);
            int fieldDataLength = StoredFieldHeader.readFieldDataLength(logicalDataIn, fieldStatus, LARGE_SLOT_SIZE);
            limitIn.setLimit(fieldDataLength);
            if (SanityManager.DEBUG) {
                if (StoredFieldHeader.isExtensible(fieldStatus)) {
                    SanityManager.THROWASSERT("extensible fields not supported yet.  columnId = " + columnId);
                }
                SanityManager.ASSERT(!StoredFieldHeader.isOverflow(fieldStatus), "overflow field is not supported yet");
            }
            Object column = row[arrayPosition];
            // Deal with Storable columns
            if (StoredFieldHeader.isNullable(fieldStatus)) {
                if (column == null) {
                    throw StandardException.newException(SQLState.DATA_NULL_STORABLE_COLUMN, Integer.toString(columnId));
                }
                // SRW-DJD RESOLVE: - fix error message
                if (!(column instanceof Storable)) {
                    throw StandardException.newException(SQLState.DATA_NULL_STORABLE_COLUMN, column.getClass().getName());
                }
                Storable sColumn = (Storable) column;
                // is the column null ?
                if (StoredFieldHeader.isNull(fieldStatus)) {
                    sColumn.restoreToNull();
                    arrayPosition++;
                    continue;
                }
                inUserCode = true;
                sColumn.readExternal(logicalDataIn);
                inUserCode = false;
                arrayPosition++;
                continue;
            }
            // Only Storables can be null ... SRW-DJD RESOLVE: - fix error message
            if (StoredFieldHeader.isNull(fieldStatus)) {
                throw StandardException.newException(SQLState.DATA_NULL_STORABLE_COLUMN, Integer.toString(columnId));
            }
            // This is a non-extensible field, which means the caller must
            // know the correct type and thus the element in row is the
            // correct type or null. If the element implements
            // Externalizable then we can just fill it in, otherwise it
            // must be Serializable and we have to throw it away.
            Object neColumn = row[arrayPosition];
            if (neColumn instanceof Externalizable) {
                Externalizable exColumn = (Externalizable) neColumn;
                inUserCode = true;
                exColumn.readExternal(logicalDataIn);
                inUserCode = false;
                arrayPosition++;
                continue;
            }
            // neColumn will be ignored
            neColumn = null;
            inUserCode = true;
            row[arrayPosition] = logicalDataIn.readObject();
            inUserCode = false;
            arrayPosition++;
            continue;
        }
    } catch (IOException ioe) {
        // make the database corrupt, just that this field is inaccessable
        if (inUserCode) {
            if (ioe instanceof EOFException) {
                throw StandardException.newException(SQLState.DATA_STORABLE_READ_MISMATCH, ioe, logicalDataIn.getErrorInfo());
            }
            throw StandardException.newException(SQLState.DATA_STORABLE_READ_EXCEPTION, ioe, logicalDataIn.getErrorInfo());
        }
        if (ioe instanceof InvalidClassException) {
            throw StandardException.newException(SQLState.DATA_STORABLE_READ_EXCEPTION, ioe, logicalDataIn.getErrorInfo());
        }
        // column, then we know there is no more rows to fetch
        if ((ioe instanceof EOFException) && (columnId == 0)) {
            close();
            return false;
        }
        throw dataFactory.markCorrupt(StandardException.newException(SQLState.DATA_CORRUPT_STREAM_CONTAINER, ioe, identity));
    } catch (ClassNotFoundException cnfe) {
        if (SanityManager.DEBUG) {
            SanityManager.ASSERT(inUserCode);
        }
        // make the database corrupt, just that this field is inaccessable
        throw StandardException.newException(SQLState.DATA_STORABLE_READ_MISSING_CLASS, cnfe, logicalDataIn.getErrorInfo());
    } catch (LinkageError le) {
        if (inUserCode) {
            throw StandardException.newException(SQLState.DATA_STORABLE_READ_EXCEPTION, le, logicalDataIn.getErrorInfo());
        }
        throw le;
    }
    return true;
}
Also used : InvalidClassException(java.io.InvalidClassException) EOFException(java.io.EOFException) Externalizable(java.io.Externalizable) IOException(java.io.IOException) Storable(org.apache.derby.iapi.services.io.Storable) StreamStorable(org.apache.derby.iapi.services.io.StreamStorable)

Aggregations

Storable (org.apache.derby.iapi.services.io.Storable)11 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)3 StreamStorable (org.apache.derby.iapi.services.io.StreamStorable)2 BufferedInputStream (java.io.BufferedInputStream)1 EOFException (java.io.EOFException)1 Externalizable (java.io.Externalizable)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InvalidClassException (java.io.InvalidClassException)1 List (java.util.List)1 DynamicByteArrayOutputStream (org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream)1 FormatIdInputStream (org.apache.derby.iapi.services.io.FormatIdInputStream)1 FormatIdOutputStream (org.apache.derby.iapi.services.io.FormatIdOutputStream)1 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)1 LimitInputStream (org.apache.derby.iapi.services.io.LimitInputStream)1 StatementContext (org.apache.derby.iapi.sql.conn.StatementContext)1 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)1 KeyHasher (org.apache.derby.iapi.store.access.KeyHasher)1 Qualifier (org.apache.derby.iapi.store.access.Qualifier)1 DataValueFactory (org.apache.derby.iapi.types.DataValueFactory)1