use of org.apache.ignite.cache.query.ScanQuery in project ignite by apache.
the class CacheUtils method foreach.
/**
* @param cacheName Cache name.
* @param fun An operation that accepts a cache entry and processes it.
* @param keyFilter Cache keys filter.
* @param <K> Cache key object type.
* @param <V> Cache value object type.
*/
public static <K, V> void foreach(String cacheName, IgniteConsumer<CacheEntry<K, V>> fun, IgnitePredicate<K> keyFilter) {
bcast(cacheName, () -> {
Ignite ignite = Ignition.localIgnite();
IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
int partsCnt = ignite.affinity(cacheName).partitions();
// Use affinity in filter for scan query. Otherwise we accept consumer in each node which is wrong.
Affinity affinity = ignite.affinity(cacheName);
ClusterNode locNode = ignite.cluster().localNode();
// 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))))) fun.accept(new CacheEntry<>(entry, cache));
}
});
}
use of org.apache.ignite.cache.query.ScanQuery in project ignite by apache.
the class CacheKeepBinaryIterationTest method doTestScanQuery.
/**
* @param ccfg Cache configuration.
*/
private void doTestScanQuery(final CacheConfiguration<Object, Object> ccfg, boolean keepBinary, boolean primitives) throws Exception {
IgniteCache<Object, Object> cache = grid(0).createCache(ccfg);
assertEquals(0, cache.size());
try {
for (int i = 0; i < KEYS; i++) if (primitives)
cache.put(i, i);
else
cache.put(new QueryTestKey(i), new QueryTestValue(i));
for (int i = 0; i < getServerNodeCount(); i++) {
IgniteCache<Object, Object> cache0 = grid(i).cache(ccfg.getName());
if (keepBinary)
cache0 = cache0.withKeepBinary();
ScanQuery<Object, Object> qry = new ScanQuery<>();
qry.setLocal(true);
int size = 0;
try (QueryCursor<Cache.Entry<Object, Object>> cur = cache0.query(qry)) {
for (Cache.Entry<Object, Object> e : cur) {
Object key = e.getKey();
Object val = e.getValue();
if (!primitives) {
assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, keepBinary == key instanceof BinaryObject);
assertTrue("Got unexpected object: " + val.getClass() + ", keepBinary: " + keepBinary, keepBinary == val instanceof BinaryObject);
} else {
assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, key instanceof Integer);
assertTrue("Got unexpected object: " + val.getClass() + ", keepBinary: " + keepBinary, val instanceof Integer);
}
++size;
}
}
assertTrue(size > 0);
}
} finally {
if (ccfg.getEvictionPolicy() != null) {
// TODO: IGNITE-3462. Fixes evictionPolicy issues at cache destroy.
stopAllGrids();
startGridsMultiThreaded(getServerNodeCount());
} else
grid(0).destroyCache(ccfg.getName());
}
}
use of org.apache.ignite.cache.query.ScanQuery in project camel by apache.
the class IgniteCacheTest method testQuery.
@Test
public void testQuery() {
IgniteCache<String, String> cache = ignite().getOrCreateCache("testcache1");
Set<String> keys = new HashSet<>();
for (int i = 0; i < 100; i++) {
cache.put("k" + i, "v" + i);
keys.add("k" + i);
}
Query<Entry<String, String>> query = new ScanQuery<String, String>(new IgniteBiPredicate<String, String>() {
private static final long serialVersionUID = 1L;
@Override
public boolean apply(String key, String value) {
return Integer.parseInt(key.replace("k", "")) >= 50;
}
});
List results = template.requestBodyAndHeader("ignite:cache:testcache1?operation=QUERY", keys, IgniteConstants.IGNITE_CACHE_QUERY, query, List.class);
assert_().that(results.size()).isEqualTo(50);
}
use of org.apache.ignite.cache.query.ScanQuery in project ignite by apache.
the class IgniteCacheProxy method query.
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public <R> QueryCursor<R> query(Query<R> qry) {
A.notNull(qry, "qry");
GridCacheGateway<K, V> gate = this.gate;
CacheOperationContext prev = onEnter(gate, opCtx);
try {
ctx.checkSecurity(SecurityPermission.CACHE_READ);
validate(qry);
convertToBinary(qry);
CacheOperationContext opCtxCall = ctx.operationContextPerCall();
boolean keepBinary = opCtxCall != null && opCtxCall.isKeepBinary();
if (qry instanceof ContinuousQuery)
return (QueryCursor<R>) queryContinuous((ContinuousQuery<K, V>) qry, qry.isLocal(), keepBinary);
if (qry instanceof SqlQuery)
return (QueryCursor<R>) ctx.kernalContext().query().querySql(ctx, (SqlQuery) qry, keepBinary);
if (qry instanceof SqlFieldsQuery)
return (FieldsQueryCursor<R>) ctx.kernalContext().query().querySqlFields(ctx, (SqlFieldsQuery) qry, keepBinary);
if (qry instanceof ScanQuery)
return query((ScanQuery) qry, null, projection(qry.isLocal()));
return (QueryCursor<R>) query(qry, projection(qry.isLocal()));
} catch (Exception e) {
if (e instanceof CacheException)
throw (CacheException) e;
throw new CacheException(e);
} finally {
onLeave(gate, prev);
}
}
use of org.apache.ignite.cache.query.ScanQuery in project ignite by apache.
the class IgniteCacheProxy method query.
/** {@inheritDoc} */
@Override
public <T, R> QueryCursor<R> query(Query<T> qry, IgniteClosure<T, R> transformer) {
A.notNull(qry, "qry");
A.notNull(transformer, "transformer");
if (!(qry instanceof ScanQuery))
throw new UnsupportedOperationException("Transformers are supported only for SCAN queries.");
GridCacheGateway<K, V> gate = this.gate;
CacheOperationContext prev = onEnter(gate, opCtx);
try {
ctx.checkSecurity(SecurityPermission.CACHE_READ);
validate(qry);
return query((ScanQuery<K, V>) qry, transformer, projection(qry.isLocal()));
} catch (Exception e) {
if (e instanceof CacheException)
throw (CacheException) e;
throw new CacheException(e);
} finally {
onLeave(gate, prev);
}
}
Aggregations