use of org.apache.ignite.events.CacheQueryReadEvent in project ignite by apache.
the class IgniteCacheAbstractQuerySelfTest method testScanQueryEvents.
/**
* @throws Exception If failed.
*/
@Test
public void testScanQueryEvents() throws Exception {
final Map<Integer, Integer> map = new ConcurrentHashMap<>();
final IgniteCache<Integer, Integer> cache = jcache(Integer.class, Integer.class);
final boolean evtsDisabled = cache.getConfiguration(CacheConfiguration.class).isEventsDisabled();
final CountDownLatch latch = new CountDownLatch(evtsDisabled ? 0 : 10);
final CountDownLatch execLatch = new CountDownLatch(evtsDisabled ? 0 : cacheMode() == REPLICATED ? 1 : gridCount());
IgnitePredicate[] objReadLsnrs = new IgnitePredicate[gridCount()];
IgnitePredicate[] qryExecLsnrs = new IgnitePredicate[gridCount()];
for (int i = 0; i < gridCount(); i++) {
IgnitePredicate<Event> pred = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assert evt instanceof CacheQueryReadEvent;
if (evtsDisabled)
fail("Cache events are disabled");
CacheQueryReadEvent<Integer, Integer> qe = (CacheQueryReadEvent<Integer, Integer>) evt;
assertEquals(SCAN.name(), qe.queryType());
assertEquals(cache.getName(), qe.cacheName());
assertNull(qe.className());
assertNull(null, qe.clause());
assertNotNull(qe.scanQueryFilter());
assertNull(qe.continuousQueryFilter());
assertNull(qe.arguments());
map.put(qe.key(), qe.value());
latch.countDown();
return true;
}
};
grid(i).events().localListen(pred, EVT_CACHE_QUERY_OBJECT_READ);
objReadLsnrs[i] = pred;
IgnitePredicate<Event> execPred = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assert evt instanceof CacheQueryExecutedEvent;
if (evtsDisabled)
fail("Cache events are disabled");
CacheQueryExecutedEvent qe = (CacheQueryExecutedEvent) evt;
assertEquals(SCAN.name(), qe.queryType());
assertEquals(cache.getName(), qe.cacheName());
assertNull(qe.className());
assertNull(null, qe.clause());
assertNotNull(qe.scanQueryFilter());
assertNull(qe.continuousQueryFilter());
assertNull(qe.arguments());
execLatch.countDown();
return true;
}
};
grid(i).events().localListen(execPred, EVT_CACHE_QUERY_EXECUTED);
qryExecLsnrs[i] = execPred;
}
try {
for (int i = 0; i < 20; i++) cache.put(i, i);
IgniteBiPredicate<Integer, Integer> filter = new IgniteBiPredicate<Integer, Integer>() {
@Override
public boolean apply(Integer k, Integer v) {
return k >= 10;
}
};
QueryCursor<Cache.Entry<Integer, Integer>> q = cache.query(new ScanQuery<>(filter));
q.getAll();
assert latch.await(1000, MILLISECONDS);
assert execLatch.await(1000, MILLISECONDS);
if (!evtsDisabled) {
assertEquals(10, map.size());
for (int i = 10; i < 20; i++) assertEquals(i, map.get(i).intValue());
}
} finally {
for (int i = 0; i < gridCount(); i++) {
grid(i).events().stopLocalListen(objReadLsnrs[i]);
grid(i).events().stopLocalListen(qryExecLsnrs[i]);
}
}
}
Aggregations