use of org.apache.ignite.cache.query.ScanQuery in project ignite by apache.
the class IgniteClientReconnectQueriesTest method testScanQueryReconnect.
/**
* @throws Exception If failed.
*/
public void testScanQueryReconnect() throws Exception {
Ignite cln = grid(serverCount());
assertTrue(cln.cluster().localNode().isClient());
final Ignite srv = clientRouter(cln);
final IgniteCache<Integer, Person> clnCache = cln.getOrCreateCache(QUERY_CACHE);
final IgniteCache<Integer, Person> srvCache = srv.getOrCreateCache(QUERY_CACHE);
for (int i = 0; i < 10_000; i++) clnCache.put(i, new Person(i, "name-" + i, "surname-" + i));
final ScanQuery<Integer, Person> scanQry = new ScanQuery<>();
scanQry.setPageSize(1);
scanQry.setFilter(new IgniteBiPredicate<Integer, Person>() {
@Override
public boolean apply(Integer integer, Person person) {
return true;
}
});
QueryCursor<Cache.Entry<Integer, Person>> qryCursor = clnCache.query(scanQry);
reconnectClientNode(cln, srv, new Runnable() {
@Override
public void run() {
srvCache.put(10_001, new Person(10_001, "name", "surname"));
try {
clnCache.query(scanQry);
fail();
} catch (CacheException e) {
check(e);
}
}
});
try {
qryCursor.getAll();
fail();
} catch (CacheException e) {
checkAndWait(e);
}
qryCursor = clnCache.query(scanQry);
assertEquals(10_001, qryCursor.getAll().size());
}
use of org.apache.ignite.cache.query.ScanQuery in project ignite by apache.
the class IgniteClientReconnectQueriesTest method scanQueryReconnectInProgress.
/**
* @param setPart If {@code true} sets partition for scan query.
* @throws Exception If failed.
*/
private void scanQueryReconnectInProgress(boolean setPart) throws Exception {
Ignite cln = grid(serverCount());
assertTrue(cln.cluster().localNode().isClient());
final Ignite srv = clientRouter(cln);
final IgniteCache<Integer, Person> clnCache = cln.getOrCreateCache(QUERY_CACHE);
clnCache.put(1, new Person(1, "name1", "surname1"));
clnCache.put(2, new Person(2, "name2", "surname2"));
clnCache.put(3, new Person(3, "name3", "surname3"));
final ScanQuery<Integer, Person> scanQry = new ScanQuery<>();
scanQry.setPageSize(1);
scanQry.setFilter(new IgniteBiPredicate<Integer, Person>() {
@Override
public boolean apply(Integer integer, Person person) {
return true;
}
});
if (setPart)
scanQry.setPartition(1);
blockMessage(GridCacheQueryResponse.class);
final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
QueryCursor<Cache.Entry<Integer, Person>> qryCursor = clnCache.query(scanQry);
qryCursor.getAll();
} catch (CacheException e) {
checkAndWait(e);
return true;
}
return false;
}
});
// Check that client waiting operation.
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override
public Object call() throws Exception {
return fut.get(200);
}
}, IgniteFutureTimeoutCheckedException.class, null);
assertNotDone(fut);
unblockMessage();
reconnectClientNode(cln, srv, null);
assertTrue((Boolean) fut.get(2, SECONDS));
QueryCursor<Cache.Entry<Integer, Person>> qryCursor2 = clnCache.query(scanQry);
List<Cache.Entry<Integer, Person>> entries = qryCursor2.getAll();
assertEquals(setPart ? 1 : 3, entries.size());
for (Cache.Entry<Integer, Person> entry : entries) {
assertEquals(Integer.class, entry.getKey().getClass());
assertEquals(Person.class, entry.getValue().getClass());
}
}
use of org.apache.ignite.cache.query.ScanQuery in project ignite by apache.
the class CacheUtils method sparseFold.
private static <K, V, A> A sparseFold(String cacheName, IgniteBiFunction<Cache.Entry<K, V>, A, A> folder, IgnitePredicate<K> keyFilter, BinaryOperator<A> accumulator, A zeroVal, V defVal, K defKey, long defValCnt, boolean isNilpotent) {
A defRes = zeroVal;
if (!isNilpotent)
for (int i = 0; i < defValCnt; i++) defRes = folder.apply(new CacheEntryImpl<>(defKey, defVal), defRes);
Collection<A> totalRes = bcast(cacheName, () -> {
Ignite ignite = Ignition.localIgnite();
IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
int partsCnt = ignite.affinity(cacheName).partitions();
// Use affinity in filter for ScanQuery. Otherwise we accept consumer in each node which is wrong.
Affinity affinity = ignite.affinity(cacheName);
ClusterNode localNode = ignite.cluster().localNode();
A a = zeroVal;
// Iterate over all partitions. Some of them will be stored on that local node.
for (int part = 0; part < partsCnt; part++) {
int p = part;
// Query returns an empty cursor if this partition is not stored on this node.
for (Cache.Entry<K, V> entry : cache.query(new ScanQuery<K, V>(part, (k, v) -> affinity.mapPartitionToNode(p) == localNode && (keyFilter == null || keyFilter.apply(k))))) a = folder.apply(entry, a);
}
return a;
});
totalRes.add(defRes);
return totalRes.stream().reduce(zeroVal, accumulator);
}
use of org.apache.ignite.cache.query.ScanQuery in project ignite by apache.
the class CacheUtils method fold.
/**
* <b>Currently fold supports only commutative operations.<b/>
*
* @param cacheName Cache name.
* @param folder Fold function operating over cache entries.
* @param <K> Cache key object type.
* @param <V> Cache value object type.
* @param <A> Fold result type.
* @return Fold operation result.
*/
public static <K, V, A> Collection<A> fold(String cacheName, IgniteBiFunction<CacheEntry<K, V>, A, A> folder, IgnitePredicate<K> keyFilter) {
return bcast(cacheName, () -> {
Ignite ignite = Ignition.localIgnite();
IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
int partsCnt = ignite.affinity(cacheName).partitions();
// Use affinity in filter for ScanQuery. Otherwise we accept consumer in each node which is wrong.
Affinity affinity = ignite.affinity(cacheName);
ClusterNode locNode = ignite.cluster().localNode();
A a = null;
// Iterate over all partitions. Some of them will be stored on that local node.
for (int part = 0; part < partsCnt; part++) {
int p = part;
// Query returns an empty cursor if this partition is not stored on this node.
for (Cache.Entry<K, V> entry : cache.query(new ScanQuery<K, V>(part, (k, v) -> affinity.mapPartitionToNode(p) == locNode && (keyFilter == null || keyFilter.apply(k))))) a = folder.apply(new CacheEntry<>(entry, cache), a);
}
return a;
});
}
use of org.apache.ignite.cache.query.ScanQuery in project ignite by apache.
the class GridCacheQueryTransformerSelfTest method testLocal.
/**
* @throws Exception If failed.
*/
public void testLocal() throws Exception {
IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
try {
for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public List<Integer> call() throws Exception {
IgniteClosure<Cache.Entry<Integer, Value>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, Value>, Integer>() {
@Override
public Integer apply(Cache.Entry<Integer, Value> e) {
return e.getValue().idx;
}
};
return ignite.cache("test-cache").query(new ScanQuery<Integer, Value>().setLocal(true), transformer).getAll();
}
});
List<Integer> res = new ArrayList<>(F.flatCollections(lists));
assertEquals(50, res.size());
Collections.sort(res);
for (int i = 0; i < 50; i++) assertEquals(i * 100, res.get(i).intValue());
} finally {
cache.destroy();
}
}
Aggregations