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

The following examples show how to use org.apache.ignite.IgniteCache#replace() . 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: IgniteDialect.java    From hibernate-ogm-ignite with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public Number nextValue(NextValueRequest request) {
	Long result = null;
	switch ( request.getKey().getMetadata().getType() ) {
		case TABLE:
			IgniteCache<String, Long> cache = provider.getIdSourceCache( request.getKey().getMetadata() );
			String idSourceKey = request.getKey().getColumnValue();
			Long previousValue = cache.get( idSourceKey );
			if ( previousValue == null ) {
				result = (long) request.getInitialValue();
				if ( !cache.putIfAbsent( idSourceKey, result ) ) {
					previousValue = (long) request.getInitialValue();
				}
			}
			if ( previousValue != null ) {
				while ( true ) {
					result = previousValue + request.getIncrement();
					if ( cache.replace( idSourceKey, previousValue, result ) ) {
						break;
					}
					else {
						previousValue = cache.get( idSourceKey );
					}
				}
			}
			break;
		case SEQUENCE:
			IgniteAtomicSequence seq = provider.atomicSequence( request.getKey().getMetadata().getName(), request.getInitialValue(), false );
			result = seq.getAndAdd( request.getIncrement() );
			break;
	}
	return result;
}
 
Example 2
Source File: IgniteCacheRandomOperationBenchmark.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param cache Ignite cache.
 * @throws Exception If failed.
 */
private void doReplace(IgniteCache<Object, Object> cache) throws Exception {
    int i = nextRandom(args.range());

    cache.replace(createRandomKey(i, cache.getName()),
        createRandomValue(i, cache.getName()),
        createRandomValue(i + 1, cache.getName()));
}
 
Example 3
Source File: IgniteCacheConfigVariationsFullApiTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception In case of error.
 */
@Test
public void testPeek() throws Exception {
    Ignite ignite = primaryIgnite("key");
    IgniteCache<String, Integer> cache = ignite.cache(cacheName());

    assertNull(cache.localPeek("key"));

    cache.put("key", 1);

    cache.replace("key", 2);

    assertEquals(2, cache.localPeek("key").intValue());
}
 
Example 4
Source File: GridCacheAtomicLocalTckMetricsSelfTestImpl.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testConditionReplace() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);

    long hitCount = 0;
    long missCount = 0;
    long putCount = 0;

    boolean result = cache.replace(1, 0, 10);

    ++missCount;
    assertFalse(result);

    assertEquals(missCount, cache.localMetrics().getCacheMisses());
    assertEquals(hitCount, cache.localMetrics().getCacheHits());
    assertEquals(putCount, cache.localMetrics().getCachePuts());

    assertNull(cache.localPeek(1));

    cache.put(1, 10);
    ++putCount;

    assertEquals(missCount, cache.localMetrics().getCacheMisses());
    assertEquals(hitCount, cache.localMetrics().getCacheHits());
    assertEquals(putCount, cache.localMetrics().getCachePuts());

    assertNotNull(cache.localPeek(1));

    result = cache.replace(1, 10, 20);

    assertTrue(result);
    ++hitCount;
    ++putCount;

    assertEquals(missCount, cache.localMetrics().getCacheMisses());
    assertEquals(hitCount, cache.localMetrics().getCacheHits());
    assertEquals(putCount, cache.localMetrics().getCachePuts());

    result = cache.replace(1, 40, 50);

    assertFalse(result);
    ++hitCount;

    assertEquals(hitCount, cache.localMetrics().getCacheHits());
    assertEquals(putCount, cache.localMetrics().getCachePuts());
    assertEquals(missCount, cache.localMetrics().getCacheMisses());
}
 
Example 5
Source File: GridCacheAtomicPartitionedTckMetricsSelfTestImpl.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testConditionReplace() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);

    long hitCount = 0;
    long missCount = 0;
    long putCount = 0;

    boolean result = cache.replace(1, 0, 10);

    ++missCount;
    assertFalse(result);

    assertEquals(missCount, cache.localMetrics().getCacheMisses());
    assertEquals(hitCount, cache.localMetrics().getCacheHits());
    assertEquals(putCount, cache.localMetrics().getCachePuts());

    assertNull(cache.localPeek(1));

    cache.put(1, 10);
    ++putCount;

    assertEquals(missCount, cache.localMetrics().getCacheMisses());
    assertEquals(hitCount, cache.localMetrics().getCacheHits());
    assertEquals(putCount, cache.localMetrics().getCachePuts());

    assertNotNull(cache.localPeek(1));

    result = cache.replace(1, 10, 20);

    assertTrue(result);
    ++hitCount;
    ++putCount;

    assertEquals(missCount, cache.localMetrics().getCacheMisses());
    assertEquals(hitCount, cache.localMetrics().getCacheHits());
    assertEquals(putCount, cache.localMetrics().getCachePuts());

    result = cache.replace(1, 40, 50);

    assertFalse(result);
    ++hitCount;

    assertEquals(hitCount, cache.localMetrics().getCacheHits());
    assertEquals(putCount, cache.localMetrics().getCachePuts());
    assertEquals(missCount, cache.localMetrics().getCacheMisses());
}
 
Example 6
Source File: IgniteReplaceIndexedValue1Benchmark.java    From ignite with Apache License 2.0 3 votes vote down vote up
/** {@inheritDoc} */
@Override public boolean test(Map<Object, Object> ctx) throws Exception {
    ThreadLocalRandom rnd = ThreadLocalRandom.current();

    IgniteCache<Integer, Object> cache = cacheForOperation();

    cache.replace(rnd.nextInt(args.range()), new Person1(rnd.nextInt(args.range())));

    return true;
}
 
Example 7
Source File: IgniteCacheAtomicProtocolTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @param backups Number of backups.
 * @throws Exception If failed.
 */
private void cacheOperations(int backups) throws Exception {
    ccfg = cacheConfiguration(backups, FULL_SYNC);

    final int SRVS = 4;

    startServers(SRVS);

    Ignite clientNode = startClientGrid(SRVS);

    final IgniteCache<Integer, Integer> nearCache = clientNode.cache(TEST_CACHE);

    Integer key = primaryKey(ignite(0).cache(TEST_CACHE));

    nearCache.replace(key, 1);

    nearCache.remove(key);

    nearCache.invoke(key, new SetValueEntryProcessor(null));

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

    List<Integer> keys = primaryKeys(ignite(0).cache(TEST_CACHE), 2);

    map.put(keys.get(0), new SetValueEntryProcessor(1));
    map.put(keys.get(1), new SetValueEntryProcessor(null));

    nearCache.invokeAll(map);

    Set<Integer> rmvAllKeys = new HashSet<>();

    for (int i = 0; i < 100; i++) {
        nearCache.put(i, i);

        if (i % 2 == 0)
            rmvAllKeys.add(i);
    }

    nearCache.removeAll(rmvAllKeys);
}