use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class IgniteCacheRandomOperationBenchmark method doContinuousQuery.
/**
* @param cache Ignite cache.
* @param map Parameters map.
* @throws Exception If failed.
*/
private void doContinuousQuery(IgniteCache<Object, Object> cache, Map<Object, Object> map) throws Exception {
List<QueryCursor> cursors = (ArrayList<QueryCursor>) map.get(cache.getName());
if (cursors == null) {
cursors = new ArrayList<>(CONTINUOUS_QUERY_PER_CACHE);
map.put(cache.getName(), cursors);
}
if (cursors.size() == CONTINUOUS_QUERY_PER_CACHE) {
QueryCursor cursor = cursors.get(nextRandom(cursors.size()));
cursor.close();
cursors.remove(cursor);
}
ContinuousQuery qry = new ContinuousQuery();
qry.setLocalListener(new ContinuousQueryUpdater());
qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new ContinuousQueryFilter()));
cursors.add(cache.query(qry));
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class IgniteRepositoryQuery method execute.
/** {@inheritDoc} */
@Override
public Object execute(Object[] prmtrs) {
Query qry = prepareQuery(prmtrs);
QueryCursor qryCursor = cache.query(qry);
return transformQueryCursor(prmtrs, qryCursor);
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class IgniteCacheProxy 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(ContinuousQuery qry, boolean loc, boolean keepBinary) {
if (qry.getInitialQuery() instanceof ContinuousQuery)
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.");
if (qry.getLocalListener() == null)
throw new IgniteException("Mandatory local listener is not set for the query: " + qry);
if (qry.getRemoteFilter() != null && qry.getRemoteFilterFactory() != null)
throw new IgniteException("Should be used either RemoterFilter or RemoteFilterFactory.");
try {
final UUID routineId = ctx.continuousQueries().executeQuery(qry.getLocalListener(), qry.getRemoteFilter(), qry.getRemoteFilterFactory(), 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);
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class IgniteCacheProxy method query.
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public <R> QueryCursor<R> query(Query<R> qry) {
A.notNull(qry, "qry");
GridCacheGateway<K, V> gate = this.gate;
CacheOperationContext prev = onEnter(gate, opCtx);
try {
ctx.checkSecurity(SecurityPermission.CACHE_READ);
validate(qry);
convertToBinary(qry);
CacheOperationContext opCtxCall = ctx.operationContextPerCall();
boolean keepBinary = opCtxCall != null && opCtxCall.isKeepBinary();
if (qry instanceof ContinuousQuery)
return (QueryCursor<R>) queryContinuous((ContinuousQuery<K, V>) 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, keepBinary);
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);
} finally {
onLeave(gate, prev);
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class CacheScanPartitionQueryFallbackSelfTest method scanFallbackOnRebalancing.
/**
* @param cur If {@code true} tests query cursor.
* @throws Exception In case of error.
*/
private void scanFallbackOnRebalancing(final boolean cur) throws Exception {
cacheMode = CacheMode.PARTITIONED;
clientMode = false;
backups = 2;
commSpiFactory = new TestFallbackOnRebalancingCommunicationSpiFactory();
syncRebalance = true;
try {
Ignite ignite = startGrids(GRID_CNT);
fillCache(ignite);
final AtomicBoolean done = new AtomicBoolean(false);
final AtomicInteger idx = new AtomicInteger(GRID_CNT);
IgniteInternalFuture fut1 = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
int id = idx.getAndIncrement();
while (!done.get()) {
startGrid(id);
Thread.sleep(3000);
info("Will stop grid: " + getTestIgniteInstanceName(id));
stopGrid(id);
if (done.get())
return null;
Thread.sleep(3000);
}
return null;
}
}, 2);
final AtomicInteger nodeIdx = new AtomicInteger();
IgniteInternalFuture fut2 = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
int nodeId = nodeIdx.getAndIncrement();
IgniteCache<Integer, Integer> cache = grid(nodeId).cache(DEFAULT_CACHE_NAME);
int cntr = 0;
while (!done.get()) {
int part = ThreadLocalRandom.current().nextInt(ignite(nodeId).affinity(DEFAULT_CACHE_NAME).partitions());
if (cntr++ % 100 == 0)
info("Running query [node=" + nodeId + ", part=" + part + ']');
try (QueryCursor<Cache.Entry<Integer, Integer>> cur0 = cache.query(new ScanQuery<Integer, Integer>(part))) {
if (cur)
doTestScanQueryCursor(cur0, part);
else
doTestScanQuery(cur0, part);
}
}
return null;
}
}, GRID_CNT);
// Test for one minute
Thread.sleep(60 * 1000);
done.set(true);
fut2.get();
fut1.get();
} finally {
stopAllGrids();
}
}
Aggregations