Search in sources :

Example 1 with SpiQuery

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

the class IgniteCacheProxy method convertToBinary.

/**
     * Convert query arguments to BinaryObjects if binary marshaller used.
     *
     * @param qry Query.
     */
private void convertToBinary(final Query qry) {
    if (ctx.binaryMarshaller()) {
        if (qry instanceof SqlQuery) {
            final SqlQuery sqlQry = (SqlQuery) qry;
            convertToBinary(sqlQry.getArgs());
        } else if (qry instanceof SpiQuery) {
            final SpiQuery spiQry = (SpiQuery) qry;
            convertToBinary(spiQry.getArgs());
        } else if (qry instanceof SqlFieldsQuery) {
            final SqlFieldsQuery fieldsQry = (SqlFieldsQuery) qry;
            convertToBinary(fieldsQry.getArgs());
        }
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) SpiQuery(org.apache.ignite.cache.query.SpiQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 2 with SpiQuery

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

the class IgniteCacheProxyImpl method convertToBinary.

/**
 * Convert query arguments to BinaryObjects if binary marshaller used.
 *
 * @param qry Query.
 */
private void convertToBinary(final Query qry) {
    GridCacheContext<K, V> ctx = getContextSafe();
    if (ctx.binaryMarshaller()) {
        if (qry instanceof SqlQuery) {
            final SqlQuery sqlQry = (SqlQuery) qry;
            convertToBinary(sqlQry.getArgs());
        } else if (qry instanceof SpiQuery) {
            final SpiQuery spiQry = (SpiQuery) qry;
            convertToBinary(spiQry.getArgs());
        } else if (qry instanceof SqlFieldsQuery) {
            final SqlFieldsQuery fieldsQry = (SqlFieldsQuery) qry;
            convertToBinary(fieldsQry.getArgs());
        }
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) SpiQuery(org.apache.ignite.cache.query.SpiQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 3 with SpiQuery

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

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

the class IndexingSpiQuerySelfTest method testNonBinaryIndexingSpi.

/**
 * @throws Exception If failed.
 */
@Test
public void testNonBinaryIndexingSpi() throws Exception {
    System.setProperty(IgniteSystemProperties.IGNITE_UNWRAP_BINARY_FOR_INDEXING_SPI, "true");
    try {
        indexingSpi = new MyIndexingSpi();
        Ignite ignite = startGrid(0);
        CacheConfiguration<PersonKey, Person> ccfg = cacheConfiguration(DEFAULT_CACHE_NAME);
        IgniteCache<PersonKey, Person> cache = ignite.createCache(ccfg);
        for (int i = 0; i < 10; i++) {
            PersonKey key = new PersonKey(i);
            cache.put(key, new Person("John Doe " + i));
        }
        QueryCursor<Cache.Entry<PersonKey, Person>> cursor = cache.query(new SpiQuery<PersonKey, Person>().setArgs(new PersonKey(2), new PersonKey(5)));
        for (Cache.Entry<PersonKey, Person> entry : cursor) System.out.println(entry);
        cache.remove(new PersonKey(9));
    } finally {
        System.clearProperty(IgniteSystemProperties.IGNITE_UNWRAP_BINARY_FOR_INDEXING_SPI);
    }
}
Also used : SpiQuery(org.apache.ignite.cache.query.SpiQuery) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 5 with SpiQuery

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

Aggregations

SpiQuery (org.apache.ignite.cache.query.SpiQuery)8 Cache (javax.cache.Cache)6 IgniteCache (org.apache.ignite.IgniteCache)6 Ignite (org.apache.ignite.Ignite)4 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)4 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)4 Test (org.junit.Test)4 Map (java.util.Map)2 NoSuchElementException (java.util.NoSuchElementException)2 CacheException (javax.cache.CacheException)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 CacheEntry (org.apache.ignite.cache.CacheEntry)2 SqlQuery (org.apache.ignite.cache.query.SqlQuery)2 TextQuery (org.apache.ignite.cache.query.TextQuery)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 IndexQuery (org.apache.ignite.cache.query.IndexQuery)1