use of org.apache.geode.cache.query.functional.StructSetOrResultsSet in project geode by apache.
the class IndexUseJUnitTest method testRangeGroupingBehaviourOfCompactMapIndex.
@Test
public void testRangeGroupingBehaviourOfCompactMapIndex() throws Exception {
QueryService qs;
qs = CacheUtils.getQueryService();
LocalRegion testRgn = (LocalRegion) CacheUtils.createRegion("testRgn", null);
int ID = 1;
// and so on
for (; ID <= 30; ++ID) {
MapKeyIndexData mkid = new MapKeyIndexData(ID);
for (int j = 1; j <= ID; ++j) {
mkid.addKeyValue("key1", j * 1);
mkid.addKeyValue("key2", j * 2);
mkid.addKeyValue("key3", j * 3);
}
testRgn.put(ID, mkid);
}
qs = CacheUtils.getQueryService();
String[] queries = { "SELECT DISTINCT * FROM /testRgn itr1 WHERE itr1.maap['key2'] >= 3 and itr1.maap['key2'] <=18", "SELECT DISTINCT * FROM /testRgn itr1 WHERE itr1.maap['key3'] >= 3 and itr1.maap['key3'] >= 13 ", "SELECT DISTINCT * FROM /testRgn itr1 WHERE itr1.maap['key2'] >= 3 and itr1.maap['key3'] >= 13 ", "SELECT DISTINCT * FROM /testRgn itr1 WHERE itr1.maap['key2'] >= 3 and itr1.maap['key3'] < 18 " };
Object[][] r = new Object[queries.length][2];
// Execute Queries without Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
r[i][0] = q.execute();
CacheUtils.log("Executed query: " + queries[i]);
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "objs.maap['key2','key3']", "/testRgn objs");
assertTrue(i1 instanceof CompactMapRangeIndex);
// Execute Queries with Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
r[i][1] = q.execute();
CacheUtils.log("Executing query: " + queries[i] + " with index created");
if (!observer.isIndexesUsed) {
fail("Index is NOT uesd");
}
Iterator itr = observer.indexesUsed.iterator();
assertTrue(itr.hasNext());
String temp = itr.next().toString();
assertEquals(temp, "Index1");
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length, queries);
}
use of org.apache.geode.cache.query.functional.StructSetOrResultsSet in project geode by apache.
the class IndexUseJUnitTest method testIndexUsageWithOrderBy.
@Test
public void testIndexUsageWithOrderBy() throws Exception {
QueryService qs;
qs = CacheUtils.getQueryService();
LocalRegion testRgn = (LocalRegion) CacheUtils.createRegion("testRgn", null);
int numObjects = 30;
// and so on
for (int i = 0; i < numObjects; i++) {
Portfolio p = new Portfolio(i);
p.pkid = ("" + (numObjects - i));
testRgn.put("" + i, p);
}
qs = CacheUtils.getQueryService();
String[] queries = { "SELECT DISTINCT * FROM /testRgn p WHERE p.ID <= 10 order by p.pkid asc limit 1", "SELECT DISTINCT * FROM /testRgn p WHERE p.ID <= 10 order by p.pkid desc limit 1" };
Object[][] r = new Object[queries.length][2];
// Execute Queries without Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
// verify individual result
SelectResults sr = (SelectResults) q.execute();
List results = sr.asList();
for (int rows = 0; rows < results.size(); rows++) {
Portfolio p = (Portfolio) results.get(0);
CacheUtils.getLogger().info("p: " + p);
if (i == 0) {
assertEquals(p.getID(), 10);
assertEquals(p.pkid, "" + (numObjects - 10));
} else if (i == 1) {
assertEquals(p.getID(), 0);
assertEquals(p.pkid, "" + numObjects);
}
}
r[i][0] = sr;
CacheUtils.log("Executed query: " + queries[i]);
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "p.ID", "/testRgn p");
// Execute Queries with Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
SelectResults sr = (SelectResults) q.execute();
List results = sr.asList();
for (int rows = 0; rows < results.size(); rows++) {
Portfolio p = (Portfolio) results.get(0);
CacheUtils.getLogger().info("index p: " + p);
if (i == 0) {
assertEquals(p.getID(), 10);
assertEquals(p.pkid, "" + (numObjects - 10));
} else if (i == 1) {
assertEquals(p.getID(), 0);
assertEquals(p.pkid, "" + numObjects);
}
}
r[i][1] = sr;
// r[i][1] = q.execute();
CacheUtils.log("Executing query: " + queries[i] + " with index created");
if (!observer.isIndexesUsed) {
fail("Index is NOT uesd");
}
Iterator itr = observer.indexesUsed.iterator();
assertTrue(itr.hasNext());
String temp = itr.next().toString();
assertEquals(temp, "Index1");
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length, queries);
}
use of org.apache.geode.cache.query.functional.StructSetOrResultsSet in project geode by apache.
the class IndexUseJUnitTest method testIndexUsageWithOrderBy2.
@Test
public void testIndexUsageWithOrderBy2() throws Exception {
QueryService qs;
qs = CacheUtils.getQueryService();
LocalRegion testRgn = (LocalRegion) CacheUtils.createRegion("testRgn", null);
int numObjects = 30;
// and so on
for (int i = 0; i < numObjects; i++) {
Portfolio p = new Portfolio(i % 2);
p.createTime = (numObjects - i);
testRgn.put("" + i, p);
}
qs = CacheUtils.getQueryService();
String[] queries = { "SELECT DISTINCT p.key, p.value FROM /testRgn.entrySet p WHERE p.value.ID <= 10 order by p.value.createTime asc limit 1", "SELECT DISTINCT p.key, p.value FROM /testRgn.entrySet p WHERE p.value.ID <= 10 order by p.value.createTime desc limit 1" };
Object[][] r = new Object[queries.length][2];
// Execute Queries without Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
// verify individual result
SelectResults sr = (SelectResults) q.execute();
List results = sr.asList();
for (int rows = 0; rows < results.size(); rows++) {
Struct s = (Struct) results.get(0);
Portfolio p = (Portfolio) s.get("value");
CacheUtils.getLogger().info("p: " + p);
if (i == 0) {
assertEquals(p.createTime, 1);
} else if (i == 1) {
assertEquals(p.createTime, numObjects);
}
}
r[i][0] = sr;
CacheUtils.log("Executed query: " + queries[i]);
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "p.value.ID", "/testRgn.entrySet p");
// Execute Queries with Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
SelectResults sr = (SelectResults) q.execute();
List results = sr.asList();
for (int rows = 0; rows < results.size(); rows++) {
Struct s = (Struct) results.get(0);
Portfolio p = (Portfolio) s.get("value");
CacheUtils.getLogger().info("index p: " + p);
if (i == 0) {
assertEquals(p.createTime, 1);
} else if (i == 1) {
assertEquals(p.createTime, numObjects);
}
}
r[i][1] = sr;
// r[i][1] = q.execute();
CacheUtils.log("Executing query: " + queries[i] + " with index created");
if (!observer.isIndexesUsed) {
fail("Index is NOT uesd");
}
Iterator itr = observer.indexesUsed.iterator();
assertTrue(itr.hasNext());
String temp = itr.next().toString();
assertEquals(temp, "Index1");
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length, queries);
}
use of org.apache.geode.cache.query.functional.StructSetOrResultsSet in project geode by apache.
the class IndexUseJUnitTest method evaluateMapTypeIndexUsage.
private void evaluateMapTypeIndexUsage(String indexExpression, String fromClause, String[] queries, String[] queriesIndexNotUsed, Class expectedIndexClass) throws Exception {
QueryService qs = CacheUtils.getQueryService();
Object[][] r = new Object[queries.length][2];
// Execute Queries without Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
r[i][0] = q.execute();
CacheUtils.log("Executed query: " + queries[i]);
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, indexExpression, fromClause);
assertTrue(i1.getClass().equals(expectedIndexClass));
// Execute Queries with Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
r[i][1] = q.execute();
CacheUtils.log("Executing query: " + queries[i] + " with index created");
if (!observer.isIndexesUsed) {
fail("Index is NOT used");
}
Iterator itr = observer.indexesUsed.iterator();
assertTrue(itr.hasNext());
String temp = itr.next().toString();
assertEquals(temp, "Index1");
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length, queries);
// Test queries index not used
for (int i = 0; i < queriesIndexNotUsed.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queriesIndexNotUsed[i]);
CacheUtils.getLogger().info("Executing query: " + queriesIndexNotUsed[i]);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
CacheUtils.log("Executing query: " + queriesIndexNotUsed[i] + " with index created");
q.execute();
assertFalse(observer.isIndexesUsed);
Iterator itr = observer.indexesUsed.iterator();
assertFalse(itr.hasNext());
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
}
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);
}
Aggregations