Search in sources :

Example 61 with SqlQuery

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

the class CacheQueryBuildValueTest method testBuilderAndQuery.

/**
 * @throws Exception If failed.
 */
public void testBuilderAndQuery() throws Exception {
    Ignite node = ignite(0);
    final IgniteCache<Object, Object> cache = node.cache(DEFAULT_CACHE_NAME);
    IgniteBinary binary = node.binary();
    BinaryObjectBuilder builder = binary.builder(TestBuilderValue.class.getName());
    cache.put(0, builder.build());
    builder.setField("iVal", 1);
    cache.put(1, builder.build());
    List<Cache.Entry<Object, Object>> entries = cache.query(new SqlQuery<>(TestBuilderValue.class, "true")).getAll();
    assertEquals(2, entries.size());
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) Ignite(org.apache.ignite.Ignite) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) IgniteBinary(org.apache.ignite.IgniteBinary)

Example 62 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 63 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 64 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 65 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)

Aggregations

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