Search in sources :

Example 76 with Cache

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

the class FunctionalQueryTest method checkSqlFieldsQuery.

/**
 * @param cache Cache.
 * @param minId Minimal ID.
 * @param pageSize Page size.
 * @param expSize The size of the expected results.
 * @param exp Expected results.
 * @param lazy Lazy mode flag.
 */
private void checkSqlFieldsQuery(ClientCache<Integer, Person> cache, int minId, int pageSize, int expSize, Map<Integer, Person> exp, boolean lazy) {
    SqlFieldsQuery qry = new SqlFieldsQuery("select id, name from Person where id >= ?").setArgs(minId).setPageSize(pageSize).setLazy(lazy);
    try (QueryCursor<List<?>> cur = cache.query(qry)) {
        List<List<?>> res = cur.getAll();
        assertEquals(expSize, res.size());
        Map<Integer, Person> act = res.stream().collect(Collectors.toMap(r -> Integer.parseInt(r.get(0).toString()), r -> new Person(Integer.parseInt(r.get(0).toString()), r.get(1).toString())));
        assertEquals(exp, act);
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) SerializedLambda(java.lang.invoke.SerializedLambda) Map(java.util.Map) SqlQuery(org.apache.ignite.cache.query.SqlQuery) Cache(javax.cache.Cache) Timeout(org.junit.rules.Timeout) Query(org.apache.ignite.cache.query.Query) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Ignition(org.apache.ignite.Ignition) Rule(org.junit.Rule) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Assert.assertEquals(org.junit.Assert.assertEquals) List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 77 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 78 with Cache

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

the class CacheIteratorScanQueryTest method testQueryGetAllClientSide.

/**
 * @throws Exception If failed.
 */
@Test
public void testQueryGetAllClientSide() throws Exception {
    Ignite server = startGrid(0);
    IgniteCache<Integer, Integer> cache = server.getOrCreateCache(DEFAULT_CACHE_NAME);
    Ignite client = startClientGrid(1);
    IgniteCache<Integer, Integer> cliCache = client.cache(DEFAULT_CACHE_NAME);
    for (int i = 0; i < 100_000; i++) cache.put(i, i);
    ScanQuery<Integer, Integer> qry = new ScanQuery<>();
    qry.setPageSize(100);
    try (QueryCursor<Cache.Entry<Integer, Integer>> cur = cliCache.query(qry)) {
        List<Cache.Entry<Integer, Integer>> res = cur.getAll();
        assertEquals(100_000, res.size());
        Collections.sort(res, (e1, e2) -> {
            return e1.getKey().compareTo(e2.getKey());
        });
        int exp = 0;
        for (Cache.Entry<Integer, Integer> e : res) {
            assertEquals(exp, e.getKey().intValue());
            assertEquals(exp, e.getValue().intValue());
            exp++;
        }
    }
}
Also used : ScanQuery(org.apache.ignite.cache.query.ScanQuery) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 79 with Cache

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

the class IgniteBinaryQueryTest method testBinaryQueries.

/**
 * Test queries in Ignite binary.
 */
@Test
public void testBinaryQueries() throws Exception {
    try (Ignite ignored = Ignition.start(Config.getServerConfiguration());
        IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(Config.SERVER))) {
        final String TYPE_NAME = "Person";
        QueryEntity qryPerson = new QueryEntity().setKeyType(Integer.class.getName()).setValueType(TYPE_NAME).setFields(Stream.of(new SimpleEntry<>("id", Integer.class.getName()), new SimpleEntry<>("name", String.class.getName())).collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue, (a, b) -> a, LinkedHashMap::new))).setIndexes(Collections.singletonList(new QueryIndex("id")));
        ClientCacheConfiguration cacheCfg = new ClientCacheConfiguration().setName("testBinaryQueries").setQueryEntities(qryPerson);
        ClientCache<Integer, BinaryObject> cache = client.getOrCreateCache(cacheCfg).withKeepBinary();
        final IgniteBinary binary = client.binary();
        Map<Integer, BinaryObject> data = IntStream.range(0, 100).boxed().collect(Collectors.toMap(i -> i, i -> binary.builder(TYPE_NAME).setField("id", i, int.class).setField("name", String.format("Person %s", i), String.class).build()));
        cache.putAll(data);
        Cache.Entry<Integer, BinaryObject> p1 = cache.query(new SqlQuery<Integer, BinaryObject>(TYPE_NAME, "id = 1")).getAll().iterator().next();
        assertEquals(Integer.valueOf(1), p1.getKey());
        assertBinaryObjectsEqual(data.get(1), p1.getValue());
    }
}
Also used : IntStream(java.util.stream.IntStream) BinaryObject(org.apache.ignite.binary.BinaryObject) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) Collectors(java.util.stream.Collectors) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) LinkedHashMap(java.util.LinkedHashMap) Stream(java.util.stream.Stream) Ignition(org.apache.ignite.Ignition) BinaryType(org.apache.ignite.binary.BinaryType) Rule(org.junit.Rule) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) Map(java.util.Map) SqlQuery(org.apache.ignite.cache.query.SqlQuery) Cache(javax.cache.Cache) Timeout(org.junit.rules.Timeout) IgniteBinary(org.apache.ignite.IgniteBinary) QueryEntity(org.apache.ignite.cache.QueryEntity) SimpleEntry(java.util.AbstractMap.SimpleEntry) Collections(java.util.Collections) QueryIndex(org.apache.ignite.cache.QueryIndex) Assert.assertEquals(org.junit.Assert.assertEquals) SqlQuery(org.apache.ignite.cache.query.SqlQuery) SimpleEntry(java.util.AbstractMap.SimpleEntry) QueryEntity(org.apache.ignite.cache.QueryEntity) IgniteBinary(org.apache.ignite.IgniteBinary) BinaryObject(org.apache.ignite.binary.BinaryObject) QueryIndex(org.apache.ignite.cache.QueryIndex) Ignite(org.apache.ignite.Ignite) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) Cache(javax.cache.Cache) Test(org.junit.Test)

Example 80 with Cache

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

the class IndexQueryKeepBinaryTest method check.

/**
 * @param left First cache key, inclusive.
 * @param right Last cache key, exclusive.
 */
private void check(QueryCursor cursor, int left, int right) {
    List<Cache.Entry<Long, BinaryObject>> all = cursor.getAll();
    assertEquals(right - left, all.size());
    for (int i = 0; i < all.size(); i++) {
        Cache.Entry<Long, ?> entry = all.get(i);
        assertEquals(left + i, entry.getKey().intValue());
        BinaryObject o = all.get(i).getValue();
        assertEquals(new Person(entry.getKey().intValue()), o.deserialize());
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

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