use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class IgniteCacheQueryCacheDestroySelfTest method runQuery.
/**
* @throws Exception If failed.
*/
private void runQuery() throws Exception {
ScanQuery<String, String> scanQuery = new ScanQuery<String, String>().setLocal(true).setFilter(new IgniteBiPredicate<String, String>() {
@Override
public boolean apply(String key, String p) {
return key != null && key.isEmpty();
}
});
Ignite ignite = ignite(ThreadLocalRandom.current().nextInt(GRID_CNT));
IgniteCache<String, String> example = ignite.cache(CACHE_NAME);
for (int partition : ignite.affinity(CACHE_NAME).primaryPartitions(ignite.cluster().localNode())) {
scanQuery.setPartition(partition);
try (QueryCursor cursor = example.query(scanQuery)) {
for (Object p : cursor) {
String value = (String) ((Cache.Entry) p).getValue();
assertNotNull(value);
}
}
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class OdbcRequestHandler method closeQuery.
/**
* {@link OdbcQueryCloseRequest} command handler.
*
* @param req Execute query request.
* @return Response.
*/
private SqlListenerResponse closeQuery(OdbcQueryCloseRequest req) {
try {
IgniteBiTuple<QueryCursor, Iterator> tuple = qryCursors.get(req.queryId());
if (tuple == null)
return new OdbcResponse(SqlListenerResponse.STATUS_FAILED, "Failed to find query with ID: " + req.queryId());
QueryCursor cur = tuple.get1();
assert (cur != null);
cur.close();
qryCursors.remove(req.queryId());
OdbcQueryCloseResult res = new OdbcQueryCloseResult(req.queryId());
return new OdbcResponse(res);
} catch (Exception e) {
qryCursors.remove(req.queryId());
U.error(log, "Failed to close SQL query [reqId=" + req.requestId() + ", req=" + req.queryId() + ']', e);
return new OdbcResponse(SqlListenerResponse.STATUS_FAILED, e.toString());
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class OdbcRequestHandler method fetchQuery.
/**
* {@link OdbcQueryFetchRequest} command handler.
*
* @param req Execute query request.
* @return Response.
*/
private SqlListenerResponse fetchQuery(OdbcQueryFetchRequest req) {
try {
IgniteBiTuple<QueryCursor, Iterator> tuple = qryCursors.get(req.queryId());
if (tuple == null)
return new OdbcResponse(SqlListenerResponse.STATUS_FAILED, "Failed to find query with ID: " + req.queryId());
Iterator iter = tuple.get2();
if (iter == null) {
QueryCursor cur = tuple.get1();
iter = cur.iterator();
tuple.put(cur, iter);
}
List<Object> items = new ArrayList<>();
for (int i = 0; i < req.pageSize() && iter.hasNext(); ++i) items.add(iter.next());
OdbcQueryFetchResult res = new OdbcQueryFetchResult(req.queryId(), items, !iter.hasNext());
return new OdbcResponse(res);
} catch (Exception e) {
U.error(log, "Failed to fetch SQL query result [reqId=" + req.requestId() + ", req=" + req + ']', e);
return new OdbcResponse(SqlListenerResponse.STATUS_FAILED, e.toString());
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class CacheContinuousQueryOrderingEventTest method doOrderingTest.
/**
* @param ccfg Cache configuration.
* @param async Async filter.
* @throws Exception If failed.
*/
protected void doOrderingTest(final CacheConfiguration ccfg, final boolean async) throws Exception {
ignite(0).createCache(ccfg);
List<QueryCursor<?>> qries = new ArrayList<>();
try {
List<BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>>> rcvdEvts = new ArrayList<>(LISTENER_CNT * NODES);
final AtomicInteger qryCntr = new AtomicInteger(0);
final int threadCnt = 20;
for (int idx = 0; idx < NODES; idx++) {
for (int i = 0; i < LISTENER_CNT; i++) {
BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>> queue = new ArrayBlockingQueue<>(ITERATION_CNT * threadCnt);
ContinuousQuery qry = new ContinuousQuery();
if (async) {
qry.setLocalListener(new TestCacheAsyncEventListener(queue, qryCntr));
qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilterAsync(ccfg.getName())));
} else {
qry.setLocalListener(new TestCacheEventListener(queue, qryCntr));
qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilter(ccfg.getName())));
}
rcvdEvts.add(queue);
IgniteCache<Object, Object> cache = grid(idx).cache(ccfg.getName());
QueryCursor qryCursor = cache.query(qry);
qries.add(qryCursor);
}
}
IgniteInternalFuture<Long> f = GridTestUtils.runMultiThreadedAsync(new Runnable() {
@Override
public void run() {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = 0; i < ITERATION_CNT; i++) {
IgniteCache<QueryTestKey, QueryTestValue> cache = grid(rnd.nextInt(NODES)).cache(ccfg.getName());
QueryTestKey key = new QueryTestKey(rnd.nextInt(KEYS));
boolean startTx = cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL && rnd.nextBoolean();
Transaction tx = null;
if (startTx)
tx = cache.unwrap(Ignite.class).transactions().txStart();
try {
if ((cache.get(key) == null) || rnd.nextBoolean()) {
cache.invoke(key, new CacheEntryProcessor<QueryTestKey, QueryTestValue, Object>() {
@Override
public Object process(MutableEntry<QueryTestKey, QueryTestValue> entry, Object... arguments) throws EntryProcessorException {
if (entry.exists())
entry.setValue(new QueryTestValue(entry.getValue().val1 + 1));
else
entry.setValue(new QueryTestValue(0));
return null;
}
});
} else {
QueryTestValue val;
QueryTestValue newVal;
do {
val = cache.get(key);
newVal = val == null ? new QueryTestValue(0) : new QueryTestValue(val.val1 + 1);
} while (!cache.replace(key, val, newVal));
}
} finally {
if (tx != null)
tx.commit();
}
}
}
}, threadCnt, "put-thread");
f.get(15, TimeUnit.SECONDS);
GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return qryCntr.get() >= ITERATION_CNT * threadCnt * LISTENER_CNT * NODES;
}
}, 1000L);
for (BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>> queue : rcvdEvts) checkEvents(queue, ITERATION_CNT * threadCnt);
assertFalse("Ordering invocations of filter broken.", fail);
} finally {
for (QueryCursor<?> qry : qries) qry.close();
ignite(0).destroyCache(ccfg.getName());
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class GridQueryProcessor method queryDistributedSql.
/**
* @param cctx Cache context.
* @param qry Query.
* @param keepBinary Keep binary flag.
* @return Cursor.
*/
private <K, V> QueryCursor<Cache.Entry<K, V>> queryDistributedSql(final GridCacheContext<?, ?> cctx, final SqlQuery qry, final boolean keepBinary) {
checkxEnabled();
if (!busyLock.enterBusy())
throw new IllegalStateException("Failed to execute query (grid is stopping).");
try {
final String schemaName = idx.schema(cctx.name());
final int mainCacheId = CU.cacheId(cctx.name());
return executeQuery(GridCacheQueryType.SQL, qry.getSql(), cctx, new IgniteOutClosureX<QueryCursor<Cache.Entry<K, V>>>() {
@Override
public QueryCursor<Cache.Entry<K, V>> applyx() throws IgniteCheckedException {
return idx.queryDistributedSql(schemaName, qry, keepBinary, mainCacheId);
}
}, true);
} catch (IgniteCheckedException e) {
throw new IgniteException(e);
} finally {
busyLock.leaveBusy();
}
}
Aggregations