Search in sources :

Example 46 with SqlQuery

use of org.apache.ignite.cache.query.SqlQuery in project ignite by apache.

the class IgniteCacheAbstractQuerySelfTest method testSimpleCustomTableName.

/**
     * JUnit.
     *
     * @throws Exception In case of error.
     */
public void testSimpleCustomTableName() throws Exception {
    final IgniteCache<Integer, Object> cache = ignite().cache(DEFAULT_CACHE_NAME);
    cache.put(10, new Type1(1, "Type1 record #1"));
    cache.put(20, new Type1(2, "Type1 record #2"));
    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<List<?>> 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>> qry = cache.query(new SqlQuery<Integer, Type1>(Type1.class, "FROM Type1"));
            qry.getAll();
            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)

Example 47 with SqlQuery

use of org.apache.ignite.cache.query.SqlQuery in project ignite by apache.

the class IgniteCacheAbstractQuerySelfTest method _testObjectQueryWithSwap.

/**
     * JUnit.
     *
     * @throws Exception In case of error.
     */
public void _testObjectQueryWithSwap() throws Exception {
    fail("http://atlassian.gridgain.com/jira/browse/GG-11216");
    IgniteCache<Integer, ObjectValue> cache = jcache(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++) {
            if (i % 2 == 0) {
                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) HashSet(java.util.HashSet) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 48 with SqlQuery

use of org.apache.ignite.cache.query.SqlQuery in project ignite by apache.

the class IgniteCacheAbstractQuerySelfTest method testExpiration.

/**
     * Expired entries are not included to result.
     *
     * @throws Exception If failed.
     */
public void testExpiration() throws Exception {
    IgniteCache<Integer, Integer> cache = jcache(Integer.class, Integer.class);
    cache.withExpiryPolicy(new TouchedExpiryPolicy(new Duration(MILLISECONDS, 1000))).put(7, 1);
    List<Cache.Entry<Integer, Integer>> qry = cache.query(new SqlQuery<Integer, Integer>(Integer.class, "1=1")).getAll();
    Cache.Entry<Integer, Integer> res = F.first(qry);
    assertEquals(1, res.getValue().intValue());
    // Less than minimal amount of time that must pass before a cache entry is considered expired.
    U.sleep(800);
    qry = cache.query(new SqlQuery<Integer, Integer>(Integer.class, "1=1")).getAll();
    res = F.first(qry);
    assertEquals(1, res.getValue().intValue());
    // No expiry guarantee here. Test should be refactored in case of fails.
    U.sleep(1200);
    qry = cache.query(new SqlQuery<Integer, Integer>(Integer.class, "1=1")).getAll();
    res = F.first(qry);
    assertNull(res);
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 49 with SqlQuery

use of org.apache.ignite.cache.query.SqlQuery in project ignite by apache.

the class IgniteBinaryObjectQueryArgumentsTest method testValQuery.

/**
     * Test simple query by value.
     *
     * @param cacheName Cache name.
     * @param val1 Value 1.
     * @param val2 Value 2.
     * @param <T> Value type.
     */
private <T> void testValQuery(final String cacheName, final T val1, final T val2) {
    final IgniteCache<Person, T> cache = ignite(0).cache(cacheName);
    final Class<?> valType = val1.getClass();
    final Person p1 = new Person("p1");
    final Person p2 = new Person("p2");
    cache.put(p1, val1);
    cache.put(p2, val2);
    final SqlQuery<Person, T> qry = new SqlQuery<>(valType, "where _val=?");
    final SqlFieldsQuery fieldsQry = new SqlFieldsQuery("select _key, _val, * from " + valType.getSimpleName() + " where _val=?");
    qry.setLocal(isLocal());
    fieldsQry.setLocal(isLocal());
    qry.setArgs(val1);
    fieldsQry.setArgs(val1);
    final List<Cache.Entry<Person, T>> res = cache.query(qry).getAll();
    final List<List<?>> fieldsRes = cache.query(fieldsQry).getAll();
    assertEquals(1, res.size());
    assertEquals(1, fieldsRes.size());
    assertEquals(p1, res.get(0).getKey());
    assertEquals(val1, res.get(0).getValue());
    assertTrue(fieldsRes.get(0).size() >= 2);
    assertEquals(p1, fieldsRes.get(0).get(0));
    assertEquals(val1, fieldsRes.get(0).get(1));
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) ArrayList(java.util.ArrayList) List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 50 with SqlQuery

use of org.apache.ignite.cache.query.SqlQuery in project ignite by apache.

the class IgniteClientReconnectQueriesTest method testQueryReconnect.

/**
     * @throws Exception If failed.
     */
public void testQueryReconnect() throws Exception {
    Ignite cln = grid(serverCount());
    assertTrue(cln.cluster().localNode().isClient());
    final Ignite srv = clientRouter(cln);
    final IgniteCache<Integer, Person> clnCache = cln.getOrCreateCache(QUERY_CACHE);
    final IgniteCache<Integer, Person> srvCache = srv.getOrCreateCache(QUERY_CACHE);
    clnCache.put(1, new Person(1, "name1", "surname1"));
    clnCache.put(2, new Person(2, "name2", "surname2"));
    clnCache.put(3, new Person(3, "name3", "surname3"));
    final SqlQuery<Integer, Person> qry = new SqlQuery<>(Person.class, "_key <> 0");
    qry.setPageSize(1);
    QueryCursor<Cache.Entry<Integer, Person>> cur = clnCache.query(qry);
    reconnectClientNode(cln, srv, new Runnable() {

        @Override
        public void run() {
            srvCache.put(4, new Person(4, "name4", "surname4"));
            try {
                clnCache.query(qry);
                fail();
            } catch (CacheException e) {
                check(e);
            }
        }
    });
    List<Cache.Entry<Integer, Person>> res = cur.getAll();
    assertNotNull(res);
    assertEquals(4, res.size());
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) CacheException(javax.cache.CacheException) Ignite(org.apache.ignite.Ignite)

Aggregations

SqlQuery (org.apache.ignite.cache.query.SqlQuery)57 Cache (javax.cache.Cache)20 IgniteCache (org.apache.ignite.IgniteCache)20 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)18 Ignite (org.apache.ignite.Ignite)17 List (java.util.List)13 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 CAX (org.apache.ignite.internal.util.typedef.CAX)9 ArrayList (java.util.ArrayList)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 BinaryObject (org.apache.ignite.binary.BinaryObject)7 CacheException (javax.cache.CacheException)6 Collection (java.util.Collection)4 Random (java.util.Random)4 QueryCursor (org.apache.ignite.cache.query.QueryCursor)4 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 UUID (java.util.UUID)3