Search in sources :

Example 16 with QueryCursor

use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.

the class IgniteCacheRandomOperationBenchmark method doContinuousQuery.

/**
     * @param cache Ignite cache.
     * @param map Parameters map.
     * @throws Exception If failed.
     */
private void doContinuousQuery(IgniteCache<Object, Object> cache, Map<Object, Object> map) throws Exception {
    List<QueryCursor> cursors = (ArrayList<QueryCursor>) map.get(cache.getName());
    if (cursors == null) {
        cursors = new ArrayList<>(CONTINUOUS_QUERY_PER_CACHE);
        map.put(cache.getName(), cursors);
    }
    if (cursors.size() == CONTINUOUS_QUERY_PER_CACHE) {
        QueryCursor cursor = cursors.get(nextRandom(cursors.size()));
        cursor.close();
        cursors.remove(cursor);
    }
    ContinuousQuery qry = new ContinuousQuery();
    qry.setLocalListener(new ContinuousQueryUpdater());
    qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new ContinuousQueryFilter()));
    cursors.add(cache.query(qry));
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ArrayList(java.util.ArrayList) QueryCursor(org.apache.ignite.cache.query.QueryCursor)

Example 17 with QueryCursor

use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.

the class IgniteRepositoryQuery method execute.

/** {@inheritDoc} */
@Override
public Object execute(Object[] prmtrs) {
    Query qry = prepareQuery(prmtrs);
    QueryCursor qryCursor = cache.query(qry);
    return transformQueryCursor(prmtrs, qryCursor);
}
Also used : Query(org.apache.ignite.cache.query.Query) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SqlQuery(org.apache.ignite.cache.query.SqlQuery) RepositoryQuery(org.springframework.data.repository.query.RepositoryQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor)

Example 18 with QueryCursor

use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.

the class IgniteCacheProxy method queryContinuous.

/**
     * Executes continuous query.
     *
     * @param qry Query.
     * @param loc Local flag.
     * @param keepBinary Keep binary flag.
     * @return Initial iteration cursor.
     */
@SuppressWarnings("unchecked")
private QueryCursor<Cache.Entry<K, V>> queryContinuous(ContinuousQuery qry, boolean loc, boolean keepBinary) {
    if (qry.getInitialQuery() instanceof ContinuousQuery)
        throw new IgniteException("Initial predicate for continuous query can't be an instance of another " + "continuous query. Use SCAN or SQL query for initial iteration.");
    if (qry.getLocalListener() == null)
        throw new IgniteException("Mandatory local listener is not set for the query: " + qry);
    if (qry.getRemoteFilter() != null && qry.getRemoteFilterFactory() != null)
        throw new IgniteException("Should be used either RemoterFilter or RemoteFilterFactory.");
    try {
        final UUID routineId = ctx.continuousQueries().executeQuery(qry.getLocalListener(), qry.getRemoteFilter(), qry.getRemoteFilterFactory(), qry.getPageSize(), qry.getTimeInterval(), qry.isAutoUnsubscribe(), loc, keepBinary, qry.isIncludeExpired());
        final QueryCursor<Cache.Entry<K, V>> cur = qry.getInitialQuery() != null ? query(qry.getInitialQuery()) : null;
        return new QueryCursor<Cache.Entry<K, V>>() {

            @Override
            public Iterator<Cache.Entry<K, V>> iterator() {
                return cur != null ? cur.iterator() : new GridEmptyIterator<Cache.Entry<K, V>>();
            }

            @Override
            public List<Cache.Entry<K, V>> getAll() {
                return cur != null ? cur.getAll() : Collections.<Cache.Entry<K, V>>emptyList();
            }

            @Override
            public void close() {
                if (cur != null)
                    cur.close();
                try {
                    ctx.kernalContext().continuous().stopRoutine(routineId).get();
                } catch (IgniteCheckedException e) {
                    throw U.convertException(e);
                }
            }
        };
    } catch (IgniteCheckedException e) {
        throw U.convertException(e);
    }
}
Also used : CacheEntry(org.apache.ignite.cache.CacheEntry) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) UUID(java.util.UUID) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 19 with QueryCursor

use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.

the class IgniteCacheProxy method query.

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public <R> QueryCursor<R> query(Query<R> qry) {
    A.notNull(qry, "qry");
    GridCacheGateway<K, V> gate = this.gate;
    CacheOperationContext prev = onEnter(gate, opCtx);
    try {
        ctx.checkSecurity(SecurityPermission.CACHE_READ);
        validate(qry);
        convertToBinary(qry);
        CacheOperationContext opCtxCall = ctx.operationContextPerCall();
        boolean keepBinary = opCtxCall != null && opCtxCall.isKeepBinary();
        if (qry instanceof ContinuousQuery)
            return (QueryCursor<R>) queryContinuous((ContinuousQuery<K, V>) qry, qry.isLocal(), keepBinary);
        if (qry instanceof SqlQuery)
            return (QueryCursor<R>) ctx.kernalContext().query().querySql(ctx, (SqlQuery) qry, keepBinary);
        if (qry instanceof SqlFieldsQuery)
            return (FieldsQueryCursor<R>) ctx.kernalContext().query().querySqlFields(ctx, (SqlFieldsQuery) qry, keepBinary);
        if (qry instanceof ScanQuery)
            return query((ScanQuery) qry, null, projection(qry.isLocal()));
        return (QueryCursor<R>) query(qry, projection(qry.isLocal()));
    } catch (Exception e) {
        if (e instanceof CacheException)
            throw (CacheException) e;
        throw new CacheException(e);
    } finally {
        onLeave(gate, prev);
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) CacheException(javax.cache.CacheException) ScanQuery(org.apache.ignite.cache.query.ScanQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor)

Example 20 with QueryCursor

use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.

the class CacheScanPartitionQueryFallbackSelfTest method scanFallbackOnRebalancing.

/**
     * @param cur If {@code true} tests query cursor.
     * @throws Exception In case of error.
     */
private void scanFallbackOnRebalancing(final boolean cur) throws Exception {
    cacheMode = CacheMode.PARTITIONED;
    clientMode = false;
    backups = 2;
    commSpiFactory = new TestFallbackOnRebalancingCommunicationSpiFactory();
    syncRebalance = true;
    try {
        Ignite ignite = startGrids(GRID_CNT);
        fillCache(ignite);
        final AtomicBoolean done = new AtomicBoolean(false);
        final AtomicInteger idx = new AtomicInteger(GRID_CNT);
        IgniteInternalFuture fut1 = multithreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                int id = idx.getAndIncrement();
                while (!done.get()) {
                    startGrid(id);
                    Thread.sleep(3000);
                    info("Will stop grid: " + getTestIgniteInstanceName(id));
                    stopGrid(id);
                    if (done.get())
                        return null;
                    Thread.sleep(3000);
                }
                return null;
            }
        }, 2);
        final AtomicInteger nodeIdx = new AtomicInteger();
        IgniteInternalFuture fut2 = multithreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                int nodeId = nodeIdx.getAndIncrement();
                IgniteCache<Integer, Integer> cache = grid(nodeId).cache(DEFAULT_CACHE_NAME);
                int cntr = 0;
                while (!done.get()) {
                    int part = ThreadLocalRandom.current().nextInt(ignite(nodeId).affinity(DEFAULT_CACHE_NAME).partitions());
                    if (cntr++ % 100 == 0)
                        info("Running query [node=" + nodeId + ", part=" + part + ']');
                    try (QueryCursor<Cache.Entry<Integer, Integer>> cur0 = cache.query(new ScanQuery<Integer, Integer>(part))) {
                        if (cur)
                            doTestScanQueryCursor(cur0, part);
                        else
                            doTestScanQuery(cur0, part);
                    }
                }
                return null;
            }
        }, GRID_CNT);
        // Test for one minute
        Thread.sleep(60 * 1000);
        done.set(true);
        fut2.get();
        fut1.get();
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteException(org.apache.ignite.IgniteException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

QueryCursor (org.apache.ignite.cache.query.QueryCursor)34 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)20 IgniteCache (org.apache.ignite.IgniteCache)16 Ignite (org.apache.ignite.Ignite)14 Cache (javax.cache.Cache)13 ArrayList (java.util.ArrayList)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)10 IgniteException (org.apache.ignite.IgniteException)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 CacheEntryEvent (javax.cache.event.CacheEntryEvent)8 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)7 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 CacheException (javax.cache.CacheException)6 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)6 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)6 SqlQuery (org.apache.ignite.cache.query.SqlQuery)6 ScanQuery (org.apache.ignite.cache.query.ScanQuery)5 PA (org.apache.ignite.internal.util.typedef.PA)5 EntryProcessorException (javax.cache.processor.EntryProcessorException)4