Search in sources :

Example 11 with ScanQuery

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

the class CacheUtils method foreach.

/**
     * @param cacheName Cache name.
     * @param fun An operation that accepts a cache entry and processes it.
     * @param keyFilter Cache keys filter.
     * @param <K> Cache key object type.
     * @param <V> Cache value object type.
     */
public static <K, V> void foreach(String cacheName, IgniteConsumer<CacheEntry<K, V>> fun, IgnitePredicate<K> keyFilter) {
    bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
        int partsCnt = ignite.affinity(cacheName).partitions();
        // Use affinity in filter for scan query. Otherwise we accept consumer in each node which is wrong.
        Affinity affinity = ignite.affinity(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        // Iterate over all partitions. Some of them will be stored on that local node.
        for (int part = 0; part < partsCnt; part++) {
            int p = part;
            // Query returns an empty cursor if this partition is not stored on this node.
            for (Cache.Entry<K, V> entry : cache.query(new ScanQuery<K, V>(part, (k, v) -> affinity.mapPartitionToNode(p) == locNode && (keyFilter == null || keyFilter.apply(k))))) fun.accept(new CacheEntry<>(entry, cache));
        }
    });
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteConsumer(org.apache.ignite.ml.math.functions.IgniteConsumer) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) Affinity(org.apache.ignite.cache.affinity.Affinity) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) SparseDistributedMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage) IgniteCallable(org.apache.ignite.lang.IgniteCallable) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ValueMapper(org.apache.ignite.ml.math.ValueMapper) Map(java.util.Map) Cache(javax.cache.Cache) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) KeyMapper(org.apache.ignite.ml.math.KeyMapper) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Collection(java.util.Collection) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) Ignite(org.apache.ignite.Ignite) BinaryOperator(java.util.function.BinaryOperator) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Ignition(org.apache.ignite.Ignition) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) Collections(java.util.Collections) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteUuid(org.apache.ignite.lang.IgniteUuid) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 12 with ScanQuery

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

the class CacheKeepBinaryIterationTest method doTestScanQuery.

/**
     * @param ccfg Cache configuration.
     */
private void doTestScanQuery(final CacheConfiguration<Object, Object> ccfg, boolean keepBinary, boolean primitives) throws Exception {
    IgniteCache<Object, Object> cache = grid(0).createCache(ccfg);
    assertEquals(0, cache.size());
    try {
        for (int i = 0; i < KEYS; i++) if (primitives)
            cache.put(i, i);
        else
            cache.put(new QueryTestKey(i), new QueryTestValue(i));
        for (int i = 0; i < getServerNodeCount(); i++) {
            IgniteCache<Object, Object> cache0 = grid(i).cache(ccfg.getName());
            if (keepBinary)
                cache0 = cache0.withKeepBinary();
            ScanQuery<Object, Object> qry = new ScanQuery<>();
            qry.setLocal(true);
            int size = 0;
            try (QueryCursor<Cache.Entry<Object, Object>> cur = cache0.query(qry)) {
                for (Cache.Entry<Object, Object> e : cur) {
                    Object key = e.getKey();
                    Object val = e.getValue();
                    if (!primitives) {
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, keepBinary == key instanceof BinaryObject);
                        assertTrue("Got unexpected object: " + val.getClass() + ", keepBinary: " + keepBinary, keepBinary == val instanceof BinaryObject);
                    } else {
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, key instanceof Integer);
                        assertTrue("Got unexpected object: " + val.getClass() + ", keepBinary: " + keepBinary, val instanceof Integer);
                    }
                    ++size;
                }
            }
            assertTrue(size > 0);
        }
    } finally {
        if (ccfg.getEvictionPolicy() != null) {
            // TODO: IGNITE-3462. Fixes evictionPolicy issues at cache destroy.
            stopAllGrids();
            startGridsMultiThreaded(getServerNodeCount());
        } else
            grid(0).destroyCache(ccfg.getName());
    }
}
Also used : ScanQuery(org.apache.ignite.cache.query.ScanQuery) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 13 with ScanQuery

use of org.apache.ignite.cache.query.ScanQuery in project camel by apache.

the class IgniteCacheTest method testQuery.

@Test
public void testQuery() {
    IgniteCache<String, String> cache = ignite().getOrCreateCache("testcache1");
    Set<String> keys = new HashSet<>();
    for (int i = 0; i < 100; i++) {
        cache.put("k" + i, "v" + i);
        keys.add("k" + i);
    }
    Query<Entry<String, String>> query = new ScanQuery<String, String>(new IgniteBiPredicate<String, String>() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean apply(String key, String value) {
            return Integer.parseInt(key.replace("k", "")) >= 50;
        }
    });
    List results = template.requestBodyAndHeader("ignite:cache:testcache1?operation=QUERY", keys, IgniteConstants.IGNITE_CACHE_QUERY, query, List.class);
    assert_().that(results.size()).isEqualTo(50);
}
Also used : Entry(javax.cache.Cache.Entry) ScanQuery(org.apache.ignite.cache.query.ScanQuery) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with ScanQuery

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

the class IgniteCacheProxy method query.

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public <R> QueryCursor<R> query(Query<R> qry) {
    A.notNull(qry, "qry");
    GridCacheGateway<K, V> gate = this.gate;
    CacheOperationContext prev = onEnter(gate, opCtx);
    try {
        ctx.checkSecurity(SecurityPermission.CACHE_READ);
        validate(qry);
        convertToBinary(qry);
        CacheOperationContext opCtxCall = ctx.operationContextPerCall();
        boolean keepBinary = opCtxCall != null && opCtxCall.isKeepBinary();
        if (qry instanceof ContinuousQuery)
            return (QueryCursor<R>) queryContinuous((ContinuousQuery<K, V>) qry, qry.isLocal(), keepBinary);
        if (qry instanceof SqlQuery)
            return (QueryCursor<R>) ctx.kernalContext().query().querySql(ctx, (SqlQuery) qry, keepBinary);
        if (qry instanceof SqlFieldsQuery)
            return (FieldsQueryCursor<R>) ctx.kernalContext().query().querySqlFields(ctx, (SqlFieldsQuery) qry, keepBinary);
        if (qry instanceof ScanQuery)
            return query((ScanQuery) qry, null, projection(qry.isLocal()));
        return (QueryCursor<R>) query(qry, projection(qry.isLocal()));
    } catch (Exception e) {
        if (e instanceof CacheException)
            throw (CacheException) e;
        throw new CacheException(e);
    } finally {
        onLeave(gate, prev);
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) CacheException(javax.cache.CacheException) ScanQuery(org.apache.ignite.cache.query.ScanQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor)

Example 15 with ScanQuery

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

the class IgniteCacheProxy method query.

/** {@inheritDoc} */
@Override
public <T, R> QueryCursor<R> query(Query<T> qry, IgniteClosure<T, R> transformer) {
    A.notNull(qry, "qry");
    A.notNull(transformer, "transformer");
    if (!(qry instanceof ScanQuery))
        throw new UnsupportedOperationException("Transformers are supported only for SCAN queries.");
    GridCacheGateway<K, V> gate = this.gate;
    CacheOperationContext prev = onEnter(gate, opCtx);
    try {
        ctx.checkSecurity(SecurityPermission.CACHE_READ);
        validate(qry);
        return query((ScanQuery<K, V>) qry, transformer, projection(qry.isLocal()));
    } catch (Exception e) {
        if (e instanceof CacheException)
            throw (CacheException) e;
        throw new CacheException(e);
    } finally {
        onLeave(gate, prev);
    }
}
Also used : CacheException(javax.cache.CacheException) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException)

Aggregations

ScanQuery (org.apache.ignite.cache.query.ScanQuery)29 Ignite (org.apache.ignite.Ignite)17 Cache (javax.cache.Cache)16 IgniteCache (org.apache.ignite.IgniteCache)16 CacheException (javax.cache.CacheException)7 List (java.util.List)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 QueryCursor (org.apache.ignite.cache.query.QueryCursor)5 ArrayList (java.util.ArrayList)4 IgniteException (org.apache.ignite.IgniteException)4 BinaryObject (org.apache.ignite.binary.BinaryObject)4 IOException (java.io.IOException)3 Collection (java.util.Collection)3 Collections (java.util.Collections)3 Map (java.util.Map)3 BinaryOperator (java.util.function.BinaryOperator)3 EntryProcessorException (javax.cache.processor.EntryProcessorException)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 Ignition (org.apache.ignite.Ignition)3 Affinity (org.apache.ignite.cache.affinity.Affinity)3