Search in sources :

Example 6 with ContinuousQueryWithTransformer

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

the class IgniteCacheProxyImpl method queryContinuous.

/**
 * Executes continuous query.
 *
 * @param qry Query.
 * @param loc Local flag.
 * @param keepBinary Keep binary flag.
 * @return Initial iteration cursor.
 */
@SuppressWarnings("unchecked")
private QueryCursor<Cache.Entry<K, V>> queryContinuous(AbstractContinuousQuery qry, boolean loc, boolean keepBinary) {
    assert qry instanceof ContinuousQuery || qry instanceof ContinuousQueryWithTransformer;
    if (qry.getInitialQuery() instanceof ContinuousQuery || qry.getInitialQuery() instanceof ContinuousQueryWithTransformer) {
        throw new IgniteException("Initial predicate for continuous query can't be an instance of another " + "continuous query. Use SCAN or SQL query for initial iteration.");
    }
    CacheEntryUpdatedListener locLsnr = null;
    EventListener locTransLsnr = null;
    CacheEntryEventSerializableFilter rmtFilter = null;
    Factory<? extends IgniteClosure> rmtTransFactory = null;
    if (qry instanceof ContinuousQuery) {
        ContinuousQuery<K, V> qry0 = (ContinuousQuery<K, V>) qry;
        if (qry0.getLocalListener() == null)
            throw new IgniteException("Mandatory local listener is not set for the query: " + qry);
        if (qry0.getRemoteFilter() != null && qry0.getRemoteFilterFactory() != null)
            throw new IgniteException("Should be used either RemoterFilter or RemoteFilterFactory.");
        locLsnr = qry0.getLocalListener();
        rmtFilter = qry0.getRemoteFilter();
    } else {
        ContinuousQueryWithTransformer<K, V, ?> qry0 = (ContinuousQueryWithTransformer<K, V, ?>) qry;
        if (qry0.getLocalListener() == null)
            throw new IgniteException("Mandatory local transformed event listener is not set for the query: " + qry);
        if (qry0.getRemoteTransformerFactory() == null)
            throw new IgniteException("Mandatory RemoteTransformerFactory is not set for the query: " + qry);
        Collection<ClusterNode> nodes = context().grid().cluster().nodes();
        for (ClusterNode node : nodes) {
            if (node.version().compareTo(CONT_QRY_WITH_TRANSFORMER_SINCE) < 0) {
                throw new IgniteException("Can't start ContinuousQueryWithTransformer, " + "because some nodes in cluster doesn't support this feature: " + node);
            }
        }
        locTransLsnr = qry0.getLocalListener();
        rmtTransFactory = qry0.getRemoteTransformerFactory();
    }
    try {
        final UUID routineId = ctx.continuousQueries().executeQuery(locLsnr, locTransLsnr, rmtFilter, qry.getRemoteFilterFactory(), rmtTransFactory, qry.getPageSize(), qry.getTimeInterval(), qry.isAutoUnsubscribe(), loc, keepBinary, qry.isIncludeExpired());
        final QueryCursor<Cache.Entry<K, V>> cur = qry.getInitialQuery() != null ? query(qry.getInitialQuery()) : null;
        return new QueryCursor<Cache.Entry<K, V>>() {

            @Override
            public Iterator<Cache.Entry<K, V>> iterator() {
                return cur != null ? cur.iterator() : new GridEmptyIterator<Cache.Entry<K, V>>();
            }

            @Override
            public List<Cache.Entry<K, V>> getAll() {
                return cur != null ? cur.getAll() : Collections.<Cache.Entry<K, V>>emptyList();
            }

            @Override
            public void close() {
                if (cur != null)
                    cur.close();
                try {
                    ctx.kernalContext().continuous().stopRoutine(routineId).get();
                } catch (IgniteCheckedException e) {
                    throw U.convertException(e);
                }
            }
        };
    } catch (IgniteCheckedException e) {
        throw U.convertException(e);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) CacheEntry(org.apache.ignite.cache.CacheEntry) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) IgniteException(org.apache.ignite.IgniteException) EventListener(org.apache.ignite.cache.query.ContinuousQueryWithTransformer.EventListener) UUID(java.util.UUID) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 7 with ContinuousQueryWithTransformer

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

the class CacheContinuousQueryWithTransformerExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws Exception If example execution failed.
 */
public static void main(String[] args) throws Exception {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Cache continuous query with transformer example started.");
        // Auto-close cache at the end of the example.
        try (IgniteCache<Integer, Organization> cache = ignite.getOrCreateCache(CACHE_NAME)) {
            // Create new continuous query with transformer.
            ContinuousQueryWithTransformer<Integer, Organization, String> qry = new ContinuousQueryWithTransformer<>();
            // Factory to create transformers.
            Factory<IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Organization>, String>> factory = FactoryBuilder.factoryOf(// Only this field will be sent over to a local node over the network.
            (IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Organization>, String>) event -> event.getValue().name());
            qry.setRemoteTransformerFactory(factory);
            // Listener that will receive transformed data.
            qry.setLocalListener(names -> {
                for (String name : names) System.out.println("New organization name: " + name);
            });
            // Execute query.
            try (QueryCursor<Cache.Entry<Integer, Organization>> cur = cache.query(qry)) {
                populateCache(cache);
                // Wait for a while while callback is notified about remaining puts.
                Thread.sleep(2000);
            }
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : Factory(javax.cache.configuration.Factory) FactoryBuilder(javax.cache.configuration.FactoryBuilder) Timestamp(java.sql.Timestamp) HashMap(java.util.HashMap) Ignite(org.apache.ignite.Ignite) ExampleNodeStartup(org.apache.ignite.examples.ExampleNodeStartup) Address(org.apache.ignite.examples.model.Address) IgniteCache(org.apache.ignite.IgniteCache) OrganizationType(org.apache.ignite.examples.model.OrganizationType) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Ignition(org.apache.ignite.Ignition) Organization(org.apache.ignite.examples.model.Organization) IgniteClosure(org.apache.ignite.lang.IgniteClosure) Map(java.util.Map) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) Organization(org.apache.ignite.examples.model.Organization) IgniteClosure(org.apache.ignite.lang.IgniteClosure) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Ignite(org.apache.ignite.Ignite)

Example 8 with ContinuousQueryWithTransformer

use of org.apache.ignite.cache.query.ContinuousQueryWithTransformer 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 9 with ContinuousQueryWithTransformer

use of org.apache.ignite.cache.query.ContinuousQueryWithTransformer 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 10 with ContinuousQueryWithTransformer

use of org.apache.ignite.cache.query.ContinuousQueryWithTransformer 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)

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