Java Code Examples for org.apache.ignite.cache.query.QueryCursor#iterator()

The following examples show how to use org.apache.ignite.cache.query.QueryCursor#iterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UpdatePlan.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param cur Query cursor.
 * @param plan Update plan.
 * @param op Operation.
 */
private AbstractIterator(QueryCursor<List<?>> cur, UpdatePlan plan,
    EnlistOperation op) {
    this.cur = cur;
    this.plan = plan;
    this.op = op;

    it = cur.iterator();
}
 
Example 2
Source File: IgniteCacheLocalQueryCancelOrTimeoutSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Tests cancellation.
 */
private void testQuery(boolean timeout, int timeoutUnits, TimeUnit timeUnit) {
    Ignite ignite = grid(0);

    IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);

    loadCache(cache);

    SqlFieldsQuery qry = new SqlFieldsQuery(QUERY);

    final QueryCursor<List<?>> cursor;
    if (timeout) {
        qry.setTimeout(timeoutUnits, timeUnit);

        cursor = cache.query(qry);
    } else {
        cursor = cache.query(qry);

        ignite.scheduler().runLocal(new Runnable() {
            @Override public void run() {
                cursor.close();
            }
        }, timeoutUnits, timeUnit);
    }

    try (QueryCursor<List<?>> ignored = cursor) {
        cursor.iterator();

        fail("Expecting timeout");
    }
    catch (Exception e) {
        assertNotNull("Must throw correct exception", X.cause(e, QueryCancelledException.class));
    }

    // Test must exit gracefully.
}
 
Example 3
Source File: IgniteCacheLocalQueryDefaultTimeoutSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Tests cancellation.
 */
private void testQuery(boolean timeout, int timeoutUnits, TimeUnit timeUnit) {
    Ignite ignite = grid(0);

    IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);

    loadCache(cache);

    SqlFieldsQuery qry = new SqlFieldsQuery(QUERY);

    final QueryCursor<List<?>> cursor;
    if (timeout) {
        qry.setTimeout(timeoutUnits, timeUnit);

        cursor = cache.query(qry);
    }
    else {
        cursor = cache.query(qry);

        ignite.scheduler().runLocal(new Runnable() {
            @Override public void run() {
                cursor.close();
            }
        }, timeoutUnits, timeUnit);
    }

    try (QueryCursor<List<?>> ignored = cursor) {
        cursor.iterator();

        fail("Expecting timeout");
    }
    catch (Exception e) {
        assertNotNull("Must throw correct exception", X.cause(e, QueryCancelledException.class));
    }

    // Test must exit gracefully.
}
 
Example 4
Source File: IgniteCacheConfigVariationsQueryTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param expMap Expected map.
 * @param cursor Query cursor.
 */
private void checkQueryResults(Map<Object, Object> expMap, QueryCursor<Cache.Entry<Object, Object>> cursor)
    throws InterruptedException {
    Iterator<Cache.Entry<Object, Object>> iter = cursor.iterator();

    try {
        assertNotNull(iter);

        int cnt = 0;

        while (iter.hasNext()) {
            Cache.Entry<Object, Object> e = iter.next();

            assertNotNull(e.getKey());
            assertNotNull(e.getValue());

            Object expVal = expMap.get(e.getKey());

            assertNotNull("Failed to resolve expected value for key: " + e.getKey(), expVal);

            assertEquals(expVal, e.getValue());

            cnt++;
        }

        assertEquals(expMap.size(), cnt);
    }
    finally {
        cursor.close();
    }

    checkEvents();
}
 
Example 5
Source File: IgniteCacheUpdateSqlQuerySelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 *
 */
@Test
public void testUpdateSingle() {
    boolean oldAllowColumnsVal = GridTestUtils.getFieldValue(UpdatePlanBuilder.class, UpdatePlanBuilder.class,
        "ALLOW_KEY_VAL_UPDATES");

    GridTestUtils.setFieldValue(UpdatePlanBuilder.class, "ALLOW_KEY_VAL_UPDATES", true);

    try {
        IgniteCache p = cache();

        QueryCursor<List<?>> c = p.query(new SqlFieldsQuery("update Person p set _val = ? where _key = ?")
            .setArgs(createPerson(2, "Jo", "White"), "FirstKey"));

        c.iterator();

        c = p.query(new SqlFieldsQuery("select _key, _val, * from Person order by id, _key"));

        List<List<?>> leftovers = c.getAll();

        assertEquals(4, leftovers.size());

        assertEqualsCollections(asList("FirstKey", createPerson(2, "Jo", "White"), 2, "Jo", "White"),
            leftovers.get(0));

        assertEqualsCollections(asList("SecondKey", createPerson(2, "Joe", "Black"), 2, "Joe", "Black"),
            leftovers.get(1));

        assertEqualsCollections(asList("k3", createPerson(3, "Sylvia", "Green"), 3, "Sylvia", "Green"),
            leftovers.get(2));

        assertEqualsCollections(asList("f0u4thk3y", createPerson(4, "Jane", "Silver"), 4, "Jane", "Silver"),
            leftovers.get(3));
    }
    finally {
        GridTestUtils.setFieldValue(UpdatePlanBuilder.class, "ALLOW_KEY_VAL_UPDATES", oldAllowColumnsVal);
    }
}
 
Example 6
Source File: H2ResultSetIteratorNullifyOnEndSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Local SQL Fields check nullification after close
 */
@Test
public void testSqlFieldsQueryLocalClose() {
    SqlFieldsQuery qry = new SqlFieldsQuery(SELECT_MAX_SAL_SQLF);

    qry.setLocal(true);

    QueryCursor<List<?>> qryCurs = cache().query(qry);

    qryCurs.iterator();

    qryCurs.close();

    H2ResultSetIterator h2It = extractGridIteratorInnerH2ResultSetIterator(qryCurs);

    checkIterator(h2It);
}
 
Example 7
Source File: IgniteCacheDeleteSqlQuerySelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 *
 */
@Test
public void testDeleteSimple() {
    IgniteCache p = cache();

    QueryCursor<List<?>> c = p.query(new SqlFieldsQuery("delete from Person p where length(p._key) = 2 " +
        "or p.secondName like '%ite'"));

    c.iterator();

    c = p.query(new SqlFieldsQuery("select _key, _val, * from Person order by id"));

    List<List<?>> leftovers = c.getAll();

    assertEquals(2, leftovers.size());

    assertEqualsCollections(Arrays.asList("SecondKey", createPerson(2, "Joe", "Black"), 2, "Joe", "Black"),
        leftovers.get(0));

    assertEqualsCollections(Arrays.asList("f0u4thk3y", createPerson(4, "Jane", "Silver"), 4, "Jane", "Silver"),
        leftovers.get(1));
}
 
Example 8
Source File: IgniteCacheDeleteSqlQuerySelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 *
 */
@Test
public void testDeleteSingle() {
    IgniteCache p = cache();

    QueryCursor<List<?>> c = p.query(new SqlFieldsQuery("delete from Person where _key = ?")
        .setArgs("FirstKey"));

    c.iterator();

    c = p.query(new SqlFieldsQuery("select _key, _val, * from Person order by id, _key"));

    List<List<?>> leftovers = c.getAll();

    assertEquals(3, leftovers.size());

    assertEqualsCollections(Arrays.asList("SecondKey", createPerson(2, "Joe", "Black"), 2, "Joe", "Black"),
        leftovers.get(0));

    assertEqualsCollections(Arrays.asList("k3", createPerson(3, "Sylvia", "Green"), 3, "Sylvia", "Green"),
        leftovers.get(1));

    assertEqualsCollections(Arrays.asList("f0u4thk3y", createPerson(4, "Jane", "Silver"), 4, "Jane", "Silver"),
        leftovers.get(2));
}
 
Example 9
Source File: IgniteCacheDeleteSqlQuerySelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * In binary mode, this test checks that inner forcing of keepBinary works - without it, EntryProcessors
 * inside DML engine would compare binary and non-binary objects with the same keys and thus fail.
 */
@Test
public void testDeleteSimpleWithoutKeepBinary() {
    IgniteCache p = ignite(0).cache("S2P");

    QueryCursor<List<?>> c = p.query(new SqlFieldsQuery("delete from Person p where length(p._key) = 2 " +
        "or p.secondName like '%ite'"));

    c.iterator();

    c = p.query(new SqlFieldsQuery("select _key, _val, * from Person order by id"));

    List<List<?>> leftovers = c.getAll();

    assertEquals(2, leftovers.size());

    assertEqualsCollections(Arrays.asList("SecondKey", new Person(2, "Joe", "Black"), 2, "Joe", "Black"),
        leftovers.get(0));

    assertEqualsCollections(Arrays.asList("f0u4thk3y", new Person(4, "Jane", "Silver"), 4, "Jane", "Silver"),
        leftovers.get(1));
}
 
Example 10
Source File: KillCommandsTests.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Test cancel of the scan query.
 *
 * @param cli Client node.
 * @param srvs Server nodes.
 * @param qryCanceler Query cancel closure.
 */
public static void doTestScanQueryCancel(IgniteEx cli, List<IgniteEx> srvs, Consumer<T3<UUID, String, Long>> qryCanceler) {
    IgniteCache<Object, Object> cache = cli.cache(DEFAULT_CACHE_NAME);

    QueryCursor<Cache.Entry<Object, Object>> qry1 = cache.query(new ScanQuery<>().setPageSize(PAGE_SZ));
    Iterator<Cache.Entry<Object, Object>> iter1 = qry1.iterator();

    // Fetch first entry and therefore caching first page.
    assertNotNull(iter1.next());

    List<List<?>> scanQries0 = execute(srvs.get(0),
        "SELECT ORIGIN_NODE_ID, CACHE_NAME, QUERY_ID FROM SYS.SCAN_QUERIES");

    assertEquals(1, scanQries0.size());

    UUID originNodeId = (UUID)scanQries0.get(0).get(0);
    String cacheName = (String)scanQries0.get(0).get(1);
    long qryId = (Long)scanQries0.get(0).get(2);

    // Opens second query.
    QueryCursor<Cache.Entry<Object, Object>> qry2 = cache.query(new ScanQuery<>().setPageSize(PAGE_SZ));
    Iterator<Cache.Entry<Object, Object>> iter2 = qry2.iterator();

    // Fetch first entry and therefore caching first page.
    assertNotNull(iter2.next());

    // Cancel first query.
    qryCanceler.accept(new T3<>(originNodeId, cacheName, qryId));

    // Fetch all cached entries. It's size equal to the {@code PAGE_SZ * NODES_CNT}.
    for (int i = 0; i < PAGE_SZ * srvs.size() - 1; i++)
        assertNotNull(iter1.next());

    // Fetch of the next page should throw the exception.
    assertThrowsWithCause(iter1::next, IgniteCheckedException.class);

    // Checking that second query works fine after canceling first.
    for (int i = 0; i < PAGE_SZ * PAGE_SZ - 1; i++)
        assertNotNull(iter2.next());

    // Checking all server node objects cleared after cancel.
    for (int i = 0; i < srvs.size(); i++) {
        IgniteEx ignite = srvs.get(i);

        int cacheId = CU.cacheId(DEFAULT_CACHE_NAME);

        GridCacheContext<?, ?> ctx = ignite.context().cache().context().cacheContext(cacheId);

        ConcurrentMap<UUID, ? extends GridCacheQueryManager<?, ?>.RequestFutureMap> qryIters =
            ctx.queries().queryIterators();

        assertTrue(qryIters.size() <= 1);

        if (qryIters.isEmpty())
            return;

        GridCacheQueryManager<?, ?>.RequestFutureMap futs = qryIters.get(cli.localNode().id());

        assertNotNull(futs);
        assertFalse(futs.containsKey(qryId));
    }
}
 
Example 11
Source File: GridCommandHandlerIndexingUtils.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Deleting a rows from the cache without updating indexes.
 *
 * @param log Logger.
 * @param internalCache Cache.
 * @param partId Partition number.
 * @param filter Entry filter.
 */
static <K, V> void breakCacheDataTree(
    IgniteLogger log,
    IgniteInternalCache<K, V> internalCache,
    int partId,
    @Nullable BiPredicate<Integer, Entry<K, V>> filter
) {
    requireNonNull(log);
    requireNonNull(internalCache);

    GridCacheContext<K, V> cacheCtx = internalCache.context();

    GridDhtLocalPartition dhtLocPart = cacheCtx.dht().topology().localPartition(partId);

    CacheDataStore cacheDataStore = cacheCtx.group().offheap().dataStore(dhtLocPart);

    String delegate = "delegate";
    if (hasField(cacheDataStore, delegate))
        cacheDataStore = field(cacheDataStore, delegate);

    CacheDataRowStore cacheDataRowStore = field(cacheDataStore, "rowStore");
    CacheDataTree cacheDataTree = field(cacheDataStore, "dataTree");

    String cacheName = internalCache.name();

    QueryCursor<Entry<K, V>> qryCursor = cacheCtx.kernalContext().grid().cache(cacheName).withKeepBinary()
        .query(new ScanQuery<>(partId));

    Iterator<Entry<K, V>> cacheEntryIter = qryCursor.iterator();

    IgniteCacheDatabaseSharedManager db = cacheCtx.shared().database();
    int cacheId = CU.cacheId(cacheName);
    int i = 0;

    while (cacheEntryIter.hasNext()) {
        Entry<K, V> entry = cacheEntryIter.next();

        if (nonNull(filter) && !filter.test(i++, entry))
            continue;

        db.checkpointReadLock();

        try {
            CacheDataRow oldRow = cacheDataTree.remove(
                new SearchRow(cacheId, cacheCtx.toCacheKeyObject(entry.getKey()))
            );

            if (nonNull(oldRow))
                cacheDataRowStore.removeRow(oldRow.link(), INSTANCE);
        }
        catch (IgniteCheckedException e) {
            throw new IgniteException("Failed to remove key skipping indexes: " + entry, e);
        }
        finally {
            db.checkpointReadUnlock();
        }
    }
}
 
Example 12
Source File: TextQueryExample.java    From ignite-book-code-samples with GNU General Public License v3.0 3 votes vote down vote up
private static void scanQuery() {
    IgniteCache<Long, Company> companyCache = Ignition.ignite().cache(COMPANY_CACHE_NAME);

    //  Query for all companies which the city 'NEW YORK' - NewYork.
    QueryCursor cursor = companyCache.query(new ScanQuery<Long, Company>((k, p) -> p.getCity().equalsIgnoreCase("NEW YORK") ));

    for (Iterator ite = cursor.iterator(); ite.hasNext(); ) {
        CacheEntryImpl company = (CacheEntryImpl) ite.next();

        log(((Company) company.getValue()).getCompanyName());
    }

    cursor.close();

}
 
Example 13
Source File: H2ResultSetIteratorNullifyOnEndSelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * Non local SQL Fields check nullification after close
 */
@Test
public void testSqlFieldsQueryClose() {
    SqlFieldsQuery qry = new SqlFieldsQuery(SELECT_MAX_SAL_SQLF);

    QueryCursor<List<?>> qryCurs = cache().query(qry);

    qryCurs.iterator();

    qryCurs.close();

    H2ResultSetIterator h2It = extractGridIteratorInnerH2ResultSetIterator(qryCurs);

    checkIterator(h2It);
}
 
Example 14
Source File: IgniteCacheUpdateSqlQuerySelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 *
 */
@Test
public void testUpdateSimple() {
    IgniteCache p = cache();

    QueryCursor<List<?>> c = p.query(new SqlFieldsQuery("update Person p set p.id = p.id * 2, p.firstName = " +
        "substring(p.firstName, 0, 2) where length(p._key) = ? or p.secondName like ?").setArgs(2, "%ite"));

    c.iterator();

    c = p.query(new SqlFieldsQuery("select _key, _val, * from Person order by _key, id"));

    List<List<?>> leftovers = c.getAll();

    assertEquals(4, leftovers.size());

    assertEqualsCollections(asList("FirstKey", createPerson(2, "Jo", "White"), 2, "Jo", "White"),
        leftovers.get(0));

    assertEqualsCollections(asList("SecondKey", createPerson(2, "Joe", "Black"), 2, "Joe", "Black"),
        leftovers.get(1));

    assertEqualsCollections(asList("f0u4thk3y", createPerson(4, "Jane", "Silver"), 4, "Jane", "Silver"),
        leftovers.get(2));

    assertEqualsCollections(asList("k3", createPerson(6, "Sy", "Green"), 6, "Sy", "Green"),
        leftovers.get(3));
}