use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.
the class IndexUseMultFrmSnglCondJUnitTest method testMultiFromWithSingleConditionUsingIndex.
@Test
public void testMultiFromWithSingleConditionUsingIndex() throws Exception {
// create region 1 and 2
Region region1 = CacheUtils.createRegion("portfolios1", Portfolio.class);
Region region2 = CacheUtils.createRegion("portfolios2", Portfolio.class);
for (int i = 0; i < 100; i++) {
Portfolio p = null;
if (i != 0 && i < 5) {
p = new Portfolio(5);
} else {
p = new Portfolio(i);
}
region1.put(i, p);
region2.put(i, p);
}
QueryService qs = CacheUtils.getQueryService();
// create and execute query
String queryString = "SELECT * from /portfolios1 P1, /portfolios2 P2 WHERE P1.ID = 5";
Query query = qs.newQuery(queryString);
SelectResults sr1 = (SelectResults) query.execute();
// create index
Index index = qs.createIndex("P1IDIndex", IndexType.FUNCTIONAL, "P1.ID", "/portfolios1 P1");
// execute query
SelectResults sr2 = (SelectResults) query.execute();
assertEquals("Index result set does not match unindexed result set size", sr1.size(), sr2.size());
// size will be number of matching in region 1 x region 2 size
assertEquals("Query result set size does not match expected size", 5 * region2.size(), sr2.size());
}
use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.
the class LikePredicateJUnitTest method likePercentageTerminated_5.
private void likePercentageTerminated_5(boolean useBindPrm) 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);
}
QueryService qs = cache.getQueryService();
Query q;
SelectResults results;
SelectResults expectedResults;
String predicate = "";
if (useBindPrm) {
predicate = "$1";
} else {
predicate = " 'a%c%'";
}
q = qs.newQuery("SELECT distinct * FROM /pos ps WHERE ps.status like " + predicate);
if (useBindPrm) {
results = (SelectResults) q.execute(new Object[] { "a%bc%" });
} 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");
q = qs.newQuery("SELECT distinct * FROM /pos ps WHERE ps.status like " + predicate);
if (useBindPrm) {
results = (SelectResults) q.execute(new Object[] { "a%bc%" });
} else {
results = (SelectResults) q.execute();
}
rs = new SelectResults[][] { { results, expectedResults } };
CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
if (useBindPrm) {
predicate = "$1";
} else {
predicate = "'abc_'";
}
q = qs.newQuery("SELECT distinct * FROM /pos ps WHERE ps.status like " + predicate);
if (useBindPrm) {
results = (SelectResults) q.execute(new Object[] { "abc_" });
} else {
results = (SelectResults) q.execute();
}
rs = new SelectResults[][] { { results, expectedResults } };
CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
if (useBindPrm) {
predicate = "$1";
} else {
predicate = "'_bc_'";
}
q = qs.newQuery("SELECT distinct * FROM /pos ps WHERE ps.status like " + predicate);
if (useBindPrm) {
results = (SelectResults) q.execute(new Object[] { "_bc_" });
} else {
results = (SelectResults) q.execute();
}
rs = new SelectResults[][] { { results, expectedResults } };
CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
}
use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.
the class LimitClauseJUnitTest method testNotLimitAtIndexLevelForMultiSingleIndexAndClauseUsage.
/*
* This test shows an error with order by. Limit does not affect this and will enable this test
* after the bug for order by is logged and fixed
*
* @Test public void testNotApplyingLimitAtIndexLevelForMultiIndexAndClauseUsageWithOrderBy()
* throws Exception { //try { Query query; SelectResults result; int limit = 25; Region region =
* CacheUtils.createRegion("portfolios1", Portfolio.class); for (int i = 30; i > 0; i--) {
* Portfolio p = new Portfolio(i); p.positions.clear(); p.positions.put("IBM", new Position("IBM",
* i)); region.put("KEY" + i, p); }
*
* String queryString =
* "<trace>SELECT distinct P FROM /portfolios1 P, P.positions.values POS WHERE P.ID > 5 AND POS.secId = 'IBM' ORDER BY P.ID"
* ; query = qs.newQuery(queryString); SelectResults resultsNoIndex = (SelectResults)
* query.execute();
*
* //Create Index on ID and secId //Index secIndex = qs.createIndex("secIdIndex", "pos.secId",
* "/portfolios1 p, p.positions.values pos"); Index idIndex = qs.createIndex("idIndex",
* IndexType.FUNCTIONAL, "P.ID", "/portfolios1 P");
*
* //assertNotNull(secIndex); assertNotNull(idIndex); assertTrue(idIndex instanceof
* CompactRangeIndex); SelectResults resultsWithIndex = (SelectResults) query.execute();
*
* assertIndexDetailsEquals(resultsNoIndex.size(), resultsWithIndex.size());
* assertIndexDetailsEquals(limit, resultsWithIndex.size());
*
* int expectedId = 6; Iterator iterator = resultsNoIndex.iterator(); while (iterator.hasNext()) {
* Portfolio p = (Portfolio) iterator.next(); assertIndexDetailsEquals(expectedId++, p.ID); }
*
* //check order by results expectedId = 6; iterator = resultsWithIndex.iterator(); while
* (iterator.hasNext()) { Portfolio p = (Portfolio) iterator.next();
* assertIndexDetailsEquals(expectedId++, p.ID); } }
*/
@Test
public void testNotLimitAtIndexLevelForMultiSingleIndexAndClauseUsage() throws Exception {
// try {
Query query;
SelectResults result;
Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
for (int i = 15; i > 0; i--) {
Portfolio p = new Portfolio(i);
// CacheUtils.log(p);
p.positions.clear();
p.positions.put("IBM", new Position("IBM", i));
region.put("KEY" + i, p);
}
String queryString = "<trace>SELECT * FROM /portfolios1 P, P.positions.values POS WHERE P.ID > 4 and P.ID < 11 AND P.ID != 8 LIMIT 5";
query = qs.newQuery(queryString);
SelectResults resultsNoIndex = (SelectResults) query.execute();
// Create Index on ID and secId
Index secIndex = qs.createIndex("secIdIndex", "pos.secId", "/portfolios1 p, p.positions.values pos");
Index idIndex = qs.createIndex("idIndex", IndexType.FUNCTIONAL, "P.ID", "/portfolios1 P, P.positions.values pos");
// assertNotNull(secIndex);
assertNotNull(idIndex);
SelectResults resultsWithIndex = (SelectResults) query.execute();
assertEquals(resultsNoIndex.size(), resultsWithIndex.size());
}
use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.
the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForResultBag.
/**
* 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 testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForResultBag() {
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 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.data.Portfolio in project geode by apache.
the class LimitClauseJUnitTest method testLimitJunctionOnRangeIndexedFieldWithAndClauseRangeIndexedField.
@Test
public void testLimitJunctionOnRangeIndexedFieldWithAndClauseRangeIndexedField() throws Exception {
Query query;
SelectResults result;
Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
for (int i = 0; i <= 15; i++) {
Portfolio p = new Portfolio(i);
p.shortID = 1;
p.positions.clear();
p.positions.put("IBM", new Position("IBM", i));
region.put("KEY" + i, p);
}
for (int i = 21; i < 100; i++) {
Portfolio p = new Portfolio(i);
p.shortID = 2;
p.positions.clear();
p.positions.put("VMW", new Position("VMW", i));
region.put("KEY" + i, p);
}
for (int i = 16; i < 21; i++) {
Portfolio p = new Portfolio(i);
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", IndexType.FUNCTIONAL, "P.ID", "/portfolios1 P, P.positions.values POS");
Index shortIdIndex = qs.createIndex("shortIdIndex", IndexType.FUNCTIONAL, "P.shortID", "/portfolios1 P, P.positions.values POS");
String queryString = "SELECT * FROM /portfolios1 P WHERE P.ID > 9 AND P.ID < 21 AND P.shortID = 2 LIMIT 5";
query = qs.newQuery(queryString);
assertNotNull(idIndex);
SelectResults resultsWithIndex = (SelectResults) query.execute();
// assertFalse(observer.limitAppliedAtIndex);
assertEquals(5, resultsWithIndex.size());
}
Aggregations