use of org.apache.ignite.internal.processors.cache.query.CacheQuery in project ignite by apache.
the class GridCacheSetImpl method size.
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public int size() {
try {
onAccess();
if (ctx.isLocal() || ctx.isReplicated()) {
GridConcurrentHashSet<SetItemKey> set = ctx.dataStructures().setData(id);
return set != null ? set.size() : 0;
}
CacheQuery qry = new GridCacheQueryAdapter<>(ctx, SET, null, null, new GridSetQueryPredicate<>(id, collocated), null, false, false);
Collection<ClusterNode> nodes = dataNodes(ctx.affinity().affinityTopologyVersion());
qry.projection(ctx.grid().cluster().forNodes(nodes));
Iterable<Integer> col = (Iterable<Integer>) qry.execute(new SumReducer()).get();
int sum = 0;
for (Integer val : col) sum += val;
return sum;
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
use of org.apache.ignite.internal.processors.cache.query.CacheQuery in project ignite by apache.
the class GridCacheSetImpl method iterator0.
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
private GridCloseableIterator<T> iterator0() {
try {
CacheQuery qry = new GridCacheQueryAdapter<>(ctx, SET, null, null, new GridSetQueryPredicate<>(id, collocated), null, false, false);
Collection<ClusterNode> nodes = dataNodes(ctx.affinity().affinityTopologyVersion());
qry.projection(ctx.grid().cluster().forNodes(nodes));
CacheQueryFuture<Map.Entry<T, ?>> fut = qry.execute();
CacheWeakQueryIteratorsHolder.WeakReferenceCloseableIterator it = ctx.itHolder().iterator(fut, new CacheIteratorConverter<T, Map.Entry<T, ?>>() {
@Override
protected T convert(Map.Entry<T, ?> e) {
return e.getKey();
}
@Override
protected void remove(T item) {
GridCacheSetImpl.this.remove(item);
}
});
if (rmvd) {
ctx.itHolder().removeIterator(it);
checkRemoved();
}
return it;
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
use of org.apache.ignite.internal.processors.cache.query.CacheQuery in project ignite by apache.
the class IgniteCacheProxy method query.
/**
* @param filter Filter.
* @param grp Optional cluster group.
* @return Cursor.
* @throws IgniteCheckedException If failed.
*/
@SuppressWarnings("unchecked")
private QueryCursor<Cache.Entry<K, V>> query(final Query filter, @Nullable ClusterGroup grp) throws IgniteCheckedException {
final CacheQuery qry;
boolean isKeepBinary = opCtx != null && opCtx.isKeepBinary();
final CacheQueryFuture fut;
if (filter instanceof TextQuery) {
TextQuery p = (TextQuery) filter;
qry = ctx.queries().createFullTextQuery(p.getType(), p.getText(), isKeepBinary);
if (grp != null)
qry.projection(grp);
fut = ctx.kernalContext().query().executeQuery(GridCacheQueryType.TEXT, p.getText(), ctx, new IgniteOutClosureX<CacheQueryFuture<Map.Entry<K, V>>>() {
@Override
public CacheQueryFuture<Map.Entry<K, V>> applyx() {
return qry.execute();
}
}, false);
} else if (filter instanceof SpiQuery) {
qry = ctx.queries().createSpiQuery(isKeepBinary);
if (grp != null)
qry.projection(grp);
fut = ctx.kernalContext().query().executeQuery(GridCacheQueryType.SPI, filter.getClass().getSimpleName(), ctx, new IgniteOutClosureX<CacheQueryFuture<Map.Entry<K, V>>>() {
@Override
public CacheQueryFuture<Map.Entry<K, V>> applyx() {
return qry.execute(((SpiQuery) filter).getArgs());
}
}, false);
} else {
if (filter instanceof SqlFieldsQuery)
throw new CacheException("Use methods 'queryFields' and 'localQueryFields' for " + SqlFieldsQuery.class.getSimpleName() + ".");
throw new CacheException("Unsupported query type: " + filter);
}
return new QueryCursorImpl<>(new GridCloseableIteratorAdapter<Entry<K, V>>() {
/** */
private Cache.Entry<K, V> cur;
@Override
protected Entry<K, V> onNext() throws IgniteCheckedException {
if (!onHasNext())
throw new NoSuchElementException();
Cache.Entry<K, V> e = cur;
cur = null;
return e;
}
@Override
protected boolean onHasNext() throws IgniteCheckedException {
if (cur != null)
return true;
Object next = fut.next();
// instead of Iterator<Map.Entry> due to IndexingSpi interface.
if (next == null)
return false;
if (next instanceof Cache.Entry)
cur = (Cache.Entry) next;
else {
Map.Entry e = (Map.Entry) next;
cur = new CacheEntryImpl(e.getKey(), e.getValue());
}
return true;
}
@Override
protected void onClose() throws IgniteCheckedException {
fut.cancel();
}
});
}
Aggregations