Search in sources :

Example 1 with CacheEntry

use of org.apache.ignite.cache.CacheEntry in project ignite by apache.

the class GridCacheQueryManager method sharedCacheSetIterator.

/**
 * @param qry Query.
 * @return Cache set items iterator.
 */
private GridCloseableIterator<IgniteBiTuple<K, V>> sharedCacheSetIterator(GridCacheQueryAdapter<?> qry) throws IgniteCheckedException {
    final GridSetQueryPredicate filter = (GridSetQueryPredicate) qry.scanFilter();
    IgniteUuid id = filter.setId();
    GridCacheQueryAdapter<CacheEntry<K, ?>> qry0 = new GridCacheQueryAdapter<>(cctx, SCAN, new IgniteBiPredicate<Object, Object>() {

        @Override
        public boolean apply(Object k, Object v) {
            return k instanceof SetItemKey && id.equals(((SetItemKey) k).setId());
        }
    }, new IgniteClosure<Map.Entry, Object>() {

        @Override
        public Object apply(Map.Entry entry) {
            return new IgniteBiTuple<K, V>((K) ((SetItemKey) entry.getKey()).item(), (V) Boolean.TRUE);
        }
    }, qry.partition(), false, true, qry.isDataPageScanEnabled());
    return scanQueryLocal(qry0, false);
}
Also used : CacheEntry(org.apache.ignite.cache.CacheEntry) CacheEntry(org.apache.ignite.cache.CacheEntry) IgniteUuid(org.apache.ignite.lang.IgniteUuid) GridSetQueryPredicate(org.apache.ignite.internal.processors.datastructures.GridSetQueryPredicate) SetItemKey(org.apache.ignite.internal.processors.datastructures.SetItemKey) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap)

Example 2 with CacheEntry

use of org.apache.ignite.cache.CacheEntry in project ignite by apache.

the class CacheSerializableTransactionsTest method testTxCommitReadOnlyGetAll.

/**
 * @throws Exception If failed.
 */
private void testTxCommitReadOnlyGetAll(boolean needVer) throws Exception {
    Ignite ignite0 = ignite(0);
    final IgniteTransactions txs = ignite0.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        try {
            IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
            Set<Integer> keys = new HashSet<>();
            for (int i = 0; i < 100; i++) keys.add(i);
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                if (needVer) {
                    Collection<CacheEntry<Integer, Integer>> c = cache.getEntries(keys);
                    assertTrue(c.isEmpty());
                } else {
                    Map<Integer, Integer> map = cache.getAll(keys);
                    assertTrue(map.isEmpty());
                }
                tx.commit();
            }
            for (Integer key : keys) checkValue(key, null, cache.getName());
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                if (needVer) {
                    Collection<CacheEntry<Integer, Integer>> c = cache.getEntries(keys);
                    assertTrue(c.isEmpty());
                } else {
                    Map<Integer, Integer> map = cache.getAll(keys);
                    assertTrue(map.isEmpty());
                }
                tx.rollback();
            }
            for (Integer key : keys) checkValue(key, null, cache.getName());
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions) CacheEntry(org.apache.ignite.cache.CacheEntry) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with CacheEntry

use of org.apache.ignite.cache.CacheEntry in project ignite by apache.

the class GridCacheAbstractFullApiSelfTest method testGetEntries.

/**
 * @throws Exception In case of error.
 */
@Test
public void testGetEntries() throws Exception {
    Transaction tx = txShouldBeUsed() ? transactions().txStart() : null;
    final IgniteCache<String, Integer> cache = jcache();
    try {
        cache.put("key1", 1);
        cache.put("key2", 2);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            cache.getEntries(null).isEmpty();
            return null;
        }
    }, NullPointerException.class, null);
    assert cache.getEntries(Collections.<String>emptySet()).isEmpty();
    Collection<CacheEntry<String, Integer>> c1 = cache.getEntries(ImmutableSet.of("key1", "key2", "key9999"));
    info("Retrieved c1: " + c1);
    assert 2 == c1.size() : "Invalid collection: " + c1;
    boolean b1 = false;
    boolean b2 = false;
    for (CacheEntry<String, Integer> e : c1) {
        if (e.getKey().equals("key1") && e.getValue().equals(1))
            b1 = true;
        if (e.getKey().equals("key2") && e.getValue().equals(2))
            b2 = true;
    }
    assertTrue(b1 && b2);
    Collection<CacheEntry<String, Integer>> c2 = cache.getEntries(ImmutableSet.of("key1", "key2", "key9999"));
    info("Retrieved c2: " + c2);
    assert 2 == c2.size() : "Invalid collection: " + c2;
    b1 = false;
    b2 = false;
    for (CacheEntry<String, Integer> e : c2) {
        if (e.getKey().equals("key1") && e.getValue().equals(1))
            b1 = true;
        if (e.getKey().equals("key2") && e.getValue().equals(2))
            b2 = true;
    }
    assertTrue(b1 && b2);
    // Now do the same checks but within transaction.
    if (txShouldBeUsed()) {
        try (Transaction tx0 = transactions().txStart()) {
            assert cache.getEntries(Collections.<String>emptySet()).isEmpty();
            c1 = cache.getEntries(ImmutableSet.of("key1", "key2", "key9999"));
            info("Retrieved c1: " + c1);
            assert 2 == c1.size() : "Invalid collection: " + c1;
            b1 = false;
            b2 = false;
            for (CacheEntry<String, Integer> e : c1) {
                if (e.getKey().equals("key1") && e.getValue().equals(1))
                    b1 = true;
                if (e.getKey().equals("key2") && e.getValue().equals(2))
                    b2 = true;
            }
            assertTrue(b1 && b2);
            c2 = cache.getEntries(ImmutableSet.of("key1", "key2", "key9999"));
            info("Retrieved c2: " + c2);
            assert 2 == c2.size() : "Invalid collection: " + c2;
            b1 = false;
            b2 = false;
            for (CacheEntry<String, Integer> e : c2) {
                if (e.getKey().equals("key1") && e.getValue().equals(1))
                    b1 = true;
                if (e.getKey().equals("key2") && e.getValue().equals(2))
                    b2 = true;
            }
            assertTrue(b1 && b2);
            tx0.commit();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) CacheEntry(org.apache.ignite.cache.CacheEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) EntryProcessorException(javax.cache.processor.EntryProcessorException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) Test(org.junit.Test)

Example 4 with CacheEntry

use of org.apache.ignite.cache.CacheEntry in project ignite by apache.

the class GridCacheInterceptorAbstractSelfTest method testGetAll.

/**
 * @throws Exception If failed.
 */
private void testGetAll(boolean needVer) throws Exception {
    Set<String> keys = new LinkedHashSet<>();
    for (int i = 0; i < 1000; i++) keys.add(String.valueOf(i));
    interceptor.retInterceptor = new NullGetInterceptor();
    IgniteCache<String, Integer> cache = jcache(0);
    Collection<CacheEntry<String, Integer>> c;
    Map<String, Integer> map;
    if (needVer) {
        c = cache.getEntries(keys);
        assertTrue(c.isEmpty());
    } else {
        map = cache.getAll(keys);
        for (String key : keys) assertEquals(null, map.get(key));
    }
    assertEquals(1000, interceptor.invokeCnt.get());
    interceptor.reset();
    interceptor.retInterceptor = new GetAllInterceptor1();
    if (needVer) {
        c = cache.getEntries(keys);
        assertEquals(500, c.size());
        for (CacheEntry<String, Integer> e : c) {
            int k = Integer.valueOf(e.getKey());
            assertEquals((Integer) (k * 2), e.getValue());
        }
    } else {
        map = cache.getAll(keys);
        for (String key : keys) {
            int k = Integer.valueOf(key);
            if (k % 2 == 0)
                assertEquals(null, map.get(key));
            else
                assertEquals((Integer) (k * 2), map.get(key));
        }
    }
    assertEquals(1000, interceptor.invokeCnt.get());
    // Put some values in cache.
    interceptor.disabled = true;
    for (int i = 0; i < 500; i++) cache.put(String.valueOf(i), i);
    interceptor.disabled = false;
    for (int j = 0; j < 2; j++) {
        interceptor.reset();
        interceptor.retInterceptor = new GetAllInterceptor2();
        if (needVer) {
            if (j == 0)
                c = cache.getEntries(keys);
            else
                c = cache.getEntriesAsync(keys).get();
            for (CacheEntry<String, Integer> e : c) {
                int k = Integer.valueOf(e.getKey());
                switch(k % 3) {
                    case 1:
                        Integer exp = k < 500 ? k : null;
                        assertEquals(exp, e.getValue());
                        break;
                    case 2:
                        assertEquals((Integer) (k * 3), e.getValue());
                        break;
                    default:
                        fail();
                }
            }
        } else {
            if (j == 0)
                map = cache.getAll(keys);
            else
                map = cache.getAllAsync(keys).get();
            int i = 0;
            for (String key : keys) {
                switch(i % 3) {
                    case 0:
                        assertEquals(null, map.get(key));
                        break;
                    case 1:
                        Integer exp = i < 500 ? i : null;
                        assertEquals(exp, map.get(key));
                        break;
                    case 2:
                        assertEquals((Integer) (i * 3), map.get(key));
                        break;
                    default:
                        fail();
                }
                i++;
            }
        }
        assertEquals(1000, interceptor.invokeCnt.get());
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CacheEntry(org.apache.ignite.cache.CacheEntry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 5 with CacheEntry

use of org.apache.ignite.cache.CacheEntry in project ignite by apache.

the class GridCacheVersionTopologyChangeTest method checkVersionIncrease.

/**
 * @param cache Cache.
 * @param vers Current versions.
 */
@SuppressWarnings("unchecked")
private void checkVersionIncrease(IgniteCache<Object, Object> cache, Map<Integer, Comparable> vers) {
    for (Integer k : vers.keySet()) {
        cache.put(k, k);
        Comparable curVer = vers.get(k);
        CacheEntry entry = cache.getEntry(k);
        if (entry != null) {
            Comparable newVer = entry.version();
            assertTrue(newVer.compareTo(curVer) > 0);
            vers.put(k, newVer);
        } else {
            CacheConfiguration ccfg = cache.getConfiguration(CacheConfiguration.class);
            assertEquals(0, ccfg.getBackups());
        }
    }
}
Also used : CacheEntry(org.apache.ignite.cache.CacheEntry) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

CacheEntry (org.apache.ignite.cache.CacheEntry)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 EntryProcessorException (javax.cache.processor.EntryProcessorException)6 Test (org.junit.Test)6 LinkedHashSet (java.util.LinkedHashSet)5 IgniteCache (org.apache.ignite.IgniteCache)4 BinaryObject (org.apache.ignite.binary.BinaryObject)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Set (java.util.Set)3 IgniteCacheConfigVariationsAbstractTest (org.apache.ignite.testframework.junits.IgniteCacheConfigVariationsAbstractTest)3 HashSet (java.util.HashSet)2 MutableEntry (javax.cache.processor.MutableEntry)2 Transaction (org.apache.ignite.transactions.Transaction)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1