/** * @param ignite Ignite. * @param orgId Organization id. * @return Count of found Person object with specified orgId */ private static int getPersonsCountByScanLocalQuery(final IgniteEx ignite, final int orgId) { List res = ignite.cache(Person.class.getSimpleName()) .query(new ScanQuery<>(new IgniteBiPredicate<Person.Key, Person>() { @Override public boolean apply(Person.Key key, Person person) { return person.getOrgId() == orgId; } }).setLocal(true)).getAll(); return res.size(); }
/** * Reads scan query. * * @param reader Binary reader. * @return Query. */ private Query readScanQuery(BinaryRawReaderEx reader) { boolean loc = reader.readBoolean(); final int pageSize = reader.readInt(); boolean hasPart = reader.readBoolean(); Integer part = hasPart ? reader.readInt() : null; ScanQuery qry = new ScanQuery().setPageSize(pageSize); qry.setPartition(part); Object pred = reader.readObjectDetached(); if (pred != null) qry.setFilter(platformCtx.createCacheEntryFilter(pred, 0)); qry.setLocal(loc); return qry; }
qry.setLocal(true); qry.setPartition(part); qry.setFilter(filter);
/** * @throws Exception If failed. */ @Test public void testScanLocalQuery() throws Exception { IgniteCache<Integer, String> cache = grid().getOrCreateCache(String.class.getSimpleName()); try { cache.query(new ScanQuery<>(new IgniteBiPredicate<Integer, String>() { @Override public boolean apply(Integer id, String s) { return s.equals(""); } }).setLocal(true)).getAll(); } catch (IgniteException ignored) { assertTrue("Scan query should work with disable query indexing.", false); } } /**
qry.setLocal(true); qry.setPartition(part); qry.setFilter(filter);
/** * @param node Node. * @param loc Local flag. * @param parts Partitions. */ protected void checkQueryPasses(Ignite node, boolean loc, int... parts) { // Scan queries don't support multiple partitions. if (parts != null && parts.length > 1) return; // TODO Local scan queries fail in non-safe modes - https://issues.apache.org/jira/browse/IGNITE-10059. if (loc) return; IgniteCache cache = node.cache(DEFAULT_CACHE_NAME); ScanQuery qry = new ScanQuery(); if (parts != null && parts.length > 0) qry.setPartition(parts[0]); if (loc) qry.setLocal(true); cache.query(qry).getAll(); }
@Override public void run() throws Exception { try { IgniteCache<Object, Object> cache = jcache(); ClusterNode locNode = testedGrid().cluster().localNode(); Affinity<Object> affinity = testedGrid().affinity(cacheName()); Map<Object, Object> map = new HashMap<>(); for (int i = 0; i < CNT; i++) { Object key = key(i); Object val = value(i); cache.put(key, val); if (!isClientMode() && (cacheMode() == REPLICATED || affinity.isPrimary(locNode, key))) map.put(key, val); } registerEventListeners(map); QueryCursor<Cache.Entry<Object, Object>> q = cache.query(new ScanQuery<>().setLocal(true)); checkQueryResults(map, q); } finally { stopListeners(); } } });
/** * @param partCntrs Expected per-partition entries count. */ private void checkScanPartition(Ignite ignite, IgniteCache<DbKey, DbValue> cache, Map<Integer, Integer> partCntrs, boolean loc) { Affinity<Object> aff = ignite.affinity(cache.getName()); int parts = aff.partitions(); for (int p = 0; p < parts; p++) { ScanQuery<DbKey, DbValue> qry = new ScanQuery<>(); qry.setPartition(p); qry.setLocal(loc); if (loc && !ignite.cluster().localNode().equals(aff.mapPartitionToNode(p))) continue; QueryCursor<Cache.Entry<DbKey, DbValue>> cur = cache.query(qry); Set<DbKey> allKeys = new HashSet<>(); for (Cache.Entry<DbKey, DbValue> e : cur) { allKeys.add(e.getKey()); assertEquals(e.getKey().val, e.getValue().iVal); } Integer exp = partCntrs.get(p); if (exp == null) exp = 0; assertEquals(exp, (Integer)allKeys.size()); } }
qry.setLocal(true);
@Override public void run() throws Exception { try { IgniteCache<Object, Object> cache = jcache(); ClusterNode locNode = testedGrid().cluster().localNode(); Map<Object, Object> map = new HashMap<>(); IgniteBiPredicate<Object, Object> filter = new IgniteBiPredicate<Object, Object>() { @Override public boolean apply(Object k, Object v) { assertNotNull(k); assertNotNull(v); return valueOf(k) >= 20 && valueOf(v) < 40; } }; for (int i = 0; i < CNT; i++) { Object key = key(i); Object val = value(i); cache.put(key, val); if (!isClientMode() && (cacheMode() == REPLICATED || testedGrid().affinity(cacheName()).isPrimary(locNode, key)) && filter.apply(key, val)) map.put(key, val); } registerEventListeners(map, true); QueryCursor<Cache.Entry<Object, Object>> q = cache.query(new ScanQuery<>(filter).setLocal(true)); checkQueryResults(map, q); } finally { stopListeners(); } } });
/** * Scan (with explicit {@code setLocal(true)}, no partition specified) should perform on the local node. * * @throws Exception If failed. */ @Test public void testScanLocalExplicitNoPart() throws Exception { cacheMode = CacheMode.PARTITIONED; backups = 0; commSpiFactory = new TestLocalCommunicationSpiFactory(); try { Ignite ignite = startGrids(GRID_CNT); IgniteCacheProxy<Integer, Integer> cache = fillCache(ignite); QueryCursor<Cache.Entry<Integer, Integer>> qry = cache.query(new ScanQuery<Integer, Integer>().setLocal(true)); assertFalse(qry.getAll().isEmpty()); } finally { stopAllGrids(); } }
/** * @param node Node. * @param loc Local flag. * @param parts Partitions. */ protected void checkQueryFails(Ignite node, boolean loc, int... parts) { if (parts.length == 0) return; IgniteCache cache = node.cache(DEFAULT_CACHE_NAME); String msg = loc ? "forced local query" : "partition has been lost"; GridTestUtils.assertThrows(log, () -> { List res = null; for (int partition : parts) { ScanQuery qry = new ScanQuery(); qry.setPartition(partition); if (loc) qry.setLocal(true); res = cache.query(qry).getAll(); } return res; }, IgniteCheckedException.class, msg); }
/** * @throws Exception If failed. */ private void runQuery() throws Exception { ScanQuery<String, String> scanQuery = new ScanQuery<String, String>() .setLocal(true) .setFilter(new IgniteBiPredicate<String, String>() { @Override public boolean apply(String key, String p) { return key != null && key.isEmpty(); } }); Ignite ignite = ignite(ThreadLocalRandom.current().nextInt(GRID_CNT)); IgniteCache<String, String> example = ignite.cache(CACHE_NAME); for (int partition : ignite.affinity(CACHE_NAME).primaryPartitions(ignite.cluster().localNode())) { scanQuery.setPartition(partition); try (QueryCursor cursor = example.query(scanQuery)) { for (Object p : cursor) { String value = (String) ((Cache.Entry)p).getValue(); assertNotNull(value); } } } }
/** {@inheritDoc} */ @Override public ClientResponse process(ClientConnectionContext ctx) { IgniteCache cache = filterPlatform == FILTER_PLATFORM_JAVA && !isKeepBinary() ? rawCache(ctx) : cache(ctx); ScanQuery qry = new ScanQuery() .setLocal(loc) .setPageSize(pageSize) .setPartition(part) .setFilter(createFilter(ctx)); ctx.incrementCursors(); try { QueryCursor cur = cache.query(qry); ClientCacheEntryQueryCursor cliCur = new ClientCacheEntryQueryCursor(cur, pageSize, ctx); long cursorId = ctx.resources().put(cliCur); cliCur.id(cursorId); return new ClientCacheQueryResponse(requestId(), cliCur); } catch (Exception e) { ctx.decrementCursors(); throw e; } }
/** * Scan (with explicit {@code setLocal(true)}) should perform on the local node. * * @throws Exception If failed. */ @Test public void testScanLocalExplicit() throws Exception { cacheMode = CacheMode.PARTITIONED; backups = 0; commSpiFactory = new TestLocalCommunicationSpiFactory(); try { Ignite ignite = startGrids(GRID_CNT); IgniteCacheProxy<Integer, Integer> cache = fillCache(ignite); int part = anyLocalPartition(cache.context()); QueryCursor<Cache.Entry<Integer, Integer>> qry = cache.query(new ScanQuery<Integer, Integer>().setPartition(part).setLocal(true)); doTestScanQuery(qry, part); GridTestUtils.assertThrows(log, (Callable<Void>)() -> { int remPart = remotePartition(cache.context()).getKey(); cache.query(new ScanQuery<Integer, Integer>().setPartition(remPart).setLocal(true)); return null; }, IgniteCheckedException.class, null); } finally { stopAllGrids(); } }
return ignite.cache("test-cache").query(new ScanQuery<Integer, Value>().setLocal(true), transformer).getAll();
ScanQuery<Object, Object> qry = new ScanQuery<>(filter); qry.setPageSize(arg.getPageSize()); qry.setLocal(arg.isLocal());
qry.setLocal(true);
/** * Execute scan query. * * @param c Cache to scan. * @param arg Job argument with query parameters. * @return Query cursor. */ private QueryCursor<Cache.Entry<Object, Object>> scan(IgniteCache<Object, Object> c, VisorScanQueryTaskArg arg, IgniteBiPredicate<Object, Object> filter) { ScanQuery<Object, Object> qry = new ScanQuery<>(filter); qry.setPageSize(arg.getPageSize()); qry.setLocal(arg.isLocal()); return c.withKeepBinary().query(qry); }
/** * Reads scan query. * * @param reader Binary reader. * @return Query. */ private Query readScanQuery(BinaryRawReaderEx reader) { boolean loc = reader.readBoolean(); final int pageSize = reader.readInt(); boolean hasPart = reader.readBoolean(); Integer part = hasPart ? reader.readInt() : null; ScanQuery qry = new ScanQuery().setPageSize(pageSize); qry.setPartition(part); Object pred = reader.readObjectDetached(); if (pred != null) qry.setFilter(platformCtx.createCacheEntryFilter(pred, 0)); qry.setLocal(loc); return qry; }