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));
}
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);
}
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();
}
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();
}
}
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;
}
Aggregations