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();
}
}
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();
}
}
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();
}
});
}
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();
}
});
}
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);
});
}
Aggregations