use of org.apache.ignite.cache.query.Query 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));
}
}
use of org.apache.ignite.cache.query.Query in project ignite by apache.
the class IgniteRepositoryQuery method prepareQuery.
/**
* @param prmtrs Prmtrs.
* @return prepared query for execution
*/
@NotNull
private Query prepareQuery(Object[] prmtrs) {
Object[] parameters = prmtrs;
String sql = qry.sql();
Query query;
switch(qry.options()) {
case SORTING:
sql = IgniteQueryGenerator.addSorting(new StringBuilder(sql), (Sort) parameters[parameters.length - 1]).toString();
parameters = Arrays.copyOfRange(parameters, 0, parameters.length - 1);
break;
case PAGINATION:
sql = IgniteQueryGenerator.addPaging(new StringBuilder(sql), (Pageable) parameters[parameters.length - 1]).toString();
parameters = Arrays.copyOfRange(parameters, 0, parameters.length - 1);
break;
}
if (qry.isFieldQuery()) {
SqlFieldsQuery sqlFieldsQry = new SqlFieldsQuery(sql);
sqlFieldsQry.setArgs(parameters);
query = sqlFieldsQry;
} else {
SqlQuery sqlQry = new SqlQuery(type, sql);
sqlQry.setArgs(parameters);
query = sqlQry;
}
return query;
}
use of org.apache.ignite.cache.query.Query in project ignite by apache.
the class LoadTest method testMultithreading.
/**
* Test thin client in multi-thread environment.
*/
@Test
public void testMultithreading() throws Exception {
final int THREAD_CNT = 8;
final int ITERATION_CNT = 20;
final int BATCH_SIZE = 1000;
final int PAGE_CNT = 3;
IgniteConfiguration srvCfg = Config.getServerConfiguration();
// No peer class loading from thin clients: we need the server to know about this class to deserialize
// ScanQuery filter.
srvCfg.setBinaryConfiguration(new BinaryConfiguration().setTypeConfigurations(Arrays.asList(new BinaryTypeConfiguration(getClass().getName()), new BinaryTypeConfiguration(SerializedLambda.class.getName()))));
try (Ignite ignored = Ignition.start(srvCfg);
IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(Config.SERVER))) {
ClientCache<Integer, String> cache = client.createCache("testMultithreading");
AtomicInteger cnt = new AtomicInteger(1);
AtomicReference<Throwable> error = new AtomicReference<>();
Runnable assertion = () -> {
try {
int rangeStart = cnt.getAndAdd(BATCH_SIZE);
int rangeEnd = rangeStart + BATCH_SIZE;
Map<Integer, String> data = IntStream.range(rangeStart, rangeEnd).boxed().collect(Collectors.toMap(i -> i, i -> String.format("String %s", i)));
cache.putAll(data);
Query<Cache.Entry<Integer, String>> qry = new ScanQuery<Integer, String>().setPageSize(data.size() / PAGE_CNT).setFilter((i, s) -> i >= rangeStart && i < rangeEnd);
try (QueryCursor<Cache.Entry<Integer, String>> cur = cache.query(qry)) {
List<Cache.Entry<Integer, String>> res = cur.getAll();
assertEquals("Unexpected number of entries", data.size(), res.size());
Map<Integer, String> act = res.stream().collect(Collectors.toMap(Cache.Entry::getKey, Cache.Entry::getValue));
assertEquals("Unexpected entries", data, act);
}
} catch (Throwable ex) {
error.set(ex);
}
};
CountDownLatch complete = new CountDownLatch(THREAD_CNT);
Runnable manyAssertions = () -> {
for (int i = 0; i < ITERATION_CNT && error.get() == null; i++) assertion.run();
complete.countDown();
};
ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_CNT);
IntStream.range(0, THREAD_CNT).forEach(t -> threadPool.submit(manyAssertions));
assertTrue("Timeout", complete.await(180, TimeUnit.SECONDS));
String errMsg = error.get() == null ? "" : error.get().getMessage();
assertNull(errMsg, error.get());
}
}
use of org.apache.ignite.cache.query.Query in project ignite by apache.
the class FunctionalQueryTest method testGettingEmptyResultWhenQueryingEmptyTable.
/**
*/
@Test
public void testGettingEmptyResultWhenQueryingEmptyTable() throws Exception {
try (Ignite ignored = Ignition.start(Config.getServerConfiguration());
IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(Config.SERVER))) {
final String TBL = "Person";
client.query(new SqlFieldsQuery(String.format("CREATE TABLE IF NOT EXISTS " + TBL + " (id INT PRIMARY KEY, name VARCHAR) WITH \"VALUE_TYPE=%s\"", Person.class.getName())).setSchema("PUBLIC")).getAll();
// IgniteClient#query() API
List<List<?>> res = client.query(new SqlFieldsQuery("SELECT * FROM " + TBL)).getAll();
assertNotNull(res);
assertEquals(0, res.size());
// ClientCache#query(SqlFieldsQuery) API
ClientCache<Integer, Person> cache = client.cache("SQL_PUBLIC_" + TBL.toUpperCase());
res = cache.query(new SqlFieldsQuery("SELECT * FROM " + TBL)).getAll();
assertNotNull(res);
assertEquals(0, res.size());
// ClientCache#query(ScanQuery) and ClientCache#query(SqlQuery) API
Collection<Query<Cache.Entry<Integer, Person>>> queries = Arrays.asList(new ScanQuery<>(), new SqlQuery<>(Person.class, "1 = 1"));
for (Query<Cache.Entry<Integer, Person>> qry : queries) {
try (QueryCursor<Cache.Entry<Integer, Person>> cur = cache.query(qry)) {
List<Cache.Entry<Integer, Person>> res2 = cur.getAll();
assertNotNull(res2);
assertEquals(0, res2.size());
}
}
}
}
use of org.apache.ignite.cache.query.Query in project ignite by apache.
the class FunctionalQueryTest method testQueries.
/**
* Tested API:
* <ul>
* <li>{@link ClientCache#query(Query)}</li>
* </ul>
*/
@Test
public void testQueries() throws Exception {
IgniteConfiguration srvCfg = Config.getServerConfiguration();
// No peer class loading from thin clients: we need the server to know about this class to deserialize
// ScanQuery filter.
srvCfg.setBinaryConfiguration(new BinaryConfiguration().setTypeConfigurations(Arrays.asList(new BinaryTypeConfiguration(getClass().getName()), new BinaryTypeConfiguration(SerializedLambda.class.getName()))));
try (Ignite ignored = Ignition.start(srvCfg);
IgniteClient client = Ignition.startClient(getClientConfiguration())) {
ClientCache<Integer, Person> cache = client.getOrCreateCache(Config.DEFAULT_CACHE_NAME);
Map<Integer, Person> data = IntStream.rangeClosed(1, 100).boxed().collect(Collectors.toMap(i -> i, i -> new Person(i, String.format("Person %s", i))));
cache.putAll(data);
int minId = data.size() / 2 + 1;
int pageSize = (data.size() - minId) / 3;
// expected query result size
int expSize = data.size() - minId + 1;
// Expected result
Map<Integer, Person> exp = data.entrySet().stream().filter(e -> e.getKey() >= minId).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
// Scan and SQL queries
Collection<Query<Cache.Entry<Integer, Person>>> queries = Arrays.asList(new ScanQuery<Integer, Person>((i, p) -> p.getId() >= minId).setPageSize(pageSize), new SqlQuery<Integer, Person>(Person.class, "id >= ?").setArgs(minId).setPageSize(pageSize));
for (Query<Cache.Entry<Integer, Person>> qry : queries) {
try (QueryCursor<Cache.Entry<Integer, Person>> cur = cache.query(qry)) {
List<Cache.Entry<Integer, Person>> res = cur.getAll();
assertEquals(String.format("Unexpected number of rows from %s", qry.getClass().getSimpleName()), expSize, res.size());
Map<Integer, Person> act = res.stream().collect(Collectors.toMap(Cache.Entry::getKey, Cache.Entry::getValue));
assertEquals(String.format("unexpected rows from %s", qry.getClass().getSimpleName()), exp, act);
}
}
checkSqlFieldsQuery(cache, minId, pageSize, expSize, exp, true);
checkSqlFieldsQuery(cache, minId, pageSize, expSize, exp, false);
}
}
Aggregations