Search in sources :

Example 61 with Cache

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

the class CacheKeepBinaryIterationTest method doTestScanQuery.

/**
 * @param ccfg Cache configuration.
 */
private void doTestScanQuery(final CacheConfiguration<Object, Object> ccfg, boolean keepBinary, boolean primitives) throws Exception {
    IgniteCache<Object, Object> cache = grid(0).createCache(ccfg);
    assertEquals(0, cache.size());
    try {
        for (int i = 0; i < KEYS; i++) if (primitives)
            cache.put(i, i);
        else
            cache.put(new QueryTestKey(i), new QueryTestValue(i));
        for (int i = 0; i < getServerNodeCount(); i++) {
            IgniteCache<Object, Object> cache0 = grid(i).cache(ccfg.getName());
            if (keepBinary)
                cache0 = cache0.withKeepBinary();
            ScanQuery<Object, Object> qry = new ScanQuery<>();
            qry.setLocal(true);
            int size = 0;
            try (QueryCursor<Cache.Entry<Object, Object>> cur = cache0.query(qry)) {
                for (Cache.Entry<Object, Object> e : cur) {
                    Object key = e.getKey();
                    Object val = e.getValue();
                    if (!primitives) {
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, keepBinary == key instanceof BinaryObject);
                        assertTrue("Got unexpected object: " + val.getClass() + ", keepBinary: " + keepBinary, keepBinary == val instanceof BinaryObject);
                    } else {
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, key instanceof Integer);
                        assertTrue("Got unexpected object: " + val.getClass() + ", keepBinary: " + keepBinary, val instanceof Integer);
                    }
                    ++size;
                }
            }
            assertTrue(size > 0);
        }
    } finally {
        if (ccfg.getEvictionPolicy() != null) {
            // TODO: IGNITE-3462. Fixes evictionPolicy issues at cache destroy.
            stopAllGrids();
            startGridsMultiThreaded(getServerNodeCount());
        } else
            grid(0).destroyCache(ccfg.getName());
    }
}
Also used : ScanQuery(org.apache.ignite.cache.query.ScanQuery) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 62 with Cache

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

the class IgniteCacheAbstractQuerySelfTest method testObjectQueryWithSwap.

/**
 * JUnit.
 */
@Test
public void testObjectQueryWithSwap() {
    CacheConfiguration<Integer, ObjectValue> config = new CacheConfiguration<Integer, ObjectValue>(cacheConfiguration());
    config.setOnheapCacheEnabled(true);
    IgniteCache<Integer, ObjectValue> cache = jcache(ignite(), config, Integer.class, ObjectValue.class);
    boolean partitioned = cache.getConfiguration(CacheConfiguration.class).getCacheMode() == PARTITIONED;
    int cnt = 10;
    for (int i = 0; i < cnt; i++) cache.put(i, new ObjectValue("test" + i, i));
    for (Ignite g : G.allGrids()) {
        IgniteCache<Integer, ObjectValue> c = g.cache(cache.getName());
        for (int i = 0; i < cnt; i++) {
            assertNotNull(c.localPeek(i, CachePeekMode.ONHEAP));
            // Swap.
            c.localEvict(Collections.singleton(i));
            if (!partitioned || g.affinity(cache.getName()).mapKeyToNode(i).isLocal()) {
                ObjectValue peekVal = c.localPeek(i, CachePeekMode.ONHEAP);
                assertNull("Non-null value for peek [key=" + i + ", val=" + peekVal + ']', peekVal);
            }
        }
    }
    QueryCursor<Cache.Entry<Integer, ObjectValue>> qry = cache.query(new SqlQuery<Integer, ObjectValue>(ObjectValue.class, "intVal >= ? order by intVal").setArgs(0));
    Iterator<Cache.Entry<Integer, ObjectValue>> iter = qry.iterator();
    assert iter != null;
    Collection<Integer> set = new HashSet<>(cnt);
    Cache.Entry<Integer, ObjectValue> next;
    while (iter.hasNext()) {
        next = iter.next();
        ObjectValue v = next.getValue();
        assert !set.contains(v.intValue());
        set.add(v.intValue());
    }
    assert !iter.hasNext();
    assertEquals(cnt, set.size());
    for (int i = 0; i < cnt; i++) assert set.contains(i);
    qry = cache.query(new SqlQuery<Integer, ObjectValue>(ObjectValue.class, "MOD(intVal, 2) = ? order by intVal").setArgs(0));
    iter = qry.iterator();
    assert iter != null;
    set.clear();
    while (iter.hasNext()) {
        next = iter.next();
        ObjectValue v = next.getValue();
        assert !set.contains(v.intValue());
        set.add(v.intValue());
    }
    assert !iter.hasNext();
    assertEquals(cnt / 2, set.size());
    for (int i = 0; i < cnt; i++) if (i % 2 == 0)
        assert set.contains(i);
    else
        assert !set.contains(i);
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) Ignite(org.apache.ignite.Ignite) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) HashSet(java.util.HashSet) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 63 with Cache

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

the class IgniteCacheAbstractQuerySelfTest method testMixedCustomTableName.

/**
 * JUnit.
 *
 * @throws Exception In case of error.
 */
@Test
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) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 64 with Cache

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

the class IgniteCachePartitionedQueryMultiThreadedSelfTest method testLuceneAndSqlMultithreaded.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@SuppressWarnings({ "TooBroadScope" })
@Test
public void testLuceneAndSqlMultithreaded() throws Exception {
    // ---------- Test parameters ---------- //
    int luceneThreads = 10;
    int sqlThreads = 10;
    long duration = 10 * 1000;
    final int logMod = 100;
    final PersonObj p1 = new PersonObj("Jon", 1500, "Master");
    final PersonObj p2 = new PersonObj("Jane", 2000, "Master");
    final PersonObj p3 = new PersonObj("Mike", 1800, "Bachelor");
    final PersonObj p4 = new PersonObj("Bob", 1900, "Bachelor");
    final IgniteCache<UUID, PersonObj> cache0 = grid(0).cache(DEFAULT_CACHE_NAME);
    cache0.put(p1.id(), p1);
    cache0.put(p2.id(), p2);
    cache0.put(p3.id(), p3);
    cache0.put(p4.id(), p4);
    assertEquals(4, cache0.localSize(CachePeekMode.ALL));
    assert grid(0).cluster().nodes().size() == GRID_CNT;
    final AtomicBoolean done = new AtomicBoolean();
    final AtomicLong luceneCnt = new AtomicLong();
    // Start lucene query threads.
    IgniteInternalFuture<?> futLucene = GridTestUtils.runMultiThreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            while (!done.get()) {
                QueryCursor<Cache.Entry<UUID, PersonObj>> master = cache0.query(new TextQuery(PersonObj.class, "Master"));
                Collection<Cache.Entry<UUID, PersonObj>> entries = master.getAll();
                checkResult(entries, p1, p2);
                long cnt = luceneCnt.incrementAndGet();
                if (cnt % logMod == 0)
                    info("Executed LUCENE queries: " + cnt);
            }
        }
    }, luceneThreads, "LUCENE-THREAD");
    final AtomicLong sqlCnt = new AtomicLong();
    // Start sql query threads.
    IgniteInternalFuture<?> futSql = GridTestUtils.runMultiThreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            while (!done.get()) {
                QueryCursor<Cache.Entry<UUID, PersonObj>> bachelors = cache0.query(new SqlQuery(PersonObj.class, "degree = 'Bachelor'"));
                Collection<Cache.Entry<UUID, PersonObj>> entries = bachelors.getAll();
                checkResult(entries, p3, p4);
                long cnt = sqlCnt.incrementAndGet();
                if (cnt % logMod == 0)
                    info("Executed SQL queries: " + cnt);
            }
        }
    }, sqlThreads, "SQL-THREAD");
    Thread.sleep(duration);
    done.set(true);
    futLucene.get();
    futSql.get();
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) TextQuery(org.apache.ignite.cache.query.TextQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Collection(java.util.Collection) CAX(org.apache.ignite.internal.util.typedef.CAX) UUID(java.util.UUID) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 65 with Cache

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

the class IgniteCacheQueryMultiThreadedSelfTest method testMultiThreadedNewQueries.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@SuppressWarnings({ "TooBroadScope" })
@Test
public void testMultiThreadedNewQueries() throws Exception {
    int threadCnt = 50;
    final int keyCnt = 10;
    final int logMod = 5000;
    final Ignite g = grid(0);
    // Put test values into cache.
    final IgniteCache<Integer, Integer> c = cache(Integer.class, Integer.class);
    for (int i = 0; i < keyCnt; i++) {
        c.put(i, i);
        c.localEvict(Arrays.asList(i));
    }
    final AtomicInteger cnt = new AtomicInteger();
    final AtomicBoolean done = new AtomicBoolean();
    IgniteInternalFuture<?> fut = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            int iter = 0;
            while (!done.get() && !Thread.currentThread().isInterrupted()) {
                iter++;
                Collection<Cache.Entry<Integer, Integer>> entries = c.query(new SqlQuery(Integer.class, "_val >= 0")).getAll();
                assert entries != null;
                assertEquals("Entries count is not as expected on iteration: " + iter, keyCnt, entries.size());
                if (cnt.incrementAndGet() % logMod == 0) {
                    GridCacheQueryManager<Object, Object> qryMgr = ((IgniteKernal) g).internalCache(c.getName()).context().queries();
                    assert qryMgr != null;
                    qryMgr.printMemoryStats();
                }
            }
        }
    }, threadCnt);
    Thread.sleep(DURATION);
    done.set(true);
    fut.get();
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheQueryManager(org.apache.ignite.internal.processors.cache.query.GridCacheQueryManager) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) CAX(org.apache.ignite.internal.util.typedef.CAX) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) AbstractIndexingCommonTest(org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest) Test(org.junit.Test)

Aggregations

Cache (javax.cache.Cache)271 IgniteCache (org.apache.ignite.IgniteCache)157 Test (org.junit.Test)130 Ignite (org.apache.ignite.Ignite)101 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)68 List (java.util.List)62 Map (java.util.Map)56 ScanQuery (org.apache.ignite.cache.query.ScanQuery)54 ArrayList (java.util.ArrayList)51 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)45 QueryCursor (org.apache.ignite.cache.query.QueryCursor)43 HashMap (java.util.HashMap)41 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)41 Collection (java.util.Collection)38 HashSet (java.util.HashSet)38 CacheManager (javax.cache.CacheManager)38 CacheException (javax.cache.CacheException)35 IgniteEx (org.apache.ignite.internal.IgniteEx)35 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)32 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)32