Search in sources :

Example 1 with IndexQuery

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

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

the class UsingScanQueries method executingIndexQueriesExample.

@Test
void executingIndexQueriesExample() {
    try (Ignite ignite = Ignition.start()) {
        // tag::idxQry[]
        // Create index by 2 fields (orgId, salary).
        QueryEntity personEntity = new QueryEntity(Integer.class, Person.class).setFields(new LinkedHashMap<String, String>() {

            {
                put("orgId", Integer.class.getName());
                put("salary", Integer.class.getName());
            }
        }).setIndexes(Collections.singletonList(new QueryIndex(Arrays.asList("orgId", "salary"), QueryIndexType.SORTED).setName("ORG_SALARY_IDX")));
        CacheConfiguration<Integer, Person> ccfg = new CacheConfiguration<Integer, Person>("entityCache").setQueryEntities(Collections.singletonList(personEntity));
        IgniteCache<Integer, Person> cache = ignite.getOrCreateCache(ccfg);
        // end::idxQry[]
        {
            // tag::idxQry[]
            // Find the persons who work in Organization 1.
            QueryCursor<Cache.Entry<Integer, Person>> cursor = cache.query(new IndexQuery<Integer, Person>(Person.class, "ORG_SALARY_IDX").setCriteria(eq("orgId", 1)));
        // end::idxQry[]
        }
        {
            // tag::idxQryMultipleCriteria[]
            // Find the persons who work in Organization 1 and have salary more than 1,000.
            QueryCursor<Cache.Entry<Integer, Person>> cursor = cache.query(new IndexQuery<Integer, Person>(Person.class, "ORG_SALARY_IDX").setCriteria(eq("orgId", 1), gt("salary", 1000)));
        // end::idxQryMultipleCriteria[]
        }
        {
            // tag::idxQryNoIdxName[]
            // Ignite finds suitable index "ORG_SALARY_IDX" by specified criterion field "orgId".
            QueryCursor<Cache.Entry<Integer, Person>> cursor = cache.query(new IndexQuery<Integer, Person>(Person.class).setCriteria(eq("orgId", 1)));
        // end::idxQryNoIdxName[]
        }
        {
            // tag::idxQryFilter[]
            // Find the persons who work in Organization 1 and whose name contains 'Vasya'.
            QueryCursor<Cache.Entry<Integer, Person>> cursor = cache.query(new IndexQuery<Integer, Person>(Person.class).setCriteria(eq("orgId", 1)).setFilter((k, v) -> v.getName().contains("Vasya")));
        // end::idxQryFilter[]
        }
    }
}
Also used : IndexQuery(org.apache.ignite.cache.query.IndexQuery) QueryIndex(org.apache.ignite.cache.QueryIndex) Ignite(org.apache.ignite.Ignite) QueryEntity(org.apache.ignite.cache.QueryEntity) QueryCursor(org.apache.ignite.cache.query.QueryCursor) LinkedHashMap(java.util.LinkedHashMap) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache) Test(org.junit.jupiter.api.Test)

Aggregations

Cache (javax.cache.Cache)2 IgniteCache (org.apache.ignite.IgniteCache)2 IndexQuery (org.apache.ignite.cache.query.IndexQuery)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 CacheException (javax.cache.CacheException)1 Ignite (org.apache.ignite.Ignite)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 CacheEntry (org.apache.ignite.cache.CacheEntry)1 QueryEntity (org.apache.ignite.cache.QueryEntity)1 QueryIndex (org.apache.ignite.cache.QueryIndex)1 QueryCursor (org.apache.ignite.cache.query.QueryCursor)1 SpiQuery (org.apache.ignite.cache.query.SpiQuery)1 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)1 TextQuery (org.apache.ignite.cache.query.TextQuery)1 CacheQuery (org.apache.ignite.internal.processors.cache.query.CacheQuery)1 CacheQueryFuture (org.apache.ignite.internal.processors.cache.query.CacheQueryFuture)1 IgniteOutClosureX (org.apache.ignite.internal.util.lang.IgniteOutClosureX)1 Test (org.junit.jupiter.api.Test)1