Search in sources :

Example 6 with GridCloseableIteratorAdapter

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;
        }
    };
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.database.CacheDataRow) GridCloseableIteratorAdapter(org.apache.ignite.internal.util.GridCloseableIteratorAdapter)

Example 7 with GridCloseableIteratorAdapter

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();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCloseableIteratorAdapter(org.apache.ignite.internal.util.GridCloseableIteratorAdapter) IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) CacheMetricsImpl(org.apache.ignite.internal.processors.cache.CacheMetricsImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SQLException(java.sql.SQLException) IOException(java.io.IOException) IgniteException(org.apache.ignite.IgniteException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) NoSuchElementException(java.util.NoSuchElementException) GridDhtUnreservedPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtUnreservedPartitionException) UUID(java.util.UUID) Cache(javax.cache.Cache) IgniteInternalCache(org.apache.ignite.internal.processors.cache.IgniteInternalCache)

Example 8 with GridCloseableIteratorAdapter

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.
        }
    };
}
Also used : GridCloseableIteratorAdapter(org.apache.ignite.internal.util.GridCloseableIteratorAdapter) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) IgniteUuid(org.apache.ignite.lang.IgniteUuid) GridSetQueryPredicate(org.apache.ignite.internal.processors.datastructures.GridSetQueryPredicate) SetItemKey(org.apache.ignite.internal.processors.datastructures.SetItemKey)

Example 9 with GridCloseableIteratorAdapter

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();
        }
    };
}
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) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

GridCloseableIteratorAdapter (org.apache.ignite.internal.util.GridCloseableIteratorAdapter)9 NoSuchElementException (java.util.NoSuchElementException)4 CacheDataRow (org.apache.ignite.internal.processors.cache.database.CacheDataRow)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 GridCloseableIterator (org.apache.ignite.internal.util.lang.GridCloseableIterator)3 Cache (javax.cache.Cache)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)2 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 IgniteException (org.apache.ignite.IgniteException)1 NodeStoppingException (org.apache.ignite.internal.NodeStoppingException)1 CacheMetricsImpl (org.apache.ignite.internal.processors.cache.CacheMetricsImpl)1 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)1 IgniteInternalCache (org.apache.ignite.internal.processors.cache.IgniteInternalCache)1 GridDhtUnreservedPartitionException (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtUnreservedPartitionException)1 GridLocalCache (org.apache.ignite.internal.processors.cache.local.GridLocalCache)1 GridSetQueryPredicate (org.apache.ignite.internal.processors.datastructures.GridSetQueryPredicate)1