use of org.apache.ignite.internal.processors.query.h2.dml.DmlUpdateResultsIterator in project ignite by apache.
the class IgniteH2Indexing method executeUpdateTransactional.
/**
* Execute update in transactional mode.
*
* @param qryId Query id.
* @param qryDesc Query descriptor.
* @param qryParams Query parameters.
* @param dml Plan.
* @param loc Local flag.
* @param cancel Cancel hook.
* @return Update result.
* @throws IgniteCheckedException If failed.
*/
private UpdateResult executeUpdateTransactional(long qryId, QueryDescriptor qryDesc, QueryParameters qryParams, QueryParserResultDml dml, boolean loc, GridQueryCancel cancel) throws IgniteCheckedException {
UpdatePlan plan = dml.plan();
GridCacheContext cctx = plan.cacheContext();
assert cctx != null;
assert cctx.transactional();
GridNearTxLocal tx = tx(ctx);
boolean implicit = (tx == null);
boolean commit = implicit && qryParams.autoCommit();
if (implicit)
tx = txStart(cctx, qryParams.timeout());
requestSnapshot(tx);
try (GridNearTxLocal toCommit = commit ? tx : null) {
DmlDistributedPlanInfo distributedPlan = loc ? null : plan.distributedPlan();
long timeout = implicit ? tx.remainingTime() : operationTimeout(qryParams.timeout(), tx);
if (cctx.isReplicated() || distributedPlan == null || ((plan.mode() == UpdateMode.INSERT || plan.mode() == UpdateMode.MERGE) && !plan.isLocalSubquery())) {
boolean sequential = true;
UpdateSourceIterator<?> it;
if (plan.fastResult()) {
IgniteBiTuple row = plan.getFastRow(qryParams.arguments());
assert row != null;
EnlistOperation op = UpdatePlan.enlistOperation(plan.mode());
it = new DmlUpdateSingleEntryIterator<>(op, op.isDeleteOrLock() ? row.getKey() : row);
} else if (plan.hasRows()) {
it = new DmlUpdateResultsIterator(UpdatePlan.enlistOperation(plan.mode()), plan, plan.createRows(qryParams.arguments()));
} else {
SqlFieldsQuery selectFieldsQry = new SqlFieldsQuery(plan.selectQuery(), qryDesc.collocated()).setArgs(qryParams.arguments()).setDistributedJoins(qryDesc.distributedJoins()).setEnforceJoinOrder(qryDesc.enforceJoinOrder()).setLocal(qryDesc.local()).setPageSize(qryParams.pageSize()).setTimeout((int) timeout, TimeUnit.MILLISECONDS).setLazy(qryParams.lazy());
FieldsQueryCursor<List<?>> cur = executeSelectForDml(qryId, qryDesc.schemaName(), selectFieldsQry, MvccUtils.mvccTracker(cctx, tx), cancel, (int) timeout);
it = plan.iteratorForTransaction(connMgr, cur);
}
// TODO: IGNITE-11176 - Need to support cancellation
IgniteInternalFuture<Long> fut = tx.updateAsync(cctx, it, qryParams.pageSize(), timeout, sequential);
UpdateResult res = new UpdateResult(fut.get(), X.EMPTY_OBJECT_ARRAY, plan.distributedPlan() != null ? plan.distributedPlan().derivedPartitions() : null);
if (commit)
toCommit.commit();
return res;
}
int[] ids = U.toIntArray(distributedPlan.getCacheIds());
int flags = 0;
if (qryDesc.enforceJoinOrder())
flags |= GridH2QueryRequest.FLAG_ENFORCE_JOIN_ORDER;
if (distributedPlan.isReplicatedOnly())
flags |= GridH2QueryRequest.FLAG_REPLICATED;
if (qryParams.lazy())
flags |= GridH2QueryRequest.FLAG_LAZY;
flags = GridH2QueryRequest.setDataPageScanEnabled(flags, qryParams.dataPageScanEnabled());
int[] parts = PartitionResult.calculatePartitions(qryParams.partitions(), distributedPlan.derivedPartitions(), qryParams.arguments());
if (parts != null && parts.length == 0)
return new UpdateResult(0, X.EMPTY_OBJECT_ARRAY, distributedPlan.derivedPartitions());
else {
// TODO: IGNITE-11176 - Need to support cancellation
IgniteInternalFuture<Long> fut = tx.updateAsync(cctx, ids, parts, qryDesc.schemaName(), qryDesc.sql(), qryParams.arguments(), flags, qryParams.pageSize(), timeout);
UpdateResult res = new UpdateResult(fut.get(), X.EMPTY_OBJECT_ARRAY, distributedPlan.derivedPartitions());
if (commit)
toCommit.commit();
return res;
}
} catch (ClusterTopologyServerNotFoundException e) {
throw new CacheServerNotFoundException(e.getMessage(), e);
} catch (IgniteCheckedException e) {
IgniteSQLException sqlEx = X.cause(e, IgniteSQLException.class);
if (sqlEx != null)
throw sqlEx;
Exception ex = IgniteUtils.convertExceptionNoWrap(e);
if (ex instanceof IgniteException)
throw (IgniteException) ex;
U.error(log, "Error during update [localNodeId=" + ctx.localNodeId() + "]", ex);
throw new IgniteSQLException("Failed to run update. " + ex.getMessage(), ex);
} finally {
if (commit)
cctx.tm().resetContext();
}
}
Aggregations