use of org.apache.geode.cache.query.internal.QueryObserver in project geode by apache.
the class MultiIndexCreationJUnitTest method testBasicMultiIndexCreation.
@Test
public void testBasicMultiIndexCreation() 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.defineIndex("IDIndex", "ID", r.getFullPath());
List<Index> indexes = qs.createDefinedIndexes();
assertEquals("Only 2 indexes should have been created. ", 2, indexes.size());
Index ind = qs.getIndex(r, "statusIndex");
assertEquals(2, ind.getStatistics().getNumberOfKeys());
assertEquals(10, ind.getStatistics().getNumberOfValues());
ind = qs.getIndex(r, "IDIndex");
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 LimitClauseJUnitTest method testLimitOnJunctionWithCompactRangeIndexedFieldWithAndOnCompactRangeIndexedField.
@Test
public void testLimitOnJunctionWithCompactRangeIndexedFieldWithAndOnCompactRangeIndexedField() 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");
Index shortIdIndex = qs.createIndex("shortIdIndex", "P.shortID", "/portfolios1 P");
String queryString = "SELECT * FROM /portfolios1 P WHERE P.ID > 9 AND P.ID < 20 AND P.shortID = 2 LIMIT 5";
query = qs.newQuery(queryString);
assertNotNull(idIndex);
SelectResults resultsWithIndex = (SelectResults) query.execute();
// assertFalse(observer.limitAppliedAtIndex);
assertEquals(5, resultsWithIndex.size());
}
use of org.apache.geode.cache.query.internal.QueryObserver in project geode by apache.
the class LimitClauseJUnitTest method testLimitOnEqualsRangeIndexedFieldWithAndClauseCompactRangeIndexedField.
// This is one where limit is applied at index for old code but we do not apply
@Test
public void testLimitOnEqualsRangeIndexedFieldWithAndClauseCompactRangeIndexedField() 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, P.positions.values POS");
Index shortIdIndex = qs.createIndex("shortIdIndex", "P.shortID", "/portfolios1 P");
String queryString = "SELECT * FROM /portfolios1 P WHERE P.ID = 10 AND P.shortID = 2 LIMIT 5";
query = qs.newQuery(queryString);
assertNotNull(idIndex);
SelectResults resultsWithIndex = (SelectResults) query.execute();
// assertFalse(observer.limitAppliedAtIndex);
assertEquals(5, resultsWithIndex.size());
}
use of org.apache.geode.cache.query.internal.QueryObserver in project geode by apache.
the class LimitClauseJUnitTest method testLimitNonDistinctQueryWithTwoCondButOneIndex.
@Test
public void testLimitNonDistinctQueryWithTwoCondButOneIndex() {
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);
}
MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
QueryObserver old = QueryObserverHolder.setInstance(observer);
// Create Index on ID
Index index = qs.createIndex("idIndex", "ID", "/portfolios1");
assertNotNull(index);
String[] queryString = new String[] { "select * from /portfolios1 where status ='inactive' AND (ID > 0 AND ID < 100) limit 10", "select * from /portfolios1 where (status > 'inactiva' AND status < 'xyz') AND (ID > 0 AND ID < 100) limit 10", "select * from /portfolios1 where (status > 'inactiva' AND status < 'xyz') AND (ID > 0 AND ID < 100) AND (\"type\"='type1' OR \"type\"='type2') limit 10" };
for (String qstr : queryString) {
query = qs.newQuery(qstr);
result = (SelectResults) query.execute();
assertEquals(10, result.size());
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.QueryObserver in project geode by apache.
the class LimitClauseJUnitTest method testLimitNonDistinctQueryWithTwoCondTwoIndex.
@Test
public void testLimitNonDistinctQueryWithTwoCondTwoIndex() {
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);
}
MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
QueryObserver old = QueryObserverHolder.setInstance(observer);
// Create Index on ID
Index index = qs.createIndex("idIndex", "ID", "/portfolios1");
assertNotNull(index);
index = qs.createIndex("statusIndex", "status", "/portfolios1");
assertNotNull(index);
String[] queryString = new String[] { "select * from /portfolios1 where status ='inactive' AND (ID > 0 AND ID < 100) limit 10" };
for (String qstr : queryString) {
query = qs.newQuery(qstr);
result = (SelectResults) query.execute();
assertEquals(10, result.size());
assertTrue(observer.limitAppliedAtIndex && observer.indexName.equals("statusIndex"));
}
} catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
} finally {
QueryObserverHolder.setInstance(new QueryObserverAdapter());
}
}
Aggregations