Search in sources :

Example 6 with DynamicByteArrayOutputStream

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

the class UpdateFieldOperation method writeOptionalDataToBuffer.

/**
 *	  Write the old column value and and new column value as optional data.
 *	  If logical undo, writes out the entire row's before image.
 *
 *		@exception IOException Can be thrown by any of the methods of ObjectOutput.
 *		@exception StandardException Standard Derby policy.
 */
private void writeOptionalDataToBuffer(RawTransaction t, Object column) throws StandardException, IOException {
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(this.page != null);
    }
    DynamicByteArrayOutputStream logBuffer = t.getLogBuffer();
    int optionalDataStart = logBuffer.getPosition();
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(optionalDataStart == 0, "Buffer for writing optional data should start at position 0");
    }
    // the after image of the column
    this.page.logColumn(doMeSlot, fieldId, column, logBuffer, 100);
    // the BI of the column
    this.page.logField(doMeSlot, fieldId, logBuffer);
    if (undo != null) {
        // RESOLVE: we want the AFTER image of the row, not the BEFORE
        // image.   This works for now because only btree needs a logical
        // undoable updateField and it always update only the pointer field
        // to point to something else.
        // 
        // But in the future, it needs to be changed.
        this.page.logRecord(doMeSlot, BasePage.LOG_RECORD_DEFAULT, recordId, (FormatableBitSet) null, logBuffer, (RecordHandle) null);
    // log the BI of the entire row
    }
    int optionalDataLength = logBuffer.getPosition() - optionalDataStart;
    if (SanityManager.DEBUG) {
        if (optionalDataLength != logBuffer.getUsed())
            SanityManager.THROWASSERT("wrong optional data length, optionalDataLength = " + optionalDataLength + ", logBuffer.getUsed() = " + logBuffer.getUsed());
    }
    // set the position to the beginning of the buffer
    logBuffer.setPosition(optionalDataStart);
    this.preparedLog = new ByteArray(logBuffer.getByteArray(), optionalDataStart, optionalDataLength);
}
Also used : DynamicByteArrayOutputStream(org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream) ByteArray(org.apache.derby.iapi.util.ByteArray)

Example 7 with DynamicByteArrayOutputStream

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

the class CopyRowsOperation method writeOptionalDataToBuffer.

/**
 *		Write the rows that are to be copied into this page
 *
 *		@exception IOException Can be thrown by any of the methods of ObjectOutput.
 *		@exception StandardException Standard Derby policy.
 */
private void writeOptionalDataToBuffer(RawTransaction t, BasePage srcPage, int srcSlot) throws StandardException, IOException {
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(this.page != null);
        SanityManager.ASSERT(srcPage != null);
    }
    DynamicByteArrayOutputStream logBuffer = t.getLogBuffer();
    int optionalDataStart = logBuffer.getPosition();
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(optionalDataStart == 0, "Buffer for writing the optional data should start at position 0");
    }
    // check to make sure the destination page have the necessary space to
    // take the rows
    int[] spaceNeeded = new int[num_rows];
    int startPosition = logBuffer.getPosition();
    for (int i = 0; i < num_rows; i++) {
        // the recordId passed in is the record Id this row will have at
        // the destination page, not the record Id this row has on the
        // srcPage.
        srcPage.logRecord(i + srcSlot, BasePage.LOG_RECORD_DEFAULT, recordIds[i], (FormatableBitSet) null, logBuffer, (RecordHandle) null);
        spaceNeeded[i] = logBuffer.getPosition() - startPosition;
        startPosition = logBuffer.getPosition();
        // now spaceNeeded[i] has the actual record size.  However, the src
        // page may actually leave more space for the record due to
        // reserved space.  Because we want to copy the reserve space as well,
        // we need to take into account that amount.
        spaceNeeded[i] += reservedSpace[i];
    }
    // page is the destination page.
    if (!page.spaceForCopy(num_rows, spaceNeeded)) {
        throw StandardException.newException(SQLState.DATA_NO_SPACE_FOR_RECORD);
    }
    int optionalDataLength = logBuffer.getPosition() - optionalDataStart;
    if (SanityManager.DEBUG) {
        if (optionalDataLength != logBuffer.getUsed())
            SanityManager.THROWASSERT("wrong optional data length, optionalDataLength = " + optionalDataLength + ", logBuffer.getUsed() = " + logBuffer.getUsed());
    }
    // set the position to the beginning of the buffer
    logBuffer.setPosition(optionalDataStart);
    this.preparedLog = new ByteArray(logBuffer.getByteArray(), optionalDataStart, optionalDataLength);
}
Also used : DynamicByteArrayOutputStream(org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream) ByteArray(org.apache.derby.iapi.util.ByteArray)

Example 8 with DynamicByteArrayOutputStream

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

the class PurgeOperation method writeOptionalDataToBuffer.

/**
 *		Write out the purged record from the page.  Used for undo only.
 *
 *		@exception IOException Can be thrown by any of the methods of ObjectOutput.
 *		@exception StandardException Standard Derby policy.
 */
private void writeOptionalDataToBuffer(RawTransaction t, boolean needDataLogged) throws StandardException, IOException {
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(this.page != null);
    }
    DynamicByteArrayOutputStream logBuffer = t.getLogBuffer();
    int optionalDataStart = logBuffer.getPosition();
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(optionalDataStart == 0, "Buffer for writing the optional data should start at position 0");
    }
    for (int i = 0; i < num_rows; i++) {
        if (needDataLogged) {
            this.page.logRecord(i + slot, BasePage.LOG_RECORD_DEFAULT, recordIds[i], (FormatableBitSet) null, logBuffer, (RecordHandle) null);
        } else {
            this.page.logRecord(i + slot, BasePage.LOG_RECORD_FOR_PURGE, recordIds[i], (FormatableBitSet) null, logBuffer, (RecordHandle) null);
        }
    }
    int optionalDataLength = logBuffer.getPosition() - optionalDataStart;
    if (SanityManager.DEBUG) {
        if (optionalDataLength != logBuffer.getUsed())
            SanityManager.THROWASSERT("wrong optional data length, optionalDataLength = " + optionalDataLength + ", logBuffer.getUsed() = " + logBuffer.getUsed());
    }
    // set the position to the beginning of the buffer
    logBuffer.setPosition(optionalDataStart);
    this.preparedLog = new ByteArray(logBuffer.getByteArray(), optionalDataStart, optionalDataLength);
}
Also used : DynamicByteArrayOutputStream(org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream) ByteArray(org.apache.derby.iapi.util.ByteArray)

Example 9 with DynamicByteArrayOutputStream

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

the class T_StreamFile method SF002.

// this test test the rowSource over head.
// when param set to 1, also gets the overhead for writeExternal for Storables
protected void SF002(int param) throws StandardException, T_Fail {
    T_RowSource rowSource = new T_RowSource(500000, 13, 2, false, null);
    DynamicByteArrayOutputStream out = new DynamicByteArrayOutputStream(16384);
    FormatIdOutputStream logicalDataOut = new FormatIdOutputStream(out);
    long startms = System.currentTimeMillis();
    System.out.println("starting rowSource test, time: " + startms);
    try {
        FormatableBitSet validColumns = rowSource.getValidColumns();
        int numberFields = 0;
        if (validColumns != null) {
            for (int i = validColumns.size() - 1; i >= 0; i--) {
                if (validColumns.get(i)) {
                    numberFields = i + 1;
                    break;
                }
            }
        }
        DataValueDescriptor[] row = rowSource.getNextRowFromRowSource();
        while (row != null) {
            if (SanityManager.DEBUG) {
                SanityManager.ASSERT(row != null, "RowSource returned null");
            }
            int arrayPosition = -1;
            for (int i = 0; i < numberFields; i++) {
                // write each column out
                if (validColumns.get(i)) {
                    arrayPosition++;
                    DataValueDescriptor column = row[arrayPosition];
                    if (param == 1) {
                        try {
                            Storable sColumn = (Storable) column;
                            if (!sColumn.isNull()) {
                                sColumn.writeExternal(logicalDataOut);
                                out.reset();
                            }
                        } catch (IOException ioe) {
                            throw T_Fail.exceptionFail(ioe);
                        }
                    }
                }
            }
            row = rowSource.getNextRowFromRowSource();
        }
    } finally {
    }
    long endms = System.currentTimeMillis();
    long time2 = endms - startms;
    if (param != 1)
        System.out.println("ended rowSource test, time: " + endms + ", time spent = " + time2);
    else
        System.out.println("------ writeExternal called....\n ended rowSource test, time: " + endms + ", time spent = " + time2);
    PASS("SF002");
}
Also used : DynamicByteArrayOutputStream(org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) FormatIdOutputStream(org.apache.derby.iapi.services.io.FormatIdOutputStream) Storable(org.apache.derby.iapi.services.io.Storable)

Example 10 with DynamicByteArrayOutputStream

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

the class T_Undoable method writeOptionalDataToBuffer.

private void writeOptionalDataToBuffer() throws StandardException, IOException {
    if (logBuffer == null) {
        // YYZ: need to revisit this.  Do we really want to allocate this much for a buffer every time?
        // init size 1K
        logBuffer = new DynamicByteArrayOutputStream(1024);
    } else {
        logBuffer.reset();
    }
    int optionalDataStart = logBuffer.getPosition();
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(optionalDataStart == 0, "Buffer for writing the optional data should start at position 0");
    }
    // MsgTrace.traceString("{{{tu.writeOpetionalData");
    if (optionalDataLen > 0) {
        byte[] buf = new byte[optionalDataLen];
        for (int ix = 0; ix < optionalDataLen; ix++) buf[ix] = (byte) ix;
        logBuffer.write(buf);
    }
    // MsgTrace.traceString("}}}tu.writeOpetionalData");
    int optionalDataLength = logBuffer.getPosition() - optionalDataStart;
    if (SanityManager.DEBUG) {
        if (optionalDataLength != logBuffer.getUsed())
            SanityManager.THROWASSERT("wrong optional data length, optionalDataLength = " + optionalDataLength + ", logBuffer.getUsed() = " + logBuffer.getUsed());
    }
    // set the position to the beginning of the buffer
    logBuffer.setPosition(optionalDataStart);
    this.preparedLog = new ByteArray(logBuffer.getByteArray(), optionalDataStart, optionalDataLength);
}
Also used : DynamicByteArrayOutputStream(org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream) ByteArray(org.apache.derby.iapi.util.ByteArray)

Aggregations

DynamicByteArrayOutputStream (org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream)15 ByteArray (org.apache.derby.iapi.util.ByteArray)6 IOException (java.io.IOException)5 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)5 RecordHandle (org.apache.derby.iapi.store.raw.RecordHandle)4 RawTransaction (org.apache.derby.iapi.store.raw.xact.RawTransaction)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 FormatIdOutputStream (org.apache.derby.iapi.services.io.FormatIdOutputStream)2 LogicalUndo (org.apache.derby.iapi.store.access.conglomerate.LogicalUndo)2 Storable (org.apache.derby.iapi.services.io.Storable)1 AuxObject (org.apache.derby.iapi.store.raw.AuxObject)1 PageKey (org.apache.derby.iapi.store.raw.PageKey)1 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)1