use of org.apache.geode.cache.query.functional.StructSetOrResultsSet in project geode by apache.
the class IndexHintJUnitTest method testMultiIndexWithSingleIndexHint.
// given a choice between two indexes, we hint on each one in different queries and verify
// that both indexes are used
@Test
public void testMultiIndexWithSingleIndexHint() throws Exception {
createRegion();
populateData(1000);
// create index
createIndex("IDIndex", "p.ID", "/Portfolios p");
createIndex("SecIndex", "p.status", "/Portfolios p");
// set observer
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
// execute query
SelectResults[][] results = new SelectResults[1][2];
QueryService qs = CacheUtils.getQueryService();
Query query = qs.newQuery("<hint 'IDIndex'>select * from /Portfolios p where p.ID > 10 and p.status = 'inactive'");
results[0][0] = (SelectResults) query.execute();
// verify index usage
assertTrue(observer.wasIndexUsed("IDIndex"));
observer.reset();
query = qs.newQuery("<hint 'SecIndex'>select * from /Portfolios p where p.ID > 10 and p.status = 'inactive'");
results[0][1] = (SelectResults) query.execute();
// verify index usage
assertTrue(observer.wasIndexUsed("SecIndex"));
StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
assertEquals(495, results[0][1].size());
// Not really with and without index but we can use this method to verify they are the same
// results
// regardless of which index used
ssOrrs.CompareQueryResultsWithoutAndWithIndexes(results, 1, new String[] { "<query with hints>" });
}
use of org.apache.geode.cache.query.functional.StructSetOrResultsSet in project geode by apache.
the class IndexHintJUnitTest method testMultiIndexWithMultiIndexHint.
// Using junction, we will hint and make sure multi index hints are functioning
@Test
public void testMultiIndexWithMultiIndexHint() throws Exception {
createRegion();
populateData(1000);
// create index
createIndex("IDIndex", "p.ID", "/Portfolios p");
createIndex("SecIndex", "p.status", "/Portfolios p");
// set observer
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
// execute query
SelectResults[][] results = new SelectResults[1][2];
QueryService qs = CacheUtils.getQueryService();
Query query = qs.newQuery("<hint 'IDIndex', 'SecIndex'>select * from /Portfolios p where p.ID > 10 and p.ID < 200 and p.status = 'inactive'");
results[0][0] = (SelectResults) query.execute();
// verify index usage
assertTrue(observer.wasIndexUsed("IDIndex"));
assertTrue(observer.wasIndexUsed("SecIndex"));
observer.reset();
query = qs.newQuery("<hint 'IDIndex'>select * from /Portfolios p where p.ID > 10 and p.ID < 200 and p.status = 'inactive'");
results[0][1] = (SelectResults) query.execute();
// verify index usage
assertTrue(observer.wasIndexUsed("IDIndex"));
StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
assertEquals(95, results[0][1].size());
// Not really with and without index but we can use this method to verify they are the same
// results
// regardless of which index used
ssOrrs.CompareQueryResultsWithoutAndWithIndexes(results, 1, new String[] { "<query with hints>" });
}
use of org.apache.geode.cache.query.functional.StructSetOrResultsSet in project geode by apache.
the class IndexHintJUnitTest method testMultiIndexWithSingleIndexHintWithRangeJunction.
// Using junction, we will hint and make sure single index hints are functioning
@Test
public void testMultiIndexWithSingleIndexHintWithRangeJunction() throws Exception {
createRegion();
populateData(1000);
// create index
createIndex("IDIndex", "p.ID", "/Portfolios p");
createIndex("SecIndex", "p.status", "/Portfolios p");
// set observer
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
// execute query
SelectResults[][] results = new SelectResults[1][2];
QueryService qs = CacheUtils.getQueryService();
Query query = qs.newQuery("<hint 'IDIndex'>select * from /Portfolios p where p.ID > 10 and p.ID < 200 and p.status = 'inactive'");
results[0][0] = (SelectResults) query.execute();
// verify index usage
assertTrue(observer.wasIndexUsed("IDIndex"));
observer.reset();
query = qs.newQuery("<hint 'SecIndex'>select * from /Portfolios p where p.ID > 10 and p.ID < 200 and p.status = 'inactive'");
results[0][1] = (SelectResults) query.execute();
// verify index usage
assertTrue(observer.wasIndexUsed("SecIndex"));
StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
assertEquals(95, results[0][1].size());
// Not really with and without index but we can use this method to verify they are the same
// results
// regardless of which index used
ssOrrs.CompareQueryResultsWithoutAndWithIndexes(results, 1, new String[] { "<query with hints>" });
}
use of org.apache.geode.cache.query.functional.StructSetOrResultsSet in project geode by apache.
the class PdxLocalQueryDUnitTest method testLocalPdxQueriesOnPR.
@Test
public void testLocalPdxQueriesOnPR() throws Exception {
final Host host = Host.getHost(0);
final VM server1 = host.getVM(0);
final VM server2 = host.getVM(1);
final VM client = host.getVM(2);
final int numberOfEntries = 10;
final String name = "/" + regionName;
final String[] queries = { "select * from " + name + " where position1 = $1", "select * from " + name + " where aDay = $1", // numberOfEntries
"select distinct * from " + name + " p where p.status = 'inactive'", // 1
"select distinct p.status from " + name + " p where p.status = 'inactive'", // numberOfEntries
"select p from " + name + " p where p.status = 'inactive'", // 4
"select * from " + name + " p, p.positions.values v where v.secId = 'IBM'", // 4
"select v from " + name + " p, p.positions.values v where v.secId = 'IBM'", // numberOfEntries
"select p.status from " + name + " p where p.status = 'inactive'", // numberOfEntries
"select distinct * from " + name + " p where p.status = 'inactive' order by p.ID", // 19
"select * from " + name + " p where p.status = 'inactive' or p.ID > 0", // numberOfEntries
"select * from " + name + " p where p.status = 'inactive' and p.ID >= 0", // numberOfEntries*2
"select * from " + name + " p where p.status in set ('inactive', 'active')", // 9
"select * from " + name + " p where p.ID > 0 and p.ID < 10", // numberOfEntries*2
"select v from " + name + " p, p.positions.values v where p.status = 'inactive'", // numberOfEntries*2
"select v.secId from " + name + " p, p.positions.values v where p.status = 'inactive'", "select distinct p from " + name + // numberOfEntries
" p, p.positions.values v where p.status = 'inactive' and v.pid >= 0", "select distinct p from " + name + // numberOfEntries*2
" p, p.positions.values v where p.status = 'inactive' or v.pid > 0", // numberOfEntries*2
"select distinct * from " + name + " p, p.positions.values v where p.status = 'inactive'", // numberOfEntries
"select * from " + name + ".values v where v.status = 'inactive'", // 19
"select v from " + name + " v where v in (select p from " + name + " p where p.ID > 0)", "select v from " + name + " v where v.status in (select distinct p.status from " + name + // numberOfEntries
" p where p.status = 'inactive')", "select * from " + name + " v where v.status = ELEMENT (select distinct p.status from " + name + // numberOfEntries
" p where p.status = 'inactive')" };
final int[] results = { 2, 3, numberOfEntries, 1, numberOfEntries, 4, 4, numberOfEntries, numberOfEntries, 19, numberOfEntries, numberOfEntries * 2, 9, numberOfEntries * 2, numberOfEntries * 2, numberOfEntries, numberOfEntries * 2, numberOfEntries * 2, numberOfEntries, 19, numberOfEntries, numberOfEntries };
// Start server1
final int port1 = (Integer) server1.invoke(new SerializableCallable("Create Server1") {
@Override
public Object call() throws Exception {
Region r1 = getCache().createRegionFactory(RegionShortcut.PARTITION).create(regionName);
for (int i = 0; i < numberOfEntries; i++) {
r1.put("key-" + i, new PortfolioPdx(i));
}
CacheServer server = getCache().addCacheServer();
int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
server.setPort(port);
server.start();
return port;
}
});
// Start server2
final int port2 = (Integer) server2.invoke(new SerializableCallable("Create Server2") {
@Override
public Object call() throws Exception {
Region r1 = getCache().createRegionFactory(RegionShortcut.PARTITION).create(regionName);
CacheServer server = getCache().addCacheServer();
int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
server.setPort(port);
server.start();
return port;
}
});
// client loads pdx objects on server
client.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
ClientCacheFactory cf = new ClientCacheFactory();
cf.addPoolServer(NetworkUtils.getServerHostName(server1.getHost()), port1);
ClientCache cache = getClientCache(cf);
Region region = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);
for (int i = numberOfEntries; i < numberOfEntries * 2; i++) {
region.put("key-" + i, new PortfolioPdx(i));
}
QueryService qs = null;
SelectResults sr = null;
// Execute query remotely
try {
qs = cache.getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
PositionPdx pos = new PositionPdx("IBM", 100);
PortfolioPdx.Day pDay = new PortfolioPdx(1).aDay;
for (int i = 0; i < queries.length; i++) {
try {
if (i == 0) {
sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pos });
} else if (i == 1) {
sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pDay });
} else {
sr = (SelectResults) qs.newQuery(queries[i]).execute();
}
assertTrue("Size of resultset should be greater than 0 for query: " + queries[i], sr.size() > 0);
assertEquals("Expected and actual results do not match for query: " + queries[i], results[i], sr.size());
for (Object result : sr) {
if (result instanceof Struct) {
Object[] r = ((Struct) result).getFieldValues();
for (int j = 0; j < r.length; j++) {
if (r[j] instanceof PdxInstance || r[j] instanceof PdxString) {
fail("Result object should be a domain object and not an instance of " + r[j].getClass() + " for query: " + queries[i]);
}
}
} else if (result instanceof PdxInstance || result instanceof PdxString) {
fail("Result object should be a domain object and not an instance of " + result.getClass() + " for query: " + queries[i]);
}
}
} catch (Exception e) {
Assert.fail("Failed executing query " + queries[i], e);
}
}
return null;
}
});
// query locally on server1
server1.invoke(new SerializableCallable("query locally on server1") {
@Override
public Object call() throws Exception {
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
QueryService qs = null;
SelectResults sr = null;
// Execute query locally
try {
qs = getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
PositionPdx pos = new PositionPdx("IBM", 100);
PortfolioPdx.Day pDay = new PortfolioPdx(1).aDay;
for (int i = 0; i < queries.length; i++) {
try {
if (i == 0) {
sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pos });
} else if (i == 1) {
sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pDay });
} else {
sr = (SelectResults) qs.newQuery(queries[i]).execute();
}
assertTrue("Size of resultset should be greater than 0 for query: " + queries[i], sr.size() > 0);
assertEquals("Expected and actual results do not match for query: " + queries[i], results[i], sr.size());
for (Object result : sr) {
if (result instanceof Struct) {
Object[] r = ((Struct) result).getFieldValues();
for (int j = 0; j < r.length; j++) {
if (r[j] instanceof PdxInstance || r[j] instanceof PdxString) {
fail("Result object should be a domain object and not an instance of " + r[j].getClass() + " for query: " + queries[i]);
}
}
} else if (result instanceof PdxInstance || result instanceof PdxString) {
fail("Result object should be a domain object and not an instance of " + result.getClass() + " for query: " + queries[i]);
}
}
} catch (Exception e) {
Assert.fail("Failed executing query " + queries[i], e);
}
}
return null;
}
});
// query locally on server2
server2.invoke(new SerializableCallable("query locally on server2") {
@Override
public Object call() throws Exception {
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
QueryService qs = null;
SelectResults[][] sr = new SelectResults[queries.length][2];
// Execute query locally
try {
qs = getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
PositionPdx pos = new PositionPdx("IBM", 100);
PortfolioPdx.Day pDay = new PortfolioPdx(1).aDay;
for (int i = 0; i < queries.length; i++) {
try {
if (i == 0) {
sr[i][0] = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pos });
} else if (i == 1) {
sr[i][0] = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pDay });
} else {
sr[i][0] = (SelectResults) qs.newQuery(queries[i]).execute();
}
assertTrue("Size of resultset should be greater than 0 for query: " + queries[i], sr[i][0].size() > 0);
assertEquals("Expected and actual results do not match for query: " + queries[i], results[i], sr[i][0].size());
for (Object result : sr[i][0]) {
if (result instanceof Struct) {
Object[] r = ((Struct) result).getFieldValues();
for (int j = 0; j < r.length; j++) {
if (r[j] instanceof PdxInstance || r[j] instanceof PdxString) {
fail("Result object should be a domain object and not an instance of " + r[j].getClass() + " for query: " + queries[i]);
}
}
} else if (result instanceof PdxInstance || result instanceof PdxString) {
fail("Result object should be a domain object and not an instance of " + result.getClass() + " for query: " + queries[i]);
}
}
} catch (Exception e) {
Assert.fail("Failed executing query " + queries[i], e);
}
}
// create index
qs.createIndex("statusIndex", "p.status", name + " p");
qs.createIndex("IDIndex", "ID", name);
qs.createIndex("pIdIndex", "pos.getPid()", name + " p, p.positions.values pos");
qs.createIndex("secIdIndex", "pos.secId", name + " p, p.positions.values pos");
for (int i = 0; i < queries.length; i++) {
try {
if (i == 0) {
sr[i][1] = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pos });
} else if (i == 1) {
sr[i][1] = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pDay });
} else {
sr[i][1] = (SelectResults) qs.newQuery(queries[i]).execute();
}
assertTrue("Size of resultset should be greater than 0 for query: " + queries[i], sr[i][1].size() > 0);
assertEquals("Expected and actual results do not match for query: " + queries[i], results[i], sr[i][1].size());
for (Object result : sr[i][1]) {
if (result instanceof Struct) {
Object[] r = ((Struct) result).getFieldValues();
for (int j = 0; j < r.length; j++) {
if (r[j] instanceof PdxInstance || r[j] instanceof PdxString) {
fail("Result object should be a domain object and not an instance of " + r[j].getClass() + " for query: " + queries[i]);
}
}
} else if (result instanceof PdxInstance || result instanceof PdxString) {
fail("Result object should be a domain object and not an instance of " + result.getClass() + " for query: " + queries[i]);
}
}
} catch (Exception e) {
Assert.fail("Failed executing query " + queries[i], e);
}
}
StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
ssOrrs.CompareQueryResultsWithoutAndWithIndexes(sr, queries.length, queries);
return null;
}
});
this.closeClient(client);
this.closeClient(server1);
this.closeClient(server2);
}
use of org.apache.geode.cache.query.functional.StructSetOrResultsSet in project geode by apache.
the class QueryIndexDUnitTest method createIndexOnOverflowRegionsWithMultipleWhereClauseAndValidateResults.
@Test
public void createIndexOnOverflowRegionsWithMultipleWhereClauseAndValidateResults() throws Exception {
Host host = Host.getHost(0);
VM[] vms = new VM[] { host.getVM(0), host.getVM(1) };
// Create and load regions on all vms.
for (int i = 0; i < vms.length; i++) {
int finalI = i;
vms[i].invoke(() -> QueryIndexDUnitTest.createAndLoadOverFlowRegions("testOfValid2" + "vm" + finalI, new Boolean(true), new Boolean(false)));
}
vms[0].invoke(new CacheSerializableRunnable("Execute query validate results") {
public void run2() throws CacheException {
Cache cache = basicGetCache();
String[] regionNames = new String[] { "replicateOverFlowRegion", "replicatePersistentOverFlowRegion", "prOverFlowRegion", "prPersistentOverFlowRegion" };
QueryService qs = cache.getQueryService();
Region region = null;
int numObjects = 10;
// The index should get updated accordingly.
for (int i = 0; i < regionNames.length; i++) {
region = cache.getRegion(regionNames[i]);
for (int cnt = 1; cnt < numObjects; cnt++) {
region.put(new Portfolio(cnt), new String("XX" + cnt));
}
}
String[] qString = new String[] { "SELECT * FROM /REGION_NAME pf WHERE pf = 'XX1'", "SELECT * FROM /REGION_NAME pf WHERE pf IN SET( 'XX5', 'XX6', 'XX7')", "SELECT * FROM /REGION_NAME.values pf WHERE pf IN SET( 'XX5', 'XX6', 'XX7')", "SELECT * FROM /REGION_NAME.keys k WHERE k.ID = 1", "SELECT key.ID FROM /REGION_NAME.keys key WHERE key.ID = 1", "SELECT ID, status FROM /REGION_NAME.keys WHERE ID = 1", "SELECT k.ID, k.status FROM /REGION_NAME.keys k WHERE k.ID = 1 and k.status = 'active'", "SELECT * FROM /REGION_NAME.keys key WHERE key.ID > 5", "SELECT key.ID FROM /REGION_NAME.keys key WHERE key.ID > 5 and key.status = 'active'" };
// Execute Query without index.
SelectResults[] srWithoutIndex = new SelectResults[qString.length * regionNames.length];
String[] queryString = new String[qString.length * regionNames.length];
int r = 0;
try {
for (int q = 0; q < qString.length; q++) {
for (int i = 0; i < regionNames.length; i++) {
String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
Query query = qs.newQuery(queryStr);
queryString[r] = queryStr;
srWithoutIndex[r] = (SelectResults) query.execute();
r++;
}
}
} catch (Exception ex) {
logger.info("Failed to Execute query", ex);
fail("Failed to Execute query.");
}
// Create index.
String indexName = "";
try {
for (int i = 0; i < regionNames.length; i++) {
region = cache.getRegion(regionNames[i]);
indexName = "idIndex" + regionNames[i];
cache.getLogger().fine("createIndexOnOverFlowRegions() checking for index: " + indexName);
try {
if (qs.getIndex(region, indexName) == null) {
cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
Index i1 = qs.createIndex(indexName, "pf", "/" + regionNames[i] + " pf");
}
indexName = "valueIndex" + regionNames[i];
if (qs.getIndex(region, indexName) == null) {
cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
Index i1 = qs.createIndex(indexName, "pf", "/" + regionNames[i] + ".values pf");
}
indexName = "keyIdIndex" + regionNames[i];
if (qs.getIndex(region, indexName) == null) {
cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
Index i2 = qs.createIndex(indexName, "key.ID", "/" + regionNames[i] + ".keys key");
}
indexName = "keyIdIndex2" + regionNames[i];
} catch (IndexNameConflictException ice) {
// Ignore. The pr may have created the index through
// remote index create message from peer.
}
}
} catch (Exception ex) {
logger.info("Failed to create index", ex);
fail("Failed to create index." + indexName);
}
// Execute Query with index.
SelectResults[] srWithIndex = new SelectResults[qString.length * regionNames.length];
try {
r = 0;
for (int q = 0; q < qString.length; q++) {
for (int i = 0; i < regionNames.length; i++) {
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
Query query = qs.newQuery(queryStr);
srWithIndex[r++] = (SelectResults) query.execute();
if (!observer.isIndexesUsed) {
fail("Index not used for query. " + queryStr);
}
}
}
} catch (Exception ex) {
logger.info("Failed to Execute query", ex);
fail("Failed to Execute query.");
}
// Compare results with and without index.
StructSetOrResultsSet ssORrs = new StructSetOrResultsSet();
SelectResults[][] sr = new SelectResults[1][2];
for (int i = 0; i < srWithIndex.length; i++) {
sr[0][0] = srWithoutIndex[i];
sr[0][1] = srWithIndex[i];
logger.info("Comparing the result for the query : " + queryString[i] + " Index in ResultSet is: " + i);
ssORrs.CompareQueryResultsWithoutAndWithIndexes(sr, 1, queryString);
}
// The index should get updated accordingly.
for (int i = 0; i < regionNames.length; i++) {
region = cache.getRegion(regionNames[i]);
for (int cnt = 1; cnt < numObjects; cnt++) {
if (cnt % 2 == 0) {
region.destroy(new Portfolio(cnt));
}
}
for (int cnt = 10; cnt < numObjects; cnt++) {
if (cnt % 2 == 0) {
region.put(new Portfolio(cnt), new String("XX" + cnt));
}
}
}
// Execute Query with index.
srWithIndex = new SelectResults[qString.length * regionNames.length];
try {
r = 0;
for (int q = 0; q < qString.length; q++) {
for (int i = 0; i < regionNames.length; i++) {
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
Query query = qs.newQuery(queryStr);
srWithIndex[r++] = (SelectResults) query.execute();
if (!observer.isIndexesUsed) {
fail("Index not used for query. " + queryStr);
}
}
}
} catch (Exception ex) {
logger.info("Failed to Execute query", ex);
fail("Failed to Execute query.");
}
}
});
}
Aggregations