Search in sources :

Example 1 with TextQuery

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

the class GridCacheFullTextQuerySelfTest method checkTextQuery.

/**
 * @param clause Query clause.
 * @param loc local query flag.
 * @param keepBinary keep binary flag.
 */
private void checkTextQuery(String clause, boolean loc, boolean keepBinary) throws Exception {
    final IgniteEx ignite = grid(0);
    if (F.isEmpty(clause))
        clause = "1*";
    // 1. Populate cache with data, calculating expected count in parallel.
    Set<Integer> exp = populateCache(ignite, loc, MAX_ITEM_COUNT, new IgnitePredicate<Integer>() {

        @Override
        public boolean apply(Integer x) {
            return String.valueOf(x).startsWith("1");
        }
    });
    // 2. Validate results.
    TextQuery qry = new TextQuery<>(Person.class, clause).setLocal(loc);
    validateQueryResults(ignite, qry, exp, keepBinary);
    clearCache(ignite);
}
Also used : IgniteEx(org.apache.ignite.internal.IgniteEx) TextQuery(org.apache.ignite.cache.query.TextQuery)

Example 2 with TextQuery

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

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

the class SqlQueryHistorySelfTest method testTextQueryHistoryNotFullyFetched.

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

Example 4 with TextQuery

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

the class GridCacheFullTextQuerySelfTest method clearCache.

/**
 * Clear cache with check.
 */
private static void clearCache(IgniteEx ignite) {
    IgniteCache<Integer, Person> cache = ignite.cache(PERSON_CACHE);
    cache.clear();
    List all = cache.query(new TextQuery<>(Person.class, "1*")).getAll();
    assertTrue(all.isEmpty());
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) TextQuery(org.apache.ignite.cache.query.TextQuery)

Example 5 with TextQuery

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

the class GridCacheFullTextQuerySelfTest method checkTextQuery.

/**
 * @param clause Query clause.
 * @param limit limits response size
 * @param loc local query flag.
 * @param keepBinary keep binary flag.
 */
private void checkTextQuery(String clause, int limit, boolean loc, boolean keepBinary) throws Exception {
    final IgniteEx ignite = grid(0);
    if (F.isEmpty(clause))
        clause = "1*";
    // 1. Populate cache with data, calculating expected count in parallel.
    Set<Integer> exp = populateCache(ignite, loc, MAX_ITEM_COUNT, new IgnitePredicate<Integer>() {

        @Override
        public boolean apply(Integer x) {
            return String.valueOf(x).startsWith("1");
        }
    });
    // 2. Validate results.
    TextQuery qry = new TextQuery<>(Person.class, clause).setLocal(loc).setLimit(limit);
    validateQueryResults(ignite, qry, exp, keepBinary);
    clearCache(ignite);
}
Also used : IgniteEx(org.apache.ignite.internal.IgniteEx) TextQuery(org.apache.ignite.cache.query.TextQuery)

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