use of org.apache.geode.cache.query.internal.QueryObserverAdapter in project geode by apache.
the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForStructBagWithIndex.
/**
* 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 of the resultset as distinct will
* eliminate them
*
* Tests StructBag behaviour
*/
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForStructBagWithIndex() {
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, pf.collectionHolderMap.keySet");
assertNotNull(index);
String queryString = "SELECT DISTINCT * FROM /portfolios1 pf, pf.collectionHolderMap.keySet WHERE pf.ID > 10 limit 20";
query = qs.newQuery(queryString);
result = (SelectResults) query.execute();
assertTrue(result instanceof SelectResults);
assertEquals(20, result.size());
SelectResults wrapper = (SelectResults) result;
assertEquals(20, wrapper.asSet().size());
// currently this is false because we disabled limit application at the range index level
assertFalse(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.QueryObserverAdapter in project geode by apache.
the class LimitClauseJUnitTest method testLimitDistinctQueryWithTwoCondWithTwoIndex.
@Test
public void testLimitDistinctQueryWithTwoCondWithTwoIndex() {
try {
Query query;
SelectResults result;
Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
for (int i = 1; i < 100; i++) {
Portfolio p = new Portfolio(i);
if (i < 50)
p.status = "active";
region.put(Integer.toString(i), p);
}
// Create Index on ID
Index index = qs.createIndex("idIndex", "ID", "/portfolios1");
assertNotNull(index);
index = qs.createIndex("statusIndex", "status", "/portfolios1");
assertNotNull(index);
MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
QueryObserver old = QueryObserverHolder.setInstance(observer);
String queryString = "select DISTINCT * from /portfolios1 where status ='inactive' AND (ID > 0 AND ID < 100) limit 10";
query = qs.newQuery(queryString);
result = (SelectResults) query.execute();
assertTrue(result instanceof SelectResults);
assertEquals(10, result.size());
SelectResults wrapper = (SelectResults) result;
assertEquals(10, wrapper.asSet().size());
assertFalse(observer.limitAppliedAtIndex && observer.indexName.equals("idIndex"));
assertTrue(observer.limitAppliedAtIndex && observer.indexName.equals("statusIndex"));
} catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
} finally {
QueryObserverHolder.setInstance(new QueryObserverAdapter());
}
}
use of org.apache.geode.cache.query.internal.QueryObserverAdapter in project geode by apache.
the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryForResultBag.
/**
* Tests the limit functionality for Iter evaluated query with distinct clause This tests the
* basic limit functionality for ResultBag wrapped by a SelectResults
*
* Tests ResultBag behaviour
*/
@Test
public void testLimitDistinctIterEvaluatedQueryForResultBag() {
try {
Query query;
SelectResults result;
String queryString = "SELECT DISTINCT * FROM /portfolios pf WHERE pf.ID > 0 limit 5";
query = qs.newQuery(queryString);
final int[] num = new int[1];
num[0] = 0;
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
public void afterIterationEvaluation(Object result) {
num[0] += 1;
}
});
result = (SelectResults) query.execute();
assertEquals(5, 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.QueryObserverAdapter in project geode by apache.
the class LikePredicateJUnitTest method likePercentageTerminated_1.
/**
* Tests simple % terminated pattern with atleast one preceding character
*
* @throws Exception
*/
private void likePercentageTerminated_1(boolean useBindParam) throws Exception {
Cache cache = CacheUtils.getCache();
AttributesFactory attributesFactory = new AttributesFactory();
RegionAttributes regionAttributes = attributesFactory.create();
Region region = cache.createRegion("pos", regionAttributes);
char ch = 'd';
String base = "abc";
for (int i = 1; i < 6; ++i) {
Portfolio pf = new Portfolio(i);
pf.status = base + ch;
ch += 1;
region.put(new Integer(i), pf);
}
base = "abd";
ch = 'd';
for (int i = 6; i < 11; ++i) {
Portfolio pf = new Portfolio(i);
pf.status = base + ch;
ch += 1;
region.put(new Integer(i), pf);
}
QueryService qs = cache.getQueryService();
Query q;
SelectResults results;
SelectResults expectedResults;
String predicate = "";
if (useBindParam) {
predicate = "$1";
} else {
predicate = " 'abc%'";
}
q = qs.newQuery("SELECT distinct * FROM /pos ps WHERE ps.status like " + predicate);
if (useBindParam) {
results = (SelectResults) q.execute(new Object[] { "abc%" });
} else {
results = (SelectResults) q.execute();
}
ResultsBag bag = new ResultsBag(null);
for (int i = 1; i < 6; ++i) {
bag.add(region.get(new Integer(i)));
}
expectedResults = new ResultsCollectionWrapper(new ObjectTypeImpl(Object.class), bag.asSet());
SelectResults[][] rs = new SelectResults[][] { { results, expectedResults } };
CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
// Create Index
qs.createIndex("status", IndexType.FUNCTIONAL, "ps.status", "/pos ps");
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexCalled = false;
public void afterIndexLookup(Collection results) {
indexCalled = true;
}
public void endQuery() {
assertTrue(indexCalled);
}
});
if (useBindParam) {
results = (SelectResults) q.execute(new Object[] { "abc%" });
} else {
results = (SelectResults) q.execute();
}
rs[0][0] = results;
rs[0][1] = expectedResults;
CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
QueryObserverHolder.setInstance(old);
}
use of org.apache.geode.cache.query.internal.QueryObserverAdapter in project geode by apache.
the class LikePredicateJUnitTest method likePercentageTerminated_4.
/**
* Tests a simple % terminated like predicate with an AND condition
*
* @throws Exception
*/
private void likePercentageTerminated_4(boolean useBindPrm) throws Exception {
Cache cache = CacheUtils.getCache();
AttributesFactory attributesFactory = new AttributesFactory();
RegionAttributes regionAttributes = attributesFactory.create();
Region region = cache.createRegion("pos", regionAttributes);
String base = "abc";
String pattern = base;
// so we will get string like abcdcdcdcdcdc
for (int i = 1; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pattern += "dc";
pf.status = pattern;
region.put(new Integer(i), pf);
}
base = "abd";
pattern = base;
// so we will get string like abddcdcdcd
for (int i = 201; i < 400; ++i) {
Portfolio pf = new Portfolio(i);
pattern += "dc";
pf.status = pattern;
region.put(new Integer(i), pf);
}
QueryService qs = cache.getQueryService();
Query q;
SelectResults results;
SelectResults expectedResults;
String predicate = "";
if (useBindPrm) {
predicate = "$1";
} else {
predicate = " 'abc%'";
}
q = qs.newQuery("SELECT distinct * FROM /pos ps WHERE ps.status like " + predicate + " AND ps.ID > 2 AND ps.ID < 150");
if (useBindPrm) {
results = (SelectResults) q.execute(new Object[] { "abc%" });
} else {
results = (SelectResults) q.execute();
}
ResultsBag bag = new ResultsBag(null);
for (int i = 3; i < 150; ++i) {
bag.add(region.get(new Integer(i)));
}
expectedResults = new ResultsCollectionWrapper(new ObjectTypeImpl(Object.class), bag.asSet());
SelectResults[][] rs = new SelectResults[][] { { results, expectedResults } };
CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
// Create Index
qs.createIndex("status", IndexType.FUNCTIONAL, "ps.status", "/pos ps");
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean[] indexCalled = new boolean[] { false, false };
private int i = 0;
public void afterIndexLookup(Collection results) {
indexCalled[i++] = true;
}
public void endQuery() {
assertTrue(indexCalled[0]);
assertFalse(indexCalled[1]);
}
});
if (useBindPrm) {
results = (SelectResults) q.execute(new Object[] { "abc%" });
} else {
results = (SelectResults) q.execute();
}
rs[0][0] = results;
rs[0][1] = expectedResults;
CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
qs.createIndex("id", IndexType.FUNCTIONAL, "ps.ID", "/pos ps");
QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean[] indexCalled = new boolean[] { false, false };
private int i = 0;
public void afterIndexLookup(Collection results) {
indexCalled[i++] = true;
}
public void endQuery() {
// Only one indexed condition should be called
boolean indexInvoked = false;
for (int i = 0; i < indexCalled.length; ++i) {
indexInvoked = indexInvoked || indexCalled[i];
}
assertTrue(indexInvoked);
}
});
if (useBindPrm) {
results = (SelectResults) q.execute(new Object[] { "abc%" });
} else {
results = (SelectResults) q.execute();
}
rs[0][0] = results;
rs[0][1] = expectedResults;
CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
QueryObserverHolder.setInstance(old);
}
Aggregations