Search in sources :

Example 11 with ContinuousQueryWithTransformer

use of org.apache.ignite.cache.query.ContinuousQueryWithTransformer in project ignite by apache.

the class CacheContinuousWithTransformerReplicatedSelfTest method runContinuousQueryWithTransformer.

/**
 * @param addEvtFilter Add event filter to ContinuousQueryWithTransformer flag.
 * @param expTransCnt Expected transformed event count.
 * @param keepBinary Keep binary flag.
 * @param async Flag to use transformed event listener with {@link IgniteAsyncCallback}.
 * @throws Exception If failed.
 */
private void runContinuousQueryWithTransformer(boolean addEvtFilter, int expTransCnt, boolean keepBinary, boolean async) throws Exception {
    Ignite ignite = gridToRunQuery();
    IgniteCache<Integer, Employee> cache = ignite.cache(DEFAULT_CACHE_NAME);
    if (keepBinary)
        cache = cache.withKeepBinary();
    populateData(cache, JOHN_CONNOR);
    CountDownLatch transUpdCntLatch = new CountDownLatch(expTransCnt);
    AtomicInteger transCnt = new AtomicInteger(0);
    EventListener<String> transLsnr = async ? new LocalEventListenerAsync(transCnt, transUpdCntLatch) : new LocalEventListener(transCnt, transUpdCntLatch);
    Factory<? extends CacheEntryEventFilter> rmtFilterFactory = null;
    if (addEvtFilter)
        rmtFilterFactory = FactoryBuilder.factoryOf(new RemoteCacheEntryEventFilter());
    Factory<? extends IgniteClosure> factory = FactoryBuilder.factoryOf(new RemoteTransformer(keepBinary));
    ContinuousQueryWithTransformer<Integer, Employee, String> qry = new ContinuousQueryWithTransformer<>();
    qry.setInitialQuery(new ScanQuery<Integer, Employee>());
    qry.setRemoteFilterFactory((Factory<? extends CacheEntryEventFilter<Integer, Employee>>) rmtFilterFactory);
    qry.setRemoteTransformerFactory((Factory<? extends IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Employee>, String>>) factory);
    qry.setLocalListener(transLsnr);
    try (QueryCursor<Cache.Entry<Integer, Employee>> cur = cache.query(qry)) {
        for (Cache.Entry<Integer, Employee> e : cur) {
            assertNotNull(e);
            if (keepBinary) {
                assertTrue(((BinaryObject) e.getValue()).field("name").toString().startsWith(JOHN_CONNOR));
            } else {
                assertTrue(e.getValue().name.startsWith(JOHN_CONNOR));
            }
        }
        populateData(cache, SARAH_CONNOR);
        assertTrue("Receive all expected events", transUpdCntLatch.await(DFLT_LATCH_TIMEOUT, MILLISECONDS));
        assertEquals("Count of updated records equal to expected", expTransCnt, transCnt.get());
    }
}
Also used : ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 12 with ContinuousQueryWithTransformer

use of org.apache.ignite.cache.query.ContinuousQueryWithTransformer in project ignite by apache.

the class CacheContinuousWithTransformerReplicatedSelfTest method testExpired.

/**
 * @throws Exception If failed.
 */
public void testExpired() throws Exception {
    Ignite ignite = gridToRunQuery();
    IgniteCache<Integer, Employee> cache = ignite.cache(DEFAULT_CACHE_NAME);
    cache = cache.withExpiryPolicy(new CreatedExpiryPolicy(new Duration(MILLISECONDS, 100)));
    final Set<Integer> keys = new GridConcurrentHashSet<>();
    final CountDownLatch latch = new CountDownLatch(2);
    ContinuousQueryWithTransformer<Integer, Employee, Integer> qry = new ContinuousQueryWithTransformer<>();
    qry.setIncludeExpired(true);
    qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheEntryEventSerializableFilter<Integer, Employee>() {

        @Override
        public boolean evaluate(CacheEntryEvent event) throws CacheEntryListenerException {
            return event.getEventType() == EventType.EXPIRED;
        }
    }));
    qry.setRemoteTransformerFactory(FactoryBuilder.factoryOf(new IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Employee>, Integer>() {

        @Override
        public Integer apply(CacheEntryEvent<? extends Integer, ? extends Employee> evt) {
            assertNull(evt.getValue());
            assertNotNull(evt.getOldValue());
            return evt.getKey();
        }
    }));
    qry.setLocalListener(new EventListener<Integer>() {

        @Override
        public void onUpdated(Iterable<? extends Integer> evts) {
            for (Integer key : evts) {
                keys.add(key);
                latch.countDown();
            }
        }
    });
    try (QueryCursor<Cache.Entry<Integer, Employee>> ignored = cache.query(qry)) {
        cache.put(1, new Employee(SARAH_CONNOR, 42));
        cache.put(2, new Employee(JOHN_CONNOR, 42));
        // Wait for expiration.
        latch.await(LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(2, keys.size());
        assertTrue(keys.contains(1));
        assertTrue(keys.contains(2));
    }
}
Also used : ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) IgniteClosure(org.apache.ignite.lang.IgniteClosure) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) Ignite(org.apache.ignite.Ignite)

Aggregations

ContinuousQueryWithTransformer (org.apache.ignite.cache.query.ContinuousQueryWithTransformer)12 CacheEntryEvent (javax.cache.event.CacheEntryEvent)10 Ignite (org.apache.ignite.Ignite)6 QueryCursor (org.apache.ignite.cache.query.QueryCursor)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 AbstractContinuousQuery (org.apache.ignite.cache.query.AbstractContinuousQuery)5 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)5 IgniteClosure (org.apache.ignite.lang.IgniteClosure)5 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)4 IgniteCache (org.apache.ignite.IgniteCache)4 CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Cache (javax.cache.Cache)3 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteException (org.apache.ignite.IgniteException)2 EventListener (org.apache.ignite.cache.query.ContinuousQueryWithTransformer.EventListener)2 FieldsQueryCursor (org.apache.ignite.cache.query.FieldsQueryCursor)2 PA (org.apache.ignite.internal.util.typedef.PA)2