Search in sources :

Example 1 with CacheQuery

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

the class GridCacheSetImpl method size.

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public int size() {
    try {
        onAccess();
        if (ctx.isLocal() || ctx.isReplicated()) {
            GridConcurrentHashSet<SetItemKey> set = ctx.dataStructures().setData(id);
            return set != null ? set.size() : 0;
        }
        CacheQuery qry = new GridCacheQueryAdapter<>(ctx, SET, null, null, new GridSetQueryPredicate<>(id, collocated), null, false, false);
        Collection<ClusterNode> nodes = dataNodes(ctx.affinity().affinityTopologyVersion());
        qry.projection(ctx.grid().cluster().forNodes(nodes));
        Iterable<Integer> col = (Iterable<Integer>) qry.execute(new SumReducer()).get();
        int sum = 0;
        for (Integer val : col) sum += val;
        return sum;
    } catch (IgniteCheckedException e) {
        throw U.convertException(e);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) CacheQuery(org.apache.ignite.internal.processors.cache.query.CacheQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheQueryAdapter(org.apache.ignite.internal.processors.cache.query.GridCacheQueryAdapter)

Example 2 with CacheQuery

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

the class GridCacheSetImpl method iterator0.

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
private GridCloseableIterator<T> iterator0() {
    try {
        CacheQuery qry = new GridCacheQueryAdapter<>(ctx, SET, null, null, new GridSetQueryPredicate<>(id, collocated), null, false, false);
        Collection<ClusterNode> nodes = dataNodes(ctx.affinity().affinityTopologyVersion());
        qry.projection(ctx.grid().cluster().forNodes(nodes));
        CacheQueryFuture<Map.Entry<T, ?>> fut = qry.execute();
        CacheWeakQueryIteratorsHolder.WeakReferenceCloseableIterator it = ctx.itHolder().iterator(fut, new CacheIteratorConverter<T, Map.Entry<T, ?>>() {

            @Override
            protected T convert(Map.Entry<T, ?> e) {
                return e.getKey();
            }

            @Override
            protected void remove(T item) {
                GridCacheSetImpl.this.remove(item);
            }
        });
        if (rmvd) {
            ctx.itHolder().removeIterator(it);
            checkRemoved();
        }
        return it;
    } catch (IgniteCheckedException e) {
        throw U.convertException(e);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) CacheQuery(org.apache.ignite.internal.processors.cache.query.CacheQuery) CacheWeakQueryIteratorsHolder(org.apache.ignite.internal.processors.cache.CacheWeakQueryIteratorsHolder) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SET(org.apache.ignite.internal.processors.cache.query.GridCacheQueryType.SET) Map(java.util.Map) GridCacheQueryAdapter(org.apache.ignite.internal.processors.cache.query.GridCacheQueryAdapter)

Example 3 with CacheQuery

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

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 CacheQuery (org.apache.ignite.internal.processors.cache.query.CacheQuery)3 Map (java.util.Map)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 GridCacheQueryAdapter (org.apache.ignite.internal.processors.cache.query.GridCacheQueryAdapter)2 NoSuchElementException (java.util.NoSuchElementException)1 Cache (javax.cache.Cache)1 CacheException (javax.cache.CacheException)1 IgniteCache (org.apache.ignite.IgniteCache)1 CacheEntry (org.apache.ignite.cache.CacheEntry)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 CacheWeakQueryIteratorsHolder (org.apache.ignite.internal.processors.cache.CacheWeakQueryIteratorsHolder)1 CacheQueryFuture (org.apache.ignite.internal.processors.cache.query.CacheQueryFuture)1 SET (org.apache.ignite.internal.processors.cache.query.GridCacheQueryType.SET)1 IgniteOutClosureX (org.apache.ignite.internal.util.lang.IgniteOutClosureX)1