/** * @param orig Originator. */ protected void doTestRegionQuery(Ignite orig) { IgniteCache<ClientKey, Client> cl = orig.cache("cl"); for (int regionId = 1; regionId <= PARTS_PER_REGION.length; regionId++) { SqlQuery<ClientKey, Client> qry1 = new SqlQuery<>(Client.class, "regionId=?"); qry1.setArgs(regionId); List<Cache.Entry<ClientKey, Client>> clients1 = cl.query(qry1).getAll(); int expRegionCnt = regionId == 5 ? 0 : PARTS_PER_REGION[regionId - 1] * CLIENTS_PER_PARTITION; assertEquals("Region " + regionId + " count", expRegionCnt, clients1.size()); validateClients(regionId, clients1); // Repeat the same query with partition set condition. List<Integer> range = REGION_TO_PART_MAP.get(regionId); SqlQuery<ClientKey, Client> qry2 = new SqlQuery<>(Client.class, "1=1"); qry2.setPartitions(createRange(range.get(0), range.get(1))); try { List<Cache.Entry<ClientKey, Client>> clients2 = cl.query(qry2).getAll(); assertEquals("Region " + regionId + " count with partition set", expRegionCnt, clients2.size()); // Query must produce only results from single region. validateClients(regionId, clients2); if (regionId == UNMAPPED_REGION) fail(); } catch (CacheException ignored) { if (regionId != UNMAPPED_REGION) fail(); } } }
/** * @param orig Originator. */ protected void doTestPartitionsQuery(Ignite orig) { IgniteCache<ClientKey, Client> cl = orig.cache("cl"); for (int regionId = 1; regionId <= PARTS_PER_REGION.length; regionId++) { log().info("Running test queries for region " + regionId); List<Integer> range = REGION_TO_PART_MAP.get(regionId); int[] parts = createRange(range.get(0), range.get(1)); int off = rnd.nextInt(parts.length); int p1 = parts[off], p2 = parts[(off + (1 + rnd.nextInt(parts.length-1))) % parts.length]; log().info("Parts: " + p1 + " " + p2); SqlQuery<ClientKey, Client> qry = new SqlQuery<>(Client.class, "1=1"); qry.setPartitions(p1, p2); try { List<Cache.Entry<ClientKey, Client>> clients = cl.query(qry).getAll(); // Query must produce only results from two partitions. for (Cache.Entry<ClientKey, Client> client : clients) { int p = orig.affinity("cl").partition(client.getKey()); assertTrue("Incorrect partition for key", p == p1 || p == p2); } if (regionId == UNMAPPED_REGION) fail(); } catch (CacheException ignored) { if (regionId != UNMAPPED_REGION) fail(); } } }
/** Tests local query over partitions. */ @Test public void testLocalQuery() { Affinity<Object> affinity = grid(0).affinity("cl"); int[] parts = affinity.primaryPartitions(grid(0).localNode()); Arrays.sort(parts); IgniteCache<ClientKey, Client> cl = grid(0).cache("cl"); SqlQuery<ClientKey, Client> qry1 = new SqlQuery<>(Client.class, "1=1"); qry1.setLocal(true); qry1.setPartitions(parts[0]); List<Cache.Entry<ClientKey, Client>> clients = cl.query(qry1).getAll(); for (Cache.Entry<ClientKey, Client> client : clients) assertEquals("Incorrect partition", parts[0], affinity.partition(client.getKey())); SqlFieldsQuery qry2 = new SqlFieldsQuery("select cl._KEY, cl._VAL from \"cl\".Client cl"); qry2.setLocal(true); qry2.setPartitions(parts[0]); List<List<?>> rows = cl.query(qry2).getAll(); for (List<?> row : rows) assertEquals("Incorrect partition", parts[0], affinity.partition(row.get(0))); } }