Java Code Examples for org.apache.ignite.IgniteCache#iterator()

The following examples show how to use org.apache.ignite.IgniteCache#iterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IgniteCacheConfigVariationsFullApiTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cache Cache.
 * @param key Key to remove.
 */
private void removeCacheIterator(IgniteCache<String, Integer> cache, String key) {
    Iterator<Cache.Entry<String, Integer>> iter = cache.iterator();

    int delCnt = 0;

    while (iter.hasNext()) {
        Cache.Entry<String, Integer> cur = iter.next();

        if (cur.getKey().equals(key)) {
            iter.remove();

            delCnt++;
        }
    }

    assertEquals(1, delCnt);
}
 
Example 2
Source File: IgniteCacheConfigVariationsFullApiTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cache Cache.
 * @param entries Expected entries in the cache.
 */
private void checkIteratorCache(IgniteCache cache, Map<String, Integer> entries) {
    Iterator<Cache.Entry<String, Integer>> iter = cache.iterator();

    int cnt = 0;

    while (iter.hasNext()) {
        Cache.Entry<String, Integer> cur = iter.next();

        assertTrue(entries.containsKey(cur.getKey()));
        assertEquals(entries.get(cur.getKey()), cur.getValue());

        cnt++;
    }

    assertEquals(entries.size(), cnt);
}
 
Example 3
Source File: GridCacheAbstractFullApiSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cache Cache.
 * @param key Key to remove.
 */
private void removeCacheIterator(IgniteCache<String, Integer> cache, String key) {
    Iterator<Cache.Entry<String, Integer>> iter = cache.iterator();

    int delCnt = 0;

    while (iter.hasNext()) {
        Cache.Entry<String, Integer> cur = iter.next();

        if (cur.getKey().equals(key)) {
            iter.remove();

            delCnt++;
        }
    }

    assertEquals(1, delCnt);
}
 
Example 4
Source File: GridCacheAbstractFullApiSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cache Cache.
 * @param entries Expected entries in the cache.
 */
private void checkIteratorCache(IgniteCache<String, Integer> cache, Map<String, Integer> entries) {
    Iterator<Cache.Entry<String, Integer>> iter = cache.iterator();

    int cnt = 0;

    while (iter.hasNext()) {
        Cache.Entry<String, Integer> cur = iter.next();

        assertTrue(entries.containsKey(cur.getKey()));
        assertEquals(entries.get(cur.getKey()), cur.getValue());

        cnt++;
    }

    assertEquals(entries.size(), cnt);
}
 
Example 5
Source File: GridCacheBinaryObjectsAbstractSelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testIterator() throws Exception {
    IgniteCache<Integer, TestObject> c = jcache(0);

    Map<Integer, TestObject> entries = new HashMap<>();

    for (int i = 0; i < ENTRY_CNT; i++) {
        TestObject val = new TestObject(i);

        c.put(i, val);

        entries.put(i, val);
    }

    IgniteCache<Integer, BinaryObject> prj = ((IgniteCacheProxy)c).keepBinary();

    Iterator<Cache.Entry<Integer, BinaryObject>> it = prj.iterator();

    assertTrue(it.hasNext());

    while (it.hasNext()) {
        Cache.Entry<Integer, BinaryObject> entry = it.next();

        assertTrue(entries.containsKey(entry.getKey()));

        TestObject o = entries.get(entry.getKey());

        BinaryObject po = entry.getValue();

        assertEquals(o.val, (int)po.field("val"));

        entries.remove(entry.getKey());
    }

    assertEquals(0, entries.size());
}
 
Example 6
Source File: StoreArrayKeyTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 *
 */
@Test
public void shouldRemoveAllCache() {
    IgniteCache<Object, Object> cache = node1.getOrCreateCache(CACHE);

    cache.put(firstKey, "val");

    cache.removeAll();

    assertEquals(0, cache.size());

    Iterator<Cache.Entry<Object, Object>> it = cache.iterator();

    assertFalse(it.hasNext());
}
 
Example 7
Source File: StoreArrayKeyTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 *
 */
@Test
public void shouldClearCache() {
    IgniteCache<Object, Object> cache = node1.getOrCreateCache(CACHE);

    cache.put(firstKey, "val");

    cache.clear();

    assertEquals(0, cache.size());

    Iterator<Cache.Entry<Object, Object>> it = cache.iterator();

    assertFalse(it.hasNext());
}
 
Example 8
Source File: CacheMvccSqlTxQueriesAbstractTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testIterator() throws Exception {
    ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT)
        .setIndexedTypes(Integer.class, Integer.class);

    startGrid(getConfiguration("grid").setMvccVacuumFrequency(Integer.MAX_VALUE));

    Ignite client = startClientGrid(getConfiguration("client"));

    IgniteCache<Object, Object> cache = client.cache(DEFAULT_CACHE_NAME);

    cache.put(1, 1);
    cache.put(2, 2);
    cache.put(3, 3);
    cache.put(4, 4);

    List<List<?>> res;

    try (Transaction tx = client.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        tx.timeout(TX_TIMEOUT);

        res = cache.query(new SqlFieldsQuery("UPDATE Integer SET _val = CASE _key " +
            "WHEN 1 THEN 10 WHEN 2 THEN 20 ELSE 30 END")).getAll();

        assertEquals(4L, res.get(0).get(0));

        tx.rollback();
    }

    try (Transaction tx = client.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        tx.timeout(TX_TIMEOUT);

        res = cache.query(new SqlFieldsQuery("UPDATE Integer SET _val = CASE _val " +
            "WHEN 1 THEN 10 WHEN 2 THEN 20 ELSE 30 END")).getAll();

        assertEquals(4L, res.get(0).get(0));

        res = cache.query(new SqlFieldsQuery("UPDATE Integer SET _val = CASE _val " +
            "WHEN 10 THEN 100 WHEN 20 THEN 200 ELSE 300 END")).getAll();

        assertEquals(4L, res.get(0).get(0));

        res = cache.query(new SqlFieldsQuery("DELETE FROM Integer WHERE _key = 4")).getAll();

        assertEquals(1L, res.get(0).get(0));

        tx.commit();
    }

    IgniteCache<Integer, Integer> cache0 = client.cache(DEFAULT_CACHE_NAME);

    Iterator<Cache.Entry<Integer, Integer>> it = cache0.iterator();

    Map<Integer, Integer> map = new HashMap<>();

    while (it.hasNext()) {
        Cache.Entry<Integer, Integer> e = it.next();

        assertNull("duplicate key returned from iterator", map.putIfAbsent(e.getKey(), e.getValue()));
    }

    assertEquals(3, map.size());

    assertEquals(100, map.get(1).intValue());
    assertEquals(200, map.get(2).intValue());
    assertEquals(300, map.get(3).intValue());
}