Search in sources :

Example 21 with IgniteClosure

use of org.apache.ignite.lang.IgniteClosure in project ignite by apache.

the class GridCacheQueryTransformerSelfTest method testLocalKeepBinaryFiltered.

/**
 * @throws Exception If failed.
 */
public void testLocalKeepBinaryFiltered() throws Exception {
    IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
    try {
        for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
        Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public List<Integer> call() throws Exception {
                IgniteBiPredicate<Integer, BinaryObject> filter = new IgniteBiPredicate<Integer, BinaryObject>() {

                    @Override
                    public boolean apply(Integer k, BinaryObject v) {
                        return v.<Integer>field("idx") % 1000 == 0;
                    }
                };
                IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer>() {

                    @Override
                    public Integer apply(Cache.Entry<Integer, BinaryObject> e) {
                        return e.getValue().field("idx");
                    }
                };
                return ignite.cache("test-cache").withKeepBinary().query(new ScanQuery<>(filter).setLocal(true), transformer).getAll();
            }
        });
        List<Integer> res = new ArrayList<>(F.flatCollections(lists));
        assertEquals(5, res.size());
        Collections.sort(res);
        for (int i = 0; i < 5; i++) assertEquals(i * 1000, res.get(i).intValue());
    } finally {
        cache.destroy();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) BinaryObject(org.apache.ignite.binary.BinaryObject) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 22 with IgniteClosure

use of org.apache.ignite.lang.IgniteClosure in project ignite by apache.

the class CacheContinuousWithTransformerFailoverTest method testServerNodeLeft.

/**
 * @throws Exception If failed.
 */
public void testServerNodeLeft() throws Exception {
    startGrids(3);
    client = true;
    final int CLIENT_ID = 3;
    Ignite clnNode = startGrid(CLIENT_ID);
    client = false;
    IgniteOutClosure<IgniteCache<Integer, Integer>> cache = new IgniteOutClosure<IgniteCache<Integer, Integer>>() {

        int cnt = 0;

        @Override
        public IgniteCache<Integer, Integer> apply() {
            ++cnt;
            return grid(CLIENT_ID).cache(DEFAULT_CACHE_NAME);
        }
    };
    final CacheEventListener lsnr = new CacheEventListener();
    ContinuousQueryWithTransformer<Object, Object, String> qry = new ContinuousQueryWithTransformer<>();
    qry.setLocalListener(lsnr);
    qry.setRemoteTransformerFactory(FactoryBuilder.factoryOf(new IgniteClosure<CacheEntryEvent<?, ?>, String>() {

        @Override
        public String apply(CacheEntryEvent<?, ?> evt) {
            return "" + evt.getKey() + evt.getValue();
        }
    }));
    QueryCursor<?> cur = clnNode.cache(DEFAULT_CACHE_NAME).query(qry);
    boolean first = true;
    int keyCnt = 1;
    for (int i = 0; i < 10; i++) {
        log.info("Start iteration: " + i);
        if (first)
            first = false;
        else {
            for (int srv = 0; srv < CLIENT_ID - 1; srv++) startGrid(srv);
        }
        lsnr.latch = new CountDownLatch(keyCnt);
        for (int key = 0; key < keyCnt; key++) cache.apply().put(key, key);
        assertTrue("Failed to wait for event. Left events: " + lsnr.latch.getCount(), lsnr.latch.await(10, SECONDS));
        for (int srv = 0; srv < CLIENT_ID - 1; srv++) stopGrid(srv);
    }
    tryClose(cur);
}
Also used : ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteCache(org.apache.ignite.IgniteCache) IgniteOutClosure(org.apache.ignite.lang.IgniteOutClosure) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Ignite(org.apache.ignite.Ignite)

Example 23 with IgniteClosure

use of org.apache.ignite.lang.IgniteClosure in project ignite by apache.

the class CacheContinuousWithTransformerFailoverTest method testTransformerException.

/**
 * @throws Exception If failed.
 */
public void testTransformerException() throws Exception {
    try {
        startGrids(1);
        Ignite ignite = ignite(0);
        IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
        final CountDownLatch latch = new CountDownLatch(10);
        ContinuousQueryWithTransformer<Integer, Integer, Integer> qry = new ContinuousQueryWithTransformer<>();
        qry.setLocalListener(new EventListener<Integer>() {

            /**
             */
            @LoggerResource
            private IgniteLogger log;

            @Override
            public void onUpdated(Iterable<? extends Integer> evts) throws CacheEntryListenerException {
                for (Integer evt : evts) {
                    log.debug("" + evt);
                }
            }
        });
        qry.setRemoteTransformerFactory(FactoryBuilder.factoryOf(new IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Integer>, Integer>() {

            @Override
            public Integer apply(CacheEntryEvent<? extends Integer, ? extends Integer> evt) {
                latch.countDown();
                throw new RuntimeException("Test error.");
            }
        }));
        qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheEntryEventSerializableFilter<Integer, Integer>() {

            @Override
            public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends Integer> evt) {
                return true;
            }
        }));
        try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
            for (int i = 0; i < 10; i++) cache.put(i, i);
            assertTrue(latch.await(10, SECONDS));
        }
    } finally {
        stopAllGrids();
    }
}
Also used : LoggerResource(org.apache.ignite.resources.LoggerResource) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) IgniteClosure(org.apache.ignite.lang.IgniteClosure) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Ignite(org.apache.ignite.Ignite) IgniteLogger(org.apache.ignite.IgniteLogger)

Example 24 with IgniteClosure

use of org.apache.ignite.lang.IgniteClosure in project ignite by apache.

the class CacheContinuousWithTransformerReplicatedSelfTest method testTransformerReturnNull.

/**
 * @throws Exception If failed.
 */
public void testTransformerReturnNull() throws Exception {
    Ignite ignite = gridToRunQuery();
    IgniteCache<Integer, Employee> cache = ignite.cache(DEFAULT_CACHE_NAME);
    ContinuousQueryWithTransformer<Integer, Employee, String> qry = new ContinuousQueryWithTransformer<>();
    final AtomicInteger cnt = new AtomicInteger(0);
    qry.setLocalListener(new EventListener() {

        @Override
        public void onUpdated(Iterable events) throws CacheEntryListenerException {
            for (Object e : events) {
                assertNull(e);
                cnt.incrementAndGet();
            }
        }
    });
    qry.setRemoteTransformerFactory(FactoryBuilder.factoryOf(new IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Employee>, String>() {

        @Override
        public String apply(CacheEntryEvent<? extends Integer, ? extends Employee> evt) {
            return null;
        }
    }));
    qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheEntryEventSerializableFilter<Integer, Employee>() {

        @Override
        public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends Employee> evt) {
            return true;
        }
    }));
    try (QueryCursor<Cache.Entry<Integer, Employee>> ignored = cache.query(qry)) {
        for (int i = 0; i < 10; i++) cache.put(i, new Employee(JOHN_CONNOR, i));
        boolean evtsReceived = GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return cnt.get() == 10;
            }
        }, 20_000);
        assertTrue(evtsReceived);
    }
}
Also used : ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) IgniteClosure(org.apache.ignite.lang.IgniteClosure) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) BinaryObject(org.apache.ignite.binary.BinaryObject) EventListener(org.apache.ignite.cache.query.ContinuousQueryWithTransformer.EventListener)

Example 25 with IgniteClosure

use of org.apache.ignite.lang.IgniteClosure 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

IgniteClosure (org.apache.ignite.lang.IgniteClosure)26 Ignite (org.apache.ignite.Ignite)15 IgniteCache (org.apache.ignite.IgniteCache)8 Cache (javax.cache.Cache)7 ArrayList (java.util.ArrayList)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 List (java.util.List)5 CacheEntryEvent (javax.cache.event.CacheEntryEvent)5 IgniteException (org.apache.ignite.IgniteException)5 ContinuousQueryWithTransformer (org.apache.ignite.cache.query.ContinuousQueryWithTransformer)5 ScanQuery (org.apache.ignite.cache.query.ScanQuery)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)4 IOException (java.io.IOException)3 UUID (java.util.UUID)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)3 BinaryObject (org.apache.ignite.binary.BinaryObject)3 CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)3