Search in sources :

Example 1 with MultiArrayOutputStream

use of org.teiid.core.util.MultiArrayOutputStream in project teiid by teiid.

the class Streamable method writeExternal.

@Override
public void writeExternal(ObjectOutput out) throws IOException {
    try {
        length();
    } catch (SQLException e) {
    }
    out.writeLong(length);
    boolean writeBuffer = false;
    MultiArrayOutputStream baos = null;
    if (referenceStreamId == null) {
        // TODO: detect when this buffering is not necessary
        if (length > Integer.MAX_VALUE) {
            // $NON-NLS-1$
            throw new AssertionError("Should not inline a lob of length " + length);
        }
        if (length > 0) {
            baos = new MultiArrayOutputStream((int) length);
        } else {
            baos = new MultiArrayOutputStream(256);
        }
        DataOutputStream dataOutput = new DataOutputStream(baos);
        try {
            writeReference(dataOutput);
            dataOutput.close();
            writeBuffer = true;
        } catch (IOException e) {
            logger.log(Level.WARNING, e.getMessage());
            // $NON-NLS-1$
            referenceStreamId = "error";
        }
    }
    out.writeObject(referenceStreamId);
    if (writeBuffer) {
        baos.writeTo(out);
    }
}
Also used : SQLException(java.sql.SQLException) DataOutputStream(java.io.DataOutputStream) MultiArrayOutputStream(org.teiid.core.util.MultiArrayOutputStream) IOException(java.io.IOException)

Example 2 with MultiArrayOutputStream

use of org.teiid.core.util.MultiArrayOutputStream in project teiid by teiid.

the class PreparedStatementImpl method executeBatch.

@Override
public int[] executeBatch() throws SQLException {
    if (batchParameterList == null || batchParameterList.isEmpty()) {
        return new int[0];
    }
    try {
        // check to see if we need to split large batches
        int[] allUpdateCounts = null;
        if (batchParameterList.size() > 256 && !this.getConnection().getServerConnection().isLocal()) {
            MultiArrayOutputStream maos = new MultiArrayOutputStream(1 << 13);
            try {
                CompactObjectOutputStream coos = new CompactObjectOutputStream(maos);
                List<List<Object>> copy = new ArrayList<List<Object>>(this.batchParameterList);
                int start = 0;
                for (int i = 0; i < copy.size(); i++) {
                    coos.writeObject(copy.get(i));
                    coos.flush();
                    // TODO: we could use max message size from the server
                    if (maos.getCount() > (1 << 19) && i + 1 < copy.size()) {
                        this.batchParameterList = copy.subList(start, i + 1);
                        start = i + 1;
                        executeSql(new String[] { this.prepareSql }, true, ResultsMode.UPDATECOUNT, true, null);
                        if (allUpdateCounts == null) {
                            allUpdateCounts = this.updateCounts;
                        } else {
                            allUpdateCounts = concatArrays(allUpdateCounts, this.updateCounts);
                        }
                        coos.reset();
                        maos.reset(0);
                    }
                }
                if (allUpdateCounts != null) {
                    if (start < copy.size()) {
                        this.batchParameterList = copy.subList(start, copy.size());
                        executeSql(new String[] { this.prepareSql }, true, ResultsMode.UPDATECOUNT, true, null);
                        allUpdateCounts = concatArrays(allUpdateCounts, this.updateCounts);
                    }
                    this.updateCounts = allUpdateCounts;
                }
            } catch (IOException e) {
                TeiidSQLException ex = TeiidSQLException.create(e);
                throw new BatchUpdateException(e.getMessage(), ex.getSQLState(), ex.getErrorCode(), allUpdateCounts == null ? new int[0] : allUpdateCounts, e);
            } catch (BatchUpdateException e) {
                if (allUpdateCounts == null) {
                    throw e;
                }
                allUpdateCounts = concatArrays(allUpdateCounts, e.getUpdateCounts());
                throw new BatchUpdateException(e.getMessage(), e.getSQLState(), e.getErrorCode(), allUpdateCounts, e);
            }
        }
        if (allUpdateCounts == null) {
            executeSql(new String[] { this.prepareSql }, true, ResultsMode.UPDATECOUNT, true, null);
        }
    } finally {
        batchParameterList.clear();
    }
    return this.updateCounts;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MultiArrayOutputStream(org.teiid.core.util.MultiArrayOutputStream) IOException(java.io.IOException) CompactObjectOutputStream(org.teiid.netty.handler.codec.serialization.CompactObjectOutputStream)

Example 3 with MultiArrayOutputStream

use of org.teiid.core.util.MultiArrayOutputStream in project teiid by teiid.

the class ResultsMessage method serialize.

/**
 * Serialize the result data
 * @return the size of the data bytes
 * @throws IOException
 */
public int serialize(boolean keepSerialization) throws IOException {
    if (serializationBuffer == null) {
        serializationBuffer = new MultiArrayOutputStream(1 << 13);
        CompactObjectOutputStream oos = new CompactObjectOutputStream(serializationBuffer);
        BatchSerializer.writeBatch(oos, dataTypes, results, clientSerializationVersion);
        oos.close();
    }
    int result = serializationBuffer.getCount();
    if (!keepSerialization) {
        serializationBuffer = null;
    }
    return result;
}
Also used : MultiArrayOutputStream(org.teiid.core.util.MultiArrayOutputStream) CompactObjectOutputStream(org.teiid.netty.handler.codec.serialization.CompactObjectOutputStream)

Aggregations

MultiArrayOutputStream (org.teiid.core.util.MultiArrayOutputStream)3 IOException (java.io.IOException)2 CompactObjectOutputStream (org.teiid.netty.handler.codec.serialization.CompactObjectOutputStream)2 DataOutputStream (java.io.DataOutputStream)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1