Search in sources :

Example 66 with SqlQuery

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

the class IgniteCacheAbstractQuerySelfTest method checkSqlQueryEvents.

/**
 * @throws Exception If failed.
 */
private void checkSqlQueryEvents() throws Exception {
    final IgniteCache<Integer, Integer> cache = jcache(Integer.class, Integer.class);
    final boolean evtsDisabled = cache.getConfiguration(CacheConfiguration.class).isEventsDisabled();
    final CountDownLatch execLatch = new CountDownLatch(evtsDisabled ? 0 : cacheMode() == REPLICATED ? 1 : gridCount());
    IgnitePredicate[] lsnrs = new IgnitePredicate[gridCount()];
    for (int i = 0; i < gridCount(); i++) {
        IgnitePredicate<Event> pred = new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                assert evt instanceof CacheQueryExecutedEvent;
                if (evtsDisabled)
                    fail("Cache events are disabled");
                CacheQueryExecutedEvent qe = (CacheQueryExecutedEvent) evt;
                assertEquals(cache.getName(), qe.cacheName());
                assertNotNull(qe.clause());
                assertNull(qe.scanQueryFilter());
                assertNull(qe.continuousQueryFilter());
                assertArrayEquals(new Integer[] { 10 }, qe.arguments());
                execLatch.countDown();
                return true;
            }
        };
        grid(i).events().localListen(pred, EVT_CACHE_QUERY_EXECUTED);
        lsnrs[i] = pred;
    }
    try {
        for (int i = 0; i < 20; i++) cache.put(i, i);
        QueryCursor<Cache.Entry<Integer, Integer>> q = cache.query(new SqlQuery<Integer, Integer>(Integer.class, "_key >= ?").setArgs(10));
        q.getAll();
        assert execLatch.await(1000, MILLISECONDS);
    } finally {
        for (int i = 0; i < gridCount(); i++) grid(i).events().stopLocalListen(lsnrs[i], EVT_CACHE_QUERY_EXECUTED);
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) Event(org.apache.ignite.events.Event) CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 67 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)

Example 68 with SqlQuery

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

the class IgniteCrossCachesJoinsQueryTest method checkOrganizationPersonsJoin.

/**
 * @param cache Cache.
 */
private void checkOrganizationPersonsJoin(IgniteCache cache) {
    if (skipQuery(cache, PERSON_CACHE_NAME, ORG_CACHE_NAME))
        return;
    qry = "checkOrganizationPersonsJoin";
    SqlFieldsQuery qry = new SqlFieldsQuery("select o.name, p.name " + "from \"" + ORG_CACHE_NAME + "\".Organization o, \"" + PERSON_CACHE_NAME + "\".Person p " + "where p.orgId = o._key and o._key=?");
    qry.setDistributedJoins(distributedJoins());
    SqlQuery qry2 = null;
    if (PERSON_CACHE_NAME.equals(cache.getName())) {
        qry2 = new SqlQuery(Person.class, "from \"" + ORG_CACHE_NAME + "\".Organization, \"" + PERSON_CACHE_NAME + "\".Person " + "where Person.orgId = Organization._key and Organization._key=?");
        qry2.setDistributedJoins(distributedJoins());
    }
    long total = 0;
    for (int i = 0; i < data.personsPerOrg.size(); i++) {
        qry.setArgs(i);
        if (qry2 != null)
            qry2.setArgs(i);
        List<List<Object>> res = cache.query(qry).getAll();
        assertEquals((int) data.personsPerOrg.get(i), res.size());
        if (qry2 != null) {
            List<List<Object>> res2 = cache.query(qry2).getAll();
            assertEquals((int) data.personsPerOrg.get(i), res2.size());
        }
        total += res.size();
    }
    SqlFieldsQuery qry3 = new SqlFieldsQuery("select count(*) " + "from \"" + ORG_CACHE_NAME + "\".Organization o, \"" + PERSON_CACHE_NAME + "\".Person p where p.orgId = o._key");
    qry3.setDistributedJoins(distributedJoins());
    List<List<Object>> res = cache.query(qry3).getAll();
    assertEquals(1, res.size());
    assertEquals(total, res.get(0).get(0));
}
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 69 with SqlQuery

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

the class IgniteCacheQueryH2IndexingLeakTest method testLeaksInIgniteH2IndexingOnUnusedThread.

/**
 * @throws Exception If failed.
 */
public void testLeaksInIgniteH2IndexingOnUnusedThread() throws Exception {
    final IgniteCache<Integer, Integer> c = grid(0).cache(DEFAULT_CACHE_NAME);
    final CountDownLatch latch = new CountDownLatch(1);
    for (int i = 0; i < ITERATIONS; ++i) {
        info("Iteration #" + i);
        // Open iterator on the created cursor: add entries to the cache
        IgniteInternalFuture<?> fut = multithreadedAsync(new CAX() {

            @Override
            public void applyx() throws IgniteCheckedException {
                c.query(new SqlQuery(Integer.class, "_val >= 0")).getAll();
                U.await(latch);
            }
        }, THREAD_COUNT);
        Thread.sleep(STMT_CACHE_CLEANUP_TIMEOUT);
        // Wait for stmtCache is cleaned up because all user threads don't perform queries a lot of time.
        assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return getStatementCacheSize(grid(0).context().query()) == 0;
            }
        }, STMT_CACHE_CLEANUP_TIMEOUT * 2));
        latch.countDown();
        fut.get();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlQuery(org.apache.ignite.cache.query.SqlQuery) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CAX(org.apache.ignite.internal.util.typedef.CAX) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 70 with SqlQuery

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

the class IgniteCacheQueryIndexSelfTest method checkQuery.

/**
 * @param cache Cache.
 * @throws Exception If failed.
 */
private void checkQuery(IgniteCache<Integer, CacheValue> cache) throws Exception {
    QueryCursor<Cache.Entry<Integer, CacheValue>> qry = cache.query(new SqlQuery(CacheValue.class, "val >= 5"));
    Collection<Cache.Entry<Integer, CacheValue>> queried = qry.getAll();
    assertEquals("Unexpected query result: " + queried, 5, queried.size());
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery)

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