use of org.apache.geode.cache.query.SelectResults 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.SelectResults in project geode by apache.
the class IndexUseJUnitTest method testSizeEstimateLTInCompactRangeIndexForNullMap.
@Test
public void testSizeEstimateLTInCompactRangeIndexForNullMap() throws Exception {
QueryService qs = CacheUtils.getQueryService();
LocalRegion testRgn = (LocalRegion) CacheUtils.createRegion("testRgn", null);
// Create indexes
Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "p.status", "/testRgn p");
Index i2 = qs.createIndex("Index2", IndexType.FUNCTIONAL, "p.ID", "/testRgn p");
// put values
testRgn.put(0, new Portfolio(0));
testRgn.put(1, new Portfolio(1));
// Set TestHook in RangeIndex
TestHook hook = new RangeIndexTestHook();
CompactRangeIndex.setTestHook(hook);
// Execute Queries without Indexes
Query q = CacheUtils.getQueryService().newQuery("<trace> SELECT * FROM /testRgn p where p.status = 'active' AND p.ID > 0 ");
// Following should throw NullPointerException.
SelectResults sr = (SelectResults) q.execute();
assertTrue("RangeIndexTestHook was not hooked for spot 2", ((RangeIndexTestHook) hook).isHooked(2));
CompactRangeIndex.setTestHook(null);
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class IndexUseJUnitTest method testIndexUsageWithOrderBy3.
@Test
public void testIndexUsageWithOrderBy3() 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 ID asc limit 1", "SELECT DISTINCT * FROM /testRgn p WHERE p.ID <= 10 order by p.ID desc limit 1" };
Object[][] r = new Object[queries.length][2];
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(), 0);
} else if (i == 1) {
assertEquals(p.getID(), 10);
}
}
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());
}
}
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class MapRangeIndexMaintenanceJUnitTest method updatingAMapFieldSameKeysSameValuesShouldUpdateCorrectly.
@Test
public void updatingAMapFieldSameKeysSameValuesShouldUpdateCorrectly() throws Exception {
// Create Partition Region
AttributesFactory af = new AttributesFactory();
af.setScope(Scope.LOCAL);
Portfolio p = new Portfolio(1, 1);
HashMap map1 = new HashMap();
map1.put("SUN", 1);
map1.put("IBM", 2);
p.positions = map1;
region = CacheUtils.createRegion("portfolio", af.create(), false);
region.put(1, p);
qs = CacheUtils.getQueryService();
keyIndex1 = (IndexProtocol) qs.createIndex(INDEX_NAME, "positions[*]", "/portfolio");
assertTrue(keyIndex1 instanceof CompactMapRangeIndex);
Portfolio p2 = new Portfolio(1, 1);
HashMap map2 = new HashMap();
p2.positions = map2;
map2.put("SUN", 1);
map2.put("IBM", 2);
region.put(1, p2);
SelectResults results = (SelectResults) qs.newQuery("select * from /portfolio p where p.positions['SUN'] = 1 OR p.positions['IBM'] = 2").execute();
assertEquals(1, results.size());
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class MapRangeIndexMaintenanceJUnitTest method updatingWithSameKeysDifferentValuesAfterRemovingUsingInplaceModificationShouldReinsertIndexMappings.
@Test
public void updatingWithSameKeysDifferentValuesAfterRemovingUsingInplaceModificationShouldReinsertIndexMappings() throws Exception {
AttributesFactory af = new AttributesFactory();
af.setScope(Scope.LOCAL);
region = CacheUtils.createRegion("portfolio", af.create(), false);
qs = CacheUtils.getQueryService();
Index keyIndex1 = qs.createIndex(INDEX_NAME, "positions[*]", "/portfolio");
Portfolio p1 = new Portfolio(1, 1);
HashMap map1 = new HashMap();
map1.put("SUN", 1);
map1.put("IBM", 2);
p1.positions = map1;
region.put(1, p1);
SelectResults results = (SelectResults) qs.newQuery("select * from /portfolio p where p.positions['SUN'] = 1").execute();
assertEquals(1, results.size());
map1.remove("SUN");
map1.remove("IBM");
map1.put("GOOG", 1);
region.put(1, p1);
results = (SelectResults) qs.newQuery("select * from /portfolio p where p.positions['SUN'] = 1").execute();
assertEquals(0, results.size());
Portfolio p3 = new Portfolio(1, 1);
HashMap map3 = new HashMap();
p3.positions = map3;
map3.put("SUN", 3);
map3.put("IBM", 4);
region.put(1, p3);
results = (SelectResults) qs.newQuery("select * from /portfolio p where p.positions['SUN'] = 1").execute();
assertEquals(0, results.size());
results = (SelectResults) qs.newQuery("select * from /portfolio p where p.positions['SUN'] = 3").execute();
assertEquals(1, results.size());
results = (SelectResults) qs.newQuery("select * from /portfolio p where p.positions['GOOG'] = 1").execute();
assertEquals(0, results.size());
}
Aggregations