Search in sources :

Example 6 with SQL

use of org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.SQL in project ignite by apache.

the class MvccRepeatableReadOperationsTest method testPutIfAbsentConsistency.

/**
 * Check getAndPut/getAndRemove operations consistency.
 *
 * @throws IgniteCheckedException If failed.
 */
@Test
public void testPutIfAbsentConsistency() throws IgniteCheckedException {
    Ignite node1 = grid(0);
    TestCache<Integer, MvccTestAccount> cache1 = new TestCache<>(node1.cache(DEFAULT_CACHE_NAME));
    final Set<Integer> keysForCreate = new HashSet<>(3);
    final Set<Integer> keysForUpdate = new HashSet<>(3);
    final Set<Integer> allKeys = generateKeySet(grid(0).cache(DEFAULT_CACHE_NAME), keysForCreate, keysForUpdate);
    final Map<Integer, MvccTestAccount> initialMap = keysForUpdate.stream().collect(Collectors.toMap(k -> k, k -> new MvccTestAccount(k, 1)));
    Map<Integer, MvccTestAccount> updatedMap = allKeys.stream().collect(Collectors.toMap(k -> k, k -> new MvccTestAccount(k, 1)));
    cache1.cache.putAll(initialMap);
    IgniteTransactions txs = node1.transactions();
    try (Transaction tx = txs.txStart(TransactionConcurrency.PESSIMISTIC, TransactionIsolation.REPEATABLE_READ)) {
        for (Integer key : keysForUpdate) // Check update.
        assertFalse(cache1.cache.putIfAbsent(key, new MvccTestAccount(key, 2)));
        for (Integer key : keysForCreate) // Check create.
        assertTrue(cache1.cache.putIfAbsent(key, new MvccTestAccount(key, 1)));
        assertEquals(updatedMap, getEntries(cache1, allKeys, SQL));
        tx.commit();
    }
    assertEquals(updatedMap, getEntries(cache1, allKeys, SQL));
    assertEquals(updatedMap, getEntries(cache1, allKeys, GET));
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) GET(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.GET) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Set(java.util.Set) HashMap(java.util.HashMap) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) Collectors(java.util.stream.Collectors) SQL(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.SQL) HashSet(java.util.HashSet) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) Map(java.util.Map) IgniteTransactions(org.apache.ignite.IgniteTransactions) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Map (java.util.Map)6 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6 Ignite (org.apache.ignite.Ignite)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IgniteTransactions (org.apache.ignite.IgniteTransactions)6 SQL (org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.SQL)6 Transaction (org.apache.ignite.transactions.Transaction)6 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)6 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)6 CacheEntryProcessor (org.apache.ignite.cache.CacheEntryProcessor)5 GET (org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.GET)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 List (java.util.List)3 Callable (java.util.concurrent.Callable)3 IgniteCache (org.apache.ignite.IgniteCache)3