Search in sources :

Example 26 with CacheOperationContext

use of org.apache.ignite.internal.processors.cache.CacheOperationContext in project ignite by apache.

the class IgniteH2Indexing method executeUpdateDistributed.

/**
 * @param qryId Query id.
 * @param qryDesc Query descriptor.
 * @param qryParams Query parameters.
 * @param dml DML statement.
 * @param cancel Query cancel.
 * @return Update result wrapped into {@link GridQueryFieldsResult}
 * @throws IgniteCheckedException if failed.
 */
@SuppressWarnings("unchecked")
private List<QueryCursorImpl<List<?>>> executeUpdateDistributed(long qryId, QueryDescriptor qryDesc, QueryParameters qryParams, QueryParserResultDml dml, GridQueryCancel cancel) throws IgniteCheckedException {
    if (qryDesc.batched()) {
        Collection<UpdateResult> ress;
        List<Object[]> argss = qryParams.batchedArguments();
        UpdatePlan plan = dml.plan();
        GridCacheContext<?, ?> cctx = plan.cacheContext();
        // For MVCC case, let's enlist batch elements one by one.
        if (plan.hasRows() && plan.mode() == UpdateMode.INSERT && !cctx.mvccEnabled()) {
            CacheOperationContext opCtx = DmlUtils.setKeepBinaryContext(cctx);
            try {
                List<List<List<?>>> cur = plan.createRows(argss);
                // TODO: IGNITE-11176 - Need to support cancellation
                ress = DmlUtils.processSelectResultBatched(plan, cur, qryParams.updateBatchSize());
            } finally {
                DmlUtils.restoreKeepBinaryContext(cctx, opCtx);
            }
        } else {
            // Fallback to previous mode.
            ress = new ArrayList<>(argss.size());
            SQLException batchException = null;
            int[] cntPerRow = new int[argss.size()];
            int cntr = 0;
            for (Object[] args : argss) {
                UpdateResult res;
                try {
                    res = executeUpdate(qryId, qryDesc, qryParams.toSingleBatchedArguments(args), dml, false, null, cancel);
                    cntPerRow[cntr++] = (int) res.counter();
                    ress.add(res);
                } catch (Exception e) {
                    SQLException sqlEx = QueryUtils.toSqlException(e);
                    batchException = DmlUtils.chainException(batchException, sqlEx);
                    cntPerRow[cntr++] = Statement.EXECUTE_FAILED;
                }
            }
            if (batchException != null) {
                BatchUpdateException e = new BatchUpdateException(batchException.getMessage(), batchException.getSQLState(), batchException.getErrorCode(), cntPerRow, batchException);
                throw new IgniteCheckedException(e);
            }
        }
        ArrayList<QueryCursorImpl<List<?>>> resCurs = new ArrayList<>(ress.size());
        for (UpdateResult res : ress) {
            res.throwIfError();
            QueryCursorImpl<List<?>> resCur = (QueryCursorImpl<List<?>>) new QueryCursorImpl(singletonList(singletonList(res.counter())), cancel, false, false);
            resCur.fieldsMeta(UPDATE_RESULT_META);
            resCurs.add(resCur);
        }
        return resCurs;
    } else {
        UpdateResult res = executeUpdate(qryId, qryDesc, qryParams, dml, false, null, cancel);
        res.throwIfError();
        QueryCursorImpl<List<?>> resCur = (QueryCursorImpl<List<?>>) new QueryCursorImpl(singletonList(singletonList(res.counter())), cancel, false, false);
        resCur.fieldsMeta(UPDATE_RESULT_META);
        resCur.partitionResult(res.partitionResult());
        return singletonList(resCur);
    }
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) BatchUpdateException(java.sql.BatchUpdateException) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteClusterReadOnlyException(org.apache.ignite.internal.processors.cache.distributed.dht.IgniteClusterReadOnlyException) CacheServerNotFoundException(org.apache.ignite.cache.CacheServerNotFoundException) SQLException(java.sql.SQLException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan) BatchUpdateException(java.sql.BatchUpdateException)

Example 27 with CacheOperationContext

use of org.apache.ignite.internal.processors.cache.CacheOperationContext in project ignite by apache.

the class GridNearTransactionalCache method getAllAsync.

/**
 * {@inheritDoc}
 */
@Override
public IgniteInternalFuture<Map<K, V>> getAllAsync(@Nullable final Collection<? extends K> keys, boolean forcePrimary, boolean skipTx, @Nullable UUID subjId, String taskName, final boolean deserializeBinary, final boolean recovery, final boolean skipVals, final boolean needVer) {
    ctx.checkSecurity(SecurityPermission.CACHE_READ);
    if (F.isEmpty(keys))
        return new GridFinishedFuture<>(Collections.<K, V>emptyMap());
    if (keyCheck)
        validateCacheKeys(keys);
    GridNearTxLocal tx = ctx.tm().threadLocalTx(ctx);
    CacheOperationContext opCtx = ctx.operationContextPerCall();
    final boolean skipStore = opCtx != null && opCtx.skipStore();
    if (tx != null && !tx.implicit() && !skipTx) {
        return asyncOp(tx, new AsyncOp<Map<K, V>>(keys) {

            @Override
            public IgniteInternalFuture<Map<K, V>> op(GridNearTxLocal tx, AffinityTopologyVersion readyTopVer) {
                return tx.getAllAsync(ctx, readyTopVer, ctx.cacheKeysView(keys), deserializeBinary, skipVals, false, skipStore, recovery, needVer);
            }
        }, opCtx, /*retry*/
        false);
    }
    subjId = ctx.subjectIdPerCall(subjId, opCtx);
    return loadAsync(null, ctx.cacheKeysView(keys), forcePrimary, subjId, taskName, deserializeBinary, recovery, skipVals ? null : opCtx != null ? opCtx.expiry() : null, skipVals, skipStore, needVer);
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Map(java.util.Map)

Example 28 with CacheOperationContext

use of org.apache.ignite.internal.processors.cache.CacheOperationContext in project ignite by apache.

the class GridDhtAtomicCache method getAsync.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteInternalFuture<V> getAsync(final K key, final boolean forcePrimary, final boolean skipTx, @Nullable UUID subjId, final String taskName, final boolean deserializeBinary, final boolean skipVals, final boolean needVer) {
    ctx.checkSecurity(SecurityPermission.CACHE_READ);
    if (keyCheck)
        validateCacheKey(key);
    CacheOperationContext opCtx = ctx.operationContextPerCall();
    subjId = ctx.subjectIdPerCall(null, opCtx);
    final UUID subjId0 = subjId;
    final ExpiryPolicy expiryPlc = skipVals ? null : opCtx != null ? opCtx.expiry() : null;
    final boolean skipStore = opCtx != null && opCtx.skipStore();
    final boolean recovery = opCtx != null && opCtx.recovery();
    return asyncOp(new CO<IgniteInternalFuture<V>>() {

        @Override
        public IgniteInternalFuture<V> apply() {
            return getAsync0(ctx.toCacheKeyObject(key), forcePrimary, subjId0, taskName, deserializeBinary, recovery, expiryPlc, skipVals, skipStore, needVer);
        }
    });
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) UUID(java.util.UUID) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture)

Example 29 with CacheOperationContext

use of org.apache.ignite.internal.processors.cache.CacheOperationContext in project ignite by apache.

the class GridDhtAtomicCache method getAllAsyncInternal.

/**
 * @param keys Keys.
 * @param forcePrimary Force primary flag.
 * @param subjId Subject ID.
 * @param taskName Task name.
 * @param deserializeBinary Deserialize binary flag.
 * @param skipVals Skip values flag.
 * @param needVer Need version flag.
 * @param asyncOp Async operation flag.
 * @return Future.
 */
private IgniteInternalFuture<Map<K, V>> getAllAsyncInternal(@Nullable final Collection<? extends K> keys, final boolean forcePrimary, @Nullable UUID subjId, final String taskName, final boolean deserializeBinary, final boolean recovery, final boolean skipVals, final boolean needVer, boolean asyncOp) {
    ctx.checkSecurity(SecurityPermission.CACHE_READ);
    if (F.isEmpty(keys))
        return new GridFinishedFuture<>(Collections.<K, V>emptyMap());
    if (keyCheck)
        validateCacheKeys(keys);
    CacheOperationContext opCtx = ctx.operationContextPerCall();
    subjId = ctx.subjectIdPerCall(subjId, opCtx);
    final UUID subjId0 = subjId;
    final ExpiryPolicy expiryPlc = skipVals ? null : opCtx != null ? opCtx.expiry() : null;
    final boolean skipStore = opCtx != null && opCtx.skipStore();
    if (asyncOp) {
        return asyncOp(new CO<IgniteInternalFuture<Map<K, V>>>() {

            @Override
            public IgniteInternalFuture<Map<K, V>> apply() {
                return getAllAsync0(ctx.cacheKeysView(keys), forcePrimary, subjId0, taskName, deserializeBinary, recovery, expiryPlc, skipVals, skipStore, needVer);
            }
        });
    } else {
        return getAllAsync0(ctx.cacheKeysView(keys), forcePrimary, subjId0, taskName, deserializeBinary, recovery, expiryPlc, skipVals, skipStore, needVer);
    }
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) UUID(java.util.UUID) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture)

Example 30 with CacheOperationContext

use of org.apache.ignite.internal.processors.cache.CacheOperationContext in project ignite by apache.

the class GridDhtAtomicCache method updateAll0.

/**
 * Entry point for all public API put/transform methods.
 *
 * @param map Put map. Either {@code map}, {@code invokeMap} or {@code conflictPutMap} should be passed.
 * @param invokeMap Invoke map. Either {@code map}, {@code invokeMap} or {@code conflictPutMap} should be passed.
 * @param invokeArgs Optional arguments for EntryProcessor.
 * @param conflictPutMap Conflict put map.
 * @param conflictRmvMap Conflict remove map.
 * @param retval Return value required flag.
 * @param rawRetval Return {@code GridCacheReturn} instance.
 * @param async Async operation flag.
 * @return Completion future.
 */
@SuppressWarnings("ConstantConditions")
private IgniteInternalFuture updateAll0(@Nullable Map<? extends K, ? extends V> map, @Nullable Map<? extends K, ? extends EntryProcessor> invokeMap, @Nullable Object[] invokeArgs, @Nullable Map<KeyCacheObject, GridCacheDrInfo> conflictPutMap, @Nullable Map<KeyCacheObject, GridCacheVersion> conflictRmvMap, final boolean retval, final boolean rawRetval, final GridCacheOperation op, boolean async) {
    assert ctx.updatesAllowed();
    ctx.checkSecurity(SecurityPermission.CACHE_PUT);
    final CacheOperationContext opCtx = ctx.operationContextPerCall();
    if (opCtx != null && opCtx.hasDataCenterId()) {
        assert conflictPutMap == null : conflictPutMap;
        assert conflictRmvMap == null : conflictRmvMap;
        if (op == GridCacheOperation.TRANSFORM) {
            assert invokeMap != null : invokeMap;
            conflictPutMap = F.viewReadOnly((Map) invokeMap, new IgniteClosure<EntryProcessor, GridCacheDrInfo>() {

                @Override
                public GridCacheDrInfo apply(EntryProcessor o) {
                    return new GridCacheDrInfo(o, nextVersion(opCtx.dataCenterId()));
                }
            });
            invokeMap = null;
        } else if (op == GridCacheOperation.DELETE) {
            assert map != null : map;
            conflictRmvMap = F.viewReadOnly((Map) map, new IgniteClosure<V, GridCacheVersion>() {

                @Override
                public GridCacheVersion apply(V o) {
                    return nextVersion(opCtx.dataCenterId());
                }
            });
            map = null;
        } else {
            assert map != null : map;
            conflictPutMap = F.viewReadOnly((Map) map, new IgniteClosure<V, GridCacheDrInfo>() {

                @Override
                public GridCacheDrInfo apply(V o) {
                    return new GridCacheDrInfo(ctx.toCacheObject(o), nextVersion(opCtx.dataCenterId()));
                }
            });
            map = null;
        }
    }
    int taskNameHash = ctx.kernalContext().job().currentTaskNameHash();
    final GridNearAtomicUpdateFuture updateFut = new GridNearAtomicUpdateFuture(ctx, this, ctx.config().getWriteSynchronizationMode(), op, map != null ? map.keySet() : invokeMap != null ? invokeMap.keySet() : conflictPutMap != null ? conflictPutMap.keySet() : conflictRmvMap.keySet(), map != null ? map.values() : invokeMap != null ? invokeMap.values() : null, invokeArgs, (Collection) (conflictPutMap != null ? conflictPutMap.values() : null), conflictRmvMap != null ? conflictRmvMap.values() : null, retval, rawRetval, opCtx != null ? opCtx.expiry() : null, CU.filterArray(null), taskNameHash, opCtx != null && opCtx.skipStore(), opCtx != null && opCtx.isKeepBinary(), opCtx != null && opCtx.recovery(), opCtx != null && opCtx.noRetries() ? 1 : MAX_RETRIES);
    if (async) {
        return asyncOp(new CO<IgniteInternalFuture<Object>>() {

            @Override
            public IgniteInternalFuture<Object> apply() {
                updateFut.map();
                return updateFut;
            }
        });
    } else {
        updateFut.map();
        return updateFut;
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridCacheDrInfo(org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo) EntryProcessor(javax.cache.processor.EntryProcessor) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) GridCacheConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap) HashMap(java.util.HashMap)

Aggregations

CacheOperationContext (org.apache.ignite.internal.processors.cache.CacheOperationContext)48 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)18 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)17 Map (java.util.Map)16 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)16 IgniteCacheExpiryPolicy (org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy)13 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)12 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)12 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)11 LinkedHashMap (java.util.LinkedHashMap)9 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)9 HashMap (java.util.HashMap)7 GridCacheConcurrentMap (org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap)7 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)7 ArrayList (java.util.ArrayList)6 CacheException (javax.cache.CacheException)6 NodeStoppingException (org.apache.ignite.internal.NodeStoppingException)6 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)6 GridCacheReturn (org.apache.ignite.internal.processors.cache.GridCacheReturn)5 GridTimeoutObject (org.apache.ignite.internal.processors.timeout.GridTimeoutObject)5