Search in sources :

Example 31 with SqlQuery

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

the class H2RowCacheSelfTest method checkDeleteEntry.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
private void checkDeleteEntry() throws Exception {
    final String cacheName = "cache";
    grid().getOrCreateCache(cacheConfiguration(cacheName, true));
    int grpId = grid().cachex(cacheName).context().groupId();
    assertEquals(grpId, grid().cachex(cacheName).context().groupId());
    fillCache(cacheName);
    H2RowCache rowCache = rowCache(grid(), grpId);
    fillRowCache(cacheName);
    assertNotNull(rowCache);
    int key = RND.nextInt(ENTRIES);
    grid().cache(cacheName).query(new SqlQuery(Value.class, "_key = " + key)).getAll();
    int rowCacheSize = rowCache.size();
    long rowLink = getLinkForKey(cacheName, rowCache, key);
    assertNotNull(rowCache.get(rowLink));
    // Remove
    grid().cache(cacheName).remove(key);
    assertNull(rowCache.get(rowLink));
    int rowCacheSizeAfterUpdate = rowCache.size();
    assertEquals(rowCacheSize - 1, rowCacheSizeAfterUpdate);
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) H2RowCache(org.apache.ignite.internal.processors.query.h2.H2RowCache)

Example 32 with SqlQuery

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

the class H2RowCacheSelfTest method getLinkForKey.

/**
 * @param cacheName Cache name.
 * @param rowCache Row cache.
 * @param key Key to find.
 * @return Row's link.
 */
private long getLinkForKey(String cacheName, H2RowCache rowCache, int key) {
    grid().cache(cacheName).query(new SqlQuery(Value.class, "_key = " + key)).getAll().size();
    ConcurrentHashMap<Long, GridH2KeyValueRowOnheap> rowsMap = GridTestUtils.getFieldValue(rowCache, "rows");
    for (Map.Entry<Long, GridH2KeyValueRowOnheap> e : rowsMap.entrySet()) {
        GridH2KeyValueRowOnheap val = e.getValue();
        if ((Integer) val.key().value(null, false) == key)
            return e.getKey();
    }
    fail("Row cache doesn't contain key [key=" + key + ']');
    return -1;
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) GridH2KeyValueRowOnheap(org.apache.ignite.internal.processors.query.h2.opt.GridH2KeyValueRowOnheap)

Example 33 with SqlQuery

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

the class IgniteCacheLocalQuerySelfTest method testQueryLocal.

/**
 * @throws Exception If test failed.
 */
public void testQueryLocal() throws Exception {
    // check for cached prepared statements this would fail.
    for (int i = 0; i < 2; i++) {
        IgniteCache<Integer, String> cache = jcache(Integer.class, String.class);
        cache.put(1, "value1");
        cache.put(2, "value2");
        cache.put(3, "value3");
        cache.put(4, "value4");
        cache.put(5, "value5");
        // Tests equals query.
        QueryCursor<Cache.Entry<Integer, String>> qry = cache.query(new SqlQuery<Integer, String>(String.class, "_val='value1'").setLocal(true));
        Iterator<Cache.Entry<Integer, String>> iter = qry.iterator();
        Cache.Entry<Integer, String> entry = iter.next();
        assert !iter.hasNext();
        assert entry != null;
        assert entry.getKey() == 1;
        assert "value1".equals(entry.getValue());
        // Tests like query.
        qry = cache.query(new SqlQuery<Integer, String>(String.class, "_val like 'value%'").setLocal(true));
        iter = qry.iterator();
        assert iter.next() != null;
        assert iter.next() != null;
        assert iter.next() != null;
        assert iter.next() != null;
        assert iter.next() != null;
        assert !iter.hasNext();
        // Test explain for primitive index.
        List<List<?>> res = cache.query(new SqlFieldsQuery("explain select _key from String where _val > 'value1'").setLocal(true)).getAll();
        assertTrue("__ explain: \n" + res, ((String) res.get(0).get(0)).toLowerCase().contains("_val_idx"));
        cache.destroy();
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) List(java.util.List) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 34 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 35 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)

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