use of javax.cache.Cache in project ignite by apache.
the class FunctionalQueryTest method checkSqlFieldsQuery.
/**
* @param cache Cache.
* @param minId Minimal ID.
* @param pageSize Page size.
* @param expSize The size of the expected results.
* @param exp Expected results.
* @param lazy Lazy mode flag.
*/
private void checkSqlFieldsQuery(ClientCache<Integer, Person> cache, int minId, int pageSize, int expSize, Map<Integer, Person> exp, boolean lazy) {
SqlFieldsQuery qry = new SqlFieldsQuery("select id, name from Person where id >= ?").setArgs(minId).setPageSize(pageSize).setLazy(lazy);
try (QueryCursor<List<?>> cur = cache.query(qry)) {
List<List<?>> res = cur.getAll();
assertEquals(expSize, res.size());
Map<Integer, Person> act = res.stream().collect(Collectors.toMap(r -> Integer.parseInt(r.get(0).toString()), r -> new Person(Integer.parseInt(r.get(0).toString()), r.get(1).toString())));
assertEquals(exp, act);
}
}
use of javax.cache.Cache 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");
}
use of javax.cache.Cache in project ignite by apache.
the class CacheIteratorScanQueryTest method testQueryGetAllClientSide.
/**
* @throws Exception If failed.
*/
@Test
public void testQueryGetAllClientSide() throws Exception {
Ignite server = startGrid(0);
IgniteCache<Integer, Integer> cache = server.getOrCreateCache(DEFAULT_CACHE_NAME);
Ignite client = startClientGrid(1);
IgniteCache<Integer, Integer> cliCache = client.cache(DEFAULT_CACHE_NAME);
for (int i = 0; i < 100_000; i++) cache.put(i, i);
ScanQuery<Integer, Integer> qry = new ScanQuery<>();
qry.setPageSize(100);
try (QueryCursor<Cache.Entry<Integer, Integer>> cur = cliCache.query(qry)) {
List<Cache.Entry<Integer, Integer>> res = cur.getAll();
assertEquals(100_000, res.size());
Collections.sort(res, (e1, e2) -> {
return e1.getKey().compareTo(e2.getKey());
});
int exp = 0;
for (Cache.Entry<Integer, Integer> e : res) {
assertEquals(exp, e.getKey().intValue());
assertEquals(exp, e.getValue().intValue());
exp++;
}
}
}
use of javax.cache.Cache in project ignite by apache.
the class IgniteBinaryQueryTest method testBinaryQueries.
/**
* Test queries in Ignite binary.
*/
@Test
public void testBinaryQueries() throws Exception {
try (Ignite ignored = Ignition.start(Config.getServerConfiguration());
IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(Config.SERVER))) {
final String TYPE_NAME = "Person";
QueryEntity qryPerson = new QueryEntity().setKeyType(Integer.class.getName()).setValueType(TYPE_NAME).setFields(Stream.of(new SimpleEntry<>("id", Integer.class.getName()), new SimpleEntry<>("name", String.class.getName())).collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue, (a, b) -> a, LinkedHashMap::new))).setIndexes(Collections.singletonList(new QueryIndex("id")));
ClientCacheConfiguration cacheCfg = new ClientCacheConfiguration().setName("testBinaryQueries").setQueryEntities(qryPerson);
ClientCache<Integer, BinaryObject> cache = client.getOrCreateCache(cacheCfg).withKeepBinary();
final IgniteBinary binary = client.binary();
Map<Integer, BinaryObject> data = IntStream.range(0, 100).boxed().collect(Collectors.toMap(i -> i, i -> binary.builder(TYPE_NAME).setField("id", i, int.class).setField("name", String.format("Person %s", i), String.class).build()));
cache.putAll(data);
Cache.Entry<Integer, BinaryObject> p1 = cache.query(new SqlQuery<Integer, BinaryObject>(TYPE_NAME, "id = 1")).getAll().iterator().next();
assertEquals(Integer.valueOf(1), p1.getKey());
assertBinaryObjectsEqual(data.get(1), p1.getValue());
}
}
use of javax.cache.Cache in project ignite by apache.
the class IndexQueryKeepBinaryTest method check.
/**
* @param left First cache key, inclusive.
* @param right Last cache key, exclusive.
*/
private void check(QueryCursor cursor, int left, int right) {
List<Cache.Entry<Long, BinaryObject>> all = cursor.getAll();
assertEquals(right - left, all.size());
for (int i = 0; i < all.size(); i++) {
Cache.Entry<Long, ?> entry = all.get(i);
assertEquals(left + i, entry.getKey().intValue());
BinaryObject o = all.get(i).getValue();
assertEquals(new Person(entry.getKey().intValue()), o.deserialize());
}
}
Aggregations