/** {@inheritDoc} */ @Override public ValueWrapper putIfAbsent(Object key, Object val) { Object old; if (val == null) old = cache.withSkipStore().getAndPutIfAbsent(key, NULL); else old = cache.getAndPutIfAbsent(key, val); return old != null ? fromValue(old) : null; }
/** */ private <K, V> void checkPutOps(Consumer<Runnable> checker, IgniteCache<K, V> cache, T2<K, V> val) { K k = val.get1(); V v = val.get2(); Stream<Runnable> ops = Stream.of( () -> cache.put(k, v), () -> cache.putIfAbsent(k, v), () -> cache.getAndPut(k, v), () -> cache.getAndPutIfAbsent(k, v), () -> cache.putAsync(k, v).get(FUT_TIMEOUT), () -> cache.putIfAbsentAsync(k, v).get(FUT_TIMEOUT), () -> cache.getAndPutAsync(k, v).get(FUT_TIMEOUT), () -> cache.getAndPutIfAbsentAsync(k, v).get(FUT_TIMEOUT) ); ops.forEach(checker); }
/** {@inheritDoc} */ @Override public V call() throws Exception { return cache().getAndPutIfAbsent(key, val); } }
@Override public void run(IgniteCache<String, Integer> cache) throws IgniteCheckedException { Iterator<Map.Entry<String, Integer>> iter = pairs(2).entrySet().iterator(); Map.Entry<String, Integer> e = iter.next(); String key = e.getKey(); Integer val = e.getValue(); assertNull(cache.getAndPutIfAbsent(key, val)); assertEquals(val, cache.getAndPutIfAbsent(key, val)); assert cache.containsKey(key); e = iter.next(); key = e.getKey(); val = e.getValue(); assert cache.putIfAbsent(key, val); assert !cache.putIfAbsent(key, val); assert cache.containsKey(key); } }, F.t(EVT_CACHE_OBJECT_PUT, 2 * gridCnt));
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public ClientResponse process(ClientConnectionContext ctx) { Object res = cache(ctx).getAndPutIfAbsent(key(), val()); return new ClientObjectResponse(requestId(), res); } }
/** */ @Test public void testGetAndPutIfAbsent() { checkOperation(() -> cache.getAndPutIfAbsent(1, 1)); }
@Override public void run(IgniteCache<String, Integer> cache) throws IgniteCheckedException { Iterator<Map.Entry<String, Integer>> iter = pairs(2).entrySet().iterator(); Map.Entry<String, Integer> e = iter.next(); String key = e.getKey(); Integer val = e.getValue(); try (Transaction tx = grid(0).transactions().txStart();) { assert cache.getAndPutIfAbsent(key, val) == null; assertEquals(val, cache.getAndPutIfAbsent(key, val)); assert cache.containsKey(key); e = iter.next(); key = e.getKey(); val = e.getValue(); assert cache.putIfAbsent(key, val); assert !cache.putIfAbsent(key, val); assert cache.containsKey(key); tx.commit(); } assert cache.containsKey(key); } }, F.t(EVT_CACHE_OBJECT_PUT, 2 * gridCnt));
/** * @param cache Cache. */ private void cachePutGetAndPutIfAbsent(IgniteCache cache) { Random rnd = ThreadLocalRandom.current(); Integer key = rnd.nextInt(); Integer val1 = rnd.nextInt(); Integer val2 = rnd.nextInt(); cache.put(key, val1); Object val0 = cache.getAndPutIfAbsent(key, val2); assertEquals(val1, val0); val0 = cache.get(key); assertEquals(val1, val0); tearDown(cache); }
@Nullable @Override public <K, V> V putIfAbsent(String cacheName, K key, V val, long ttl) { try { if (ttl > 0) { ExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl)); IgniteCache<K, V> cache = ctx.cache().<K, V>publicJCache(cacheName).withExpiryPolicy(plc); return cache.getAndPutIfAbsent(key, val); } else return ctx.cache().<K, V>jcache(cacheName).getAndPutIfAbsent(key, val); } catch (IgniteCheckedException e) { throw CU.convertToCacheException(e); } }
/** * Creates a new web session with the specified id. * * @param ses Base session. * @param sesId Session id. * @return New session. */ private WebSessionV2 createSessionV2(final HttpSession ses, final String sesId) throws IOException { assert ses != null; assert sesId != null; WebSessionV2 cached = new WebSessionV2(sesId, ses, true, ctx, null, marshaller); final WebSessionEntity marshaledEntity = cached.marshalAttributes(); for (int i = 0; i < retries; i++) { try { final IgniteCache<String, WebSessionEntity> cache0 = cacheWithExpiryPolicy( cached.getMaxInactiveInterval(), binaryCache); final WebSessionEntity old = cache0.getAndPutIfAbsent(sesId, marshaledEntity); if (old != null) cached = new WebSessionV2(sesId, ses, false, ctx, old, marshaller); else cached = new WebSessionV2(sesId, ses, false, ctx, marshaledEntity, marshaller); break; } catch (CacheException | IgniteException | IllegalStateException e) { handleCreateSessionException(sesId, i, e); } } return cached; }
/** {@inheritDoc} */ @Override public int increment() { locIncrements.incrementAndGet(); try { while (true) { Value val = cache.get(key); if (val == null) { Value old = cache.getAndPutIfAbsent(key, val = new Value(0)); if (old != null) val = old; } Value newVal = new Value(val.get() + 1); if (cache.replace(key, val, newVal)) return newVal.get(); } } catch (Exception e) { throw new IgniteException(e); } }
/** * Creates a new web session with the specified id. * * @param ses Base session. * @param sesId Session id. * @return New session. */ private WebSession createSession(HttpSession ses, String sesId) { WebSession cached = new WebSession(sesId, ses, true); cached.genSes(ses); if (log.isDebugEnabled()) log.debug("Session created: " + sesId); for (int i = 0; i < retries; i++) { try { final IgniteCache<String, WebSession> cache0 = cacheWithExpiryPolicy(cached.getMaxInactiveInterval(), cache); final WebSession old = cache0.getAndPutIfAbsent(sesId, cached); if (old != null) { cached = old; if (cached.isNew()) cached = new WebSession(cached.getId(), cached, false); } break; } catch (CacheException | IgniteException | IllegalStateException e) { handleCreateSessionException(sesId, i, e); } } return cached; }
/** * @throws IgniteCheckedException If failed. */ @Test public void testAtomicOps() throws IgniteCheckedException { IgniteCache<String, Integer> c = jcache(); final int cnt = 10; for (int i = 0; i < cnt; i++) assertNull(c.getAndPutIfAbsent("k" + i, i)); for (int i = 0; i < cnt; i++) { boolean wrong = i % 2 == 0; String key = "k" + i; boolean res = c.replace(key, wrong ? i + 1 : i, -1); assertEquals(wrong, !res); } for (int i = 0; i < cnt; i++) { boolean success = i % 2 != 0; String key = "k" + i; boolean res = c.remove(key, -1); assertTrue(success == res); } }
/** * @throws IgniteCheckedException If failed. */ @Test public void testAtomicOps() throws IgniteCheckedException { IgniteCache<String, Integer> c = jcache(); final int cnt = 10; for (int i = 0; i < cnt; i++) assertNull(c.getAndPutIfAbsent("k" + i, i)); for (int i = 0; i < cnt; i++) { boolean wrong = i % 2 == 0; String key = "k" + i; boolean res = c.replace(key, wrong ? i + 1 : i, -1); assertEquals(wrong, !res); } for (int i = 0; i < cnt; i++) { boolean success = i % 2 != 0; String key = "k" + i; boolean res = c.remove(key, -1); assertTrue(success == res); } }
cacheAsync.getAndPutIfAbsent("key", 1); assertEquals((Integer)1, cache.get("key")); cacheAsync.getAndPutIfAbsent("key", 2); cacheAsync.getAndPutIfAbsent("key2", 3); storeStgy.putToStore("key3", 3); cacheAsync.getAndPutIfAbsent("key3", 4); cacheAsync.getAndPutIfAbsent("key2", 3);
/** * @throws Exception If failed. */ @Test public void testPutDebug() throws Exception { for (int i = 0; i < gridCount(); i++) info(">>>>> Grid" + i + ": " + grid(i).localNode().id()); final int size = 10; IgniteCache<Object, Object> cache0 = grid(0).cache(DEFAULT_CACHE_NAME); for (int i = 0; i < size; i++) { info("Putting value [i=" + i + ']'); cache0.put(i, i); info("Finished putting value [i=" + i + ']'); } for (int i = 0; i < gridCount(); i++) executeOnLocalOrRemoteJvm(i, new CheckAffinityTask(size)); for (int i = 0; i < size; i++) { info("Putting value 2 [i=" + i + ']'); assertEquals(i, cache0.getAndPutIfAbsent(i, i * i)); info("Finished putting value 2 [i=" + i + ']'); } for (int i = 0; i < size; i++) assertEquals(i, cache0.get(i)); }
/** * @throws Exception If failed. */ private void doTestNoStore() throws Exception { factory = null; Ignite ignite = startGrid("client-1"); IgniteCache<Object, Object> cache = ignite.cache(CACHE_NAME); cache.get(0); cache.getAll(F.asSet(0, 1)); cache.getAndPut(0, 0); cache.getAndPutIfAbsent(0, 0); cache.getAndRemove(0); cache.getAndReplace(0, 0); cache.put(0, 0); cache.putAll(F.asMap(0, 0, 1, 1)); cache.putIfAbsent(0, 0); cache.remove(0); cache.remove(0, 0); cache.removeAll(F.asSet(0, 1)); cache.removeAll(); cache.invoke(0, new EP()); cache.invokeAll(F.asSet(0, 1), new EP()); }
/** * @throws Exception If failed. */ @Test public void testCorrectStore() throws Exception { nearEnabled = false; cacheMode = CacheMode.PARTITIONED; factory = new Factory1(); startGrids(2); Ignite ignite = startGrid("client-1"); IgniteCache<Object, Object> cache = ignite.cache(CACHE_NAME); cache.get(0); cache.getAll(F.asSet(0, 1)); cache.getAndPut(0, 0); cache.getAndPutIfAbsent(0, 0); cache.getAndRemove(0); cache.getAndReplace(0, 0); cache.put(0, 0); cache.putAll(F.asMap(0, 0, 1, 1)); cache.putIfAbsent(0, 0); cache.remove(0); cache.remove(0, 0); cache.removeAll(F.asSet(0, 1)); cache.removeAll(); cache.invoke(0, new EP()); cache.invokeAll(F.asSet(0, 1), new EP()); }
/** @throws Exception If failed. */ @Test public void testConcurrentOps() throws Exception { // Don't create missing values. store.create(false); IgniteCache<Integer, String> near = jcache(0); int key = 1; assertTrue(near.putIfAbsent(key, "1")); assertFalse(near.putIfAbsent(key, "1")); assertEquals("1", near.getAndPutIfAbsent(key, "2")); assertEquals("1", near.localPeek(key)); assertEquals(1, near.localSize(CachePeekMode.ALL)); assertEquals(1, near.localSize(CachePeekMode.ALL)); assertEquals("1", near.getAndReplace(key, "2")); assertEquals("2", near.localPeek(key)); assertTrue(near.replace(key, "2")); assertEquals("2", near.localPeek(key)); assertEquals(1, near.localSize(CachePeekMode.ALL)); assertEquals(1, near.localSize(CachePeekMode.ALL)); assertTrue(near.remove(key, "2")); assertEquals(0, near.localSize(CachePeekMode.ALL)); }
assertBinaryEnum(cache, key, TestEnum.VAL3); assertEquals(TestEnum.VAL3, cache.getAndPutIfAbsent(key, TestEnum.VAL1)); assertNull(cache.getAndPutIfAbsent(key, TestEnum.VAL1));