use of javax.cache.processor.EntryProcessorResult in project hazelcast by hazelcast.
the class ClientCacheWriteQuorumTest method testInvokeAllOperationSuccessfulWhenQuorumSizeMet.
@Test
public void testInvokeAllOperationSuccessfulWhenQuorumSizeMet() {
HashSet<Integer> hashSet = new HashSet<Integer>();
hashSet.add(123);
EntryProcessorResult epr = cache1.invokeAll(hashSet, new SimpleEntryProcessor()).get(123);
assertNull(epr);
}
use of javax.cache.processor.EntryProcessorResult in project ignite by apache.
the class GridDhtAtomicCache method invokeAll0.
/**
* @param async Async operation flag.
* @param keys Keys.
* @param entryProcessor Entry processor.
* @param args Entry processor arguments.
* @return Future.
*/
private <T> IgniteInternalFuture<Map<K, EntryProcessorResult<T>>> invokeAll0(boolean async, Set<? extends K> keys, final EntryProcessor<K, V, T> entryProcessor, Object... args) {
A.notNull(keys, "keys", entryProcessor, "entryProcessor");
if (keyCheck)
validateCacheKeys(keys);
Map<? extends K, EntryProcessor> invokeMap = F.viewAsMap(keys, new C1<K, EntryProcessor>() {
@Override
public EntryProcessor apply(K k) {
return entryProcessor;
}
});
CacheOperationContext opCtx = ctx.operationContextPerCall();
final boolean keepBinary = opCtx != null && opCtx.isKeepBinary();
IgniteInternalFuture<Map<K, EntryProcessorResult<T>>> resFut = updateAll0(null, invokeMap, args, null, null, false, false, TRANSFORM, async);
return resFut.chain(new CX1<IgniteInternalFuture<Map<K, EntryProcessorResult<T>>>, Map<K, EntryProcessorResult<T>>>() {
@Override
public Map<K, EntryProcessorResult<T>> applyx(IgniteInternalFuture<Map<K, EntryProcessorResult<T>>> fut) throws IgniteCheckedException {
Map<Object, EntryProcessorResult> resMap = (Map) fut.get();
return ctx.unwrapInvokeResult(resMap, keepBinary);
}
});
}
use of javax.cache.processor.EntryProcessorResult in project ignite by apache.
the class GridCacheReturn method addEntryProcessResult.
/**
* @param cctx Context.
* @param key Key.
* @param key0 Key value.
* @param res Result.
* @param err Error.
* @param keepBinary Keep binary.
*/
@SuppressWarnings("unchecked")
public synchronized void addEntryProcessResult(GridCacheContext cctx, KeyCacheObject key, @Nullable Object key0, @Nullable Object res, @Nullable Exception err, boolean keepBinary) {
assert v == null || v instanceof Map : v;
assert key != null;
assert res != null || err != null;
invokeRes = true;
if (loc) {
HashMap<Object, EntryProcessorResult> resMap = (HashMap<Object, EntryProcessorResult>) v;
if (resMap == null) {
resMap = new HashMap<>();
v = resMap;
}
CacheInvokeResult res0 = err == null ? CacheInvokeResult.fromResult(res) : CacheInvokeResult.fromError(err);
Object resKey = key0 != null ? key0 : ((keepBinary && key instanceof BinaryObject) ? key : CU.value(key, cctx, true));
resMap.put(resKey, res0);
} else {
assert v == null;
assert cacheId == 0 || cacheId == cctx.cacheId();
cacheId = cctx.cacheId();
if (invokeResCol == null)
invokeResCol = new ArrayList<>();
CacheInvokeDirectResult res0 = err == null ? new CacheInvokeDirectResult(key, cctx.toCacheObject(res)) : new CacheInvokeDirectResult(key, err);
invokeResCol.add(res0);
}
}
use of javax.cache.processor.EntryProcessorResult in project ignite by apache.
the class GridLocalAtomicCache method updateWithBatch.
/**
* Updates entries using batched write-through.
*
* @param op Operation.
* @param keys Keys.
* @param vals Values.
* @param invokeArgs Optional arguments for EntryProcessor.
* @param expiryPlc Expiry policy.
* @param ver Cache version.
* @param filter Optional filter.
* @param subjId Subject ID.
* @param taskName Task name.
* @return Results map for invoke operation.
* @throws CachePartialUpdateCheckedException If update failed.
*/
@SuppressWarnings({ "ForLoopReplaceableByForEach", "unchecked" })
private Map<K, EntryProcessorResult> updateWithBatch(GridCacheOperation op, Collection<? extends K> keys, @Nullable Iterable<?> vals, @Nullable Object[] invokeArgs, @Nullable ExpiryPolicy expiryPlc, GridCacheVersion ver, @Nullable CacheEntryPredicate[] filter, boolean keepBinary, UUID subjId, String taskName) throws IgniteCheckedException {
List<GridCacheEntryEx> locked = lockEntries(keys);
try {
int size = locked.size();
Map<KeyCacheObject, CacheObject> putMap = null;
Collection<KeyCacheObject> rmvKeys = null;
List<CacheObject> writeVals = null;
Map<K, EntryProcessorResult> invokeResMap = op == TRANSFORM ? U.<K, EntryProcessorResult>newHashMap(size) : null;
List<GridCacheEntryEx> filtered = new ArrayList<>(size);
CachePartialUpdateCheckedException err = null;
Iterator<?> valsIter = vals != null ? vals.iterator() : null;
boolean intercept = ctx.config().getInterceptor() != null;
for (int i = 0; i < size; i++) {
GridCacheEntryEx entry = locked.get(i);
Object val = valsIter != null ? valsIter.next() : null;
if (val == null && op != DELETE)
throw new NullPointerException("Null value.");
try {
try {
if (!ctx.isAllLocked(entry, filter)) {
if (log.isDebugEnabled())
log.debug("Entry did not pass the filter (will skip write) [entry=" + entry + ", filter=" + Arrays.toString(filter) + ']');
continue;
}
} catch (IgniteCheckedException e) {
if (err == null)
err = partialUpdateException();
err.add(F.asList(entry.key()), e);
continue;
}
if (op == TRANSFORM) {
ctx.kernalContext().resource().inject(val, GridResourceIoc.AnnotationSet.ENTRY_PROCESSOR, ctx.name());
EntryProcessor<Object, Object, Object> entryProcessor = (EntryProcessor<Object, Object, Object>) val;
CacheObject old = entry.innerGet(null, null, /*read-through*/
true, /*update-metrics*/
true, /*event*/
true, subjId, entryProcessor, taskName, null, keepBinary);
Object oldVal = null;
CacheInvokeEntry<Object, Object> invokeEntry = new CacheInvokeEntry<>(entry.key(), old, entry.version(), keepBinary, entry);
CacheObject updated;
Object updatedVal = null;
CacheInvokeResult invokeRes = null;
try {
Object computed = entryProcessor.process(invokeEntry, invokeArgs);
updatedVal = ctx.unwrapTemporary(invokeEntry.getValue());
updated = ctx.toCacheObject(updatedVal);
if (computed != null)
invokeRes = CacheInvokeResult.fromResult(ctx.unwrapTemporary(computed));
} catch (Exception e) {
invokeRes = CacheInvokeResult.fromError(e);
updated = old;
}
if (invokeRes != null)
invokeResMap.put((K) entry.key().value(ctx.cacheObjectContext(), false), invokeRes);
if (updated == null) {
if (intercept) {
IgniteBiTuple<Boolean, ?> interceptorRes = ctx.config().getInterceptor().onBeforeRemove(new CacheLazyEntry(ctx, entry.key(), invokeEntry.key(), old, oldVal, keepBinary));
if (ctx.cancelRemove(interceptorRes))
continue;
}
// Update previous batch.
if (putMap != null) {
err = updatePartialBatch(filtered, ver, writeVals, putMap, null, expiryPlc, keepBinary, err, subjId, taskName);
putMap = null;
writeVals = null;
filtered = new ArrayList<>();
}
// Start collecting new batch.
if (rmvKeys == null)
rmvKeys = new ArrayList<>(size);
rmvKeys.add(entry.key());
} else {
if (intercept) {
Object interceptorVal = ctx.config().getInterceptor().onBeforePut(new CacheLazyEntry(ctx, entry.key(), invokeEntry.getKey(), old, oldVal, keepBinary), updatedVal);
if (interceptorVal == null)
continue;
updated = ctx.toCacheObject(ctx.unwrapTemporary(interceptorVal));
}
// Update previous batch.
if (rmvKeys != null) {
err = updatePartialBatch(filtered, ver, null, null, rmvKeys, expiryPlc, keepBinary, err, subjId, taskName);
rmvKeys = null;
filtered = new ArrayList<>();
}
if (putMap == null) {
putMap = new LinkedHashMap<>(size, 1.0f);
writeVals = new ArrayList<>(size);
}
putMap.put(entry.key(), updated);
writeVals.add(updated);
}
} else if (op == UPDATE) {
CacheObject cacheVal = ctx.toCacheObject(val);
if (intercept) {
CacheObject old = entry.innerGet(null, null, /*read-through*/
ctx.loadPreviousValue(), /**update-metrics*/
true, /**event*/
true, subjId, null, taskName, null, keepBinary);
Object interceptorVal = ctx.config().getInterceptor().onBeforePut(new CacheLazyEntry(ctx, entry.key(), old, keepBinary), val);
if (interceptorVal == null)
continue;
cacheVal = ctx.toCacheObject(ctx.unwrapTemporary(interceptorVal));
}
if (putMap == null) {
putMap = new LinkedHashMap<>(size, 1.0f);
writeVals = new ArrayList<>(size);
}
putMap.put(entry.key(), cacheVal);
writeVals.add(cacheVal);
} else {
assert op == DELETE;
if (intercept) {
CacheObject old = entry.innerGet(null, null, /*read-through*/
ctx.loadPreviousValue(), /**update-metrics*/
true, /**event*/
true, subjId, null, taskName, null, keepBinary);
IgniteBiTuple<Boolean, ?> interceptorRes = ctx.config().getInterceptor().onBeforeRemove(new CacheLazyEntry(ctx, entry.key(), old, keepBinary));
if (ctx.cancelRemove(interceptorRes))
continue;
}
if (rmvKeys == null)
rmvKeys = new ArrayList<>(size);
rmvKeys.add(entry.key());
}
filtered.add(entry);
} catch (IgniteCheckedException e) {
if (err == null)
err = partialUpdateException();
err.add(F.asList(entry.key()), e);
} catch (GridCacheEntryRemovedException ignore) {
assert false : "Entry cannot become obsolete while holding lock.";
}
}
// Store final batch.
if (putMap != null || rmvKeys != null) {
err = updatePartialBatch(filtered, ver, writeVals, putMap, rmvKeys, expiryPlc, keepBinary, err, subjId, taskName);
} else
assert filtered.isEmpty();
if (err != null)
throw err;
return invokeResMap;
} finally {
unlockEntries(locked);
}
}
use of javax.cache.processor.EntryProcessorResult in project ignite by apache.
the class GridLocalAtomicCache method updateAllInternal.
/**
* Entry point for all public update methods (put, remove, invoke).
*
* @param op Operation.
* @param keys Keys.
* @param vals Values.
* @param invokeArgs Optional arguments for EntryProcessor.
* @param expiryPlc Expiry policy.
* @param retval Return value required flag.
* @param rawRetval Return {@code GridCacheReturn} instance.
* @param filter Cache entry filter.
* @param writeThrough Write through.
* @param readThrough Read through.
* @return Update result.
* @throws IgniteCheckedException If failed.
*/
@SuppressWarnings("unchecked")
private Object updateAllInternal(GridCacheOperation op, Collection<? extends K> keys, @Nullable Iterable<?> vals, @Nullable Object[] invokeArgs, @Nullable ExpiryPolicy expiryPlc, boolean retval, boolean rawRetval, CacheEntryPredicate filter, boolean writeThrough, boolean readThrough, boolean keepBinary) throws IgniteCheckedException {
if (keyCheck)
validateCacheKeys(keys);
if (op == DELETE)
ctx.checkSecurity(SecurityPermission.CACHE_REMOVE);
else
ctx.checkSecurity(SecurityPermission.CACHE_PUT);
String taskName = ctx.kernalContext().job().currentTaskName();
GridCacheVersion ver = ctx.versions().next();
UUID subjId = ctx.subjectIdPerCall(null);
CacheEntryPredicate[] filters = CU.filterArray(filter);
ctx.shared().database().ensureFreeSpace(ctx.memoryPolicy());
if (writeThrough && keys.size() > 1) {
return updateWithBatch(op, keys, vals, invokeArgs, expiryPlc, ver, filters, keepBinary, subjId, taskName);
}
Iterator<?> valsIter = vals != null ? vals.iterator() : null;
IgniteBiTuple<Boolean, ?> res = null;
CachePartialUpdateCheckedException err = null;
boolean intercept = ctx.config().getInterceptor() != null;
for (K key : keys) {
if (key == null)
throw new NullPointerException("Null key.");
Object val = valsIter != null ? valsIter.next() : null;
if (val == null && op != DELETE)
throw new NullPointerException("Null value.");
KeyCacheObject cacheKey = ctx.toCacheKeyObject(key);
if (op == UPDATE)
val = ctx.toCacheObject(val);
else if (op == TRANSFORM)
ctx.kernalContext().resource().inject(val, GridResourceIoc.AnnotationSet.ENTRY_PROCESSOR, ctx.name());
while (true) {
GridCacheEntryEx entry = null;
try {
entry = entryEx(cacheKey);
GridTuple3<Boolean, Object, EntryProcessorResult<Object>> t = entry.innerUpdateLocal(ver, val == null ? DELETE : op, val, invokeArgs, writeThrough, readThrough, retval, keepBinary, expiryPlc, true, true, filters, intercept, subjId, taskName);
if (op == TRANSFORM) {
if (t.get3() != null) {
Map<K, EntryProcessorResult> computedMap;
if (res == null) {
computedMap = U.newHashMap(keys.size());
res = new IgniteBiTuple<>(true, computedMap);
} else
computedMap = (Map<K, EntryProcessorResult>) res.get2();
computedMap.put(key, t.get3());
}
} else if (res == null)
res = new T2(t.get1(), t.get2());
// While.
break;
} catch (GridCacheEntryRemovedException ignored) {
if (log.isDebugEnabled())
log.debug("Got removed entry while updating (will retry): " + key);
entry = null;
} catch (IgniteCheckedException e) {
if (err == null)
err = partialUpdateException();
err.add(F.asList(key), e);
U.error(log, "Failed to update key : " + key, e);
break;
} finally {
if (entry != null)
ctx.evicts().touch(entry, ctx.affinity().affinityTopologyVersion());
}
}
}
if (err != null)
throw err;
Object ret = res == null ? null : rawRetval ? new GridCacheReturn(ctx, true, keepBinary, res.get2(), res.get1()) : (retval || op == TRANSFORM) ? res.get2() : res.get1();
if (op == TRANSFORM && ret == null)
ret = Collections.emptyMap();
return ret;
}
Aggregations