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

The following examples show how to use org.apache.ignite.IgniteCache#putIfAbsent() . 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: 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 2
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 3
Source File: GridCacheAtomicLocalTckMetricsSelfTestImpl.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testPutIfAbsent() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);

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

    boolean result = cache.putIfAbsent(1, 1);

    ++putCount;
    ++missCount;

    assertTrue(result);

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

    result = cache.putIfAbsent(1, 1);

    ++hitCount;

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

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

    boolean result = cache.putIfAbsent(1, 1);

    ++putCount;
    ++missCount;

    assertTrue(result);

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

    result = cache.putIfAbsent(1, 1);

    ++hitCount;

    cache.containsKey(123);

    assertFalse(result);
    assertEquals(hitCount, cache.localMetrics().getCacheHits());
    assertEquals(putCount, cache.localMetrics().getCachePuts());
    assertEquals(missCount, cache.localMetrics().getCacheMisses());
}
 
Example 6
Source File: IgniteCacheRandomOperationBenchmark.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param cache Ignite cache.
 * @throws Exception If failed.
 */
private void doPutIfAbsent(IgniteCache<Object, Object> cache) throws Exception {
    int i = nextRandom(args.range());

    cache.putIfAbsent(createRandomKey(i, cache.getName()), createRandomValue(i, cache.getName()));
}
 
Example 7
Source File: GridCacheOffheapUpdateSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testUpdateInPessimisticTxOnRemoteNode() throws Exception {
    try {
        Ignite ignite = startGrids(2);

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

        int key = 0;

        while (!ignite.affinity(DEFAULT_CACHE_NAME).isPrimary(grid(1).localNode(), key))
            key++;

        IgniteCache<Object, Object> locCache = grid(1).cache(DEFAULT_CACHE_NAME);

        try (Transaction tx = grid(1).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
            locCache.putIfAbsent(key, 0);

            tx.commit();
        }

        try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
            assertEquals(0, rmtCache.get(key));

            rmtCache.put(key, 1);

            tx.commit();
        }

        try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
            assertEquals(1, rmtCache.get(key));

            rmtCache.put(key, 2);

            tx.commit();
        }
    }
    finally {
        stopAllGrids();
    }
}
 
Example 8
Source File: IgnitePutIfAbsentIndexedValue1Benchmark.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 {
    IgniteCache<Integer, Object> cache = cacheForOperation();

    int key = insCnt.getAndIncrement();

    cache.putIfAbsent(key, new Person1(key));

    return true;
}
 
Example 9
Source File: CacheConnectionLeakStoreTxTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @param cache Cache.
 */
private void cacheOp(IgniteCache<Integer, Integer> cache) {
    boolean b = cache.putIfAbsent(42, 42);

    log.info("PutIfAbsent: " + b);

    Integer val = cache.get(42);

    log.info("Get: " + val);
}