Search in sources :

Example 21 with SqlQuery

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

the class IgniteBinaryObjectFieldsQuerySelfTest method checkQuery.

/**
     * @throws Exception If failed.
     */
private void checkQuery(CacheMode cacheMode, CacheAtomicityMode atomicity) throws Exception {
    IgniteCache<Object, Object> cache = grid(GRID_CNT - 1).getOrCreateCache(cache(cacheMode, atomicity));
    try {
        populate(cache);
        QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(new SqlQuery("Person", "order " + "by id asc"));
        List<Cache.Entry<Object, Object>> all = cur.getAll();
        assertEquals(100, all.size());
        for (int i = 0; i < 100; i++) {
            Object person = all.get(i).getValue();
            assertEquals(Integer.valueOf(i), U.field(person, "id"));
            assertEquals("person-" + i, U.field(person, "name"));
            assertEquals("person-last-" + i, U.field(person, "lastName"));
            assertEquals((double) (i * 25), U.field(person, "salary"));
        }
        int max = 49;
        // Check local scan query with keepBinary flag set.
        ScanQuery<BinaryObject, BinaryObject> scanQry = new ScanQuery<>(new PersonKeyFilter(max));
        QueryCursor<Cache.Entry<BinaryObject, BinaryObject>> curs = grid(GRID_CNT - 1).cache(DEFAULT_CACHE_NAME).withKeepBinary().query(scanQry);
        List<Cache.Entry<BinaryObject, BinaryObject>> records = curs.getAll();
        assertEquals(50, records.size());
        for (Cache.Entry<BinaryObject, BinaryObject> entry : records) {
            BinaryObject key = entry.getKey();
            assertTrue(key.<Integer>field("id") <= max);
            assertEquals(PERSON_KEY_CLS_NAME, key.deserialize().getClass().getName());
        }
    } finally {
        grid(GRID_CNT - 1).cache(DEFAULT_CACHE_NAME).removeAll();
        grid(GRID_CNT - 1).destroyCache(DEFAULT_CACHE_NAME);
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) 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 22 with SqlQuery

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

the class CacheQueryFilterExpiredTest method checkFilterExpired.

/**
     * @param ignite Node.
     * @param atomicityMode Cache atomicity mode.
     * @param eagerTtl Value for {@link CacheConfiguration#setEagerTtl(boolean)}.
     * @throws Exception If failed.
     */
private void checkFilterExpired(Ignite ignite, CacheAtomicityMode atomicityMode, boolean eagerTtl) throws Exception {
    CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setAtomicityMode(atomicityMode);
    ccfg.setEagerTtl(eagerTtl);
    ccfg.setIndexedTypes(Integer.class, Integer.class);
    final IgniteCache<Integer, Integer> cache = ignite.createCache(ccfg);
    try {
        IgniteCache<Integer, Integer> expCache = cache.withExpiryPolicy(new TouchedExpiryPolicy(new Duration(0, 2000)));
        for (int i = 0; i < 10; i++) {
            IgniteCache<Integer, Integer> cache0 = i % 2 == 0 ? cache : expCache;
            cache0.put(i, i);
        }
        assertEquals(10, cache.query(new SqlQuery<Integer, Integer>(Integer.class, "1=1")).getAll().size());
        assertEquals(10, cache.query(new SqlFieldsQuery("select _key, _val from Integer")).getAll().size());
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return cache.query(new SqlQuery<Integer, Integer>(Integer.class, "1=1")).getAll().size() == 5 && cache.query(new SqlFieldsQuery("select _key, _val from Integer")).getAll().size() == 5;
            }
        }, 5000);
        assertEquals(5, cache.query(new SqlQuery<Integer, Integer>(Integer.class, "1=1")).getAll().size());
        assertEquals(5, cache.query(new SqlFieldsQuery("select _key, _val from Integer")).getAll().size());
    } finally {
        ignite.destroyCache(ccfg.getName());
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 23 with SqlQuery

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

the class IgniteCrossCachesJoinsQueryTest method checkOrganizationPersonAccountJoin.

/**
     * @param cache Cache.
     * @throws Exception If failed.
     */
private void checkOrganizationPersonAccountJoin(IgniteCache cache) throws Exception {
    if (skipQuery(cache, PERSON_CACHE_NAME, ORG_CACHE_NAME, ACC_CACHE_NAME))
        return;
    qry = "checkOrganizationPersonAccountJoin";
    List<String> sqlFields = new ArrayList<>();
    sqlFields.add("select o.name, p.name, a._key " + "from " + "\"" + ORG_CACHE_NAME + "\".Organization o, " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.orgId = o.id and p.id = a.personId and o.id = ?");
    sqlFields.add("select o.name, p.name, a._key " + "from " + "\"" + ORG_CACHE_NAME + "\".Organization o, " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.orgDateId = o.dateId and p.strId = a.personStrId and o.id = ?");
    sqlFields.add("select o.name, p.name, a._key " + "from " + "\"" + ORG_CACHE_NAME + "\".Organization o, " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.orgStrId = o.strId and p.id = a.personId and o.id = ?");
    for (Organization org : data.orgs) {
        for (String sql : sqlFields) compareQueryRes0(cache, sql, distributedJoins(), new Object[] { org.id }, Ordering.RANDOM);
    }
    if (ACC_CACHE_NAME.equals(cache.getName())) {
        for (int orgId = 0; orgId < data.accountsPerOrg.size(); orgId++) {
            SqlQuery q = new SqlQuery(Account.class, "from " + "\"" + ORG_CACHE_NAME + "\".Organization , " + "\"" + PERSON_CACHE_NAME + "\".Person , " + "\"" + ACC_CACHE_NAME + "\".Account  " + "where Person.orgId = Organization.id and Person.id = Account.personId and Organization.id = ?");
            q.setDistributedJoins(distributedJoins());
            q.setArgs(orgId);
            List<List<Object>> res = cache.query(q).getAll();
            assertEquals((int) data.accountsPerOrg.get(orgId), res.size());
        }
    }
    String sql = "select count(*) " + "from " + "\"" + ORG_CACHE_NAME + "\".Organization o, " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.orgId = o.id and p.id = a.personId";
    compareQueryRes0(cache, sql, distributedJoins(), new Object[0], Ordering.RANDOM);
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 24 with SqlQuery

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

the class IgniteCrossCachesJoinsQueryTest method checkPersonAccountsJoin.

/**
     * @param cache Cache.
     * @param cnts Accounts per person counts.
     */
private void checkPersonAccountsJoin(IgniteCache cache, Map<Integer, Integer> cnts) {
    if (skipQuery(cache, PERSON_CACHE_NAME, ACC_CACHE_NAME))
        return;
    qry = "checkPersonAccountsJoin";
    List<Query> qrys = new ArrayList<>();
    qrys.add(new SqlFieldsQuery("select p.name from " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.id = a.personId and p.id=?"));
    qrys.add(new SqlFieldsQuery("select p.name from " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.dateId = a.personDateId and p.id=?"));
    qrys.add(new SqlFieldsQuery("select p.name from " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.strId = a.personStrId and p.id=?"));
    qrys.add(new SqlFieldsQuery("select p.name from " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.id = a.personId and p.id=?"));
    qrys.add(new SqlFieldsQuery("select p.name from " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.dateId = a.personDateId and p.id=?"));
    qrys.add(new SqlFieldsQuery("select p.name from " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.strId = a.personStrId and p.id=?"));
    if (PERSON_CACHE_NAME.equals(cache.getName())) {
        qrys.add(new SqlQuery(Person.class, "from \"" + PERSON_CACHE_NAME + "\".Person , \"" + ACC_CACHE_NAME + "\".Account  " + "where Person.id = Account.personId and Person.id=?"));
        qrys.add(new SqlQuery(Person.class, "from \"" + PERSON_CACHE_NAME + "\".Person , \"" + ACC_CACHE_NAME + "\".Account  " + "where Person.id = Account.personId and Person.id=?"));
    }
    List<Integer> keys = new ArrayList<>(cnts.keySet());
    for (int i = 0; i < 10; i++) {
        Integer key = keys.get(rnd.nextInt(keys.size()));
        List<List<Object>> res;
        for (Query q : qrys) {
            if (q instanceof SqlFieldsQuery) {
                ((SqlFieldsQuery) q).setDistributedJoins(distributedJoins());
                ((SqlFieldsQuery) q).setArgs(key);
            } else {
                ((SqlQuery) q).setDistributedJoins(distributedJoins());
                ((SqlQuery) q).setArgs(key);
            }
            res = cache.query(q).getAll();
            assertEquals((int) cnts.get(key), res.size());
        }
    }
    qrys.clear();
    qrys.add(new SqlFieldsQuery("select count(*) " + "from \"" + PERSON_CACHE_NAME + "\".Person p, \"" + ACC_CACHE_NAME + "\".Account" + " a " + "where p.id = a.personId"));
    qrys.add(new SqlFieldsQuery("select count(*) " + "from \"" + PERSON_CACHE_NAME + "\".Person p, \"" + ACC_CACHE_NAME + "\".Account" + " a " + "where p.Dateid = a.personDateId"));
    qrys.add(new SqlFieldsQuery("select count(*) " + "from \"" + PERSON_CACHE_NAME + "\".Person p, \"" + ACC_CACHE_NAME + "\".Account" + " a " + "where p.strId = a.personStrId"));
    qrys.add(new SqlFieldsQuery("select count(*) " + "from \"" + PERSON_CACHE_NAME + "\".Person p, \"" + ACC_CACHE_NAME + "\".Account" + " a " + "where p.id = a.personId"));
    long total = 0;
    for (Integer cnt : data.accountsPerPerson.values()) total += cnt;
    for (Query q : qrys) {
        ((SqlFieldsQuery) q).setDistributedJoins(distributedJoins());
        List<List<Object>> res = cache.query(q).getAll();
        assertEquals(1, res.size());
        assertEquals(total, res.get(0).get(0));
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SqlQuery(org.apache.ignite.cache.query.SqlQuery) Query(org.apache.ignite.cache.query.Query) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 25 with SqlQuery

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

the class BinarySerializationQuerySelfTest method check.

/**
     * Internal check routine.
     *
     * @param cls Entity class.
     * @throws Exception If failed.
     */
@SuppressWarnings("unchecked")
private void check(Class cls) throws Exception {
    cache.put(1, createInstance(cls, 10));
    cache.put(2, createInstance(cls, 20));
    cache.put(3, createInstance(cls, 30));
    Iterator iter = cache.query(new SqlQuery(cls, "val=20")).iterator();
    assert iter.hasNext();
    Cache.Entry res = (Cache.Entry) iter.next();
    assertEquals(2, res.getKey());
    assertEquals(Integer.valueOf(20), U.field(res.getValue(), "val"));
    assert !iter.hasNext();
    iter = cache.query(new SqlFieldsQuery("SELECT p.val FROM " + cls.getSimpleName() + " p WHERE p.val=20")).iterator();
    assert iter.hasNext();
    List<Object> fieldsRes = (List<Object>) iter.next();
    assertEquals(20, fieldsRes.get(0));
    assert !iter.hasNext();
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

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