Search in sources :

Example 21 with Cache

use of javax.cache.Cache in project ignite by apache.

the class IgniteCacheBinaryObjectsScanSelfTest method testScanNoClasses.

/**
     * @throws Exception If failed.
     */
public void testScanNoClasses() throws Exception {
    Ignite client = grid("client");
    IgniteCache<Object, Object> cache = client.cache("testCache");
    List<Cache.Entry<Object, Object>> entries = cache.query(new ScanQuery<>()).getAll();
    assertEquals(100, entries.size());
    for (Cache.Entry<Object, Object> entry : entries) {
        assertEquals(PERSON_KEY_CLS_NAME, entry.getKey().getClass().getName());
        assertEquals(PERSON_CLS_NAME, entry.getValue().getClass().getName());
    }
    entries = new ArrayList<>();
    int partCnt = client.affinity("testCache").partitions();
    for (int i = 0; i < partCnt; i++) entries.addAll(cache.query(new ScanQuery<>(i)).getAll());
    assertEquals(100, entries.size());
    for (Cache.Entry<Object, Object> entry : entries) {
        assertEquals(PERSON_KEY_CLS_NAME, entry.getKey().getClass().getName());
        assertEquals(PERSON_CLS_NAME, entry.getValue().getClass().getName());
    }
}
Also used : ScanQuery(org.apache.ignite.cache.query.ScanQuery) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 22 with Cache

use of javax.cache.Cache in project ignite by apache.

the class IgniteCacheConfigVariationsFullApiTest method testContinuousQuery.

/**
     * @throws Exception If failed.
     */
public void testContinuousQuery() throws Exception {
    runInAllDataModes(new TestRunnable() {

        @Override
        public void run() throws Exception {
            final AtomicInteger updCnt = new AtomicInteger();
            ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
            qry.setInitialQuery(new ScanQuery<>(new IgniteBiPredicate<Object, Object>() {

                @Override
                public boolean apply(Object key, Object val) {
                    return valueOf(key) >= 3;
                }
            }));
            qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<? extends Object, ? extends Object>> evts) throws CacheEntryListenerException {
                    for (CacheEntryEvent<? extends Object, ? extends Object> evt : evts) {
                        int v = valueOf(evt.getKey());
                        // Check filter.
                        assertTrue("v=" + v, v >= 10 && v < 15);
                        updCnt.incrementAndGet();
                    }
                }
            });
            qry.setRemoteFilter(new TestCacheEntryEventSerializableFilter());
            IgniteCache<Object, Object> cache = jcache();
            for (int i = 0; i < 10; i++) cache.put(key(i), value(i));
            try (QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(qry)) {
                int cnt = 0;
                for (Cache.Entry<Object, Object> e : cur) {
                    cnt++;
                    int val = valueOf(e.getKey());
                    assertTrue("v=" + val, val >= 3);
                }
                assertEquals(7, cnt);
                for (int i = 10; i < 20; i++) cache.put(key(i), value(i));
                GridTestUtils.waitForCondition(new GridAbsPredicateX() {

                    @Override
                    public boolean applyx() throws IgniteCheckedException {
                        return updCnt.get() == 5;
                    }
                }, 30_000);
            }
        }
    });
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) MutableEntry(javax.cache.processor.MutableEntry) CacheEntry(org.apache.ignite.cache.CacheEntry) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) GridAbsPredicateX(org.apache.ignite.internal.util.lang.GridAbsPredicateX) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 23 with Cache

use of javax.cache.Cache in project ignite by apache.

the class GridCacheMultithreadedFailoverAbstractTest method compareCaches.

/**
     * Compare caches.
     *
     * @param expVals Expected values.
     * @return {@code True} if check passed successfully.
     * @throws Exception If failed.
     */
@SuppressWarnings({ "TooBroadScope", "ConstantIfStatement" })
private boolean compareCaches(Map<Integer, Integer> expVals) throws Exception {
    List<IgniteCache<Integer, Integer>> caches = new ArrayList<>(dataNodes());
    List<GridDhtCacheAdapter<Integer, Integer>> dhtCaches = null;
    for (int i = 0; i < dataNodes(); i++) {
        IgniteCache<Integer, Integer> cache = G.ignite(nodeName(i)).cache(CACHE_NAME);
        assert cache != null;
        caches.add(cache);
        GridCacheAdapter<Integer, Integer> cache0 = (GridCacheAdapter<Integer, Integer>) ((IgniteKernal) cache.unwrap(Ignite.class)).<Integer, Integer>getCache(CACHE_NAME);
        if (cache0.isNear()) {
            if (dhtCaches == null)
                dhtCaches = new ArrayList<>(dataNodes());
            dhtCaches.add(((GridNearCacheAdapter<Integer, Integer>) cache0).dht());
        }
    }
    // Compare key sets on each cache.
    Collection<Integer> cacheKeys = new HashSet<>();
    Collection<Integer> dhtCacheKeys = new HashSet<>();
    for (int i = 0; i < dataNodes(); i++) {
        for (Cache.Entry<Integer, Integer> entry : caches.get(i)) cacheKeys.add(entry.getKey());
        if (dhtCaches != null)
            dhtCacheKeys.addAll(dhtCaches.get(i).keySet());
    }
    boolean failed = false;
    if (!F.eq(expVals.keySet(), cacheKeys)) {
        Collection<Integer> expOnly = new HashSet<>();
        Collection<Integer> cacheOnly = new HashSet<>();
        expOnly.addAll(expVals.keySet());
        expOnly.removeAll(cacheKeys);
        cacheOnly.addAll(cacheKeys);
        cacheOnly.removeAll(expVals.keySet());
        if (!expOnly.isEmpty())
            log.error("Cache does not contain expected keys: " + expOnly);
        if (!cacheOnly.isEmpty())
            log.error("Cache does contain unexpected keys: " + cacheOnly);
        failed = true;
    }
    if (dhtCaches != null && !F.eq(expVals.keySet(), dhtCacheKeys)) {
        Collection<Integer> expOnly = new HashSet<>();
        Collection<Integer> cacheOnly = new HashSet<>();
        expOnly.addAll(expVals.keySet());
        expOnly.removeAll(dhtCacheKeys);
        cacheOnly.addAll(dhtCacheKeys);
        cacheOnly.removeAll(expVals.keySet());
        if (!expOnly.isEmpty())
            log.error("DHT cache does not contain expected keys: " + expOnly);
        if (!cacheOnly.isEmpty())
            log.error("DHT cache does contain unexpected keys: " + cacheOnly);
        failed = true;
    }
    // Compare values.
    Collection<Integer> failedKeys = new HashSet<>();
    for (Map.Entry<Integer, Integer> entry : expVals.entrySet()) {
        for (int i = 0; i < dataNodes(); i++) {
            if (!F.eq(caches.get(i).get(entry.getKey()), entry.getValue()))
                failedKeys.add(entry.getKey());
        }
    }
    if (!failedKeys.isEmpty()) {
        log.error("Cache content is incorrect for " + failedKeys.size() + " keys:");
        for (Integer key : failedKeys) {
            for (int i = 0; i < dataNodes(); i++) {
                IgniteCache<Integer, Integer> cache = caches.get(i);
                UUID nodeId = G.ignite(nodeName(i)).cluster().localNode().id();
                if (!F.eq(cache.get(key), expVals.get(key)))
                    log.error("key=" + key + ", expVal=" + expVals.get(key) + ", nodeId=" + nodeId);
            }
        }
        failed = true;
    }
    return !failed;
}
Also used : GridDhtCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter) ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) UUID(java.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 24 with Cache

use of javax.cache.Cache in project ignite by apache.

the class CacheBinaryKeyConcurrentQueryTest method startUpdate.

/**
     * @param cacheName Cache name.
     * @return Future.
     */
private IgniteInternalFuture<?> startUpdate(final String cacheName) {
    final long stopTime = System.currentTimeMillis() + 30_000;
    final AtomicInteger idx = new AtomicInteger();
    return GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Void call() {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            IgniteCache cache = ignite(idx.getAndIncrement() % NODES).cache(cacheName).withKeepBinary();
            while (System.currentTimeMillis() < stopTime) {
                switch(rnd.nextInt(5)) {
                    case 0:
                        {
                            TestKey key = new TestKey(rnd.nextInt(KEYS));
                            CacheEntry e = cache.getEntry(key);
                            assertNotNull(e);
                            assertTrue(e.getKey() instanceof BinaryObject);
                            cache.put(e.getKey(), new TestValue(rnd.nextInt(KEYS)));
                            break;
                        }
                    case 1:
                        {
                            Iterator<Cache.Entry> it = cache.iterator();
                            for (int i = 0; i < 100 && it.hasNext(); i++) {
                                Cache.Entry e = it.next();
                                assertTrue(e.getKey() instanceof BinaryObject);
                                cache.put(e.getKey(), new TestValue(rnd.nextInt(KEYS)));
                            }
                            break;
                        }
                    case 2:
                        {
                            SqlFieldsQuery qry = new SqlFieldsQuery("select _key " + "from \"" + cache.getName() + "\".TestValue where id=?");
                            qry.setArgs(rnd.nextInt(KEYS));
                            List<List> res = cache.query(qry).getAll();
                            assertEquals(1, res.size());
                            BinaryObject key = (BinaryObject) res.get(0).get(0);
                            cache.put(key, new TestValue(rnd.nextInt(KEYS)));
                            break;
                        }
                    case 3:
                        {
                            SqlQuery qry = new SqlQuery("TestValue", "id=?");
                            qry.setArgs(rnd.nextInt(KEYS));
                            List<Cache.Entry> res = cache.query(qry).getAll();
                            assertEquals(1, res.size());
                            break;
                        }
                    case 4:
                        {
                            SqlQuery qry = new SqlQuery("TestValue", "order by id");
                            int cnt = 0;
                            for (Cache.Entry e : (Iterable<Cache.Entry>) cache.query(qry)) {
                                assertNotNull(cache.get(e.getKey()));
                                cnt++;
                            }
                            assertTrue(cnt > 0);
                            break;
                        }
                    default:
                        fail();
                }
            }
            return null;
        }
    }, NODES * 2, "test-thread");
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) IgniteCache(org.apache.ignite.IgniteCache) CacheEntry(org.apache.ignite.cache.CacheEntry) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CacheEntry(org.apache.ignite.cache.CacheEntry) BinaryObject(org.apache.ignite.binary.BinaryObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BinaryObject(org.apache.ignite.binary.BinaryObject) List(java.util.List) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 25 with Cache

use of javax.cache.Cache in project ignite by apache.

the class IgniteCacheAbstractQuerySelfTest method testMixedCustomTableName.

/**
     * JUnit.
     *
     * @throws Exception In case of error.
     */
public void testMixedCustomTableName() throws Exception {
    final IgniteCache<Integer, Object> cache = jcache(Integer.class, Object.class);
    cache.put(10, new Type1(1, "Type1 record #1"));
    cache.put(20, new Type1(2, "Type1 record #2"));
    cache.put(30, new Type2(1, "Type2 record #1"));
    cache.put(40, new Type2(2, "Type2 record #2"));
    cache.put(50, new Type2(3, "Type2 record #3"));
    QueryCursor<Cache.Entry<Integer, Type1>> qry1 = cache.query(new SqlQuery<Integer, Type1>(Type1.class, "FROM Type2"));
    List<Cache.Entry<Integer, Type1>> all = qry1.getAll();
    assertEquals(2, all.size());
    QueryCursor<Cache.Entry<Integer, Type2>> qry2 = cache.query(new SqlQuery<Integer, Type2>(Type2.class, "FROM Type1"));
    assertEquals(3, qry2.getAll().size());
    QueryCursor<List<?>> qry = cache.query(new SqlFieldsQuery("SELECT name FROM Type1"));
    assertEquals(3, qry.getAll().size());
    qry = cache.query(new SqlFieldsQuery("SELECT name FROM Type2"));
    assertEquals(2, qry.getAll().size());
    GridTestUtils.assertThrows(log, new GridPlainCallable<Void>() {

        @Override
        public Void call() throws Exception {
            QueryCursor<Cache.Entry<Integer, Type1>> qry1 = cache.query(new SqlQuery<Integer, Type1>(Type1.class, "FROM Type1"));
            qry1.getAll().size();
            return null;
        }
    }, CacheException.class, null);
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) IOException(java.io.IOException) BinaryObject(org.apache.ignite.binary.BinaryObject) List(java.util.List) ArrayList(java.util.ArrayList) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

Cache (javax.cache.Cache)107 IgniteCache (org.apache.ignite.IgniteCache)71 Ignite (org.apache.ignite.Ignite)32 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 SqlQuery (org.apache.ignite.cache.query.SqlQuery)17 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)16 CacheManager (javax.cache.CacheManager)15 ArrayList (java.util.ArrayList)14 List (java.util.List)14 ScanQuery (org.apache.ignite.cache.query.ScanQuery)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)11 Test (org.junit.Test)11 HazelcastInstance (com.hazelcast.core.HazelcastInstance)10 CacheException (javax.cache.CacheException)10 BinaryObject (org.apache.ignite.binary.BinaryObject)10 Collection (java.util.Collection)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 CachingProvider (javax.cache.spi.CachingProvider)8