Search in sources :

Example 11 with TextQuery

use of org.apache.ignite.cache.query.TextQuery in project ignite by apache.

the class IgniteCachePartitionedQueryMultiThreadedSelfTest method testLuceneAndSqlMultithreaded.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@SuppressWarnings({ "TooBroadScope" })
@Test
public void testLuceneAndSqlMultithreaded() throws Exception {
    // ---------- Test parameters ---------- //
    int luceneThreads = 10;
    int sqlThreads = 10;
    long duration = 10 * 1000;
    final int logMod = 100;
    final PersonObj p1 = new PersonObj("Jon", 1500, "Master");
    final PersonObj p2 = new PersonObj("Jane", 2000, "Master");
    final PersonObj p3 = new PersonObj("Mike", 1800, "Bachelor");
    final PersonObj p4 = new PersonObj("Bob", 1900, "Bachelor");
    final IgniteCache<UUID, PersonObj> cache0 = grid(0).cache(DEFAULT_CACHE_NAME);
    cache0.put(p1.id(), p1);
    cache0.put(p2.id(), p2);
    cache0.put(p3.id(), p3);
    cache0.put(p4.id(), p4);
    assertEquals(4, cache0.localSize(CachePeekMode.ALL));
    assert grid(0).cluster().nodes().size() == GRID_CNT;
    final AtomicBoolean done = new AtomicBoolean();
    final AtomicLong luceneCnt = new AtomicLong();
    // Start lucene query threads.
    IgniteInternalFuture<?> futLucene = GridTestUtils.runMultiThreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            while (!done.get()) {
                QueryCursor<Cache.Entry<UUID, PersonObj>> master = cache0.query(new TextQuery(PersonObj.class, "Master"));
                Collection<Cache.Entry<UUID, PersonObj>> entries = master.getAll();
                checkResult(entries, p1, p2);
                long cnt = luceneCnt.incrementAndGet();
                if (cnt % logMod == 0)
                    info("Executed LUCENE queries: " + cnt);
            }
        }
    }, luceneThreads, "LUCENE-THREAD");
    final AtomicLong sqlCnt = new AtomicLong();
    // Start sql query threads.
    IgniteInternalFuture<?> futSql = GridTestUtils.runMultiThreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            while (!done.get()) {
                QueryCursor<Cache.Entry<UUID, PersonObj>> bachelors = cache0.query(new SqlQuery(PersonObj.class, "degree = 'Bachelor'"));
                Collection<Cache.Entry<UUID, PersonObj>> entries = bachelors.getAll();
                checkResult(entries, p3, p4);
                long cnt = sqlCnt.incrementAndGet();
                if (cnt % logMod == 0)
                    info("Executed SQL queries: " + cnt);
            }
        }
    }, sqlThreads, "SQL-THREAD");
    Thread.sleep(duration);
    done.set(true);
    futLucene.get();
    futSql.get();
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) TextQuery(org.apache.ignite.cache.query.TextQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Collection(java.util.Collection) CAX(org.apache.ignite.internal.util.typedef.CAX) UUID(java.util.UUID) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 12 with TextQuery

use of org.apache.ignite.cache.query.TextQuery 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 13 with TextQuery

use of org.apache.ignite.cache.query.TextQuery in project ignite by apache.

the class CacheAbstractQueryDetailMetricsSelfTest method testTextQueryNotFullyFetchedMetrics.

/**
 * Test metrics for Sql queries.
 *
 * @throws Exception In case of error.
 */
@Test
public void testTextQueryNotFullyFetchedMetrics() throws Exception {
    IgniteCache<Integer, String> cache = grid(0).context().cache().jcache("A");
    TextQuery qry = new TextQuery<>("String", "1");
    qry.setPageSize(10);
    checkQueryNotFullyFetchedMetrics(cache, qry, true);
}
Also used : TextQuery(org.apache.ignite.cache.query.TextQuery) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 14 with TextQuery

use of org.apache.ignite.cache.query.TextQuery in project ignite by apache.

the class SqlQueryHistorySelfTest method testTextQueryMetrics.

/**
 * Test metrics for Sql queries.
 */
@Test
public void testTextQueryMetrics() {
    TextQuery qry = new TextQuery<>("String", "1");
    checkNoQueryMetrics(qry);
}
Also used : TextQuery(org.apache.ignite.cache.query.TextQuery) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

TextQuery (org.apache.ignite.cache.query.TextQuery)14 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)9 Test (org.junit.Test)9 Cache (javax.cache.Cache)3 IgniteCache (org.apache.ignite.IgniteCache)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 Map (java.util.Map)2 NoSuchElementException (java.util.NoSuchElementException)2 CacheException (javax.cache.CacheException)2 CacheEntry (org.apache.ignite.cache.CacheEntry)2 SpiQuery (org.apache.ignite.cache.query.SpiQuery)2 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)2 IgniteEx (org.apache.ignite.internal.IgniteEx)2 CacheQuery (org.apache.ignite.internal.processors.cache.query.CacheQuery)2 CacheQueryFuture (org.apache.ignite.internal.processors.cache.query.CacheQueryFuture)2 IgniteOutClosureX (org.apache.ignite.internal.util.lang.IgniteOutClosureX)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 UUID (java.util.UUID)1