Search in sources :

Example 1 with CompactObjectOutputStream

use of org.teiid.netty.handler.codec.serialization.CompactObjectOutputStream 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 2 with CompactObjectOutputStream

use of org.teiid.netty.handler.codec.serialization.CompactObjectOutputStream in project teiid by teiid.

the class ObjectEncoder method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    ByteBuf out = allocateBuffer(ctx, this.estimatedLength, this.preferDirect);
    int startIdx = out.writerIndex();
    ByteBufOutputStream bout = new ByteBufOutputStream(out);
    bout.write(LENGTH_PLACEHOLDER);
    final CompactObjectOutputStream oout = new CompactObjectOutputStream(bout);
    try {
        oout.writeObject(msg);
        ExternalizeUtil.writeCollection(oout, oout.getReferences());
        oout.flush();
        oout.close();
        int endIdx = out.writerIndex();
        out.setInt(startIdx, endIdx - startIdx - 4);
        if (out.isReadable()) {
            ctx.write(out, promise);
            for (InputStream is : oout.getStreams()) {
                ctx.write(new AnonymousChunkedStream(new BufferedInputStream(is, CHUNK_SIZE)), promise);
            }
        } else {
            out.release();
            ctx.write(Unpooled.EMPTY_BUFFER, promise);
        }
        ctx.flush();
        out = null;
    } catch (Throwable t) {
        throw new FailedWriteException(msg, t);
    } finally {
        if (out != null) {
            out.release();
        }
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ObjectInputStream(java.io.ObjectInputStream) ObjectDecoderInputStream(org.teiid.netty.handler.codec.serialization.ObjectDecoderInputStream) InputStream(java.io.InputStream) ByteBuf(io.netty.buffer.ByteBuf) CompactObjectOutputStream(org.teiid.netty.handler.codec.serialization.CompactObjectOutputStream)

Example 3 with CompactObjectOutputStream

use of org.teiid.netty.handler.codec.serialization.CompactObjectOutputStream 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

CompactObjectOutputStream (org.teiid.netty.handler.codec.serialization.CompactObjectOutputStream)3 MultiArrayOutputStream (org.teiid.core.util.MultiArrayOutputStream)2 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ObjectDecoderInputStream (org.teiid.netty.handler.codec.serialization.ObjectDecoderInputStream)1