Search in sources :

Example 16 with GridCloseableIteratorAdapter

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);
                }
            }
        }
    };
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCloseableIteratorAdapter(org.apache.ignite.internal.util.GridCloseableIteratorAdapter) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) ArrayList(java.util.ArrayList) IoStatisticsHolder(org.apache.ignite.internal.metric.IoStatisticsHolder) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

GridCloseableIteratorAdapter (org.apache.ignite.internal.util.GridCloseableIteratorAdapter)16 CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)8 GridCursor (org.apache.ignite.internal.util.lang.GridCursor)5 NoSuchElementException (java.util.NoSuchElementException)4 CacheDataRow (org.apache.ignite.internal.processors.cache.database.CacheDataRow)4 GridCloseableIterator (org.apache.ignite.internal.util.lang.GridCloseableIterator)4 Cache (javax.cache.Cache)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 GridDhtLocalPartition (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition)2 IgniteUuid (org.apache.ignite.lang.IgniteUuid)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1