Search in sources :

Example 6 with ScanQuery

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

the class IgniteClientReconnectCacheQueriesFailoverTest method testReconnectScanQuery.

/**
     * @throws Exception If failed.
     */
public void testReconnectScanQuery() throws Exception {
    final Ignite client = grid(serverCount());
    final IgniteCache<Integer, Person> cache = client.cache(DEFAULT_CACHE_NAME);
    assertNotNull(cache);
    final Affinity<Integer> aff = client.affinity(DEFAULT_CACHE_NAME);
    final Map<Integer, Integer> partMap = new HashMap<>();
    for (int i = 0; i < aff.partitions(); i++) partMap.put(i, 0);
    for (int i = 0; i <= 10_000; i++) {
        Integer part = aff.partition(i);
        Integer size = partMap.get(part);
        partMap.put(part, size + 1);
    }
    reconnectFailover(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ScanQuery<Integer, Person> qry = new ScanQuery<>(new IgniteBiPredicate<Integer, Person>() {

                @Override
                public boolean apply(Integer key, Person val) {
                    return val.getId() % 2 == 1;
                }
            });
            assertEquals(5000, cache.query(qry).getAll().size());
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            Integer part = rnd.nextInt(0, aff.partitions());
            qry = new ScanQuery<>(part);
            assertEquals((int) partMap.get(part), cache.query(qry).getAll().size());
            return null;
        }
    });
}
Also used : HashMap(java.util.HashMap) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) CacheException(javax.cache.CacheException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Example 7 with ScanQuery

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

the class IgniteDbPutGetAbstractTest method checkScan.

/**
     * @param total Expected total entries.
     */
private void checkScan(int total) {
    for (int i = 0; i < gridCount(); i++) {
        Set<DbKey> allKeys = new HashSet<>();
        Ignite ignite0 = grid(i);
        IgniteCache<DbKey, DbValue> cache0 = ignite0.cache("non-primitive");
        ScanQuery<DbKey, DbValue> qry = new ScanQuery<>();
        QueryCursor<Cache.Entry<DbKey, DbValue>> cur = cache0.query(qry);
        for (Cache.Entry<DbKey, DbValue> e : cur) {
            allKeys.add(e.getKey());
            assertEquals(e.getKey().val, e.getValue().iVal);
        }
        assertEquals(total, allKeys.size());
    }
}
Also used : ScanQuery(org.apache.ignite.cache.query.ScanQuery) Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 8 with ScanQuery

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

the class IgniteDbPutGetAbstractTest method checkLocalScan.

/**
     * @param total Expected total entries.
     * @param cntrs Expected per-node entries count.
     */
private void checkLocalScan(int total, Map<UUID, Integer> cntrs) {
    Set<DbKey> allKeys = new HashSet<>();
    for (int i = 0; i < gridCount(); i++) {
        Ignite ignite0 = grid(i);
        IgniteCache<DbKey, DbValue> cache0 = ignite0.cache("non-primitive");
        int cnt = 0;
        ScanQuery<DbKey, DbValue> qry = new ScanQuery<>();
        qry.setLocal(true);
        QueryCursor<Cache.Entry<DbKey, DbValue>> cur = cache0.query(qry);
        Map<Integer, Integer> partCntrs = new HashMap<>();
        Affinity<Object> aff = ignite0.affinity(cache0.getName());
        for (Cache.Entry<DbKey, DbValue> e : cur) {
            cnt++;
            allKeys.add(e.getKey());
            assertEquals(e.getKey().val, e.getValue().iVal);
            int part = aff.partition(e.getKey());
            Integer partCntr = partCntrs.get(part);
            if (partCntr == null)
                partCntr = 1;
            else
                partCntr += 1;
            partCntrs.put(part, partCntr);
        }
        assertEquals(cntrs.get(ignite0.cluster().localNode().id()), (Integer) cnt);
        checkScanPartition(ignite0, cache0, partCntrs, true);
    }
    assertEquals(total, allKeys.size());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 9 with ScanQuery

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

the class CacheScanPartitionQueryFallbackSelfTest method testScanRemote.

/**
     * Scan should perform on the remote node.
     *
     * @throws Exception If failed.
     */
public void testScanRemote() throws Exception {
    cacheMode = CacheMode.PARTITIONED;
    backups = 0;
    commSpiFactory = new TestRemoteCommunicationSpiFactory();
    try {
        Ignite ignite = startGrids(GRID_CNT);
        IgniteCacheProxy<Integer, Integer> cache = fillCache(ignite);
        IgniteBiTuple<Integer, UUID> tup = remotePartition(cache.context());
        int part = tup.get1();
        expNodeId = tup.get2();
        QueryCursor<Cache.Entry<Integer, Integer>> qry = cache.query(new ScanQuery<Integer, Integer>().setPartition(part));
        doTestScanQuery(qry, part);
    } finally {
        stopAllGrids();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID)

Example 10 with ScanQuery

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

the class IgniteBinaryObjectFieldsQuerySelfTest method checkQuery.

/**
     * @throws Exception If failed.
     */
private void checkQuery(CacheMode cacheMode, CacheAtomicityMode atomicity) throws Exception {
    IgniteCache<Object, Object> cache = grid(GRID_CNT - 1).getOrCreateCache(cache(cacheMode, atomicity));
    try {
        populate(cache);
        QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(new SqlQuery("Person", "order " + "by id asc"));
        List<Cache.Entry<Object, Object>> all = cur.getAll();
        assertEquals(100, all.size());
        for (int i = 0; i < 100; i++) {
            Object person = all.get(i).getValue();
            assertEquals(Integer.valueOf(i), U.field(person, "id"));
            assertEquals("person-" + i, U.field(person, "name"));
            assertEquals("person-last-" + i, U.field(person, "lastName"));
            assertEquals((double) (i * 25), U.field(person, "salary"));
        }
        int max = 49;
        // Check local scan query with keepBinary flag set.
        ScanQuery<BinaryObject, BinaryObject> scanQry = new ScanQuery<>(new PersonKeyFilter(max));
        QueryCursor<Cache.Entry<BinaryObject, BinaryObject>> curs = grid(GRID_CNT - 1).cache(DEFAULT_CACHE_NAME).withKeepBinary().query(scanQry);
        List<Cache.Entry<BinaryObject, BinaryObject>> records = curs.getAll();
        assertEquals(50, records.size());
        for (Cache.Entry<BinaryObject, BinaryObject> entry : records) {
            BinaryObject key = entry.getKey();
            assertTrue(key.<Integer>field("id") <= max);
            assertEquals(PERSON_KEY_CLS_NAME, key.deserialize().getClass().getName());
        }
    } finally {
        grid(GRID_CNT - 1).cache(DEFAULT_CACHE_NAME).removeAll();
        grid(GRID_CNT - 1).destroyCache(DEFAULT_CACHE_NAME);
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) 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)

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