Search in sources :

Example 1 with SqlQuery

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

the class IgniteCacheProxy method convertToBinary.

/**
     * Convert query arguments to BinaryObjects if binary marshaller used.
     *
     * @param qry Query.
     */
private void convertToBinary(final Query qry) {
    if (ctx.binaryMarshaller()) {
        if (qry instanceof SqlQuery) {
            final SqlQuery sqlQry = (SqlQuery) qry;
            convertToBinary(sqlQry.getArgs());
        } else if (qry instanceof SpiQuery) {
            final SpiQuery spiQry = (SpiQuery) qry;
            convertToBinary(spiQry.getArgs());
        } else if (qry instanceof SqlFieldsQuery) {
            final SqlFieldsQuery fieldsQry = (SqlFieldsQuery) qry;
            convertToBinary(fieldsQry.getArgs());
        }
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) SpiQuery(org.apache.ignite.cache.query.SpiQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 2 with SqlQuery

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

the class H2IndexingAbstractGeoSelfTest method checkGeoMultithreaded.

/**
     * Check geo indexing multithreaded with dynamic index creation.
     *
     * @param dynamic Whether index should be created dynamically.
     * @throws Exception If failed.
     */
@SuppressWarnings("unchecked")
private void checkGeoMultithreaded(boolean dynamic) throws Exception {
    final IgniteCache<Integer, EnemyCamp> cache1 = createCache("camp", true, Integer.class, EnemyCamp.class, dynamic);
    final IgniteCache<Integer, EnemyCamp> cache2 = grid(1).cache("camp");
    final IgniteCache<Integer, EnemyCamp> cache3 = grid(2).cache("camp");
    try {
        final String[] points = new String[CNT];
        WKTReader r = new WKTReader();
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
        for (int idx = 0; idx < CNT; idx++) {
            int x = rnd.nextInt(1, 100);
            int y = rnd.nextInt(1, 100);
            cache1.getAndPut(idx, new EnemyCamp(r.read("POINT(" + x + " " + y + ")"), Integer.toString(idx)));
            points[idx] = Integer.toString(idx);
        }
        Thread.sleep(200);
        final AtomicBoolean stop = new AtomicBoolean();
        final AtomicReference<Exception> err = new AtomicReference<>();
        IgniteInternalFuture<?> putFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                WKTReader r = new WKTReader();
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                while (!stop.get()) {
                    int cacheIdx = rnd.nextInt(0, 3);
                    IgniteCache<Integer, EnemyCamp> cache = cacheIdx == 0 ? cache1 : cacheIdx == 1 ? cache2 : cache3;
                    int idx = rnd.nextInt(CNT);
                    int x = rnd.nextInt(1, 100);
                    int y = rnd.nextInt(1, 100);
                    cache.getAndPut(idx, new EnemyCamp(r.read("POINT(" + x + " " + y + ")"), Integer.toString(idx)));
                    U.sleep(50);
                }
                return null;
            }
        }, Runtime.getRuntime().availableProcessors(), "put-thread");
        IgniteInternalFuture<?> qryFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                WKTReader r = new WKTReader();
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                while (!stop.get()) {
                    try {
                        int cacheIdx = rnd.nextInt(0, 3);
                        IgniteCache<Integer, EnemyCamp> cache = cacheIdx == 0 ? cache1 : cacheIdx == 1 ? cache2 : cache3;
                        SqlQuery<Integer, EnemyCamp> qry = new SqlQuery<>(EnemyCamp.class, "coords && ?");
                        Collection<Cache.Entry<Integer, EnemyCamp>> res = cache.query(qry.setArgs(r.read("POLYGON((0 0, 0 100, 100 100, 100 0, 0 0))"))).getAll();
                        checkPoints(res, points);
                        U.sleep(5);
                    } catch (Exception e) {
                        err.set(e);
                        stop.set(true);
                        break;
                    }
                }
                return null;
            }
        }, 4, "qry-thread");
        U.sleep(6000L);
        stop.set(true);
        putFut.get();
        qryFut.get();
        Exception err0 = err.get();
        if (err0 != null)
            throw err0;
    } finally {
        destroy(cache1, grid(0), dynamic);
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) IgniteCache(org.apache.ignite.IgniteCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) WKTReader(com.vividsolutions.jts.io.WKTReader) ParseException(com.vividsolutions.jts.io.ParseException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Collection(java.util.Collection) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 3 with SqlQuery

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

the class H2IndexingAbstractGeoSelfTest method checkGeo.

/**
     * Check geo-index (dynamic).
     *
     * @param dynamic Whether index should be created dynamically.
     * @throws Exception If failed.
     */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
private void checkGeo(boolean dynamic) throws Exception {
    IgniteCache<Integer, EnemyCamp> cache = createCache("camp", true, Integer.class, EnemyCamp.class, dynamic);
    try {
        WKTReader r = new WKTReader();
        cache.getAndPut(0, new EnemyCamp(r.read("POINT(25 75)"), "A"));
        cache.getAndPut(1, new EnemyCamp(r.read("POINT(70 70)"), "B"));
        cache.getAndPut(2, new EnemyCamp(r.read("POINT(70 30)"), "C"));
        cache.getAndPut(3, new EnemyCamp(r.read("POINT(75 25)"), "D"));
        SqlQuery<Integer, EnemyCamp> qry = new SqlQuery(EnemyCamp.class, "coords && ?");
        Collection<Cache.Entry<Integer, EnemyCamp>> res = cache.query(qry.setArgs(r.read("POLYGON((5 70, 5 80, 30 80, 30 70, 5 70))"))).getAll();
        checkPoints(res, "A");
        res = cache.query(qry.setArgs(r.read("POLYGON((10 5, 10 35, 70 30, 75 25, 10 5))"))).getAll();
        checkPoints(res, "C", "D");
        // Move B to the first polygon.
        cache.getAndPut(1, new EnemyCamp(r.read("POINT(20 75)"), "B"));
        res = cache.query(qry.setArgs(r.read("POLYGON((5 70, 5 80, 30 80, 30 70, 5 70))"))).getAll();
        checkPoints(res, "A", "B");
        // Move B to the second polygon.
        cache.getAndPut(1, new EnemyCamp(r.read("POINT(30 30)"), "B"));
        res = cache.query(qry.setArgs(r.read("POLYGON((10 5, 10 35, 70 30, 75 25, 10 5))"))).getAll();
        checkPoints(res, "B", "C", "D");
        // Remove B.
        cache.getAndRemove(1);
        res = cache.query(qry.setArgs(r.read("POLYGON((5 70, 5 80, 30 80, 30 70, 5 70))"))).getAll();
        checkPoints(res, "A");
        res = cache.query(qry.setArgs(r.read("POLYGON((10 5, 10 35, 70 30, 75 25, 10 5))"))).getAll();
        checkPoints(res, "C", "D");
        // Check explain request.
        String plan = cache.query(new SqlFieldsQuery("explain select * from EnemyCamp " + "where coords && 'POINT(25 75)'")).getAll().get(0).get(0).toString().toLowerCase();
        assertTrue("__ explain: " + plan, plan.contains("coords_idx"));
        if (dynamic)
            cache.query(new SqlFieldsQuery("DROP INDEX \"EnemyCamp_coords_idx\"")).getAll();
    } finally {
        destroy(cache, grid(0), dynamic);
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) WKTReader(com.vividsolutions.jts.io.WKTReader) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 4 with SqlQuery

use of org.apache.ignite.cache.query.SqlQuery 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 5 with SqlQuery

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

the class CacheAbstractQueryMetricsSelfTest method testSqlQueryNotFullyFetchedMetrics.

/**
     * Test metrics for Sql queries.
     *
     * @throws Exception In case of error.
     */
public void testSqlQueryNotFullyFetchedMetrics() throws Exception {
    IgniteCache<Integer, String> cache = grid(0).context().cache().jcache("A");
    SqlQuery qry = new SqlQuery<>("String", "from String");
    qry.setPageSize(10);
    checkQueryNotFullyFetchedMetrics(cache, qry, true);
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery)

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