Java Code Examples for org.apache.ignite.IgniteCache#getAndPutIfAbsent()

The following examples show how to use org.apache.ignite.IgniteCache#getAndPutIfAbsent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CacheClientStoreSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
private void doTestNoStore() throws Exception {
    factory = null;

    Ignite ignite = startClientGrid("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());
}
 
Example 2
Source File: WebSessionFilter.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: CacheClientStoreSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testCorrectStore() throws Exception {
    nearEnabled = false;
    cacheMode = CacheMode.PARTITIONED;
    factory = new Factory1();

    startGrids(2);

    Ignite ignite = startClientGrid("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());
}
 
Example 4
Source File: IgniteCacheConfigVariationsFullApiTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testGetAndPutIfAbsentAsyncOld() throws Exception {
    Transaction tx = txShouldBeUsed() ? transactions().txStart() : null;

    IgniteCache<String, Integer> cache = jcache();

    IgniteCache<String, Integer> cacheAsync = cache.withAsync();

    try {
        cacheAsync.getAndPutIfAbsent("key", 1);

        IgniteFuture<Integer> fut1 = cacheAsync.future();

        assertNull(fut1.get());
        assertEquals((Integer)1, cache.get("key"));

        cacheAsync.getAndPutIfAbsent("key", 2);

        IgniteFuture<Integer> fut2 = cacheAsync.future();

        assertEquals((Integer)1, fut2.get());
        assertEquals((Integer)1, cache.get("key"));

        if (tx != null)
            tx.commit();
    }
    finally {
        if (tx != null)
            tx.close();
    }

    if (!storeEnabled())
        return;

    // Check swap.
    cache.put("key2", 1);

    cache.localEvict(Collections.singleton("key2"));

    if (!isLoadPreviousValue())
        cache.get("key2");

    cacheAsync.getAndPutIfAbsent("key2", 3);

    assertEquals((Integer)1, cacheAsync.<Integer>future().get());

    // Check db.
    if (storeEnabled() && isLoadPreviousValue() && !isMultiJvm()) {
        putToStore("key3", 3);

        cacheAsync.getAndPutIfAbsent("key3", 4);

        assertEquals((Integer)3, cacheAsync.<Integer>future().get());
    }

    cache.localEvict(Collections.singleton("key2"));

    if (!isLoadPreviousValue())
        cache.get("key2");

    // Same checks inside tx.
    tx = txShouldBeUsed() ? transactions().txStart() : null;

    try {
        cacheAsync.getAndPutIfAbsent("key2", 3);

        assertEquals(1, cacheAsync.future().get());

        if (tx != null)
            tx.commit();

        assertEquals((Integer)1, cache.get("key2"));
    }
    finally {
        if (tx != null)
            tx.close();
    }
}
 
Example 5
Source File: IgniteCacheGroupsTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @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);
}
 
Example 6
Source File: GridCacheAbstractFullApiSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testGetAndPutIfAbsentAsyncOld() throws Exception {
    Transaction tx = txShouldBeUsed() ? transactions().txStart() : null;

    IgniteCache<String, Integer> cache = jcache();

    IgniteCache<String, Integer> cacheAsync = cache.withAsync();

    try {
        cacheAsync.getAndPutIfAbsent("key", 1);

        IgniteFuture<Integer> fut1 = cacheAsync.future();

        assertNull(fut1.get());
        assertEquals((Integer)1, cache.get("key"));

        cacheAsync.getAndPutIfAbsent("key", 2);

        IgniteFuture<Integer> fut2 = cacheAsync.future();

        assertEquals((Integer)1, fut2.get());
        assertEquals((Integer)1, cache.get("key"));

        if (tx != null)
            tx.commit();
    }
    finally {
        if (tx != null)
            tx.close();
    }

    // Check swap.
    cache.put("key2", 1);

    cache.localEvict(Collections.singleton("key2"));

    cacheAsync.getAndPutIfAbsent("key2", 3);

    assertEquals((Integer)1, cacheAsync.<Integer>future().get());

    // Check db.
    if (!isMultiJvm()) {
        storeStgy.putToStore("key3", 3);

        cacheAsync.getAndPutIfAbsent("key3", 4);

        assertEquals((Integer)3, cacheAsync.<Integer>future().get());
    }

    cache.localEvict(Collections.singleton("key2"));

    // Same checks inside tx.
    tx = txShouldBeUsed() ? transactions().txStart() : null;

    try {
        cacheAsync.getAndPutIfAbsent("key2", 3);

        assertEquals(1, cacheAsync.future().get());

        if (tx != null)
            tx.commit();

        assertEquals((Integer)1, cache.get("key2"));
    }
    finally {
        if (tx != null)
            tx.close();
    }
}