Search in sources :

Example 1 with IgniteOutClosureX

use of org.apache.ignite.internal.util.lang.IgniteOutClosureX in project ignite by apache.

the class GridQueryProcessor method querySqlFieldsNoCache.

/**
     * Query SQL fields without strict dependency on concrete cache.
     *
     * @param qry Query.
     * @param keepBinary Keep binary flag.
     * @return Cursor.
     */
public FieldsQueryCursor<List<?>> querySqlFieldsNoCache(final SqlFieldsQuery qry, final boolean keepBinary) {
    checkxEnabled();
    validateSqlFieldsQuery(qry);
    if (qry.isLocal())
        throw new IgniteException("Local query is not supported without specific cache.");
    if (qry.getSchema() == null)
        qry.setSchema(QueryUtils.DFLT_SCHEMA);
    if (!busyLock.enterBusy())
        throw new IllegalStateException("Failed to execute query (grid is stopping).");
    try {
        IgniteOutClosureX<FieldsQueryCursor<List<?>>> clo = new IgniteOutClosureX<FieldsQueryCursor<List<?>>>() {

            @Override
            public FieldsQueryCursor<List<?>> applyx() throws IgniteCheckedException {
                GridQueryCancel cancel = new GridQueryCancel();
                return idx.queryDistributedSqlFields(qry.getSchema(), qry, keepBinary, cancel, null);
            }
        };
        return executeQuery(GridCacheQueryType.SQL_FIELDS, qry.getSql(), null, clo, true);
    } catch (IgniteCheckedException e) {
        throw new CacheException(e);
    } finally {
        busyLock.leaveBusy();
    }
}
Also used : IgniteOutClosureX(org.apache.ignite.internal.util.lang.IgniteOutClosureX) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 2 with IgniteOutClosureX

use of org.apache.ignite.internal.util.lang.IgniteOutClosureX in project ignite by apache.

the class GridQueryProcessor method querySqlFields.

/**
 * Query SQL fields.
 *
 * @param cctx Cache context.
 * @param qry Query.
 * @param cliCtx Client context.
 * @param keepBinary Keep binary flag.
 * @param failOnMultipleStmts If {@code true} the method must throws exception when query contains
 *      more then one SQL statement.
 * @return Cursor.
 */
@SuppressWarnings("unchecked")
public List<FieldsQueryCursor<List<?>>> querySqlFields(@Nullable final GridCacheContext<?, ?> cctx, final SqlFieldsQuery qry, final SqlClientContext cliCtx, final boolean keepBinary, final boolean failOnMultipleStmts) {
    checkxEnabled();
    validateSqlFieldsQuery(qry);
    if (!ctx.state().publicApiActiveState(true)) {
        throw new IgniteException("Can not perform the operation because the cluster is inactive. Note, that " + "the cluster is considered inactive by default if Ignite Persistent Store is used to let all the nodes " + "join the cluster. To activate the cluster call Ignite.active(true).");
    }
    if (!busyLock.enterBusy())
        throw new IllegalStateException("Failed to execute query (grid is stopping).");
    GridCacheContext oldCctx = curCache.get();
    curCache.set(cctx);
    final String schemaName = qry.getSchema() != null ? qry.getSchema() : (cctx != null ? idx.schema(cctx.name()) : QueryUtils.DFLT_SCHEMA);
    try {
        IgniteOutClosureX<List<FieldsQueryCursor<List<?>>>> clo = new IgniteOutClosureX<List<FieldsQueryCursor<List<?>>>>() {

            @Override
            public List<FieldsQueryCursor<List<?>>> applyx() throws IgniteCheckedException {
                GridQueryCancel cancel = new GridQueryCancel();
                List<FieldsQueryCursor<List<?>>> res = idx.querySqlFields(schemaName, qry, cliCtx, keepBinary, failOnMultipleStmts, cancel);
                if (cctx != null)
                    sendQueryExecutedEvent(qry.getSql(), qry.getArgs(), cctx);
                return res;
            }
        };
        return executeQuery(GridCacheQueryType.SQL_FIELDS, qry.getSql(), cctx, clo, true);
    } catch (IgniteCheckedException e) {
        throw new CacheException(e);
    } finally {
        curCache.set(oldCctx);
        busyLock.leaveBusy();
    }
}
Also used : IgniteOutClosureX(org.apache.ignite.internal.util.lang.IgniteOutClosureX) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 3 with IgniteOutClosureX

use of org.apache.ignite.internal.util.lang.IgniteOutClosureX in project ignite by apache.

the class IgniteCacheProxyImpl method query.

/**
 * @param query Query.
 * @param grp Optional cluster group.
 * @return Cursor.
 * @throws IgniteCheckedException If failed.
 */
@SuppressWarnings("unchecked")
private QueryCursor<Cache.Entry<K, V>> query(final Query query, @Nullable ClusterGroup grp) throws IgniteCheckedException {
    GridCacheContext<K, V> ctx = getContextSafe();
    final CacheQuery qry;
    CacheOperationContext opCtxCall = ctx.operationContextPerCall();
    boolean isKeepBinary = opCtxCall != null && opCtxCall.isKeepBinary();
    final CacheQueryFuture fut;
    if (query instanceof TextQuery) {
        TextQuery q = (TextQuery) query;
        qry = ctx.queries().createFullTextQuery(q.getType(), q.getText(), q.getLimit(), q.getPageSize(), isKeepBinary);
        if (grp != null)
            qry.projection(grp);
        fut = ctx.kernalContext().query().executeQuery(GridCacheQueryType.TEXT, q.getText(), ctx, new IgniteOutClosureX<CacheQueryFuture<Map.Entry<K, V>>>() {

            @Override
            public CacheQueryFuture<Map.Entry<K, V>> applyx() {
                return qry.execute();
            }
        }, false);
    } else if (query instanceof SpiQuery) {
        qry = ctx.queries().createSpiQuery(isKeepBinary);
        if (grp != null)
            qry.projection(grp);
        fut = ctx.kernalContext().query().executeQuery(GridCacheQueryType.SPI, query.getClass().getSimpleName(), ctx, new IgniteOutClosureX<CacheQueryFuture<Map.Entry<K, V>>>() {

            @Override
            public CacheQueryFuture<Map.Entry<K, V>> applyx() {
                return qry.execute(((SpiQuery) query).getArgs());
            }
        }, false);
    } else if (query instanceof IndexQuery) {
        IndexQuery q = (IndexQuery) query;
        qry = ctx.queries().createIndexQuery(q, isKeepBinary);
        if (q.getPageSize() > 0)
            qry.pageSize(q.getPageSize());
        if (grp != null)
            qry.projection(grp);
        fut = ctx.kernalContext().query().executeQuery(GridCacheQueryType.INDEX, q.getValueType(), ctx, new IgniteOutClosureX<CacheQueryFuture<Map.Entry<K, V>>>() {

            @Override
            public CacheQueryFuture<Map.Entry<K, V>> applyx() {
                return qry.execute();
            }
        }, false);
    } else {
        if (query instanceof SqlFieldsQuery)
            throw new CacheException("Use methods 'queryFields' and 'localQueryFields' for " + SqlFieldsQuery.class.getSimpleName() + ".");
        throw new CacheException("Unsupported query type: " + query);
    }
    return new QueryCursorImpl<>(new GridCloseableIteratorAdapter<Entry<K, V>>() {

        /**
         */
        private Cache.Entry<K, V> cur;

        @Override
        protected Entry<K, V> onNext() throws IgniteCheckedException {
            if (!onHasNext())
                throw new NoSuchElementException();
            Cache.Entry<K, V> e = cur;
            cur = null;
            return e;
        }

        @Override
        protected boolean onHasNext() throws IgniteCheckedException {
            if (cur != null)
                return true;
            Object next = fut.next();
            // instead of Iterator<Map.Entry> due to IndexingSpi interface.
            if (next == null)
                return false;
            if (next instanceof Cache.Entry)
                cur = (Cache.Entry) next;
            else {
                Map.Entry e = (Map.Entry) next;
                cur = new CacheEntryImpl(e.getKey(), e.getValue());
            }
            return true;
        }

        @Override
        protected void onClose() throws IgniteCheckedException {
            fut.cancel();
        }
    });
}
Also used : CacheException(javax.cache.CacheException) SpiQuery(org.apache.ignite.cache.query.SpiQuery) CacheQuery(org.apache.ignite.internal.processors.cache.query.CacheQuery) CacheEntry(org.apache.ignite.cache.CacheEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteOutClosureX(org.apache.ignite.internal.util.lang.IgniteOutClosureX) IndexQuery(org.apache.ignite.cache.query.IndexQuery) CacheQueryFuture(org.apache.ignite.internal.processors.cache.query.CacheQueryFuture) TextQuery(org.apache.ignite.cache.query.TextQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 4 with IgniteOutClosureX

use of org.apache.ignite.internal.util.lang.IgniteOutClosureX in project ignite by apache.

the class IgniteCacheProxy method query.

/**
     * @param filter Filter.
     * @param grp Optional cluster group.
     * @return Cursor.
     * @throws IgniteCheckedException If failed.
     */
@SuppressWarnings("unchecked")
private QueryCursor<Cache.Entry<K, V>> query(final Query filter, @Nullable ClusterGroup grp) throws IgniteCheckedException {
    final CacheQuery qry;
    boolean isKeepBinary = opCtx != null && opCtx.isKeepBinary();
    final CacheQueryFuture fut;
    if (filter instanceof TextQuery) {
        TextQuery p = (TextQuery) filter;
        qry = ctx.queries().createFullTextQuery(p.getType(), p.getText(), isKeepBinary);
        if (grp != null)
            qry.projection(grp);
        fut = ctx.kernalContext().query().executeQuery(GridCacheQueryType.TEXT, p.getText(), ctx, new IgniteOutClosureX<CacheQueryFuture<Map.Entry<K, V>>>() {

            @Override
            public CacheQueryFuture<Map.Entry<K, V>> applyx() {
                return qry.execute();
            }
        }, false);
    } else if (filter instanceof SpiQuery) {
        qry = ctx.queries().createSpiQuery(isKeepBinary);
        if (grp != null)
            qry.projection(grp);
        fut = ctx.kernalContext().query().executeQuery(GridCacheQueryType.SPI, filter.getClass().getSimpleName(), ctx, new IgniteOutClosureX<CacheQueryFuture<Map.Entry<K, V>>>() {

            @Override
            public CacheQueryFuture<Map.Entry<K, V>> applyx() {
                return qry.execute(((SpiQuery) filter).getArgs());
            }
        }, false);
    } else {
        if (filter instanceof SqlFieldsQuery)
            throw new CacheException("Use methods 'queryFields' and 'localQueryFields' for " + SqlFieldsQuery.class.getSimpleName() + ".");
        throw new CacheException("Unsupported query type: " + filter);
    }
    return new QueryCursorImpl<>(new GridCloseableIteratorAdapter<Entry<K, V>>() {

        /** */
        private Cache.Entry<K, V> cur;

        @Override
        protected Entry<K, V> onNext() throws IgniteCheckedException {
            if (!onHasNext())
                throw new NoSuchElementException();
            Cache.Entry<K, V> e = cur;
            cur = null;
            return e;
        }

        @Override
        protected boolean onHasNext() throws IgniteCheckedException {
            if (cur != null)
                return true;
            Object next = fut.next();
            // instead of Iterator<Map.Entry> due to IndexingSpi interface.
            if (next == null)
                return false;
            if (next instanceof Cache.Entry)
                cur = (Cache.Entry) next;
            else {
                Map.Entry e = (Map.Entry) next;
                cur = new CacheEntryImpl(e.getKey(), e.getValue());
            }
            return true;
        }

        @Override
        protected void onClose() throws IgniteCheckedException {
            fut.cancel();
        }
    });
}
Also used : IgniteOutClosureX(org.apache.ignite.internal.util.lang.IgniteOutClosureX) CacheException(javax.cache.CacheException) SpiQuery(org.apache.ignite.cache.query.SpiQuery) CacheQueryFuture(org.apache.ignite.internal.processors.cache.query.CacheQueryFuture) CacheQuery(org.apache.ignite.internal.processors.cache.query.CacheQuery) TextQuery(org.apache.ignite.cache.query.TextQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CacheEntry(org.apache.ignite.cache.CacheEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 5 with IgniteOutClosureX

use of org.apache.ignite.internal.util.lang.IgniteOutClosureX in project ignite by apache.

the class GridQueryProcessor method querySqlFields.

/**
 * Query SQL fields.
 *
 * @param cctx Cache context.
 * @param qry Query.
 * @param cliCtx Client context.
 * @param keepBinary Keep binary flag.
 * @param failOnMultipleStmts If {@code true} the method must throws exception when query contains
 *      more then one SQL statement.
 * @param qryType Real query type.
 * @param cancel Hook for query cancellation.
 * @return Cursor.
 */
public List<FieldsQueryCursor<List<?>>> querySqlFields(@Nullable final GridCacheContext<?, ?> cctx, final SqlFieldsQuery qry, final SqlClientContext cliCtx, final boolean keepBinary, final boolean failOnMultipleStmts, GridCacheQueryType qryType, @Nullable final GridQueryCancel cancel) {
    // Validate.
    checkxEnabled();
    if (qry.isDistributedJoins() && qry.getPartitions() != null)
        throw new CacheException("Using both partitions and distributed JOINs is not supported for the same query");
    if (qry.isLocal() && ctx.clientNode() && (cctx == null || cctx.config().getCacheMode() != CacheMode.LOCAL))
        throw new CacheException("Execution of local SqlFieldsQuery on client node disallowed.");
    return executeQuerySafe(cctx, () -> {
        assert idx != null;
        final String schemaName = qry.getSchema() == null ? schemaName(cctx) : qry.getSchema();
        IgniteOutClosureX<List<FieldsQueryCursor<List<?>>>> clo = new IgniteOutClosureX<List<FieldsQueryCursor<List<?>>>>() {

            @Override
            public List<FieldsQueryCursor<List<?>>> applyx() {
                GridQueryCancel cancel0 = cancel != null ? cancel : new GridQueryCancel();
                List<FieldsQueryCursor<List<?>>> res = idx.querySqlFields(schemaName, qry, cliCtx, keepBinary, failOnMultipleStmts, cancel0);
                if (cctx != null)
                    sendQueryExecutedEvent(qry.getSql(), qry.getArgs(), cctx, qryType);
                return res;
            }
        };
        return executeQuery(qryType, qry.getSql(), cctx, clo, true);
    });
}
Also used : IgniteOutClosureX(org.apache.ignite.internal.util.lang.IgniteOutClosureX) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Aggregations

CacheException (javax.cache.CacheException)5 IgniteOutClosureX (org.apache.ignite.internal.util.lang.IgniteOutClosureX)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 FieldsQueryCursor (org.apache.ignite.cache.query.FieldsQueryCursor)3 Map (java.util.Map)2 NoSuchElementException (java.util.NoSuchElementException)2 Cache (javax.cache.Cache)2 IgniteCache (org.apache.ignite.IgniteCache)2 IgniteException (org.apache.ignite.IgniteException)2 CacheEntry (org.apache.ignite.cache.CacheEntry)2 SpiQuery (org.apache.ignite.cache.query.SpiQuery)2 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)2 TextQuery (org.apache.ignite.cache.query.TextQuery)2 CacheQuery (org.apache.ignite.internal.processors.cache.query.CacheQuery)2 CacheQueryFuture (org.apache.ignite.internal.processors.cache.query.CacheQueryFuture)2 IndexQuery (org.apache.ignite.cache.query.IndexQuery)1 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)1