Search in sources :

Example 26 with ScanQuery

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

the class GridCacheQueryTransformerSelfTest method testLocalKeepBinaryFiltered.

/**
     * @throws Exception If failed.
     */
public void testLocalKeepBinaryFiltered() 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 {
                IgniteBiPredicate<Integer, BinaryObject> filter = new IgniteBiPredicate<Integer, BinaryObject>() {

                    @Override
                    public boolean apply(Integer k, BinaryObject v) {
                        return v.<Integer>field("idx") % 1000 == 0;
                    }
                };
                IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer>() {

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

Example 27 with ScanQuery

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

the class GridCacheQueryTransformerSelfTest method testLocalKeepBinary.

/**
     * @throws Exception If failed.
     */
public void testLocalKeepBinary() 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, BinaryObject>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer>() {

                    @Override
                    public Integer apply(Cache.Entry<Integer, BinaryObject> e) {
                        return e.getValue().field("idx");
                    }
                };
                return ignite.cache("test-cache").withKeepBinary().query(new ScanQuery<Integer, BinaryObject>().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) BinaryObject(org.apache.ignite.binary.BinaryObject) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 28 with ScanQuery

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

the class IgniteCacheQueryCacheDestroySelfTest method runQuery.

/**
     * @throws Exception If failed.
     */
private void runQuery() throws Exception {
    ScanQuery<String, String> scanQuery = new ScanQuery<String, String>().setLocal(true).setFilter(new IgniteBiPredicate<String, String>() {

        @Override
        public boolean apply(String key, String p) {
            return key != null && key.isEmpty();
        }
    });
    Ignite ignite = ignite(ThreadLocalRandom.current().nextInt(GRID_CNT));
    IgniteCache<String, String> example = ignite.cache(CACHE_NAME);
    for (int partition : ignite.affinity(CACHE_NAME).primaryPartitions(ignite.cluster().localNode())) {
        scanQuery.setPartition(partition);
        try (QueryCursor cursor = example.query(scanQuery)) {
            for (Object p : cursor) {
                String value = (String) ((Cache.Entry) p).getValue();
                assertNotNull(value);
            }
        }
    }
}
Also used : ScanQuery(org.apache.ignite.cache.query.ScanQuery) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 29 with ScanQuery

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

the class PlatformCache method readScanQuery.

/**
     * Reads scan query.
     *
     * @param reader Binary reader.
     * @return Query.
     */
private Query readScanQuery(BinaryRawReaderEx reader) {
    boolean loc = reader.readBoolean();
    final int pageSize = reader.readInt();
    boolean hasPart = reader.readBoolean();
    Integer part = hasPart ? reader.readInt() : null;
    ScanQuery qry = new ScanQuery().setPageSize(pageSize);
    qry.setPartition(part);
    Object pred = reader.readObjectDetached();
    if (pred != null)
        qry.setFilter(platformCtx.createCacheEntryFilter(pred, 0));
    qry.setLocal(loc);
    return qry;
}
Also used : ScanQuery(org.apache.ignite.cache.query.ScanQuery)

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