Search in sources :

Example 1 with FormatIdOutputStream

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

the class SQLChar method getUTF8Length.

/**
 * Get the number of bytes needed to represent a string in modified
 * UTF-8, which is the encoding used by {@code writeExternal()} and
 * {@code writeUTF()}.
 *
 * @param string the string whose length to calculate
 * @param start start index (inclusive)
 * @param end end index (exclusive)
 */
private int getUTF8Length(String string, int start, int end) throws StandardException {
    CounterOutputStream cs = new CounterOutputStream();
    try {
        FormatIdOutputStream out = new FormatIdOutputStream(cs);
        for (int i = start; i < end; i++) {
            writeUTF(out, string.charAt(i));
        }
        out.close();
    } catch (IOException ioe) {
        throw StandardException.newException(SQLState.LANG_IO_EXCEPTION, ioe, ioe.toString());
    }
    return cs.getCount();
}
Also used : CounterOutputStream(org.apache.derby.iapi.services.io.CounterOutputStream) IOException(java.io.IOException) FormatIdOutputStream(org.apache.derby.iapi.services.io.FormatIdOutputStream)

Example 2 with FormatIdOutputStream

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

the class StoredPage method createOutStreams.

/**
 * Create the output streams.
 * <p>
 * Create the output streams, these are created on demand
 * to avoid creating unrequired objects for pages that are
 * never modified during their lifetime in the cache.
 * <p>
 *
 * @exception  StandardException  Standard exception policy.
 */
private void createOutStreams() {
    rawDataOut = new ArrayOutputStream();
    rawDataOut.setData(pageData);
    logicalDataOut = new FormatIdOutputStream(rawDataOut);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) DynamicByteArrayOutputStream(org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream) ArrayOutputStream(org.apache.derby.iapi.services.io.ArrayOutputStream) FormatIdOutputStream(org.apache.derby.iapi.services.io.FormatIdOutputStream)

Example 3 with FormatIdOutputStream

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

the class StreamFileContainer method load.

/**
 * load data into this container.
 * <p>
 * populate the stream container with data in the rowSource
 * <p>
 *
 * @param rowSource The row source to get rows to load into this container.
 *
 * @exception  StandardException  Standard exception policy.
 */
public void load(RowSource rowSource) throws StandardException {
    // use this output stream to buffer rows before inserting into file.
    out = new DynamicByteArrayOutputStream(bufferSize);
    logicalDataOut = new FormatIdOutputStream(out);
    boolean encrypted = dataFactory.databaseEncrypted();
    // it is not dataFactory.getEncryptionBlockSize() aligned.
    if (encrypted) {
        if (zeroBytes == null)
            zeroBytes = new byte[dataFactory.getEncryptionBlockSize() - 1];
        out.write(zeroBytes, 0, dataFactory.getEncryptionBlockSize() - 1);
    }
    try {
        fileOut = privGetOutputStream(file);
        FormatableBitSet validColumns = rowSource.getValidColumns();
        Object[] row = rowSource.getNextRowFromRowSource();
        int numberFields = 0;
        if (validColumns != null) {
            for (int i = validColumns.getLength() - 1; i >= 0; i--) {
                if (validColumns.isSet(i)) {
                    numberFields = i + 1;
                    break;
                }
            }
        } else {
            numberFields = row.length;
        }
        // make the record header to have 0 record id
        recordHeader = new StoredRecordHeader(0, numberFields);
        // write the record header once for all the rows, directly to the
        // beginning of the file.
        int rhLen = recordHeader.write(out);
        int validColumnsSize = validColumns == null ? 0 : validColumns.getLength();
        while (row != null) {
            int arrayPosition = -1;
            for (int i = 0; i < numberFields; i++) {
                // write each column out
                if (validColumns == null) {
                    arrayPosition++;
                    Object column = row[arrayPosition];
                    writeColumn(column);
                } else {
                    if (validColumnsSize > i && validColumns.isSet(i)) {
                        arrayPosition++;
                        Object column = row[arrayPosition];
                        writeColumn(column);
                    } else {
                        // it is a non-existent column
                        writeColumn(null);
                    }
                }
                // in the buffer
                if ((out.getUsed() >= bufferSize) || ((bufferSize - out.getUsed()) < MIN_BUFFER_SIZE)) {
                    writeToFile();
                }
            }
            // get the next row and its valid columns from the rowSource
            row = rowSource.getNextRowFromRowSource();
        }
        // dataFactory.getEncryptionBlockSize() - 1 if this is an encypted database
        if (encrypted) {
            if (out.getUsed() > (dataFactory.getEncryptionBlockSize() - 1))
                writeToFile();
        } else if (out.getUsed() > 0) {
            writeToFile();
        }
    } catch (IOException ioe) {
        // handle IO error...
        throw StandardException.newException(SQLState.DATA_UNEXPECTED_EXCEPTION, ioe);
    } finally {
        close();
    }
}
Also used : DynamicByteArrayOutputStream(org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) IOException(java.io.IOException) FormatIdOutputStream(org.apache.derby.iapi.services.io.FormatIdOutputStream)

Example 4 with FormatIdOutputStream

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

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

the class FileContainer method writeHeaderToArray.

/**
 *		Write containerInfo into a byte array
 *		The container Header thus put together can be read by readHeaderFromArray.
 *
 *		@exception IOException error in writing the header
 */
private void writeHeaderToArray(byte[] a) throws IOException {
    if (SanityManager.DEBUG)
        SanityManager.ASSERT(a.length >= CONTAINER_INFO_SIZE, "header won't fit in array");
    ArrayOutputStream a_out = new ArrayOutputStream(a);
    FormatIdOutputStream outStream = new FormatIdOutputStream(a_out);
    int status = 0;
    if (getDroppedState())
        status |= FILE_DROPPED;
    if (getCommittedDropState())
        status |= FILE_COMMITTED_DROP;
    if (isReusableRecordId())
        status |= FILE_REUSABLE_RECORDID;
    a_out.setPosition(0);
    a_out.setLimit(CONTAINER_INFO_SIZE);
    outStream.writeInt(formatIdInteger);
    outStream.writeInt(status);
    outStream.writeInt(pageSize);
    outStream.writeInt(spareSpace);
    outStream.writeInt(minimumRecordSize);
    outStream.writeShort(initialPages);
    // write spare1
    outStream.writeShort(PreAllocSize);
    outStream.writeLong(firstAllocPageNumber);
    outStream.writeLong(firstAllocPageOffset);
    outStream.writeLong(containerVersion);
    outStream.writeLong(estimatedRowCount);
    outStream.writeLong(reusableRecordIdSequenceNumber);
    // Write spare3
    outStream.writeLong(0);
    checksum.reset();
    checksum.update(a, 0, CONTAINER_INFO_SIZE - CHECKSUM_SIZE);
    // write the checksum to the array
    outStream.writeLong(checksum.getValue());
    a_out.clearLimit();
}
Also used : ArrayOutputStream(org.apache.derby.iapi.services.io.ArrayOutputStream) FormatIdOutputStream(org.apache.derby.iapi.services.io.FormatIdOutputStream)

Aggregations

FormatIdOutputStream (org.apache.derby.iapi.services.io.FormatIdOutputStream)5 DynamicByteArrayOutputStream (org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream)3 IOException (java.io.IOException)2 ArrayOutputStream (org.apache.derby.iapi.services.io.ArrayOutputStream)2 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 CounterOutputStream (org.apache.derby.iapi.services.io.CounterOutputStream)1 Storable (org.apache.derby.iapi.services.io.Storable)1 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)1