use of org.apache.ignite.internal.util.GridCloseableIteratorAdapter in project ignite by apache.
the class IgniteCacheOffheapManagerImpl method iterator.
/** {@inheritDoc} */
@Override
public GridIterator<CacheDataRow> iterator(int part) throws IgniteCheckedException {
CacheDataStore data = partitionData(part);
if (data == null)
return new GridEmptyCloseableIterator<>();
final GridCursor<? extends CacheDataRow> cur = data.cursor();
return new GridCloseableIteratorAdapter<CacheDataRow>() {
/** */
private CacheDataRow next;
@Override
protected CacheDataRow onNext() {
CacheDataRow res = next;
next = null;
return res;
}
@Override
protected boolean onHasNext() throws IgniteCheckedException {
if (next != null)
return true;
if (cur.next())
next = cur.get();
return next != null;
}
};
}
use of org.apache.ignite.internal.util.GridCloseableIteratorAdapter in project ignite by apache.
the class GridCacheQueryManager method scanQueryLocal.
/**
* Process local scan query.
*
* @param qry Query.
* @param updStatisticsIfNeeded Update statistics flag.
*/
@SuppressWarnings({ "unchecked", "serial" })
protected GridCloseableIterator scanQueryLocal(final GridCacheQueryAdapter qry, final boolean updStatisticsIfNeeded) throws IgniteCheckedException {
if (!enterBusy())
throw new IllegalStateException("Failed to process query request (grid is stopping).");
final boolean statsEnabled = cctx.config().isStatisticsEnabled();
boolean needUpdStatistics = updStatisticsIfNeeded && statsEnabled;
long startTime = U.currentTimeMillis();
final String namex = cctx.name();
try {
assert qry.type() == SCAN;
if (log.isDebugEnabled())
log.debug("Running local SCAN query: " + qry);
final String taskName = cctx.kernalContext().task().resolveTaskName(qry.taskHash());
final IgniteBiPredicate filter = qry.scanFilter();
final ClusterNode locNode = cctx.localNode();
final UUID subjId = qry.subjectId();
if (cctx.gridEvents().isRecordable(EVT_CACHE_QUERY_EXECUTED)) {
cctx.gridEvents().record(new CacheQueryExecutedEvent<>(locNode, "Scan query executed.", EVT_CACHE_QUERY_EXECUTED, CacheQueryType.SCAN.name(), namex, null, null, filter, null, null, subjId, taskName));
}
final GridCloseableIterator<IgniteBiTuple<K, V>> iter = scanIterator(qry, true);
if (updStatisticsIfNeeded)
needUpdStatistics = false;
final boolean readEvt = cctx.gridEvents().isRecordable(EVT_CACHE_QUERY_OBJECT_READ);
return new GridCloseableIteratorAdapter<Object>() {
@Override
protected Object onNext() throws IgniteCheckedException {
long start = statsEnabled ? System.nanoTime() : 0L;
IgniteBiTuple<K, V> next = iter.nextX();
if (statsEnabled) {
CacheMetricsImpl metrics = cctx.cache().metrics0();
metrics.onRead(true);
metrics.addGetTimeNanos(System.nanoTime() - start);
}
if (readEvt) {
cctx.gridEvents().record(new CacheQueryReadEvent<>(cctx.localNode(), "Scan query entry read.", EVT_CACHE_QUERY_OBJECT_READ, CacheQueryType.SCAN.name(), namex, null, null, filter, null, null, subjId, taskName, next.getKey(), next.getValue(), null, null));
}
IgniteClosure transform = qry.transform();
if (transform == null)
return next;
Cache.Entry<K, V> entry;
if (qry.keepBinary())
entry = cctx.cache().keepBinary().getEntry(next.getKey());
else
entry = cctx.cache().getEntry(next.getKey());
return transform.apply(entry);
}
@Override
protected boolean onHasNext() throws IgniteCheckedException {
return iter.hasNextX();
}
@Override
protected void onClose() throws IgniteCheckedException {
iter.close();
}
};
} catch (Exception e) {
if (needUpdStatistics)
cctx.queries().collectMetrics(GridCacheQueryType.SCAN, namex, startTime, U.currentTimeMillis() - startTime, true);
throw e;
} finally {
leaveBusy();
}
}
use of org.apache.ignite.internal.util.GridCloseableIteratorAdapter in project ignite by apache.
the class GridCacheQueryManager method setIterator.
/**
* @param qry Query.
* @return Cache set items iterator.
*/
private GridCloseableIterator<IgniteBiTuple<K, V>> setIterator(GridCacheQueryAdapter<?> qry) {
final GridSetQueryPredicate filter = (GridSetQueryPredicate) qry.scanFilter();
filter.init(cctx);
IgniteUuid id = filter.setId();
Collection<SetItemKey> data = cctx.dataStructures().setData(id);
if (data == null)
data = Collections.emptyList();
final GridIterator<IgniteBiTuple<K, V>> it = F.iterator(data, new C1<SetItemKey, IgniteBiTuple<K, V>>() {
@Override
public IgniteBiTuple<K, V> apply(SetItemKey e) {
return new IgniteBiTuple<>((K) e.item(), (V) Boolean.TRUE);
}
}, true, new P1<SetItemKey>() {
@Override
public boolean apply(SetItemKey e) {
return filter.apply(e, null);
}
});
return new GridCloseableIteratorAdapter<IgniteBiTuple<K, V>>() {
@Override
protected boolean onHasNext() {
return it.hasNext();
}
@Override
protected IgniteBiTuple<K, V> onNext() {
return it.next();
}
@Override
protected void onRemove() {
it.remove();
}
@Override
protected void onClose() {
// No-op.
}
};
}
use of org.apache.ignite.internal.util.GridCloseableIteratorAdapter in project ignite by apache.
the class GridCacheDistributedQueryManager method scanQueryDistributed.
/** {@inheritDoc} */
@SuppressWarnings({ "unchecked", "serial" })
@Override
public GridCloseableIterator scanQueryDistributed(final GridCacheQueryAdapter qry, Collection<ClusterNode> nodes) throws IgniteCheckedException {
assert !cctx.isLocal() : cctx.name();
assert qry.type() == GridCacheQueryType.SCAN : qry;
GridCloseableIterator locIter0 = null;
for (ClusterNode node : nodes) {
if (node.isLocal()) {
locIter0 = scanQueryLocal(qry, false);
Collection<ClusterNode> rmtNodes = new ArrayList<>(nodes.size() - 1);
for (ClusterNode n : nodes) {
// Equals by reference can be used here.
if (n != node)
rmtNodes.add(n);
}
nodes = rmtNodes;
break;
}
}
final GridCloseableIterator locIter = locIter0;
final GridCacheQueryBean bean = new GridCacheQueryBean(qry, null, qry.<K, V>transform(), null);
final CacheQueryFuture fut = (CacheQueryFuture) queryDistributed(bean, nodes);
return new GridCloseableIteratorAdapter() {
/** */
private Object cur;
@Override
protected Object onNext() throws IgniteCheckedException {
if (!onHasNext())
throw new NoSuchElementException();
Object e = cur;
cur = null;
return e;
}
@Override
protected boolean onHasNext() throws IgniteCheckedException {
if (cur != null)
return true;
if (locIter != null && locIter.hasNextX())
cur = locIter.nextX();
return cur != null || (cur = fut.next()) != null;
}
@Override
protected void onClose() throws IgniteCheckedException {
super.onClose();
if (locIter != null)
locIter.close();
if (fut != null)
fut.cancel();
}
};
}
Aggregations