use of io.crate.analyze.AnalyzedCommit in project crate by crate.
the class Session method execute.
@Nullable
public CompletableFuture<?> execute(String portalName, int maxRows, ResultReceiver<?> resultReceiver) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("method=execute portalName={} maxRows={}", portalName, maxRows);
}
Portal portal = getSafePortal(portalName);
var analyzedStmt = portal.analyzedStatement();
if (isReadOnly && analyzedStmt.isWriteOperation()) {
throw new ReadOnlyException(portal.preparedStmt().rawStatement());
}
if (analyzedStmt instanceof AnalyzedBegin) {
currentTransactionState = TransactionState.IN_TRANSACTION;
resultReceiver.allFinished(false);
} else if (analyzedStmt instanceof AnalyzedCommit) {
currentTransactionState = TransactionState.IDLE;
resultReceiver.allFinished(false);
return resultReceiver.completionFuture();
} else if (analyzedStmt instanceof AnalyzedDeallocate) {
String stmtToDeallocate = ((AnalyzedDeallocate) analyzedStmt).preparedStmtName();
if (stmtToDeallocate != null) {
close((byte) 'S', stmtToDeallocate);
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("deallocating all prepared statements");
}
preparedStatements.clear();
}
resultReceiver.allFinished(false);
} else if (analyzedStmt instanceof AnalyzedDiscard) {
AnalyzedDiscard discard = (AnalyzedDiscard) analyzedStmt;
// See https://www.postgresql.org/docs/current/sql-discard.html
if (discard.target() == Target.ALL) {
close();
}
resultReceiver.allFinished(false);
} else if (analyzedStmt.isWriteOperation()) {
/* We defer the execution for any other statements to `sync` messages so that we can efficiently process
* bulk operations. E.g. If we receive `INSERT INTO (x) VALUES (?)` bindings/execute multiple times
* We want to create bulk requests internally: /
* - To reduce network overhead
* - To have 1 disk flush per shard instead of 1 disk flush per item
*
* Many clients support this by doing something like this:
*
* var preparedStatement = conn.prepareStatement("...")
* for (var args in manyArgs):
* preparedStatement.execute(args)
* conn.commit()
*/
deferredExecutionsByStmt.compute(portal.preparedStmt().parsedStatement(), (key, oldValue) -> {
DeferredExecution deferredExecution = new DeferredExecution(portal, maxRows, resultReceiver);
if (oldValue == null) {
ArrayList<DeferredExecution> deferredExecutions = new ArrayList<>();
deferredExecutions.add(deferredExecution);
return deferredExecutions;
} else {
oldValue.add(deferredExecution);
return oldValue;
}
});
return resultReceiver.completionFuture();
} else {
if (!deferredExecutionsByStmt.isEmpty()) {
throw new UnsupportedOperationException("Only write operations are allowed in Batch statements");
}
if (activeExecution == null) {
activeExecution = singleExec(portal, resultReceiver, maxRows);
} else {
activeExecution = activeExecution.thenCompose(ignored -> singleExec(portal, resultReceiver, maxRows));
}
return activeExecution;
}
return null;
}
Aggregations