use of org.apache.geode.cache.query.internal.QueryObserver in project geode by apache.
the class MultiIndexCreationJUnitTest method testBasicMultiIndexCreationDifferentTypes.
@Test
public void testBasicMultiIndexCreationDifferentTypes() throws Exception {
Region r = CacheUtils.getRegion(regionName);
for (int i = 0; i < 10; i++) {
r.put("" + i, new Portfolio(i));
}
QueryService qs = CacheUtils.getQueryService();
qs.defineIndex("statusIndex", "status", r.getFullPath());
qs.defineHashIndex("IDIndex", "ID", r.getFullPath());
qs.defineKeyIndex("keyIDIndex", "ID", r.getFullPath());
List<Index> indexes = qs.createDefinedIndexes();
assertEquals("Only 3 indexes should have been created. ", 3, indexes.size());
Index ind = qs.getIndex(r, "statusIndex");
assertTrue(ind instanceof CompactRangeIndex);
assertEquals(2, ind.getStatistics().getNumberOfKeys());
assertEquals(10, ind.getStatistics().getNumberOfValues());
ind = qs.getIndex(r, "IDIndex");
assertTrue(ind instanceof HashIndex);
assertEquals(10, ind.getStatistics().getNumberOfValues());
ind = qs.getIndex(r, "keyIDIndex");
assertTrue(ind instanceof PrimaryKeyIndex);
assertEquals(10, ind.getStatistics().getNumberOfKeys());
assertEquals(10, ind.getStatistics().getNumberOfValues());
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexCalled = false;
public void afterIndexLookup(Collection results) {
indexCalled = true;
}
public void endQuery() {
assertTrue(indexCalled);
}
});
String[] queries = { "select * from " + r.getFullPath() + " where status = 'active'", "select * from " + r.getFullPath() + " where ID > 4" };
for (int i = 0; i < queries.length; i++) {
SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
assertEquals(5, sr.size());
}
QueryObserverHolder.setInstance(old);
}
use of org.apache.geode.cache.query.internal.QueryObserver in project geode by apache.
the class MultiIndexCreationJUnitTest method testIndexCreationOnMultipleRegions.
@Test
public void testIndexCreationOnMultipleRegions() throws Exception {
Region pr = CacheUtils.getCache().createRegionFactory(RegionShortcut.PARTITION).create(prRegionName);
for (int i = 0; i < 10; i++) {
pr.put("" + i, new Portfolio(i));
}
Region overflow = CacheUtils.getCache().createRegionFactory(RegionShortcut.REPLICATE_OVERFLOW).create(overflowRegionName);
for (int i = 0; i < 10; i++) {
overflow.put("" + i, new Portfolio(i));
}
Region r = CacheUtils.getRegion(regionName);
for (int i = 0; i < 10; i++) {
r.put("" + i, new Portfolio(i));
}
QueryService qs = CacheUtils.getQueryService();
qs.defineIndex("IDIndex", "ID", pr.getFullPath());
qs.defineIndex("secIDIndex", "pos.secId", r.getFullPath() + " p, p.positions.values pos ");
qs.defineIndex("statusIndex", "status", overflow.getFullPath());
List<Index> indexes = qs.createDefinedIndexes();
assertEquals("Only 3 indexes should have been created. ", 3, indexes.size());
Index ind = qs.getIndex(overflow, "statusIndex");
assertEquals(2, ind.getStatistics().getNumberOfKeys());
assertEquals(10, ind.getStatistics().getNumberOfValues());
ind = qs.getIndex(pr, "IDIndex");
assertEquals(10, ind.getStatistics().getNumberOfKeys());
assertEquals(10, ind.getStatistics().getNumberOfValues());
ind = qs.getIndex(r, "secIDIndex");
assertEquals(12, ind.getStatistics().getNumberOfKeys());
assertEquals(20, ind.getStatistics().getNumberOfValues());
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexCalled = false;
public void afterIndexLookup(Collection results) {
indexCalled = true;
}
public void endQuery() {
assertTrue(indexCalled);
}
});
String[] queries = { "select * from " + overflow.getFullPath() + " where status = 'active'", "select * from " + pr.getFullPath() + " where ID > 4", "select * from " + r.getFullPath() + " p, p.positions.values pos where pos.secId != NULL" };
for (int i = 0; i < queries.length; i++) {
SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
if (i == 2) {
assertEquals("Incorrect results for query: " + queries[i], 20, sr.size());
} else {
assertEquals("Incorrect results for query: " + queries[i], 5, sr.size());
}
}
QueryObserverHolder.setInstance(old);
}
use of org.apache.geode.cache.query.internal.QueryObserver in project geode by apache.
the class MultiIndexCreationJUnitTest method testIndexCreationOnMultipleRegionsBeforePuts.
@Test
public void testIndexCreationOnMultipleRegionsBeforePuts() throws Exception {
Region pr = CacheUtils.getCache().createRegionFactory(RegionShortcut.PARTITION).create(prRegionName);
Region overflow = CacheUtils.getCache().createRegionFactory(RegionShortcut.REPLICATE_OVERFLOW).create(overflowRegionName);
Region r = CacheUtils.getRegion(regionName);
QueryService qs = CacheUtils.getQueryService();
qs.defineIndex("IDIndex", "ID", pr.getFullPath());
qs.defineIndex("secIDIndex", "pos.secId", r.getFullPath() + " p, p.positions.values pos ");
qs.defineIndex("statusIndex", "status", overflow.getFullPath());
List<Index> indexes = qs.createDefinedIndexes();
for (int i = 0; i < 10; i++) {
r.put("" + i, new Portfolio(i));
}
for (int i = 0; i < 10; i++) {
pr.put("" + i, new Portfolio(i));
}
for (int i = 0; i < 10; i++) {
overflow.put("" + i, new Portfolio(i));
}
assertEquals("Only 3 indexes should have been created. ", 3, indexes.size());
Index ind = qs.getIndex(overflow, "statusIndex");
assertEquals(2, ind.getStatistics().getNumberOfKeys());
assertEquals(10, ind.getStatistics().getNumberOfValues());
ind = qs.getIndex(pr, "IDIndex");
assertEquals(10, ind.getStatistics().getNumberOfKeys());
assertEquals(10, ind.getStatistics().getNumberOfValues());
ind = qs.getIndex(r, "secIDIndex");
assertEquals(12, ind.getStatistics().getNumberOfKeys());
assertEquals(20, ind.getStatistics().getNumberOfValues());
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexCalled = false;
public void afterIndexLookup(Collection results) {
indexCalled = true;
}
public void endQuery() {
assertTrue(indexCalled);
}
});
String[] queries = { "select * from " + overflow.getFullPath() + " where status = 'active'", "select * from " + pr.getFullPath() + " where ID > 4", "select * from " + r.getFullPath() + " p, p.positions.values pos where pos.secId != NULL" };
for (int i = 0; i < queries.length; i++) {
SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
if (i == 2) {
assertEquals("Incorrect results for query: " + queries[i], 20, sr.size());
} else {
assertEquals("Incorrect results for query: " + queries[i], 5, sr.size());
}
}
QueryObserverHolder.setInstance(old);
}
use of org.apache.geode.cache.query.internal.QueryObserver in project geode by apache.
the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryForResultBagWithProjectionAttributeWithIndex.
/**
* Tests the limit functionality for Iter evaluated query with distinct clause This tests the
* basic limit functionality for ResultBag wrapped by a SelectResults . This test contains
* projection attributes. Since the attribute is unique every time, the limit will be satisfied
* with first 5 iterations
*
* Tests ResultBag behaviour
*
*/
@Test
public void testLimitDistinctIterEvaluatedQueryForResultBagWithProjectionAttributeWithIndex() {
try {
Query query;
SelectResults result;
String queryString = "SELECT DISTINCT pf.ID FROM /portfolios pf WHERE pf.ID > 0 limit 5";
query = qs.newQuery(queryString);
MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
QueryObserver old = QueryObserverHolder.setInstance(observer);
Index index = qs.createIndex("idIndex", "pf.ID", "/portfolios pf");
assertNotNull(index);
result = (SelectResults) query.execute();
assertTrue(result instanceof SelectResults);
assertEquals(5, result.size());
SelectResults wrapper = (SelectResults) result;
assertEquals(5, wrapper.asSet().size());
assertTrue(observer.limitAppliedAtIndex);
} catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
} finally {
QueryObserverHolder.setInstance(new QueryObserverAdapter());
}
}
use of org.apache.geode.cache.query.internal.QueryObserver in project geode by apache.
the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForResultBagWithIndex.
/**
* Tests the limit functionality for Iter evaluated query with distinct clause This tests the
* basic limit functionality for ResultBag wrapped by a SelectResults if the iteration included
* duplicate elements. If the distinct clause is present then duplicate elements even if
* satisfying the where clause should not be considered as part as distinct will eliminate them.
* This test validates the above behaviour if projection sttribute is present and the projection
* attribute may be duplicate
*
* Tests ResultBag behaviour
*/
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForResultBagWithIndex() {
try {
Region region1 = CacheUtils.createRegion("portfolios1", Portfolio.class);
// Add 5 pairs of same Object starting from 11 to 20
for (int i = 11; i < 21; ) {
region1.put(Integer.toString(i), new Portfolio(i));
region1.put(Integer.toString(i + 1), new Portfolio(i));
i += 2;
}
Query query;
SelectResults result;
MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
QueryObserver old = QueryObserverHolder.setInstance(observer);
Index index = qs.createIndex("idIndex", "pf.ID", "/portfolios1 pf");
assertNotNull(index);
String queryString = "SELECT DISTINCT pf.ID FROM /portfolios1 pf WHERE pf.ID > 10 limit 5";
query = qs.newQuery(queryString);
result = (SelectResults) query.execute();
assertTrue(result instanceof SelectResults);
assertEquals(5, result.size());
SelectResults wrapper = (SelectResults) result;
assertEquals(5, wrapper.asSet().size());
assertTrue(observer.limitAppliedAtIndex);
} catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
} finally {
QueryObserverHolder.setInstance(new QueryObserverAdapter());
}
}
Aggregations