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[]
}
}
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);
}
}
}
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);
}
}
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();
}
}
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());
}
}
Aggregations