use of org.apache.geode.cache.query.internal.QueryObserver in project geode by apache.
the class IndexMaintenanceJUnitTest method testQueryOnHeterogenousObjects.
/**
* Tests query on region containing heterogenous objects
*/
@Test
public void testQueryOnHeterogenousObjects() {
try {
DefaultQueryService.TEST_QUERY_HETEROGENEOUS_OBJECTS = true;
Cache cache = CacheUtils.getCache();
region = CacheUtils.createRegion("portfolio1", null);
for (int i = 0; i < 5; ++i) {
Portfolio p = new Portfolio(i + 1);
region.put(new Integer(i + 1), p);
}
for (int i = 5; i < 10; ++i) {
region.put(new Integer(i + 1), new Integer(i + 1));
}
String queryStr = "Select distinct * from /portfolio1 pf1 where pf1.getID() > 3";
Query q = qs.newQuery(queryStr);
SelectResults rs = (SelectResults) q.execute();
assertEquals(2, rs.size());
Iterator itr = rs.iterator();
while (itr.hasNext()) {
Portfolio p = (Portfolio) itr.next();
assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)));
}
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "pf.getID()", "/portfolio1 pf");
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexUsed = false;
public void beforeIndexLookup(Index index, int oper, Object key) {
indexUsed = true;
}
public void endQuery() {
assertTrue(indexUsed);
}
});
rs = (SelectResults) q.execute();
assertEquals(2, rs.size());
itr = rs.iterator();
while (itr.hasNext()) {
Portfolio p = (Portfolio) itr.next();
assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)));
}
qs.removeIndex(i1);
queryStr = "Select distinct * from /portfolio1 pf1 where pf1.pkid > '3'";
q = qs.newQuery(queryStr);
rs = (SelectResults) q.execute();
assertEquals(2, rs.size());
itr = rs.iterator();
while (itr.hasNext()) {
Portfolio p = (Portfolio) itr.next();
assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)));
}
i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "pf.pkid", "/portfolio1 pf");
QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexUsed = false;
public void beforeIndexLookup(Index index, int oper, Object key) {
indexUsed = true;
}
public void endQuery() {
assertTrue(indexUsed);
}
});
rs = (SelectResults) q.execute();
assertEquals(2, rs.size());
itr = rs.iterator();
while (itr.hasNext()) {
Portfolio p = (Portfolio) itr.next();
assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)));
}
} catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
} finally {
DefaultQueryService.TEST_QUERY_HETEROGENEOUS_OBJECTS = false;
}
}
use of org.apache.geode.cache.query.internal.QueryObserver in project geode by apache.
the class IndexStatisticsJUnitTest method verifyReadLockCountStatsForRangeIndex.
public void verifyReadLockCountStatsForRangeIndex(boolean isPr) throws Exception {
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserver old = QueryObserverHolder.setInstance(observer);
String regionName = "exampleRegion";
String name = "/" + regionName;
final Cache cache = CacheUtils.getCache();
Region r1 = cache.getRegion(regionName);
QueryService qs = cache.getQueryService();
Index secIdIndex = qs.createIndex("secId", "pos.secId", name + " p, p.positions.values pos");
assertEquals(cache.getQueryService().getIndexes().size(), 1);
for (int i = 0; i < 10; i++) {
r1.put("key-" + i, new Portfolio(i));
}
String query = "select distinct * from " + name + " p, p.positions.values pos where pos.secId = 'IBM' ";
final Query q = cache.getQueryService().newQuery(query);
SelectResults sr = (SelectResults) q.execute();
assertEquals("Read locks should have been taken by the query ", 1, observer.readLockCount);
assertEquals("Read lock count should have been released by the query ", 0, secIdIndex.getStatistics().getReadLockCount());
QueryObserverHolder.setInstance(old);
qs.removeIndexes();
}
use of org.apache.geode.cache.query.internal.QueryObserver in project geode by apache.
the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForStructBag.
/**
* Tests the limit functionality for Iter evaluated query with distinct clause This tests the
* basic limit functionality for StructBag 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 attribute is present and the projection
* attribute may be duplicate
*
* Tests StructBag behaviour
*/
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForStructBag() {
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;
final int[] num = new int[1];
num[0] = 0;
final int[] numRepeat = new int[1];
numRepeat[0] = 0;
final Set data = new HashSet();
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
public void afterIterationEvaluation(Object result) {
num[0] += 1;
}
public void beforeIterationEvaluation(CompiledValue ritr, Object currObject) {
if (data.contains(currObject)) {
numRepeat[0] += 1;
} else {
data.add(currObject);
}
}
});
String queryString = "SELECT DISTINCT pf.ID , pf.createTime FROM /portfolios1 pf WHERE pf.ID > 10 limit 5";
query = qs.newQuery(queryString);
result = (SelectResults) query.execute();
assertEquals((5 + numRepeat[0]), num[0]);
assertTrue(result instanceof SelectResults);
assertEquals(5, result.size());
SelectResults wrapper = (SelectResults) result;
assertEquals(5, wrapper.asSet().size());
} 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 testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForResultBag.
/**
* 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 of the resultset as distinct will
* eliminate them
*
* Tests ResultBag behaviour
*/
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForResultBag() {
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;
final int[] num = new int[1];
final int[] numRepeat = new int[1];
numRepeat[0] = 0;
final Set data = new HashSet();
num[0] = 0;
// In the worst possible case all the unique values come in
// consecutive order & hence only 5 iterations will yield the
// result
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
public void afterIterationEvaluation(Object result) {
num[0] += 1;
}
public void beforeIterationEvaluation(CompiledValue ritr, Object currObject) {
if (data.contains(currObject)) {
numRepeat[0] += 1;
} else {
data.add(currObject);
}
}
});
String queryString = "SELECT DISTINCT * FROM /portfolios1 pf WHERE pf.ID > 10 limit 5";
query = qs.newQuery(queryString);
result = (SelectResults) query.execute();
assertEquals((5 + numRepeat[0]), num[0]);
assertTrue(result instanceof SelectResults);
assertEquals(5, result.size());
SelectResults wrapper = (SelectResults) result;
assertEquals(5, wrapper.asSet().size());
} 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 testLimitOnJunctionWithCompactRangeIndexedFieldWithAndClauseJunctionNonIndexedField.
@Test
public void testLimitOnJunctionWithCompactRangeIndexedFieldWithAndClauseJunctionNonIndexedField() throws Exception {
Query query;
SelectResults result;
Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
for (int i = 0; i <= 15; i++) {
Portfolio p = new Portfolio(10);
p.shortID = 1;
p.positions.clear();
p.positions.put("IBM", new Position("IBM", i));
region.put("KEY" + i, p);
}
for (int i = 16; i < 21; i++) {
Portfolio p = new Portfolio(10);
p.shortID = 2;
p.positions.clear();
p.positions.put("VMW", new Position("VMW", i));
region.put("KEY" + i, p);
}
MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
QueryObserver old = QueryObserverHolder.setInstance(observer);
// Create Index on ID
Index idIndex = qs.createIndex("idIndex", "P.ID", "/portfolios1 P");
String queryString = "SELECT * FROM /portfolios1 P WHERE P.ID > 9 AND P.ID < 20 AND P.shortID > 1 AND P.shortID < 3 LIMIT 5";
query = qs.newQuery(queryString);
assertNotNull(idIndex);
SelectResults resultsWithIndex = (SelectResults) query.execute();
assertFalse(observer.limitAppliedAtIndex);
assertEquals(5, resultsWithIndex.size());
}
Aggregations