Search in sources :

Example 1 with CacheQueryFuture

use of org.apache.ignite.internal.processors.cache.query.CacheQueryFuture in project ignite by apache.

the class GridCacheFullTextQueryMultithreadedSelfTest method testH2Text.

/**
 * JUnit.
 *
 * @throws Exception In case of error.
 */
@SuppressWarnings({ "TooBroadScope" })
@Test
public void testH2Text() throws Exception {
    int duration = 20 * 1000;
    final int keyCnt = 5000;
    final int logFreq = 50;
    final String txt = "Value";
    final int limit = 0;
    final GridCacheAdapter<Integer, H2TextValue> c = ((IgniteKernal) grid(0)).internalCache(DEFAULT_CACHE_NAME);
    IgniteInternalFuture<?> fut1 = multithreadedAsync(new Callable() {

        @Override
        public Object call() throws Exception {
            for (int i = 0; i < keyCnt; i++) {
                c.getAndPut(i, new H2TextValue(txt));
                if (i % logFreq == 0)
                    X.println("Stored values: " + i);
            }
            return null;
        }
    }, 1);
    // Create query.
    final CacheQuery<Map.Entry<Integer, H2TextValue>> qry = c.context().queries().createFullTextQuery(H2TextValue.class.getSimpleName(), txt, limit, Query.DFLT_PAGE_SIZE, false);
    qry.enableDedup(false);
    qry.includeBackups(false);
    qry.timeout(TEST_TIMEOUT);
    final AtomicBoolean stop = new AtomicBoolean();
    IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable() {

        @Override
        public Object call() throws Exception {
            int cnt = 0;
            while (!stop.get()) {
                CacheQueryFuture<Map.Entry<Integer, H2TextValue>> qryFut = qry.execute();
                int size = 0;
                while (qryFut.next() != null) size++;
                cnt++;
                if (cnt % logFreq == 0) {
                    X.println("Result set: " + size);
                    X.println("Executed queries: " + cnt);
                }
            }
            return null;
        }
    }, 1);
    Thread.sleep(duration);
    fut1.get();
    stop.set(true);
    fut2.get();
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) CacheQueryFuture(org.apache.ignite.internal.processors.cache.query.CacheQueryFuture) Callable(java.util.concurrent.Callable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Map(java.util.Map) Test(org.junit.Test)

Example 2 with CacheQueryFuture

use of org.apache.ignite.internal.processors.cache.query.CacheQueryFuture in project ignite by apache.

the class IgniteCacheProxyImpl method query.

/**
 * @param query Query.
 * @param grp Optional cluster group.
 * @return Cursor.
 * @throws IgniteCheckedException If failed.
 */
@SuppressWarnings("unchecked")
private QueryCursor<Cache.Entry<K, V>> query(final Query query, @Nullable ClusterGroup grp) throws IgniteCheckedException {
    GridCacheContext<K, V> ctx = getContextSafe();
    final CacheQuery qry;
    CacheOperationContext opCtxCall = ctx.operationContextPerCall();
    boolean isKeepBinary = opCtxCall != null && opCtxCall.isKeepBinary();
    final CacheQueryFuture fut;
    if (query instanceof TextQuery) {
        TextQuery q = (TextQuery) query;
        qry = ctx.queries().createFullTextQuery(q.getType(), q.getText(), q.getLimit(), q.getPageSize(), isKeepBinary);
        if (grp != null)
            qry.projection(grp);
        fut = ctx.kernalContext().query().executeQuery(GridCacheQueryType.TEXT, q.getText(), ctx, new IgniteOutClosureX<CacheQueryFuture<Map.Entry<K, V>>>() {

            @Override
            public CacheQueryFuture<Map.Entry<K, V>> applyx() {
                return qry.execute();
            }
        }, false);
    } else if (query instanceof SpiQuery) {
        qry = ctx.queries().createSpiQuery(isKeepBinary);
        if (grp != null)
            qry.projection(grp);
        fut = ctx.kernalContext().query().executeQuery(GridCacheQueryType.SPI, query.getClass().getSimpleName(), ctx, new IgniteOutClosureX<CacheQueryFuture<Map.Entry<K, V>>>() {

            @Override
            public CacheQueryFuture<Map.Entry<K, V>> applyx() {
                return qry.execute(((SpiQuery) query).getArgs());
            }
        }, false);
    } else if (query instanceof IndexQuery) {
        IndexQuery q = (IndexQuery) query;
        qry = ctx.queries().createIndexQuery(q, isKeepBinary);
        if (q.getPageSize() > 0)
            qry.pageSize(q.getPageSize());
        if (grp != null)
            qry.projection(grp);
        fut = ctx.kernalContext().query().executeQuery(GridCacheQueryType.INDEX, q.getValueType(), ctx, new IgniteOutClosureX<CacheQueryFuture<Map.Entry<K, V>>>() {

            @Override
            public CacheQueryFuture<Map.Entry<K, V>> applyx() {
                return qry.execute();
            }
        }, false);
    } else {
        if (query instanceof SqlFieldsQuery)
            throw new CacheException("Use methods 'queryFields' and 'localQueryFields' for " + SqlFieldsQuery.class.getSimpleName() + ".");
        throw new CacheException("Unsupported query type: " + query);
    }
    return new QueryCursorImpl<>(new GridCloseableIteratorAdapter<Entry<K, V>>() {

        /**
         */
        private Cache.Entry<K, V> cur;

        @Override
        protected Entry<K, V> onNext() throws IgniteCheckedException {
            if (!onHasNext())
                throw new NoSuchElementException();
            Cache.Entry<K, V> e = cur;
            cur = null;
            return e;
        }

        @Override
        protected boolean onHasNext() throws IgniteCheckedException {
            if (cur != null)
                return true;
            Object next = fut.next();
            // instead of Iterator<Map.Entry> due to IndexingSpi interface.
            if (next == null)
                return false;
            if (next instanceof Cache.Entry)
                cur = (Cache.Entry) next;
            else {
                Map.Entry e = (Map.Entry) next;
                cur = new CacheEntryImpl(e.getKey(), e.getValue());
            }
            return true;
        }

        @Override
        protected void onClose() throws IgniteCheckedException {
            fut.cancel();
        }
    });
}
Also used : CacheException(javax.cache.CacheException) SpiQuery(org.apache.ignite.cache.query.SpiQuery) CacheQuery(org.apache.ignite.internal.processors.cache.query.CacheQuery) CacheEntry(org.apache.ignite.cache.CacheEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteOutClosureX(org.apache.ignite.internal.util.lang.IgniteOutClosureX) IndexQuery(org.apache.ignite.cache.query.IndexQuery) CacheQueryFuture(org.apache.ignite.internal.processors.cache.query.CacheQueryFuture) TextQuery(org.apache.ignite.cache.query.TextQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 3 with CacheQueryFuture

use of org.apache.ignite.internal.processors.cache.query.CacheQueryFuture in project ignite by apache.

the class IgniteCacheProxy method query.

/**
     * @param filter Filter.
     * @param grp Optional cluster group.
     * @return Cursor.
     * @throws IgniteCheckedException If failed.
     */
@SuppressWarnings("unchecked")
private QueryCursor<Cache.Entry<K, V>> query(final Query filter, @Nullable ClusterGroup grp) throws IgniteCheckedException {
    final CacheQuery qry;
    boolean isKeepBinary = opCtx != null && opCtx.isKeepBinary();
    final CacheQueryFuture fut;
    if (filter instanceof TextQuery) {
        TextQuery p = (TextQuery) filter;
        qry = ctx.queries().createFullTextQuery(p.getType(), p.getText(), isKeepBinary);
        if (grp != null)
            qry.projection(grp);
        fut = ctx.kernalContext().query().executeQuery(GridCacheQueryType.TEXT, p.getText(), ctx, new IgniteOutClosureX<CacheQueryFuture<Map.Entry<K, V>>>() {

            @Override
            public CacheQueryFuture<Map.Entry<K, V>> applyx() {
                return qry.execute();
            }
        }, false);
    } else if (filter instanceof SpiQuery) {
        qry = ctx.queries().createSpiQuery(isKeepBinary);
        if (grp != null)
            qry.projection(grp);
        fut = ctx.kernalContext().query().executeQuery(GridCacheQueryType.SPI, filter.getClass().getSimpleName(), ctx, new IgniteOutClosureX<CacheQueryFuture<Map.Entry<K, V>>>() {

            @Override
            public CacheQueryFuture<Map.Entry<K, V>> applyx() {
                return qry.execute(((SpiQuery) filter).getArgs());
            }
        }, false);
    } else {
        if (filter instanceof SqlFieldsQuery)
            throw new CacheException("Use methods 'queryFields' and 'localQueryFields' for " + SqlFieldsQuery.class.getSimpleName() + ".");
        throw new CacheException("Unsupported query type: " + filter);
    }
    return new QueryCursorImpl<>(new GridCloseableIteratorAdapter<Entry<K, V>>() {

        /** */
        private Cache.Entry<K, V> cur;

        @Override
        protected Entry<K, V> onNext() throws IgniteCheckedException {
            if (!onHasNext())
                throw new NoSuchElementException();
            Cache.Entry<K, V> e = cur;
            cur = null;
            return e;
        }

        @Override
        protected boolean onHasNext() throws IgniteCheckedException {
            if (cur != null)
                return true;
            Object next = fut.next();
            // instead of Iterator<Map.Entry> due to IndexingSpi interface.
            if (next == null)
                return false;
            if (next instanceof Cache.Entry)
                cur = (Cache.Entry) next;
            else {
                Map.Entry e = (Map.Entry) next;
                cur = new CacheEntryImpl(e.getKey(), e.getValue());
            }
            return true;
        }

        @Override
        protected void onClose() throws IgniteCheckedException {
            fut.cancel();
        }
    });
}
Also used : IgniteOutClosureX(org.apache.ignite.internal.util.lang.IgniteOutClosureX) CacheException(javax.cache.CacheException) SpiQuery(org.apache.ignite.cache.query.SpiQuery) CacheQueryFuture(org.apache.ignite.internal.processors.cache.query.CacheQueryFuture) CacheQuery(org.apache.ignite.internal.processors.cache.query.CacheQuery) TextQuery(org.apache.ignite.cache.query.TextQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CacheEntry(org.apache.ignite.cache.CacheEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 4 with CacheQueryFuture

use of org.apache.ignite.internal.processors.cache.query.CacheQueryFuture in project ignite by apache.

the class GridQueryProcessor method executeQuery.

/**
 * @param qryType Query type.
 * @param qry Query description.
 * @param cctx Cache context.
 * @param clo Closure.
 * @param complete Complete.
 */
public <R> R executeQuery(GridCacheQueryType qryType, String qry, @Nullable GridCacheContext<?, ?> cctx, IgniteOutClosureX<R> clo, boolean complete) throws IgniteCheckedException {
    final long startTime = U.currentTimeMillis();
    Throwable err = null;
    R res = null;
    try {
        res = clo.apply();
        if (res instanceof CacheQueryFuture) {
            CacheQueryFuture fut = (CacheQueryFuture) res;
            err = fut.error();
        }
        return res;
    } catch (GridClosureException e) {
        err = e.unwrap();
        throw (IgniteCheckedException) err;
    } catch (CacheException | IgniteException e) {
        err = e;
        throw e;
    } catch (Exception e) {
        err = e;
        throw new IgniteCheckedException(e);
    } finally {
        boolean failed = err != null;
        long duration = U.currentTimeMillis() - startTime;
        if (complete || failed) {
            if (cctx != null)
                cctx.queries().collectMetrics(qryType, qry, startTime, duration, failed);
            if (log.isTraceEnabled())
                log.trace("Query execution [startTime=" + startTime + ", duration=" + duration + ", fail=" + failed + ", res=" + res + ']');
        }
    }
}
Also used : GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) CacheQueryFuture(org.apache.ignite.internal.processors.cache.query.CacheQueryFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) IgniteException(org.apache.ignite.IgniteException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) CacheException(javax.cache.CacheException) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException)

Aggregations

CacheQueryFuture (org.apache.ignite.internal.processors.cache.query.CacheQueryFuture)4 Map (java.util.Map)3 CacheException (javax.cache.CacheException)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 NoSuchElementException (java.util.NoSuchElementException)2 Cache (javax.cache.Cache)2 IgniteCache (org.apache.ignite.IgniteCache)2 CacheEntry (org.apache.ignite.cache.CacheEntry)2 SpiQuery (org.apache.ignite.cache.query.SpiQuery)2 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)2 TextQuery (org.apache.ignite.cache.query.TextQuery)2 CacheQuery (org.apache.ignite.internal.processors.cache.query.CacheQuery)2 IgniteOutClosureX (org.apache.ignite.internal.util.lang.IgniteOutClosureX)2 Callable (java.util.concurrent.Callable)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 IgniteException (org.apache.ignite.IgniteException)1 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)1 IndexQuery (org.apache.ignite.cache.query.IndexQuery)1 IgniteKernal (org.apache.ignite.internal.IgniteKernal)1 NodeStoppingException (org.apache.ignite.internal.NodeStoppingException)1