Search in sources :

Example 11 with CompletionListenerFuture

use of javax.cache.integration.CompletionListenerFuture in project cache2k by cache2k.

the class CacheLoaderWriterTest method shouldLoadMultipleExistingEntryUsingLoadAll.

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)}
 * for multiple existing entries will be reloaded but not written.
 */
@Test
public void shouldLoadMultipleExistingEntryUsingLoadAll() throws Exception {
    HashSet<String> keys = new HashSet<>();
    keys.add("gudday");
    keys.add("hello");
    keys.add("howdy");
    keys.add("bonjour");
    String value = "other";
    for (String key : keys) {
        assertThat(cache.containsKey(key), is(false));
        cache.put(key, value);
        assertThat(cache.containsKey(key), is(true));
    }
    CompletionListenerFuture future = new CompletionListenerFuture();
    cache.loadAll(keys, true, future);
    // wait for the load to complete
    future.get();
    assertThat(future.isDone(), is(true));
    assertThat(recordingCacheLoader.getLoadCount(), is(keys.size()));
    for (String key : keys) {
        assertThat(cache.get(key), is(equalTo(key)));
        assertThat(recordingCacheLoader.hasLoaded(key), is(true));
    }
    // ensure nothing has been written
    assertThat(recordingCacheWriter.getWriteCount(), is(4L));
    assertThat(recordingCacheWriter.getDeleteCount(), is(0L));
}
Also used : CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with CompletionListenerFuture

use of javax.cache.integration.CompletionListenerFuture in project ignite by apache.

the class IgniteCacheLoadAllAbstractTest method testLoadAll.

/**
 * @throws Exception If failed.
 */
@Test
public void testLoadAll() throws Exception {
    IgniteCache<Integer, String> cache0 = jcache(0);
    // Put some data in cache, it also should be put in store.
    final int KEYS = 10;
    for (int i = 0; i < KEYS; i++) cache0.put(i, String.valueOf(i));
    // Restart nodes with write-through disabled so that data in store does not change.
    stopAllGrids();
    writeThrough = false;
    startGrids();
    cache0 = jcache(0);
    Set<Integer> keys = new HashSet<>();
    Map<Integer, String> expVals = new HashMap<>();
    for (int i = 0; i < KEYS / 2; i++) {
        keys.add(i);
        expVals.put(i, String.valueOf(i));
    }
    for (int i = KEYS + 1000; i < KEYS + 1010; i++) keys.add(i);
    CompletionListenerFuture fut = new CompletionListenerFuture();
    log.info("Load1.");
    cache0.loadAll(keys, false, fut);
    fut.get();
    checkValues(KEYS, expVals);
    HashMap<Integer, String> expChangedVals = new HashMap<>();
    for (int i = 0; i < KEYS / 2; i++) {
        String val = "changed";
        cache0.put(i, val);
        expChangedVals.put(i, val);
    }
    checkValues(KEYS, expChangedVals);
    fut = new CompletionListenerFuture();
    log.info("Load2.");
    cache0.loadAll(keys, false, fut);
    fut.get();
    checkValues(KEYS, expChangedVals);
    log.info("Load3.");
    fut = new CompletionListenerFuture();
    cache0.loadAll(keys, true, fut);
    fut.get();
    checkValues(KEYS, expVals);
    for (int i = 0; i < KEYS; i++) {
        keys.add(i);
        expVals.put(i, String.valueOf(i));
    }
    fut = new CompletionListenerFuture();
    log.info("Load4.");
    cache0.loadAll(keys, false, fut);
    fut.get();
    checkValues(KEYS, expVals);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet) IgniteCacheAbstractTest(org.apache.ignite.internal.processors.cache.IgniteCacheAbstractTest) Test(org.junit.Test)

Example 13 with CompletionListenerFuture

use of javax.cache.integration.CompletionListenerFuture in project ignite by apache.

the class CacheTtlAbstractSelfTest method defaultTimeToLiveLoadAll.

/**
 * @param replaceExisting Replace existing value flag.
 * @throws Exception If failed.
 */
private void defaultTimeToLiveLoadAll(boolean replaceExisting) throws Exception {
    IgniteCache<Integer, Integer> cache = jcache(0);
    CompletionListenerFuture fut = new CompletionListenerFuture();
    Set<Integer> keys = new HashSet<>();
    for (int i = 0; i < SIZE; ++i) keys.add(i);
    cache.loadAll(keys, replaceExisting, fut);
    fut.get();
    checkSizeBeforeLive(SIZE);
    Thread.sleep(DEFAULT_TIME_TO_LIVE + 500);
    checkSizeAfterLive();
}
Also used : CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet)

Example 14 with CompletionListenerFuture

use of javax.cache.integration.CompletionListenerFuture in project ignite by apache.

the class IgniteCacheExpiryPolicyWithStoreAbstractTest method testLoadAll.

/**
 * @throws Exception If failed.
 */
@Test
public void testLoadAll() throws Exception {
    IgniteCache<Integer, Integer> cache = jcache(0);
    final Integer key = primaryKey(cache);
    storeMap.put(key, 100);
    try {
        CompletionListenerFuture fut = new CompletionListenerFuture();
        cache.loadAll(F.asSet(key), false, fut);
        fut.get();
        checkTtl(key, 500, false);
        assertEquals((Integer) 100, cache.localPeek(key, CachePeekMode.ONHEAP));
        U.sleep(600);
        checkExpired(key);
        cache = cache.withExpiryPolicy(new ExpiryPolicy() {

            @Override
            public Duration getExpiryForCreation() {
                return new Duration(TimeUnit.MILLISECONDS, 501);
            }

            @Override
            public Duration getExpiryForAccess() {
                return new Duration(TimeUnit.MILLISECONDS, 601);
            }

            @Override
            public Duration getExpiryForUpdate() {
                return new Duration(TimeUnit.MILLISECONDS, 701);
            }
        });
        fut = new CompletionListenerFuture();
        cache.loadAll(F.asSet(key), false, fut);
        fut.get();
        checkTtl(key, 501, false);
    } finally {
        cache.removeAll();
    }
}
Also used : ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Duration(javax.cache.expiry.Duration) CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) IgniteCacheAbstractTest(org.apache.ignite.internal.processors.cache.IgniteCacheAbstractTest) Test(org.junit.Test)

Example 15 with CompletionListenerFuture

use of javax.cache.integration.CompletionListenerFuture in project ignite by apache.

the class IgniteCacheQueryLoadSelfTest method testReloadAll.

/**
 * @throws Exception If failed.
 */
@Test
public void testReloadAll() throws Exception {
    for (int i = 0; i < PUT_CNT; i++) STORE_MAP.put(i, new ValueObject(i));
    IgniteCache<Integer, ValueObject> cache = jcache();
    Integer[] keys = new Integer[PUT_CNT - 5];
    for (int i = 0; i < PUT_CNT - 5; i++) keys[i] = i + 5;
    CompletionListenerFuture fut = new CompletionListenerFuture();
    grid().<Integer, Integer>cache(DEFAULT_CACHE_NAME).loadAll(F.asSet(keys), true, fut);
    fut.get();
    assert cache.size() == PUT_CNT - 5;
    Collection<Cache.Entry<Integer, ValueObject>> res = cache.query(new SqlQuery(ValueObject.class, "val >= 0")).getAll();
    assert res != null;
    assert res.size() == PUT_CNT - 5;
    assert size(ValueObject.class) == PUT_CNT - 5;
    cache.clear();
    assert cache.size() == 0;
    assertEquals(0, cache.size());
    fut = new CompletionListenerFuture();
    grid().<Integer, Integer>cache(DEFAULT_CACHE_NAME).loadAll(F.asSet(keys), true, fut);
    fut.get();
    assertEquals(PUT_CNT - 5, cache.size());
    res = cache.query(new SqlQuery(ValueObject.class, "val >= 0")).getAll();
    assert res != null;
    assert res.size() == PUT_CNT - 5;
    assert size(ValueObject.class) == PUT_CNT - 5;
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

CompletionListenerFuture (javax.cache.integration.CompletionListenerFuture)32 Test (org.junit.Test)30 HashSet (java.util.HashSet)26 IgniteCacheAbstractTest (org.apache.ignite.internal.processors.cache.IgniteCacheAbstractTest)3 ExecutionException (java.util.concurrent.ExecutionException)2 Duration (javax.cache.expiry.Duration)2 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)2 TimeStep (com.hazelcast.simulator.test.annotations.TimeStep)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 MutableConfiguration (javax.cache.configuration.MutableConfiguration)1 CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)1 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)1 Affinity (org.apache.ignite.cache.affinity.Affinity)1 SqlQuery (org.apache.ignite.cache.query.SqlQuery)1 GridCacheAbstractSelfTest (org.apache.ignite.internal.processors.cache.GridCacheAbstractSelfTest)1 Transaction (org.apache.ignite.transactions.Transaction)1 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)1 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)1