Search in sources :

Example 71 with Cache

use of javax.cache.Cache in project ignite by apache.

the class GridCacheFullTextQuerySelfTest method checkForMissedKeys.

/**
 * Check if there is missed keys.
 *
 * @throws IgniteCheckedException if failed.
 */
private static void checkForMissedKeys(IgniteEx ignite, Collection<Integer> exp, List<Cache.Entry<Integer, ?>> all) throws IgniteCheckedException {
    if (exp.size() == 0)
        return;
    IgniteInternalCache<Integer, Person> cache = ignite.cachex(PERSON_CACHE);
    assertNotNull(cache);
    StringBuilder sb = new StringBuilder();
    Affinity<Integer> aff = cache.affinity();
    for (Integer key : exp) {
        Integer part = aff.partition(key);
        sb.append(String.format("Query did not return expected key '%d' (exists: %s), partition '%d', partition nodes: ", key, cache.get(key) != null, part));
        Collection<ClusterNode> partNodes = aff.mapPartitionToPrimaryAndBackups(part);
        for (ClusterNode node : partNodes) sb.append(node).append("  ");
        sb.append(";\n");
    }
    sb.append("Returned keys: ");
    for (Cache.Entry e : all) sb.append(e.getKey()).append(" ");
    sb.append(";\n");
    fail(sb.toString());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 72 with Cache

use of javax.cache.Cache in project ignite by apache.

the class IgniteDbPutGetAbstractTest method testRandomPutGetRemove.

/**
 * @throws Exception If failed.
 */
@Test
public void testRandomPutGetRemove() throws Exception {
    final IgniteCache<Integer, DbValue> cache = cache(DEFAULT_CACHE_NAME);
    int cnt = KEYS_COUNT;
    Map<Integer, DbValue> map = new HashMap<>(cnt);
    long seed = System.currentTimeMillis();
    int iterations = SF.apply(MvccFeatureChecker.forcedMvcc() ? 30_000 : 90_000);
    X.println("Seed: " + seed);
    X.println("Iterations total: " + iterations);
    Random rnd = new GridRandom(seed);
    for (int i = 0; i < iterations; i++) {
        if (i % 5000 == 0)
            X.println("Iteration #" + i);
        int key = rnd.nextInt(cnt);
        DbValue v0 = new DbValue(key, "test-value-" + rnd.nextInt(200), rnd.nextInt(500));
        switch(rnd.nextInt(3)) {
            case 0:
                assertEquals(map.put(key, v0), cache.getAndPut(key, v0));
            case 1:
                assertEquals(map.get(key), cache.get(key));
                break;
            case 2:
                assertEquals(map.remove(key), cache.getAndRemove(key));
                assertNull(cache.get(key));
        }
    }
    assertEquals(map.size(), cache.size());
    for (Cache.Entry<Integer, DbValue> entry : cache.query(new ScanQuery<Integer, DbValue>())) assertEquals(map.get(entry.getKey()), entry.getValue());
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) Test(org.junit.Test)

Example 73 with Cache

use of javax.cache.Cache 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 74 with Cache

use of javax.cache.Cache in project ignite by apache.

the class IgniteDbPutGetAbstractTest method checkLocalEntries.

/**
 * @param total Expected total entries.
 * @param cntrs Expected per-node entries count.
 */
private void checkLocalEntries(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;
        for (Cache.Entry<DbKey, DbValue> e : cache0.localEntries()) {
            cnt++;
            allKeys.add(e.getKey());
            assertEquals(e.getKey().val, e.getValue().iVal);
        }
        assertEquals(cntrs.get(ignite0.cluster().localNode().id()), (Integer) cnt);
    }
    assertEquals(total, allKeys.size());
}
Also used : Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 75 with Cache

use of javax.cache.Cache 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)

Aggregations

Cache (javax.cache.Cache)271 IgniteCache (org.apache.ignite.IgniteCache)157 Test (org.junit.Test)130 Ignite (org.apache.ignite.Ignite)101 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)68 List (java.util.List)62 Map (java.util.Map)56 ScanQuery (org.apache.ignite.cache.query.ScanQuery)54 ArrayList (java.util.ArrayList)51 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)45 QueryCursor (org.apache.ignite.cache.query.QueryCursor)43 HashMap (java.util.HashMap)41 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)41 Collection (java.util.Collection)38 HashSet (java.util.HashSet)38 CacheManager (javax.cache.CacheManager)38 CacheException (javax.cache.CacheException)35 IgniteEx (org.apache.ignite.internal.IgniteEx)35 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)32 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)32