Search in sources :

Example 21 with ScanQuery

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

the class IgniteClientReconnectQueriesTest method testScanQueryReconnect.

/**
     * @throws Exception If failed.
     */
public void testScanQueryReconnect() throws Exception {
    Ignite cln = grid(serverCount());
    assertTrue(cln.cluster().localNode().isClient());
    final Ignite srv = clientRouter(cln);
    final IgniteCache<Integer, Person> clnCache = cln.getOrCreateCache(QUERY_CACHE);
    final IgniteCache<Integer, Person> srvCache = srv.getOrCreateCache(QUERY_CACHE);
    for (int i = 0; i < 10_000; i++) clnCache.put(i, new Person(i, "name-" + i, "surname-" + i));
    final ScanQuery<Integer, Person> scanQry = new ScanQuery<>();
    scanQry.setPageSize(1);
    scanQry.setFilter(new IgniteBiPredicate<Integer, Person>() {

        @Override
        public boolean apply(Integer integer, Person person) {
            return true;
        }
    });
    QueryCursor<Cache.Entry<Integer, Person>> qryCursor = clnCache.query(scanQry);
    reconnectClientNode(cln, srv, new Runnable() {

        @Override
        public void run() {
            srvCache.put(10_001, new Person(10_001, "name", "surname"));
            try {
                clnCache.query(scanQry);
                fail();
            } catch (CacheException e) {
                check(e);
            }
        }
    });
    try {
        qryCursor.getAll();
        fail();
    } catch (CacheException e) {
        checkAndWait(e);
    }
    qryCursor = clnCache.query(scanQry);
    assertEquals(10_001, qryCursor.getAll().size());
}
Also used : CacheException(javax.cache.CacheException) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Ignite(org.apache.ignite.Ignite)

Example 22 with ScanQuery

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

the class IgniteClientReconnectQueriesTest method scanQueryReconnectInProgress.

/**
     * @param setPart If {@code true} sets partition for scan query.
     * @throws Exception If failed.
     */
private void scanQueryReconnectInProgress(boolean setPart) throws Exception {
    Ignite cln = grid(serverCount());
    assertTrue(cln.cluster().localNode().isClient());
    final Ignite srv = clientRouter(cln);
    final IgniteCache<Integer, Person> clnCache = cln.getOrCreateCache(QUERY_CACHE);
    clnCache.put(1, new Person(1, "name1", "surname1"));
    clnCache.put(2, new Person(2, "name2", "surname2"));
    clnCache.put(3, new Person(3, "name3", "surname3"));
    final ScanQuery<Integer, Person> scanQry = new ScanQuery<>();
    scanQry.setPageSize(1);
    scanQry.setFilter(new IgniteBiPredicate<Integer, Person>() {

        @Override
        public boolean apply(Integer integer, Person person) {
            return true;
        }
    });
    if (setPart)
        scanQry.setPartition(1);
    blockMessage(GridCacheQueryResponse.class);
    final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                QueryCursor<Cache.Entry<Integer, Person>> qryCursor = clnCache.query(scanQry);
                qryCursor.getAll();
            } catch (CacheException e) {
                checkAndWait(e);
                return true;
            }
            return false;
        }
    });
    // Check that client waiting operation.
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            return fut.get(200);
        }
    }, IgniteFutureTimeoutCheckedException.class, null);
    assertNotDone(fut);
    unblockMessage();
    reconnectClientNode(cln, srv, null);
    assertTrue((Boolean) fut.get(2, SECONDS));
    QueryCursor<Cache.Entry<Integer, Person>> qryCursor2 = clnCache.query(scanQry);
    List<Cache.Entry<Integer, Person>> entries = qryCursor2.getAll();
    assertEquals(setPart ? 1 : 3, entries.size());
    for (Cache.Entry<Integer, Person> entry : entries) {
        assertEquals(Integer.class, entry.getKey().getClass());
        assertEquals(Person.class, entry.getValue().getClass());
    }
}
Also used : CacheException(javax.cache.CacheException) ScanQuery(org.apache.ignite.cache.query.ScanQuery) CacheException(javax.cache.CacheException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 23 with ScanQuery

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

the class CacheUtils method sparseFold.

private static <K, V, A> A sparseFold(String cacheName, IgniteBiFunction<Cache.Entry<K, V>, A, A> folder, IgnitePredicate<K> keyFilter, BinaryOperator<A> accumulator, A zeroVal, V defVal, K defKey, long defValCnt, boolean isNilpotent) {
    A defRes = zeroVal;
    if (!isNilpotent)
        for (int i = 0; i < defValCnt; i++) defRes = folder.apply(new CacheEntryImpl<>(defKey, defVal), defRes);
    Collection<A> totalRes = bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
        int partsCnt = ignite.affinity(cacheName).partitions();
        // Use affinity in filter for ScanQuery. Otherwise we accept consumer in each node which is wrong.
        Affinity affinity = ignite.affinity(cacheName);
        ClusterNode localNode = ignite.cluster().localNode();
        A a = zeroVal;
        // 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) == localNode && (keyFilter == null || keyFilter.apply(k))))) a = folder.apply(entry, a);
        }
        return a;
    });
    totalRes.add(defRes);
    return totalRes.stream().reduce(zeroVal, accumulator);
}
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) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 24 with ScanQuery

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

the class CacheUtils method fold.

/**
     * <b>Currently fold supports only commutative operations.<b/>
     *
     * @param cacheName Cache name.
     * @param folder Fold function operating over cache entries.
     * @param <K> Cache key object type.
     * @param <V> Cache value object type.
     * @param <A> Fold result type.
     * @return Fold operation result.
     */
public static <K, V, A> Collection<A> fold(String cacheName, IgniteBiFunction<CacheEntry<K, V>, A, A> folder, IgnitePredicate<K> keyFilter) {
    return bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
        int partsCnt = ignite.affinity(cacheName).partitions();
        // Use affinity in filter for ScanQuery. Otherwise we accept consumer in each node which is wrong.
        Affinity affinity = ignite.affinity(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        A a = null;
        // 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))))) a = folder.apply(new CacheEntry<>(entry, cache), a);
        }
        return a;
    });
}
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 25 with ScanQuery

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

the class GridCacheQueryTransformerSelfTest method testLocal.

/**
     * @throws Exception If failed.
     */
public void testLocal() throws Exception {
    IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
    try {
        for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
        Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public List<Integer> call() throws Exception {
                IgniteClosure<Cache.Entry<Integer, Value>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, Value>, Integer>() {

                    @Override
                    public Integer apply(Cache.Entry<Integer, Value> e) {
                        return e.getValue().idx;
                    }
                };
                return ignite.cache("test-cache").query(new ScanQuery<Integer, Value>().setLocal(true), transformer).getAll();
            }
        });
        List<Integer> res = new ArrayList<>(F.flatCollections(lists));
        assertEquals(50, res.size());
        Collections.sort(res);
        for (int i = 0; i < 50; i++) assertEquals(i * 100, res.get(i).intValue());
    } finally {
        cache.destroy();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

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