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

The following examples show how to use org.apache.ignite.IgniteCache#getAndPut() . 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: IgniteDbMemoryLeakSqlQueryTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override protected void operation(IgniteCache<Object, Object> cache) {
    Object key = key();
    Object val = value(key);

    switch (nextInt(4)) {
        case 0:
            cache.getAndPut(key, val);

            break;

        case 1:
            cache.get(key);

            break;

        case 2:
            cache.getAndRemove(key);

            break;

        case 3:
            cache.query(sqlQuery(cache)).getAll();
    }
}
 
Example 2
Source File: GridCacheNearOneNodeSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** @throws Exception If failed. */
@Test
public void testSingleLockPut() throws Exception {
    IgniteCache<Integer, String> near = jcache();

    Lock lock = near.lock(1);

    lock.lock();

    try {
        near.put(1, "1");
        near.put(2, "2");

        String one = near.getAndPut(1, "3");

        assertNotNull(one);
        assertEquals("1", one);
    }
    finally {
        lock.unlock();
    }
}
 
Example 3
Source File: GridCacheNearMultiNodeSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** @throws Exception If failed. */
@Test
public void testNoTransactionWriteThrough() throws Exception {
    IgniteCache<Integer, String> near = jcache(0);

    near.put(2, "2");

    String s = near.getAndPut(3, "3");

    assertNotNull(s);
    assertEquals("3", s);

    assertEquals("2", near.localPeek(2));
    assertEquals("3", near.localPeek(3));

    assertEquals("2", near.get(2));
    assertEquals("3", near.get(3));

    assertEquals("2", localPeek(dht(primaryGrid(2)), 2));
    assertEquals("3", localPeek(dht(primaryGrid(3)), 3));

    assertEquals(2, near.localSize(CachePeekMode.ALL));
    assertEquals(2, near.localSize(CachePeekMode.ALL));
}
 
Example 4
Source File: CacheClientStoreSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
private void doTestNoStore() throws Exception {
    factory = null;

    Ignite ignite = startClientGrid("client-1");

    IgniteCache<Object, Object> cache = ignite.cache(CACHE_NAME);

    cache.get(0);
    cache.getAll(F.asSet(0, 1));
    cache.getAndPut(0, 0);
    cache.getAndPutIfAbsent(0, 0);
    cache.getAndRemove(0);
    cache.getAndReplace(0, 0);
    cache.put(0, 0);
    cache.putAll(F.asMap(0, 0, 1, 1));
    cache.putIfAbsent(0, 0);
    cache.remove(0);
    cache.remove(0, 0);
    cache.removeAll(F.asSet(0, 1));
    cache.removeAll();
    cache.invoke(0, new EP());
    cache.invokeAll(F.asSet(0, 1), new EP());
}
 
Example 5
Source File: IgniteDbMemoryLeakAbstractTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cache IgniteCache.
 */
protected void operation(IgniteCache<Object, Object> cache) {
    Object key = key();
    Object val = value(key);

    switch (nextInt(3)) {
        case 0:
            cache.getAndPut(key, val);

            break;

        case 1:
            cache.get(key);

            break;

        case 2:
            cache.getAndRemove(key);
    }
}
 
Example 6
Source File: GridIndexingWithNoopSwapSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** @throws Exception If failed. */
@Test
public void testQuery() throws Exception {
    IgniteCache<Integer, ObjectValue> cache = ignite.cache(DEFAULT_CACHE_NAME);

    int cnt = 10;

    for (int i = 0; i < cnt; i++)
        cache.getAndPut(i, new ObjectValue("test" + i, i));

    for (int i = 0; i < cnt; i++) {
        assertNotNull(cache.localPeek(i, ONHEAP_PEEK_MODES));

        cache.localEvict(Collections.singleton(i)); // Swap.
    }

    SqlQuery<Integer, ObjectValue> qry =
        new SqlQuery(ObjectValue.class, "intVal >= ? order by intVal");

    assertEquals(10, cache.query(qry.setArgs(0)).getAll().size());
}
 
Example 7
Source File: IgnitePutAllLargeBatchSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
private void checkPreviousValue() throws Exception {
    startGrids(GRID_CNT);

    awaitPartitionMapExchange();

    try {
        Map<Integer, Integer> checkMap = new HashMap<>();

        IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);

        for (int r = 0; r < 3; r++) {
            for (int i = 0; i < 10; i++) {
                info("Put: " + i + ", " + r);

                Integer cachePrev = cache.getAndPut(i, r);

                Integer mapPrev = checkMap.put(i, r);

                assertEquals(mapPrev, cachePrev);
            }

            info(">>>>>>> Done round: " + r);
        }
    }
    finally {
        stopAllGrids();
    }
}
 
Example 8
Source File: GridCacheAbstractMetricsSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testMissesOnEmptyCache() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);

    assertEquals("Expected 0 read", 0, cache.localMetrics().getCacheGets());
    assertEquals("Expected 0 miss", 0, cache.localMetrics().getCacheMisses());

    Integer key = null;

    for (int i = 0; i < 1000; i++) {
        if (affinity(cache).isPrimary(grid(0).localNode(), i)) {
            key = i;

            break;
        }
    }

    assertNotNull(key);

    cache.get(key);

    assertEquals("Expected 1 read", 1, cache.localMetrics().getCacheGets());
    assertEquals("Expected 1 miss", 1, cache.localMetrics().getCacheMisses());

    cache.getAndPut(key, key); // +1 read, +1 miss.

    assertEquals("Expected 2 reads", 2, cache.localMetrics().getCacheGets());

    cache.get(key);

    assertEquals("Expected 1 write", 1, cache.localMetrics().getCachePuts());
    assertEquals("Expected 3 reads", 3, cache.localMetrics().getCacheGets());
    assertEquals("Expected 2 misses", 2, cache.localMetrics().getCacheMisses());
    assertEquals("Expected 1 hit", 1, cache.localMetrics().getCacheHits());
}
 
Example 9
Source File: CacheClientStoreSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testCorrectStore() throws Exception {
    nearEnabled = false;
    cacheMode = CacheMode.PARTITIONED;
    factory = new Factory1();

    startGrids(2);

    Ignite ignite = startClientGrid("client-1");

    IgniteCache<Object, Object> cache = ignite.cache(CACHE_NAME);

    cache.get(0);
    cache.getAll(F.asSet(0, 1));
    cache.getAndPut(0, 0);
    cache.getAndPutIfAbsent(0, 0);
    cache.getAndRemove(0);
    cache.getAndReplace(0, 0);
    cache.put(0, 0);
    cache.putAll(F.asMap(0, 0, 1, 1));
    cache.putIfAbsent(0, 0);
    cache.remove(0);
    cache.remove(0, 0);
    cache.removeAll(F.asSet(0, 1));
    cache.removeAll();
    cache.invoke(0, new EP());
    cache.invokeAll(F.asSet(0, 1), new EP());
}
 
Example 10
Source File: GridCacheOffheapIndexEntryEvictTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testUpdates() throws Exception {
    final int ENTRIES = 500;

    IgniteCache<Integer, TestValue> cache = grid(0).cache(DEFAULT_CACHE_NAME);

    for (int i = 0; i < ENTRIES; i++) {
        for (int j = 0; j < 3; j++) {
            cache.getAndPut(i, new TestValue(i));

            assertNotNull(cache.get(i));

            assertNotNull(cache.localPeek(i));
        }

        checkQuery(cache, "_key >= 0", i + 1);
    }

    for (int i = 0; i < ENTRIES; i++) {
        if (i % 2 == 0)
            cache.getAndRemove(i);
        else
            cache.remove(i);

        checkQuery(cache, "_key >= 0", ENTRIES - (i + 1));
    }
}
 
Example 11
Source File: CacheSpringStoreExample.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Executes transaction with read/write-through to persistent store.
 *
 * @param cache Cache to execute transaction on.
 */
private static void executeTransaction(IgniteCache<Long, Person> cache) {
    try (Transaction tx = Ignition.ignite().transactions().txStart()) {
        Person val = cache.get(id);

        System.out.println("Read value: " + val);

        val = cache.getAndPut(id, new Person(id, "Isaac", "Newton"));

        System.out.println("Overwrote old value: " + val);

        val = cache.get(id);

        System.out.println("Read value: " + val);

        tx.commit();
    }

    System.out.println("Read value after commit: " + cache.get(id));
}
 
Example 12
Source File: IgniteGetAndPutTxBytesKeyBenchmark.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public void setUp(BenchmarkConfiguration cfg) throws Exception {
    super.setUp(cfg);

    if (!IgniteSystemProperties.getBoolean("SKIP_MAP_CHECK"))
        ignite().compute().broadcast(new WaitMapExchangeFinishCallable());

    txs = ignite().transactions();

    clo = () -> {
        int base = nextRandom(args.range());

        byte[] key = String.valueOf(base).getBytes();

        IgniteCache<byte[], Object> cache = cacheForOperation();

        cache.getAndPut(key, new SampleValue(base));

        return null;
    };
}
 
Example 13
Source File: GridCacheAbstractFullApiSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception In case of error.
 */
@Test
public void testGetAndPutAsyncOld() throws Exception {
    IgniteCache<String, Integer> cache = jcache();

    IgniteCache<String, Integer> cacheAsync = cache.withAsync();

    cache.put("key1", 1);
    cache.put("key2", 2);

    cacheAsync.getAndPut("key1", 10);

    IgniteFuture<Integer> fut1 = cacheAsync.future();

    cacheAsync.getAndPut("key2", 11);

    IgniteFuture<Integer> fut2 = cacheAsync.future();

    assertEquals((Integer)1, fut1.get(5000));
    assertEquals((Integer)2, fut2.get(5000));

    assertEquals((Integer)10, cache.get("key1"));
    assertEquals((Integer)11, cache.get("key2"));
}
 
Example 14
Source File: IgniteCacheConfigVariationsFullApiTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception In case of error.
 */
@Test
public void testGetAndPutAsyncOld() throws Exception {
    IgniteCache<String, Integer> cache = jcache();

    IgniteCache<String, Integer> cacheAsync = cache.withAsync();

    cache.put("key1", 1);
    cache.put("key2", 2);

    cacheAsync.getAndPut("key1", 10);

    IgniteFuture<Integer> fut1 = cacheAsync.future();

    cacheAsync.getAndPut("key2", 11);

    IgniteFuture<Integer> fut2 = cacheAsync.future();

    assertEquals((Integer)1, fut1.get(5000));
    assertEquals((Integer)2, fut2.get(5000));

    assertEquals((Integer)10, cache.get("key1"));
    assertEquals((Integer)11, cache.get("key2"));
}
 
Example 15
Source File: IgniteCacheConfigVariationsFullApiTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testGetAndPutSerializableObject() throws Exception {
    IgniteCache<String, SerializableObject> cache = ignite(0).cache(cacheName());

    SerializableObject val1 = new SerializableObject(1);
    SerializableObject val2 = new SerializableObject(2);

    cache.put("key1", val1);

    SerializableObject oldVal = cache.get("key1");

    assertEquals(val1, oldVal);

    oldVal = cache.getAndPut("key1", val2);

    assertEquals(val1, oldVal);

    SerializableObject updVal = cache.get("key1");

    assertEquals(val2, updVal);
}
 
Example 16
Source File: IgniteCacheGroupsTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param cache Cache.
 */
private void cachePutGetAndPut(IgniteCache cache) {
    Random rnd = ThreadLocalRandom.current();

    Integer key = rnd.nextInt();
    Integer val1 = rnd.nextInt();
    Integer val2 = rnd.nextInt();

    cache.put(key, val1);

    Object val0 = cache.getAndPut(key, val2);

    assertEquals(val1, val0);

    val0 = cache.get(key);

    assertEquals(val2, val0);

    tearDown(cache);
}
 
Example 17
Source File: GridCacheNearMetricsSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testPrimaryPut() throws Exception {
    Ignite g0 = grid(0);

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

    int key;

    // Put and get a few keys.
    for (int i = 0; ; i++) {
        if (affinity(cache0).isPrimary(g0.cluster().localNode(), i)) {
            cache0.getAndPut(i, i); // +1 read

            cache0.get(i); // +1 read.

            key = i;

            info("Puts: " + cache0.localMetrics().getCachePuts());
            info("Reads: " + cache0.localMetrics().getCacheGets());
            info("Affinity nodes: " + U.nodes2names(affinity(cache0).mapKeyToPrimaryAndBackups(i)));

            break;
        }
    }

    for (int j = 0; j < gridCount(); j++) {
        Ignite g = grid(j);

        info("Checking grid: " + g.name());

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

        info("Puts: " + jcache.localMetrics().getCachePuts());
        info("Reads: " + jcache.localMetrics().getCacheGets());

        if (affinity(jcache).isPrimaryOrBackup(g.cluster().localNode(), key))
            assertEquals(1, jcache.localMetrics().getCachePuts());
        else
            assertEquals(0, jcache.localMetrics().getCachePuts());

        if (affinity(jcache).isPrimary(g.cluster().localNode(), key)) {
            assertEquals(2, jcache.localMetrics().getCacheGets());
            assertEquals(1, jcache.localMetrics().getCacheHits());
            assertEquals(1, jcache.localMetrics().getCacheMisses());
        }
        else {
            assertEquals(0, jcache.localMetrics().getCacheGets());
            assertEquals(0, jcache.localMetrics().getCacheHits());
            assertEquals(0, jcache.localMetrics().getCacheMisses());
        }
    }
}
 
Example 18
Source File: GridCacheNearMetricsSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testBackupPut() throws Exception {
    Ignite g0 = grid(0);

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

    int key;

    // Put and get a few keys.
    for (int i = 0; ; i++) {
        if (affinity(cache0).isBackup(g0.cluster().localNode(), i)) {
            cache0.getAndPut(i, i); // +1 read.

            cache0.get(i); // +1 read.

            key = i;

            info("Puts: " + cache0.localMetrics().getCachePuts());
            info("Reads: " + cache0.localMetrics().getCacheGets());
            info("Affinity nodes: " + U.nodes2names(affinity(cache0).mapKeyToPrimaryAndBackups(i)));

            break;
        }
    }

    for (int j = 0; j < gridCount(); j++) {
        Ignite g = grid(j);
        IgniteCache<Object, Object> jcache = g.cache(DEFAULT_CACHE_NAME);

        if (affinity(jcache).isPrimaryOrBackup(g.cluster().localNode(), key))
            assertEquals(1, jcache.localMetrics().getCachePuts());
        else
            assertEquals(0, jcache.localMetrics().getCachePuts());

        if (affinity(jcache).isPrimary(g.cluster().localNode(), key)) {
            assertEquals(1, jcache.localMetrics().getCacheGets());
            assertEquals(0, jcache.localMetrics().getCacheHits());
            assertEquals(1, jcache.localMetrics().getCacheMisses());
        }
        else if (affinity(jcache).isBackup(g.cluster().localNode(), key)) {
            assertEquals(1, jcache.localMetrics().getCacheGets());
            assertEquals(1, jcache.localMetrics().getCacheHits());
            assertEquals(0, jcache.localMetrics().getCacheMisses());
        }
        else {
            assertEquals(0, jcache.localMetrics().getCacheGets());
            assertEquals(0, jcache.localMetrics().getCacheHits());
            assertEquals(0, jcache.localMetrics().getCacheMisses());
        }
    }
}
 
Example 19
Source File: GridCacheNearMetricsSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testNearPut() throws Exception {
    Ignite g0 = grid(0);

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

    int key;

    // Put and get a few keys.
    for (int i = 0; ; i++) {
        if (!affinity(cache0).isPrimaryOrBackup(g0.cluster().localNode(), i)) {
            cache0.getAndPut(i, i); // +1 read.

            cache0.get(i); // +1 read.

            key = i;

            info("Writes: " + cache0.localMetrics().getCachePuts());
            info("Reads: " + cache0.localMetrics().getCacheGets());
            info("Affinity nodes: " + U.nodes2names(affinity(cache0).mapKeyToPrimaryAndBackups(i)));

            break;
        }
    }

    for (int j = 0; j < gridCount(); j++) {
        Ignite g = grid(j);

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

        assertEquals(1, jcache.localMetrics().getCachePuts());

        if (affinity(jcache).isPrimary(g.cluster().localNode(), key)) {
            assertEquals(1, jcache.localMetrics().getCacheGets());
            assertEquals(0, jcache.localMetrics().getCacheHits());
            assertEquals(1, jcache.localMetrics().getCacheMisses());
        }
        else if (affinity(jcache).isBackup(g.cluster().localNode(), key)) {
            assertEquals(0, jcache.localMetrics().getCacheGets());
            assertEquals(0, jcache.localMetrics().getCacheHits());
            assertEquals(0, jcache.localMetrics().getCacheMisses());
        }
        else {
            assertEquals(1, jcache.localMetrics().getCacheGets());
            assertEquals(1, jcache.localMetrics().getCacheHits());
            assertEquals(0, jcache.localMetrics().getCacheMisses());
        }
    }
}
 
Example 20
Source File: H2IndexingAbstractGeoSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Check geo-index (dynamic).
 *
 * @param dynamic Whether index should be created dynamically.
 * @throws Exception If failed.
 */
@SuppressWarnings({"unchecked", "ConstantConditions"})
private void checkGeo(boolean dynamic) throws Exception {
    IgniteCache<Integer, EnemyCamp> cache = createCache("camp", true, Integer.class, EnemyCamp.class, dynamic);

    try {
        WKTReader r = new WKTReader();

        cache.getAndPut(0, new EnemyCamp(r.read("POINT(25 75)"), "A"));
        cache.getAndPut(1, new EnemyCamp(r.read("POINT(70 70)"), "B"));
        cache.getAndPut(2, new EnemyCamp(r.read("POINT(70 30)"), "C"));
        cache.getAndPut(3, new EnemyCamp(r.read("POINT(75 25)"), "D"));

        SqlQuery<Integer, EnemyCamp> qry = new SqlQuery(EnemyCamp.class, "coords && ?");

        Collection<Cache.Entry<Integer, EnemyCamp>> res = cache.query(
            qry.setArgs(r.read("POLYGON((5 70, 5 80, 30 80, 30 70, 5 70))"))).getAll();

        checkPoints(res, "A");

        res = cache.query(
            qry.setArgs(r.read("POLYGON((10 5, 10 35, 70 30, 75 25, 10 5))"))).getAll();

        checkPoints(res, "C", "D");

        // Move B to the first polygon.
        cache.getAndPut(1, new EnemyCamp(r.read("POINT(20 75)"), "B"));

        res = cache.query(
            qry.setArgs(r.read("POLYGON((5 70, 5 80, 30 80, 30 70, 5 70))"))).getAll();

        checkPoints(res, "A", "B");

        // Move B to the second polygon.
        cache.getAndPut(1, new EnemyCamp(r.read("POINT(30 30)"), "B"));

        res = cache.query(
            qry.setArgs(r.read("POLYGON((10 5, 10 35, 70 30, 75 25, 10 5))"))).getAll();

        checkPoints(res, "B", "C", "D");

        // Remove B.
        cache.getAndRemove(1);

        res = cache.query(
            qry.setArgs(r.read("POLYGON((5 70, 5 80, 30 80, 30 70, 5 70))"))).getAll();

        checkPoints(res, "A");

        res = cache.query(
            qry.setArgs(r.read("POLYGON((10 5, 10 35, 70 30, 75 25, 10 5))"))).getAll();

        checkPoints(res, "C", "D");

        // Check explain request.
        String plan = cache.query(new SqlFieldsQuery("explain select * from EnemyCamp " +
            "where coords && 'POINT(25 75)'")).getAll().get(0).get(0).toString().toLowerCase();

        assertTrue("__ explain: " + plan, plan.contains("coords_idx"));

        if (dynamic)
            cache.query(new SqlFieldsQuery("DROP INDEX \"EnemyCamp_coords_idx\"")).getAll();
    }
    finally {
        destroy(cache, grid(0), dynamic);
    }
}