Search in sources :

Example 46 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class IndexingSpiQuerySelfTest method testNonBinaryIndexingSpi.

/**
     * @throws Exception If failed.
     */
public void testNonBinaryIndexingSpi() throws Exception {
    System.setProperty(IgniteSystemProperties.IGNITE_UNWRAP_BINARY_FOR_INDEXING_SPI, "true");
    IgniteConfiguration cfg = configuration();
    cfg.setIndexingSpi(new MyIndexingSpi());
    Ignite ignite = Ignition.start(cfg);
    CacheConfiguration<PersonKey, Person> ccfg = cacheConfiguration(CACHE_NAME);
    IgniteCache<PersonKey, Person> cache = ignite.createCache(ccfg);
    for (int i = 0; i < 10; i++) {
        PersonKey key = new PersonKey(i);
        cache.put(key, new Person("John Doe " + i));
    }
    QueryCursor<Cache.Entry<PersonKey, Person>> cursor = cache.query(new SpiQuery<PersonKey, Person>().setArgs(new PersonKey(2), new PersonKey(5)));
    for (Cache.Entry<PersonKey, Person> entry : cursor) System.out.println(entry);
    cache.remove(new PersonKey(9));
}
Also used : SpiQuery(org.apache.ignite.cache.query.SpiQuery) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 47 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class GridCacheQueryTransformerSelfTest method testLocalFiltered.

/**
     * @throws Exception If failed.
     */
public void testLocalFiltered() throws Exception {
    IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
    try {
        for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
        Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public List<Integer> call() throws Exception {
                IgniteBiPredicate<Integer, Value> filter = new IgniteBiPredicate<Integer, Value>() {

                    @Override
                    public boolean apply(Integer k, Value v) {
                        return v.idx % 1000 == 0;
                    }
                };
                IgniteClosure<Cache.Entry<Integer, Value>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, Value>, Integer>() {

                    @Override
                    public Integer apply(Cache.Entry<Integer, Value> e) {
                        return e.getValue().idx;
                    }
                };
                return ignite.cache("test-cache").query(new ScanQuery<>(filter).setLocal(true), transformer).getAll();
            }
        });
        List<Integer> res = new ArrayList<>(F.flatCollections(lists));
        assertEquals(5, res.size());
        Collections.sort(res);
        for (int i = 0; i < 5; i++) assertEquals(i * 1000, res.get(i).intValue());
    } finally {
        cache.destroy();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 48 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class IgniteCachePartitionedQueryMultiThreadedSelfTest method testLuceneAndSqlMultithreaded.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
@SuppressWarnings({ "TooBroadScope" })
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)

Example 49 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class IgniteCacheQueryMultiThreadedSelfTest method testMultiThreadedSameQuery.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
@SuppressWarnings({ "TooBroadScope" })
public void testMultiThreadedSameQuery() 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("Query results [entries=" + entries + ", aff=" + affinityNodes(entries, g) + ", 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);
    info("Finishing test...");
    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)

Example 50 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class IgniteCacheQueryMultiThreadedSelfTest method testMultiThreadedNewQueries.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
@SuppressWarnings({ "TooBroadScope" })
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)

Aggregations

IgniteCache (org.apache.ignite.IgniteCache)298 Ignite (org.apache.ignite.Ignite)161 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)104 Cache (javax.cache.Cache)72 Transaction (org.apache.ignite.transactions.Transaction)71 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)51 IgniteException (org.apache.ignite.IgniteException)50 ArrayList (java.util.ArrayList)47 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)40 CacheException (javax.cache.CacheException)37 Map (java.util.Map)34 List (java.util.List)33 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)33 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)31 HashMap (java.util.HashMap)27 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)26 CyclicBarrier (java.util.concurrent.CyclicBarrier)25 IgniteKernal (org.apache.ignite.internal.IgniteKernal)24 Random (java.util.Random)23 IgniteTransactions (org.apache.ignite.IgniteTransactions)22