use of org.apache.ignite.internal.processors.cache.query.CacheQueryFuture in project ignite by apache.
the class GridCacheFullTextQueryMultithreadedSelfTest method testH2Text.
/**
* JUnit.
*
* @throws Exception In case of error.
*/
@SuppressWarnings({ "TooBroadScope" })
@Test
public void testH2Text() throws Exception {
int duration = 20 * 1000;
final int keyCnt = 5000;
final int logFreq = 50;
final String txt = "Value";
final int limit = 0;
final GridCacheAdapter<Integer, H2TextValue> c = ((IgniteKernal) grid(0)).internalCache(DEFAULT_CACHE_NAME);
IgniteInternalFuture<?> fut1 = multithreadedAsync(new Callable() {
@Override
public Object call() throws Exception {
for (int i = 0; i < keyCnt; i++) {
c.getAndPut(i, new H2TextValue(txt));
if (i % logFreq == 0)
X.println("Stored values: " + i);
}
return null;
}
}, 1);
// Create query.
final CacheQuery<Map.Entry<Integer, H2TextValue>> qry = c.context().queries().createFullTextQuery(H2TextValue.class.getSimpleName(), txt, limit, Query.DFLT_PAGE_SIZE, false);
qry.enableDedup(false);
qry.includeBackups(false);
qry.timeout(TEST_TIMEOUT);
final AtomicBoolean stop = new AtomicBoolean();
IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable() {
@Override
public Object call() throws Exception {
int cnt = 0;
while (!stop.get()) {
CacheQueryFuture<Map.Entry<Integer, H2TextValue>> qryFut = qry.execute();
int size = 0;
while (qryFut.next() != null) size++;
cnt++;
if (cnt % logFreq == 0) {
X.println("Result set: " + size);
X.println("Executed queries: " + cnt);
}
}
return null;
}
}, 1);
Thread.sleep(duration);
fut1.get();
stop.set(true);
fut2.get();
}
use of org.apache.ignite.internal.processors.cache.query.CacheQueryFuture 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.processors.cache.query.CacheQueryFuture 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.processors.cache.query.CacheQueryFuture in project ignite by apache.
the class GridQueryProcessor method executeQuery.
/**
* @param qryType Query type.
* @param qry Query description.
* @param cctx Cache context.
* @param clo Closure.
* @param complete Complete.
*/
public <R> R executeQuery(GridCacheQueryType qryType, String qry, @Nullable GridCacheContext<?, ?> cctx, IgniteOutClosureX<R> clo, boolean complete) throws IgniteCheckedException {
final long startTime = U.currentTimeMillis();
Throwable err = null;
R res = null;
try {
res = clo.apply();
if (res instanceof CacheQueryFuture) {
CacheQueryFuture fut = (CacheQueryFuture) res;
err = fut.error();
}
return res;
} catch (GridClosureException e) {
err = e.unwrap();
throw (IgniteCheckedException) err;
} catch (CacheException | IgniteException e) {
err = e;
throw e;
} catch (Exception e) {
err = e;
throw new IgniteCheckedException(e);
} finally {
boolean failed = err != null;
long duration = U.currentTimeMillis() - startTime;
if (complete || failed) {
if (cctx != null)
cctx.queries().collectMetrics(qryType, qry, startTime, duration, failed);
if (log.isTraceEnabled())
log.trace("Query execution [startTime=" + startTime + ", duration=" + duration + ", fail=" + failed + ", res=" + res + ']');
}
}
}
Aggregations