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;
}
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();
}
}
}
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;
}
Aggregations