Search in sources :

Example 26 with QueryCursor

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);
            }
        }
    }
}
Also used : ScanQuery(org.apache.ignite.cache.query.ScanQuery) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 27 with QueryCursor

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());
    }
}
Also used : Iterator(java.util.Iterator) QueryCursor(org.apache.ignite.cache.query.QueryCursor)

Example 28 with QueryCursor

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());
    }
}
Also used : Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) QueryCursor(org.apache.ignite.cache.query.QueryCursor)

Example 29 with QueryCursor

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());
    }
}
Also used : ArrayList(java.util.ArrayList) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) QueryCursor(org.apache.ignite.cache.query.QueryCursor) BlockingQueue(java.util.concurrent.BlockingQueue) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) IgniteCache(org.apache.ignite.IgniteCache) PA(org.apache.ignite.internal.util.typedef.PA) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor)

Example 30 with QueryCursor

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();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) Cache(javax.cache.Cache)

Aggregations

QueryCursor (org.apache.ignite.cache.query.QueryCursor)34 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)20 IgniteCache (org.apache.ignite.IgniteCache)16 Ignite (org.apache.ignite.Ignite)14 Cache (javax.cache.Cache)13 ArrayList (java.util.ArrayList)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)10 IgniteException (org.apache.ignite.IgniteException)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 CacheEntryEvent (javax.cache.event.CacheEntryEvent)8 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)7 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 CacheException (javax.cache.CacheException)6 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)6 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)6 SqlQuery (org.apache.ignite.cache.query.SqlQuery)6 ScanQuery (org.apache.ignite.cache.query.ScanQuery)5 PA (org.apache.ignite.internal.util.typedef.PA)5 EntryProcessorException (javax.cache.processor.EntryProcessorException)4