use of org.apache.ignite.cache.query.AbstractContinuousQuery in project ignite by apache.
the class IgniteCacheProxyImpl method query.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public <R> QueryCursor<R> query(Query<R> qry) {
A.notNull(qry, "qry");
try {
ctx.checkSecurity(SecurityPermission.CACHE_READ);
validate(qry);
convertToBinary(qry);
CacheOperationContext opCtxCall = ctx.operationContextPerCall();
boolean keepBinary = opCtxCall != null && opCtxCall.isKeepBinary();
if (qry instanceof ContinuousQuery || qry instanceof ContinuousQueryWithTransformer)
return (QueryCursor<R>) queryContinuous((AbstractContinuousQuery) qry, qry.isLocal(), keepBinary);
if (qry instanceof SqlQuery)
return (QueryCursor<R>) ctx.kernalContext().query().querySql(ctx, (SqlQuery) qry, keepBinary);
if (qry instanceof SqlFieldsQuery)
return (FieldsQueryCursor<R>) ctx.kernalContext().query().querySqlFields(ctx, (SqlFieldsQuery) qry, null, keepBinary, true).get(0);
if (qry instanceof ScanQuery)
return query((ScanQuery) qry, null, projection(qry.isLocal()));
return (QueryCursor<R>) query(qry, projection(qry.isLocal()));
} catch (Exception e) {
if (e instanceof CacheException)
throw (CacheException) e;
throw new CacheException(e.getMessage(), e);
}
}
use of org.apache.ignite.cache.query.AbstractContinuousQuery in project ignite by apache.
the class CacheContinuousQueryRandomOperationsTest method singleOperation.
/**
* @param ccfg Cache configuration.
* @throws Exception If failed.
*/
private void singleOperation(CacheConfiguration ccfg) throws Exception {
IgniteCache<QueryTestKey, QueryTestValue> cache = grid(getClientIndex()).createCache(ccfg);
try {
AbstractContinuousQuery<QueryTestKey, QueryTestValue> qry = createQuery();
final List<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> evts = new CopyOnWriteArrayList<>();
if (noOpFilterFactory() != null)
qry.setRemoteFilterFactory(noOpFilterFactory());
if (qry instanceof ContinuousQuery) {
((ContinuousQuery<QueryTestKey, QueryTestValue>) qry).setLocalListener(new CacheEntryUpdatedListener<QueryTestKey, QueryTestValue>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> events) throws CacheEntryListenerException {
for (CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e : events) evts.add(e);
}
});
} else if (qry instanceof ContinuousQueryWithTransformer)
initQueryWithTransformer((ContinuousQueryWithTransformer<QueryTestKey, QueryTestValue, CacheEntryEvent>) qry, evts);
else
fail("Unknown query type");
QueryTestKey key = new QueryTestKey(1);
try (QueryCursor qryCur = cache.query(qry)) {
for (int i = 0; i < ITERATION_CNT; i++) {
log.info("Start iteration: " + i);
// Not events.
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
// Get events.
cache.put(key, new QueryTestValue(1));
cache.remove(key);
// Not events.
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
cache.remove(key);
// Get events.
cache.put(key, new QueryTestValue(2));
// Not events.
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
// Get events.
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
// Not events.
cache.remove(key);
// Get events.
cache.put(key, new QueryTestValue(3));
cache.put(key, new QueryTestValue(4));
// Not events.
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
cache.putIfAbsent(key, new QueryTestValue(5));
cache.putIfAbsent(key, new QueryTestValue(5));
cache.putIfAbsent(key, new QueryTestValue(5));
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
cache.remove(key, new QueryTestValue(5));
// Get events.
cache.remove(key, new QueryTestValue(4));
cache.putIfAbsent(key, new QueryTestValue(5));
// Not events.
cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
// Get events.
cache.replace(key, new QueryTestValue(5), new QueryTestValue(6));
assert GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return evts.size() == 9;
}
}, 5_000);
checkSingleEvent(evts.get(0), CREATED, new QueryTestValue(1), null);
checkSingleEvent(evts.get(1), REMOVED, null, new QueryTestValue(1));
checkSingleEvent(evts.get(2), CREATED, new QueryTestValue(2), null);
checkSingleEvent(evts.get(3), REMOVED, null, new QueryTestValue(2));
checkSingleEvent(evts.get(4), CREATED, new QueryTestValue(3), null);
checkSingleEvent(evts.get(5), EventType.UPDATED, new QueryTestValue(4), new QueryTestValue(3));
checkSingleEvent(evts.get(6), REMOVED, null, new QueryTestValue(4));
checkSingleEvent(evts.get(7), CREATED, new QueryTestValue(5), null);
checkSingleEvent(evts.get(8), EventType.UPDATED, new QueryTestValue(6), new QueryTestValue(5));
evts.clear();
cache.remove(key);
cache.remove(key);
assert GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return evts.size() == 1;
}
}, 5_000);
evts.clear();
log.info("Finish iteration: " + i);
}
}
} finally {
grid(getClientIndex()).destroyCache(ccfg.getName());
}
}
use of org.apache.ignite.cache.query.AbstractContinuousQuery in project ignite by apache.
the class CacheContinuousQueryRandomOperationsTest method doTestContinuousQuery.
/**
* @param ccfg Cache configuration.
* @param deploy The place where continuous query will be started.
* @throws Exception If failed.
*/
protected void doTestContinuousQuery(CacheConfiguration<Object, Object> ccfg, ContinuousDeploy deploy) throws Exception {
ignite(0).createCache(ccfg);
try {
long seed = System.currentTimeMillis();
Random rnd = new Random(seed);
log.info("Random seed: " + seed);
List<BlockingQueue<CacheEntryEvent<?, ?>>> evtsQueues = new ArrayList<>();
Collection<QueryCursor<?>> curs = new ArrayList<>();
if (deploy == CLIENT) {
AbstractContinuousQuery<Object, Object> qry = createQuery();
final BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue = new ArrayBlockingQueue<>(50_000);
if (qry instanceof ContinuousQuery) {
((ContinuousQuery<Object, Object>) qry).setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
for (CacheEntryEvent<?, ?> evt : evts) evtsQueue.add(evt);
}
});
} else if (qry instanceof ContinuousQueryWithTransformer)
initQueryWithTransformer((ContinuousQueryWithTransformer<Object, Object, CacheEntryEvent>) qry, evtsQueue);
else
fail("Unknown query type");
evtsQueues.add(evtsQueue);
QueryCursor<?> cur = grid(getClientIndex()).cache(ccfg.getName()).query(qry);
curs.add(cur);
} else if (deploy == SERVER) {
AbstractContinuousQuery<Object, Object> qry = createQuery();
final BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue = new ArrayBlockingQueue<>(50_000);
if (qry instanceof ContinuousQuery) {
((ContinuousQuery<Object, Object>) qry).setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
for (CacheEntryEvent<?, ?> evt : evts) evtsQueue.add(evt);
}
});
} else if (qry instanceof ContinuousQueryWithTransformer)
initQueryWithTransformer((ContinuousQueryWithTransformer<Object, Object, CacheEntryEvent>) qry, evtsQueue);
else
fail("Unknown query type");
evtsQueues.add(evtsQueue);
QueryCursor<?> cur = grid(rnd.nextInt(getServerNodeCount())).cache(ccfg.getName()).query(qry);
curs.add(cur);
} else {
for (int i = 0; i <= getServerNodeCount(); i++) {
AbstractContinuousQuery<Object, Object> qry = createQuery();
final BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue = new ArrayBlockingQueue<>(50_000);
if (qry instanceof ContinuousQuery) {
((ContinuousQuery<Object, Object>) qry).setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
for (CacheEntryEvent<?, ?> evt : evts) evtsQueue.add(evt);
}
});
} else if (qry instanceof ContinuousQueryWithTransformer)
initQueryWithTransformer((ContinuousQueryWithTransformer<Object, Object, CacheEntryEvent>) qry, evtsQueue);
else
fail("Unknown query type");
evtsQueues.add(evtsQueue);
QueryCursor<?> cur = ignite(i).cache(ccfg.getName()).query(qry);
curs.add(cur);
}
}
ConcurrentMap<Object, Object> expData = new ConcurrentHashMap<>();
Map<Integer, Long> partCntr = new ConcurrentHashMap<>();
try {
for (int i = 0; i < ITERATION_CNT; i++) {
if (i % 20 == 0)
log.info("Iteration: " + i);
for (int idx = 0; idx < getServerNodeCount(); idx++) randomUpdate(rnd, evtsQueues, expData, partCntr, grid(idx).cache(ccfg.getName()));
}
} finally {
for (QueryCursor<?> cur : curs) cur.close();
}
} finally {
ignite(0).destroyCache(ccfg.getName());
}
}
use of org.apache.ignite.cache.query.AbstractContinuousQuery in project ignite by apache.
the class CacheContinuousQueryRandomOperationsTest method batchOperation.
/**
* @param ccfg Cache configuration.
* @throws Exception If failed.
*/
private void batchOperation(CacheConfiguration ccfg) throws Exception {
IgniteCache<QueryTestKey, QueryTestValue> cache = grid(getClientIndex()).createCache(ccfg);
try {
AbstractContinuousQuery<QueryTestKey, QueryTestValue> qry = createQuery();
final List<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> evts = new CopyOnWriteArrayList<>();
if (noOpFilterFactory() != null)
qry.setRemoteFilterFactory(noOpFilterFactory());
if (qry instanceof ContinuousQuery) {
((ContinuousQuery<QueryTestKey, QueryTestValue>) qry).setLocalListener(new CacheEntryUpdatedListener<QueryTestKey, QueryTestValue>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> events) throws CacheEntryListenerException {
for (CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e : events) evts.add(e);
}
});
} else if (qry instanceof ContinuousQueryWithTransformer)
initQueryWithTransformer((ContinuousQueryWithTransformer<QueryTestKey, QueryTestValue, CacheEntryEvent>) qry, evts);
else
fail("Unknown query type");
Map<QueryTestKey, QueryTestValue> map = new TreeMap<>();
for (int i = 0; i < KEYS; i++) map.put(new QueryTestKey(i), new QueryTestValue(i));
try (QueryCursor qryCur = cache.query(qry)) {
for (int i = 0; i < ITERATION_CNT / 2; i++) {
log.info("Start iteration: " + i);
// Not events.
cache.removeAll(map.keySet());
cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
// Get events.
cache.putAll(map);
assert GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return evts.size() == KEYS;
}
}, 5_000);
checkEvents(evts, CREATED);
evts.clear();
// Not events.
cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
U.sleep(100);
assertEquals(0, evts.size());
// Get events.
cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
// Not events.
cache.removeAll(map.keySet());
cache.removeAll(map.keySet());
assert GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return evts.size() == KEYS;
}
}, 5_000);
checkEvents(evts, REMOVED);
evts.clear();
log.info("Finish iteration: " + i);
}
}
} finally {
grid(getClientIndex()).destroyCache(ccfg.getName());
}
}
use of org.apache.ignite.cache.query.AbstractContinuousQuery 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);
}
}
Aggregations