Search in sources :

Example 76 with ContinuousQuery

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

the class UsingContinuousQueries method initialQueryExample.

public static void initialQueryExample() {
    try (Ignite ignite = Ignition.start()) {
        // tag::initialQry[]
        IgniteCache<Integer, String> cache = ignite.getOrCreateCache("myCache");
        // end::initialQry[]
        cache.put(100, "100");
        // tag::initialQry[]
        ContinuousQuery<Integer, String> query = new ContinuousQuery<>();
        // Setting an optional initial query.
        // The query will return entries for the keys greater than 10.
        query.setInitialQuery(new ScanQuery<>((k, v) -> k > 10));
        // mandatory local listener
        query.setLocalListener(events -> {
        });
        try (QueryCursor<Cache.Entry<Integer, String>> cursor = cache.query(query)) {
            // Iterating over the entries returned by the initial query
            for (Cache.Entry<Integer, String> e : cursor) System.out.println("key=" + e.getKey() + ", val=" + e.getValue());
        }
    // end::initialQry[]
    }
}
Also used : Factory(javax.cache.configuration.Factory) FactoryBuilder(javax.cache.configuration.FactoryBuilder) Ignite(org.apache.ignite.Ignite) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCache(org.apache.ignite.IgniteCache) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) Ignition(org.apache.ignite.Ignition) IgniteClosure(org.apache.ignite.lang.IgniteClosure) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 77 with ContinuousQuery

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

the class CacheContinuousAsyncQueryExample 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 example started.");
        // Auto-close cache at the end of the example.
        try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(CACHE_NAME)) {
            int keyCnt = 20;
            // These entries will be queried by initial predicate.
            for (int i = 0; i < keyCnt; i++) cache.put(i, Integer.toString(i));
            // Create new continuous query.
            ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
            qry.setInitialQuery(new ScanQuery<>(new IgniteBiPredicate<Integer, String>() {

                @Override
                public boolean apply(Integer key, String val) {
                    return key > 10;
                }
            }));
            // Callback that is called locally when update notifications are received.
            qry.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> evts) {
                    for (CacheEntryEvent<? extends Integer, ? extends String> e : evts) System.out.println("Updated entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
                }
            });
            // This filter will be evaluated remotely on all nodes.
            // Entry that pass this filter will be sent to the caller.
            qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter<Integer, String>>() {

                @Override
                public CacheEntryEventFilter<Integer, String> create() {
                    return new CacheEntryFilter();
                }
            });
            // Execute query.
            try (QueryCursor<Cache.Entry<Integer, String>> cur = cache.query(qry)) {
                // Iterate through existing data.
                for (Cache.Entry<Integer, String> e : cur) System.out.println("Queried existing entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
                // Add a few more keys and watch more query notifications.
                for (int i = 0; i < keyCnt; i++) cache.put(i, Integer.toString(i));
                // Wait for a while while callback is notified about remaining puts.
                Thread.sleep(2000);
            }
            // Iterate through entries which was updated from filter.
            for (int i = 0; i < 10; i++) System.out.println("Entry updated from filter [key=" + i + ", val=" + cache.get(i) + ']');
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 78 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery 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) {
    GridCacheContext<K, V> ctx = getContextSafe();
    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 && qry0.getRemoteFilterFactory() == null && qry0.getRemoteFilter() == null) {
            throw new IgniteException("LocalListener, RemoterFilter " + "or RemoteFilterFactory must be specified 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 && qry0.getRemoteFilterFactory() == null) {
            throw new IgniteException("LocalListener " + "or RemoteFilterFactory must be specified 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());
        try {
            final QueryCursor<Cache.Entry<K, V>> cur = qry.getInitialQuery() != null ? query(qry.getInitialQuery()) : null;
            return new QueryCursorEx<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);
                    }
                }

                @Override
                public void getAll(Consumer<Entry<K, V>> c) {
                // No-op.
                }

                @Override
                public List<GridQueryFieldMetadata> fieldsMeta() {
                    // noinspection rawtypes
                    return cur instanceof QueryCursorEx ? ((QueryCursorEx) cur).fieldsMeta() : null;
                }
            };
        } catch (Throwable t) {
            // Initial query failed: stop the routine.
            ctx.kernalContext().continuous().stopRoutine(routineId).get();
            throw t;
        }
    } 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) GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) 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) QueryCursorEx(org.apache.ignite.internal.processors.cache.query.QueryCursorEx) EventListener(org.apache.ignite.cache.query.ContinuousQueryWithTransformer.EventListener) UUID(java.util.UUID) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 79 with ContinuousQuery

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

the class PlatformContinuousQueryImpl method start.

/**
 * Start query execution.
 *
 * @param cache Cache.
 * @param loc Local flag.
 * @param bufSize Buffer size.
 * @param timeInterval Time interval.
 * @param autoUnsubscribe Auto-unsubscribe flag.
 * @param includeExpired Whether to include expired events.
 * @param initialQry Initial query.
 */
@SuppressWarnings("unchecked")
@Override
public void start(IgniteCacheProxy cache, boolean loc, int bufSize, long timeInterval, boolean autoUnsubscribe, Query initialQry, boolean includeExpired) throws IgniteCheckedException {
    lock.writeLock().lock();
    try {
        try {
            ContinuousQuery qry = new ContinuousQuery();
            qry.setLocalListener(this);
            // noinspection deprecation
            // Filter must be set always for correct resource release.
            qry.setRemoteFilter(this);
            qry.setPageSize(bufSize);
            qry.setTimeInterval(timeInterval);
            qry.setAutoUnsubscribe(autoUnsubscribe);
            qry.setInitialQuery(initialQry);
            qry.setIncludeExpired(includeExpired);
            cursor = cache.query(qry.setLocal(loc));
            initialQryCur = getInitialQueryCursor(initialQry);
        } catch (Exception e) {
            try {
                close0();
            } catch (Exception ignored) {
            // Ignore
            }
            throw PlatformUtils.unwrapQueryException(e);
        }
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ObjectStreamException(java.io.ObjectStreamException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException)

Example 80 with ContinuousQuery

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

the class CacheContinuousQueryExecuteInPrimaryTest method doTestWithoutEventsEntries.

/**
 * @throws Exception If failed.
 */
private void doTestWithoutEventsEntries(CacheConfiguration<Integer, String> ccfg) throws Exception {
    try (IgniteCache<Integer, String> cache = grid(0).createCache(ccfg)) {
        int ITERATION_CNT = 100;
        final AtomicBoolean noOneListen = new AtomicBoolean(true);
        for (int i = 0; i < ITERATION_CNT; i++) {
            ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
            qry.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> iterable) throws CacheEntryListenerException {
                    noOneListen.set(false);
                }
            });
            qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheEntryEventSerializableFilter<Integer, String>() {

                @Override
                public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> cacheEntryEvent) throws CacheEntryListenerException {
                    return false;
                }
            }));
            executeQuery(cache, qry, ccfg.getAtomicityMode() != ATOMIC);
        }
        assertTrue(noOneListen.get());
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery)

Aggregations

ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)123 Test (org.junit.Test)74 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)70 Ignite (org.apache.ignite.Ignite)60 CacheEntryEvent (javax.cache.event.CacheEntryEvent)58 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)53 CountDownLatch (java.util.concurrent.CountDownLatch)48 IgniteCache (org.apache.ignite.IgniteCache)43 QueryCursor (org.apache.ignite.cache.query.QueryCursor)38 ArrayList (java.util.ArrayList)32 IgniteException (org.apache.ignite.IgniteException)28 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)27 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)24 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)22 Cache (javax.cache.Cache)22 HashMap (java.util.HashMap)21 List (java.util.List)19 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)19 CacheException (javax.cache.CacheException)17 IgniteEx (org.apache.ignite.internal.IgniteEx)17