/** * @param ignite Ignite. * @param orgId Organization id. * @return Count of found Person object with specified orgId */ private static int getPersonsCountBySqlLocalQuery(final IgniteEx ignite, int orgId) { List res = ignite.cache(Person.class.getSimpleName()) .query(new SqlQuery<Person.Key, Person>(Person.class, "orgId = ?").setArgs(orgId).setLocal(true)) .getAll(); return res.size(); }
qry.setLocal(isLocal()); qry.setArgs(val.uuid, val.str, val.decimal, val.integer, val.date, val.ts, val.person, val.enumKey);
sqlQrys.add(new SqlQuery<Integer, MvccTestSqlIndexValue>(MvccTestSqlIndexValue.class, "idxVal1=?").setLocal(locQry)); sqlQrys.add(new SqlQuery<Integer, MvccTestSqlIndexValue>(MvccTestSqlIndexValue.class, "idxVal1=? or idxVal1=?").setLocal(locQry)); sqlQrys.add(new SqlQuery<Integer, MvccTestSqlIndexValue>(MvccTestSqlIndexValue.class, "_key=?").setLocal(locQry));
/** * Tests local sql query. */ @Test public void testLocalSqlQuery() { IgniteCache<Integer, Value> cache = grid(0).cache(DEFAULT_CACHE_NAME); SqlQuery<Integer, Value> qry = new SqlQuery<>(Value.class.getSimpleName(), "select * from Value"); qry.setLocal(true); List<Cache.Entry<Integer, Value>> all = cache.query(qry).getAll(); assertFalse(all.isEmpty()); for (Cache.Entry<Integer, Value> entry : all) entry.getValue().str = "after"; check(cache); }
/** * @throws Exception If failed. */ @Test public void testSqlLocalQuery() throws Exception { IgniteCache<Integer, SqlValue> cache = grid().getOrCreateCache(SqlValue.class.getSimpleName()); try { cache.query(new SqlQuery<Integer, SqlValue>(SqlValue.class, "val >= 0").setLocal(true)).getAll(); assert false; } catch (CacheException e) { X.println("Caught expected exception: " + e); } catch (Exception ignored) { assert false; } }
/** * @param cache Cache. * @param loc Explicit query locality flag. * @throws Exception If check failed. */ private void checkLocalQueryResults(IgniteCache<CacheKey, CacheValue> cache, boolean loc) throws Exception { QueryCursor<Cache.Entry<CacheKey, CacheValue>> qry = cache.query(new SqlQuery<CacheKey, CacheValue>(CacheValue.class, "val > 1 and val < 4") .setReplicatedOnly(true).setLocal(loc)); Iterator<Cache.Entry<CacheKey, CacheValue>> iter = qry.iterator(); assert iter != null; assert iter.hasNext(); Cache.Entry<CacheKey, CacheValue> entry = iter.next(); assert entry.getKey().equals(new CacheKey(2)) || entry.getKey().equals(new CacheKey(3)); assert iter.hasNext(); entry = iter.next(); assert entry.getKey().equals(new CacheKey(2)) || entry.getKey().equals(new CacheKey(3)); assert !iter.hasNext(); }
qry.setLocal(isLocal()); fieldsQry.setLocal(isLocal());
/** * Ctor. * * @param reader Reader. */ public ClientCacheSqlQueryRequest(BinaryRawReaderEx reader) { super(reader); qry = new SqlQuery(reader.readString(), reader.readString()) .setArgs(PlatformCache.readQueryArgs(reader)) .setDistributedJoins(reader.readBoolean()) .setLocal(reader.readBoolean()) .setReplicatedOnly(reader.readBoolean()) .setPageSize(reader.readInt()) .setTimeout((int) reader.readLong(), TimeUnit.MILLISECONDS); }
/** * Test collecting info about running. * * @throws Exception If failed. */ @Test public void testRunningSqlQuery() throws Exception { IgniteInternalFuture<?> fut = runQueryAsync(new SqlQuery<Integer, Value>(Value.class, "id > sleep(100)")); Thread.sleep(500); GridQueryProcessor qryProc = grid(0).context().query(); Collection<GridRunningQueryInfo> queries = qryProc.runningQueries(0); assertEquals(1, queries.size()); fut.get(); queries = qryProc.runningQueries(0); assertEquals(0, queries.size()); SqlQuery<Integer, Value> qry = new SqlQuery<>(Value.class, "id > sleep(100)"); qry.setLocal(true); fut = runQueryAsync(qry); Thread.sleep(500); queries = qryProc.runningQueries(0); assertEquals(1, queries.size()); fut.get(); queries = qryProc.runningQueries(0); assertEquals(0, queries.size()); }
qry = cache3.query(new SqlQuery<CacheKey, CacheValue>(CacheValue.class, "val > 1 and val < 4").setLocal(true));
/** * Test simple query by key. * * @param cacheName Cache name. * @param key1 Key 1. * @param key2 Key 2. * @param <T> Key type. */ private <T> void testKeyQuery(final String cacheName, final T key1, final T key2) { final IgniteCache<T, Person> cache = ignite(0).cache(cacheName); final Person p1 = new Person("p1"); final Person p2 = new Person("p2"); cache.put(key1, p1); cache.put(key2, p2); final SqlQuery<T, Person> qry = new SqlQuery<>(Person.class, "where _key=?"); final SqlFieldsQuery fieldsQry = new SqlFieldsQuery("select _key, _val, * from Person where _key=?"); qry.setLocal(isLocal()); fieldsQry.setLocal(isLocal()); qry.setArgs(key1); fieldsQry.setArgs(key1); final List<Cache.Entry<T, Person>> 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).getValue()); assertEquals(key1, res.get(0).getKey()); assertTrue(fieldsRes.get(0).size() >= 2); assertEquals(key1, fieldsRes.get(0).get(0)); assertEquals(p1, fieldsRes.get(0).get(1)); }
/** * Reads sql query. * * @param reader Binary reader. * @return Query. */ private Query readSqlQuery(BinaryRawReaderEx reader) { boolean loc = reader.readBoolean(); String sql = reader.readString(); String typ = reader.readString(); final int pageSize = reader.readInt(); Object[] args = readQueryArgs(reader); boolean distrJoins = reader.readBoolean(); int timeout = reader.readInt(); boolean replicated = reader.readBoolean(); return new SqlQuery(typ, sql) .setPageSize(pageSize) .setArgs(args) .setLocal(loc) .setDistributedJoins(distrJoins) .setTimeout(timeout, TimeUnit.MILLISECONDS) .setReplicatedOnly(replicated); }
/** * @throws Exception If failed. */ @Test public void testLocalSqlQueryFromClient() throws Exception { try { Ignite g = startGrid("client"); IgniteCache<Integer, Integer> c = jcache(g, Integer.class, Integer.class); for (int i = 0; i < 10; i++) c.put(i, i); SqlQuery<Integer, Integer> qry = new SqlQuery<>(Integer.class, "_key >= 5 order by _key"); qry.setLocal(true); assertThrowsWithCause(() -> c.query(qry), CacheException.class); } finally { stopGrid("client"); } }
/** {@inheritDoc} */ @Test @Override public void testLocalSqlQueryFromClient() throws Exception { try { Ignite g = startGrid("client"); IgniteCache<Integer, Integer> c = jcache(g, Integer.class, Integer.class); for (int i = 0; i < 10; i++) c.put(i, i); SqlQuery<Integer, Integer> qry = new SqlQuery<>(Integer.class, "_key >= 5 order by _key"); qry.setLocal(true); try(QueryCursor<Cache.Entry<Integer, Integer>> qryCursor = c.query(qry)) { assertNotNull(qryCursor); List<Cache.Entry<Integer, Integer>> res = qryCursor.getAll(); assertNotNull(res); assertEquals(5, res.size()); } } finally { stopGrid("client"); } }
/** Tests local query over partitions. */ @Test public void testLocalQuery() { Affinity<Object> affinity = grid(0).affinity("cl"); int[] parts = affinity.primaryPartitions(grid(0).localNode()); Arrays.sort(parts); IgniteCache<ClientKey, Client> cl = grid(0).cache("cl"); SqlQuery<ClientKey, Client> qry1 = new SqlQuery<>(Client.class, "1=1"); qry1.setLocal(true); qry1.setPartitions(parts[0]); List<Cache.Entry<ClientKey, Client>> clients = cl.query(qry1).getAll(); for (Cache.Entry<ClientKey, Client> client : clients) assertEquals("Incorrect partition", parts[0], affinity.partition(client.getKey())); SqlFieldsQuery qry2 = new SqlFieldsQuery("select cl._KEY, cl._VAL from \"cl\".Client cl"); qry2.setLocal(true); qry2.setPartitions(parts[0]); List<List<?>> rows = cl.query(qry2).getAll(); for (List<?> row : rows) assertEquals("Incorrect partition", parts[0], affinity.partition(row.get(0))); } }
cache.query(new SqlQuery<>(MvccTestSqlIndexValue.class, "true").setLocal(loc)).getAll().size());
/** * Ctor. * * @param reader Reader. */ public ClientCacheSqlQueryRequest(BinaryRawReaderEx reader) { super(reader); qry = new SqlQuery(reader.readString(), reader.readString()) .setArgs(PlatformCache.readQueryArgs(reader)) .setDistributedJoins(reader.readBoolean()) .setLocal(reader.readBoolean()) .setReplicatedOnly(reader.readBoolean()) .setPageSize(reader.readInt()) .setTimeout((int) reader.readLong(), TimeUnit.MILLISECONDS); }
/** * Reads sql query. * * @param reader Binary reader. * @return Query. */ private Query readSqlQuery(BinaryRawReaderEx reader) { boolean loc = reader.readBoolean(); String sql = reader.readString(); String typ = reader.readString(); final int pageSize = reader.readInt(); Object[] args = readQueryArgs(reader); boolean distrJoins = reader.readBoolean(); int timeout = reader.readInt(); boolean replicated = reader.readBoolean(); return new SqlQuery(typ, sql) .setPageSize(pageSize) .setArgs(args) .setLocal(loc) .setDistributedJoins(distrJoins) .setTimeout(timeout, TimeUnit.MILLISECONDS) .setReplicatedOnly(replicated); }