Search in sources :

Example 71 with QueryCursor

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

the class BaseSqlTest method select.

/**
 * Performs scan query with custom transformer (mapper).
 *
 * @param cache cache to query.
 * @param filter filter for rows.
 * @param transformer result mapper.
 */
@SuppressWarnings("unchecked")
protected static <K, V, R> List<R> select(IgniteCache<K, V> cache, @Nullable IgnitePredicate<Map<String, Object>> filter, IgniteClosure<Map<String, Object>, R> transformer) {
    Collection<QueryEntity> entities = cache.getConfiguration(CacheConfiguration.class).getQueryEntities();
    assert entities.size() == 1 : "Cache should contain exactly one table";
    final QueryEntity meta = entities.iterator().next();
    IgniteClosure<Cache.Entry<K, V>, R> transformerAdapter = entry -> {
        Map<String, Object> row = entryToMap(meta, entry.getKey(), entry.getValue());
        return transformer.apply(row);
    };
    IgniteBiPredicate<K, V> filterAdapter = (filter == null) ? null : (key, val) -> filter.apply(entryToMap(meta, key, val));
    QueryCursor<R> cursor = cache.withKeepBinary().query(new ScanQuery<>(filterAdapter), transformerAdapter);
    return cursor.getAll();
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) Arrays(java.util.Arrays) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) HashMap(java.util.HashMap) Random(java.util.Random) Callable(java.util.concurrent.Callable) IgniteEx(org.apache.ignite.internal.IgniteEx) IgniteBiClosure(org.apache.ignite.lang.IgniteBiClosure) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Map(java.util.Map) Cache(javax.cache.Cache) CacheException(javax.cache.CacheException) QueryEntity(org.apache.ignite.cache.QueryEntity) ExpectedException(org.junit.rules.ExpectedException) F(org.apache.ignite.internal.util.typedef.F) IdentityHashMap(java.util.IdentityHashMap) Iterator(java.util.Iterator) Collection(java.util.Collection) AbstractIndexingCommonTest(org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest) Set(java.util.Set) Test(org.junit.Test) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) Ignition(org.apache.ignite.Ignition) Rule(org.junit.Rule) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Comparator(java.util.Comparator) Collections(java.util.Collections) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) QueryEntity(org.apache.ignite.cache.QueryEntity) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 72 with QueryCursor

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

the class KillCommandsTests method checkScanQueryCancelDuringFetching.

/**
 * Checks cancel of the scan query during fetching.
 *
 * @param cli Client node.
 * @param srvs Server nodes.
 * @param qryCanceler Query cancel closure.
 */
private static void checkScanQueryCancelDuringFetching(IgniteEx cli, List<IgniteEx> srvs, Consumer<T3<UUID, String, Long>> qryCanceler) throws Exception {
    filterLatch = new CountDownLatch(1);
    cancelLatch = new CountDownLatch(1);
    IgniteCache<Object, Object> cache = cli.cache(DEFAULT_CACHE_NAME);
    QueryCursor<Cache.Entry<Object, Object>> qry = cache.query(new ScanQuery<>().setFilter((o, o2) -> {
        try {
            filterLatch.countDown();
            cancelLatch.await(TIMEOUT, TimeUnit.MILLISECONDS);
        } catch (Exception ignored) {
        // No-op.
        }
        return true;
    }));
    IgniteInternalFuture<?> fut = GridTestUtils.runAsync((Runnable) () -> qry.iterator().next());
    assertTrue(filterLatch.await(TIMEOUT, TimeUnit.MILLISECONDS));
    T3<UUID, String, Long> qryInfo = scanQuery(srvs.get(0));
    qryCanceler.accept(qryInfo);
    cancelLatch.countDown();
    assertThrowsAnyCause(null, fut::get, NoSuchElementException.class, "Iterator has been closed.");
    checkScanQueryResources(cli, srvs, qryInfo.get3());
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) KillCommandsSQLTest.execute(org.apache.ignite.util.KillCommandsSQLTest.execute) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) GridCacheQueryManager(org.apache.ignite.internal.processors.cache.query.GridCacheQueryManager) Transaction(org.apache.ignite.transactions.Transaction) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) IgniteEx(org.apache.ignite.internal.IgniteEx) ConcurrentMap(java.util.concurrent.ConcurrentMap) ServiceContext(org.apache.ignite.services.ServiceContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractSchemaSelfTest.queryProcessor(org.apache.ignite.internal.processors.cache.index.AbstractSchemaSelfTest.queryProcessor) BiConsumer(java.util.function.BiConsumer) Cache(javax.cache.Cache) CacheException(javax.cache.CacheException) ServiceConfiguration(org.apache.ignite.services.ServiceConfiguration) NoSuchElementException(java.util.NoSuchElementException) GridTestUtils.assertThrowsAnyCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsAnyCause) IgniteFuture(org.apache.ignite.lang.IgniteFuture) SVCS_VIEW(org.apache.ignite.internal.processors.service.IgniteServiceProcessor.SVCS_VIEW) ServiceView(org.apache.ignite.spi.systemview.view.ServiceView) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) Iterator(java.util.Iterator) SystemView(org.apache.ignite.spi.systemview.view.SystemView) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) Assert.assertTrue(org.junit.Assert.assertTrue) UUID(java.util.UUID) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) SCAN_QRY_SYS_VIEW(org.apache.ignite.internal.managers.systemview.ScanQuerySystemView.SCAN_QRY_SYS_VIEW) IgniteCache(org.apache.ignite.IgniteCache) T3(org.apache.ignite.internal.util.typedef.T3) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) QueryCursor(org.apache.ignite.cache.query.QueryCursor) CU(org.apache.ignite.internal.util.typedef.internal.CU) Service(org.apache.ignite.services.Service) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Assert.assertEquals(org.junit.Assert.assertEquals) ScanQuery(org.apache.ignite.cache.query.ScanQuery) CountDownLatch(java.util.concurrent.CountDownLatch) CacheException(javax.cache.CacheException) NoSuchElementException(java.util.NoSuchElementException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) UUID(java.util.UUID)

Aggregations

QueryCursor (org.apache.ignite.cache.query.QueryCursor)72 IgniteCache (org.apache.ignite.IgniteCache)44 Ignite (org.apache.ignite.Ignite)38 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)36 Cache (javax.cache.Cache)33 Test (org.junit.Test)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 ScanQuery (org.apache.ignite.cache.query.ScanQuery)26 ArrayList (java.util.ArrayList)24 IgniteException (org.apache.ignite.IgniteException)22 List (java.util.List)19 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)19 Map (java.util.Map)17 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)17 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)17 CacheException (javax.cache.CacheException)16 GridTestUtils (org.apache.ignite.testframework.GridTestUtils)16 Collection (java.util.Collection)15 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)15 Transaction (org.apache.ignite.transactions.Transaction)15