Search in sources :

Example 16 with QueryCursorImpl

use of org.apache.ignite.internal.processors.cache.QueryCursorImpl in project ignite by apache.

the class IgniteH2Indexing method queryLocalSql.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public <K, V> QueryCursor<Cache.Entry<K, V>> queryLocalSql(String schemaName, String cacheName, final SqlQuery qry, final IndexingQueryFilter filter, final boolean keepBinary) throws IgniteCheckedException {
    String type = qry.getType();
    String sqlQry = qry.getSql();
    String alias = qry.getAlias();
    Object[] params = qry.getArgs();
    GridQueryCancel cancel = new GridQueryCancel();
    final GridCloseableIterator<IgniteBiTuple<K, V>> i = queryLocalSql(schemaName, cacheName, sqlQry, alias, F.asList(params), type, filter, cancel);
    return new QueryCursorImpl<>(new Iterable<Cache.Entry<K, V>>() {

        @Override
        public Iterator<Cache.Entry<K, V>> iterator() {
            return new ClIter<Cache.Entry<K, V>>() {

                @Override
                public void close() throws Exception {
                    i.close();
                }

                @Override
                public boolean hasNext() {
                    return i.hasNext();
                }

                @Override
                public Cache.Entry<K, V> next() {
                    IgniteBiTuple<K, V> t = i.next();
                    K key = (K) CacheObjectUtils.unwrapBinaryIfNeeded(objectContext(), t.get1(), keepBinary, false);
                    V val = (V) CacheObjectUtils.unwrapBinaryIfNeeded(objectContext(), t.get2(), keepBinary, false);
                    return new CacheEntryImpl<>(key, val);
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    }, cancel);
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) SQLException(java.sql.SQLException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridQueryCancel(org.apache.ignite.internal.processors.query.GridQueryCancel) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) Iterator(java.util.Iterator) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) Cache(javax.cache.Cache)

Example 17 with QueryCursorImpl

use of org.apache.ignite.internal.processors.cache.QueryCursorImpl in project ignite by apache.

the class IgniteH2Indexing method queryDistributedSql.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public <K, V> QueryCursor<Cache.Entry<K, V>> queryDistributedSql(String schemaName, String cacheName, SqlQuery qry, boolean keepBinary) {
    String type = qry.getType();
    H2TableDescriptor tblDesc = tableDescriptor(schemaName, cacheName, type);
    if (tblDesc == null)
        throw new IgniteSQLException("Failed to find SQL table for type: " + type, IgniteQueryErrorCode.TABLE_NOT_FOUND);
    String sql;
    try {
        sql = generateQuery(qry.getSql(), qry.getAlias(), tblDesc);
    } catch (IgniteCheckedException e) {
        throw new IgniteException(e);
    }
    SqlFieldsQuery fqry = new SqlFieldsQuery(sql);
    fqry.setArgs(qry.getArgs());
    fqry.setPageSize(qry.getPageSize());
    fqry.setDistributedJoins(qry.isDistributedJoins());
    fqry.setPartitions(qry.getPartitions());
    fqry.setLocal(qry.isLocal());
    if (qry.getTimeout() > 0)
        fqry.setTimeout(qry.getTimeout(), TimeUnit.MILLISECONDS);
    final QueryCursor<List<?>> res = querySqlFields(schemaName, fqry, null, keepBinary, true, null).get(0);
    final Iterable<Cache.Entry<K, V>> converted = new Iterable<Cache.Entry<K, V>>() {

        @Override
        public Iterator<Cache.Entry<K, V>> iterator() {
            final Iterator<List<?>> iter0 = res.iterator();
            return new Iterator<Cache.Entry<K, V>>() {

                @Override
                public boolean hasNext() {
                    return iter0.hasNext();
                }

                @Override
                public Cache.Entry<K, V> next() {
                    List<?> l = iter0.next();
                    return new CacheEntryImpl<>((K) l.get(0), (V) l.get(1));
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
    // No metadata for SQL queries.
    return new QueryCursorImpl<Cache.Entry<K, V>>(converted) {

        @Override
        public void close() {
            res.close();
        }
    };
}
Also used : QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) Iterator(java.util.Iterator) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) ArrayList(java.util.ArrayList) List(java.util.List) Cache(javax.cache.Cache)

Example 18 with QueryCursorImpl

use of org.apache.ignite.internal.processors.cache.QueryCursorImpl in project ignite by apache.

the class IgniteH2Indexing method doRunDistributedQuery.

/**
 * Run distributed query on detected set of partitions.
 * @param schemaName Schema name.
 * @param qry Original query.
 * @param twoStepQry Two-step query.
 * @param meta Metadata to set to cursor.
 * @param keepBinary Keep binary flag.
 * @param cancel Cancel handler.
 * @return Cursor representing distributed query result.
 */
private FieldsQueryCursor<List<?>> doRunDistributedQuery(String schemaName, SqlFieldsQuery qry, GridCacheTwoStepQuery twoStepQry, List<GridQueryFieldMetadata> meta, boolean keepBinary, GridQueryCancel cancel) {
    if (log.isDebugEnabled())
        log.debug("Parsed query: `" + qry.getSql() + "` into two step query: " + twoStepQry);
    twoStepQry.pageSize(qry.getPageSize());
    if (cancel == null)
        cancel = new GridQueryCancel();
    int[] partitions = qry.getPartitions();
    if (partitions == null && twoStepQry.derivedPartitions() != null) {
        try {
            partitions = calculateQueryPartitions(twoStepQry.derivedPartitions(), qry.getArgs());
        } catch (IgniteCheckedException e) {
            throw new CacheException("Failed to calculate derived partitions: [qry=" + qry.getSql() + ", params=" + Arrays.deepToString(qry.getArgs()) + "]", e);
        }
    }
    QueryCursorImpl<List<?>> cursor = new QueryCursorImpl<>(runQueryTwoStep(schemaName, twoStepQry, keepBinary, qry.isEnforceJoinOrder(), qry.getTimeout(), cancel, qry.getArgs(), partitions, qry.isLazy()), cancel);
    cursor.fieldsMeta(meta);
    return cursor;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) GridQueryCancel(org.apache.ignite.internal.processors.query.GridQueryCancel) ArrayList(java.util.ArrayList) List(java.util.List) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl)

Example 19 with QueryCursorImpl

use of org.apache.ignite.internal.processors.cache.QueryCursorImpl in project ignite by apache.

the class IgniteH2Indexing method queryLocalSqlFields.

/**
 * {@inheritDoc}
 */
@Override
public FieldsQueryCursor<List<?>> queryLocalSqlFields(String schemaName, SqlFieldsQuery qry, final boolean keepBinary, IndexingQueryFilter filter, GridQueryCancel cancel) throws IgniteCheckedException {
    String sql = qry.getSql();
    Object[] args = qry.getArgs();
    final GridQueryFieldsResult res = queryLocalSqlFields(schemaName, sql, F.asList(args), filter, qry.isEnforceJoinOrder(), qry.getTimeout(), cancel);
    QueryCursorImpl<List<?>> cursor = new QueryCursorImpl<>(new Iterable<List<?>>() {

        @Override
        public Iterator<List<?>> iterator() {
            try {
                return new GridQueryCacheObjectsIterator(res.iterator(), objectContext(), keepBinary);
            } catch (IgniteCheckedException e) {
                throw new IgniteException(e);
            }
        }
    }, cancel);
    cursor.fieldsMeta(res.metaData());
    return cursor;
}
Also used : QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) GridQueryFieldsResult(org.apache.ignite.internal.processors.query.GridQueryFieldsResult) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) Iterator(java.util.Iterator) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 20 with QueryCursorImpl

use of org.apache.ignite.internal.processors.cache.QueryCursorImpl in project ignite by apache.

the class DmlStatementsProcessor method streamUpdateQuery.

/**
 * Perform given statement against given data streamer. Only rows based INSERT is supported.
 *
 * @param schemaName Schema name.
 * @param streamer Streamer to feed data to.
 * @param stmt Statement.
 * @param args Statement arguments.
 * @return Number of rows in given INSERT statement.
 * @throws IgniteCheckedException if failed.
 */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
long streamUpdateQuery(String schemaName, IgniteDataStreamer streamer, PreparedStatement stmt, final Object[] args) throws IgniteCheckedException {
    idx.checkStatementStreamable(stmt);
    Prepared p = GridSqlQueryParser.prepared(stmt);
    assert p != null;
    final UpdatePlan plan = getPlanForStatement(schemaName, null, p, null, true, null);
    assert plan.isLocalSubquery();
    final GridCacheContext cctx = plan.cacheContext();
    QueryCursorImpl<List<?>> cur;
    final ArrayList<List<?>> data = new ArrayList<>(plan.rowCount());
    QueryCursorImpl<List<?>> stepCur = new QueryCursorImpl<>(new Iterable<List<?>>() {

        @Override
        public Iterator<List<?>> iterator() {
            try {
                Iterator<List<?>> it;
                if (!F.isEmpty(plan.selectQuery())) {
                    GridQueryFieldsResult res = idx.queryLocalSqlFields(idx.schema(cctx.name()), plan.selectQuery(), F.asList(U.firstNotNull(args, X.EMPTY_OBJECT_ARRAY)), null, false, 0, null);
                    it = res.iterator();
                } else
                    it = plan.createRows(U.firstNotNull(args, X.EMPTY_OBJECT_ARRAY)).iterator();
                return new GridQueryCacheObjectsIterator(it, idx.objectContext(), cctx.keepBinary());
            } catch (IgniteCheckedException e) {
                throw new IgniteException(e);
            }
        }
    }, null);
    data.addAll(stepCur.getAll());
    cur = new QueryCursorImpl<>(new Iterable<List<?>>() {

        @Override
        public Iterator<List<?>> iterator() {
            return data.iterator();
        }
    }, null);
    if (plan.rowCount() == 1) {
        IgniteBiTuple t = plan.processRow(cur.iterator().next());
        streamer.addData(t.getKey(), t.getValue());
        return 1;
    }
    Map<Object, Object> rows = new LinkedHashMap<>(plan.rowCount());
    for (List<?> row : cur) {
        final IgniteBiTuple t = plan.processRow(row);
        rows.put(t.getKey(), t.getValue());
    }
    streamer.addData(rows);
    return rows.size();
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Prepared(org.h2.command.Prepared) ArrayList(java.util.ArrayList) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) GridQueryFieldsResult(org.apache.ignite.internal.processors.query.GridQueryFieldsResult) LinkedHashMap(java.util.LinkedHashMap) GridBoundedConcurrentLinkedHashMap(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) IgniteSingletonIterator(org.apache.ignite.internal.util.lang.IgniteSingletonIterator) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)

Aggregations

QueryCursorImpl (org.apache.ignite.internal.processors.cache.QueryCursorImpl)21 List (java.util.List)20 ArrayList (java.util.ArrayList)16 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)12 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)9 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)9 IgniteException (org.apache.ignite.IgniteException)8 GridQueryCacheObjectsIterator (org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator)8 Iterator (java.util.Iterator)7 IgniteSystemProperties.getString (org.apache.ignite.IgniteSystemProperties.getString)6 FieldsQueryCursor (org.apache.ignite.cache.query.FieldsQueryCursor)5 GridQueryFieldsResult (org.apache.ignite.internal.processors.query.GridQueryFieldsResult)5 SQLException (java.sql.SQLException)4 LinkedHashMap (java.util.LinkedHashMap)4 SqlFieldsQueryEx (org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx)4 GridEmptyCloseableIterator (org.apache.ignite.internal.util.GridEmptyCloseableIterator)4 GridCloseableIterator (org.apache.ignite.internal.util.lang.GridCloseableIterator)4 Cache (javax.cache.Cache)3 CacheException (javax.cache.CacheException)3 GridQueryCancel (org.apache.ignite.internal.processors.query.GridQueryCancel)3