use of org.apache.ignite.internal.util.GridCloseableIteratorAdapter in project ignite by apache.
the class GridCacheDistributedQueryManager method scanQueryDistributed.
/**
* {@inheritDoc}
*/
@SuppressWarnings({ "unchecked" })
@Override
public GridCloseableIterator scanQueryDistributed(final GridCacheQueryAdapter qry, Collection<ClusterNode> nodes) throws IgniteCheckedException {
assert !cctx.isLocal() : cctx.name();
assert qry.type() == GridCacheQueryType.SCAN : qry;
assert qry.mvccSnapshot() != null || !cctx.mvccEnabled();
boolean performanceStatsEnabled = cctx.kernalContext().performanceStatistics().enabled();
long startTime = performanceStatsEnabled ? System.currentTimeMillis() : 0;
long startTimeNanos = performanceStatsEnabled ? System.nanoTime() : 0;
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 = queryDistributed(bean, nodes);
return new GridCloseableIteratorAdapter() {
/**
*/
private Object cur;
/**
* Logical reads.
*/
private long logicalReads;
/**
* Physical reads.
*/
private long physicalReads;
@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) {
if (performanceStatsEnabled)
IoStatisticsQueryHelper.startGatheringQueryStatistics();
try {
if (locIter.hasNextX())
cur = locIter.nextX();
} finally {
if (performanceStatsEnabled) {
IoStatisticsHolder stat = IoStatisticsQueryHelper.finishGatheringQueryStatistics();
logicalReads += stat.logicalReads();
physicalReads += stat.physicalReads();
}
}
}
return cur != null || (cur = convert(fut.next())) != null;
}
/**
* @param obj Entry to convert.
* @return Cache entry
*/
private Object convert(Object obj) {
if (qry.transform() != null)
return obj;
Map.Entry e = (Map.Entry) obj;
return e == null ? null : new CacheQueryEntry(e.getKey(), e.getValue());
}
@Override
protected void onClose() throws IgniteCheckedException {
super.onClose();
if (locIter != null)
locIter.close();
if (fut != null)
fut.cancel();
if (performanceStatsEnabled) {
cctx.kernalContext().performanceStatistics().query(SCAN, cctx.name(), ((GridCacheDistributedQueryFuture) fut).requestId(), startTime, System.nanoTime() - startTimeNanos, true);
if (logicalReads > 0 || physicalReads > 0) {
cctx.kernalContext().performanceStatistics().queryReads(SCAN, cctx.localNodeId(), ((GridCacheDistributedQueryFuture) fut).requestId(), logicalReads, physicalReads);
}
}
}
};
}
Aggregations