use of org.apache.geode.cache.query.data.Position in project geode by apache.
the class LimitClauseJUnitTest method testLimitJunctionOnCompactRangeIndexedFieldWithAndClauseOnNonIndexedField.
@Test
public void testLimitJunctionOnCompactRangeIndexedFieldWithAndClauseOnNonIndexedField() 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.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(i);
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");
String queryString = "SELECT * FROM /portfolios1 P, P.positions.values POS WHERE P.ID > 9 AND P.ID < 21 AND POS.secId = 'VMW' 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.data.Position in project geode by apache.
the class BugJUnitTest method testBug36659.
@Test
public void testBug36659() throws Exception {
// Task ID: NQIU 9
CacheUtils.getQueryService();
String queries = "select distinct p.x from (select distinct x, pos from /pos x, x.positions.values pos) p, (select distinct * from /pos/positions rtPos where rtPos.secId = p.pos.secId)";
Region r = CacheUtils.getRegion("/pos");
Region r1 = r.createSubregion("positions", new AttributesFactory().createRegionAttributes());
r1.put("1", new Position("SUN", 2.272));
r1.put("2", new Position("IBM", 2.272));
r1.put("3", new Position("YHOO", 2.272));
r1.put("4", new Position("GOOG", 2.272));
r1.put("5", new Position("MSFT", 2.272));
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries);
CacheUtils.getLogger().info("Executing query: " + queries);
// DebuggerSupport.waitForJavaDebugger(CacheUtils.getLogger());
SelectResults rs = (SelectResults) q.execute();
assertTrue("Resultset size should be > 0", rs.size() > 0);
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.apache.geode.cache.query.data.Position in project geode by apache.
the class CopyOnReadQueryJUnitTest method createData.
/**
*
* @param region
* @param numObjects
* @param objectsAndResultsMultiplier number of similar objects to put into the cache so that
* results from queries will be satisfied by the multiple
*/
private void createData(Region region, int numObjects, int objectsAndResultsMultiplier) {
for (int i = 0; i < numObjects; i++) {
for (int j = 0; j < objectsAndResultsMultiplier; j++) {
int regionKey = i * objectsAndResultsMultiplier + j;
Portfolio p = new Portfolio(regionKey);
p.indexKey = i;
p.status = "testStatus";
p.positions = new HashMap();
p.positions.put("" + 1, new Position("" + i, i));
region.put("key-" + regionKey, p);
}
}
}
use of org.apache.geode.cache.query.data.Position in project geode by apache.
the class LikePredicateJUnitTest method testUndefined.
@Test
public void testUndefined() throws Exception {
String[] queries = { "select * from /pos where status.toUpperCase() like '%ACT%'", "select * from /pos where status.toUpperCase() like 'ACT%'", "select * from /pos where status.toUpperCase() like '%IVE'", "select * from /pos where status.toUpperCase() like 'ACT_VE'", "select * from /pos where status like '%CT%'", "select * from /pos where status like 'ACT%'", "select * from /pos where status like 'ACT_VE'", "select * from /pos where position1.secId like '%B%'", "select * from /pos where position1.secId like 'IB%'", "select * from /pos where position1.secId like 'I_M'", "select * from /pos p, p.positions.values pos where pos.secId like '%B%'", "select * from /pos p, p.positions.values pos where pos.secId like 'IB%'", "select * from /pos p, p.positions.values pos where pos.secId like 'I_M'" };
Cache cache = CacheUtils.getCache();
AttributesFactory attributesFactory = new AttributesFactory();
RegionAttributes regionAttributes = attributesFactory.create();
Region region = cache.createRegion("pos", regionAttributes);
for (int i = 0; i < 10; i++) {
Portfolio p = new Portfolio(i);
if (i % 2 == 0) {
p.status = null;
p.position1 = null;
p.positions = null;
} else {
p.position1 = new Position("IBM", 0);
p.status = "ACTIVE";
}
region.put("key-" + i, p);
}
SelectResults[][] res = new SelectResults[queries.length][2];
QueryService qs = CacheUtils.getCache().getQueryService();
SelectResults results = null;
for (int i = 0; i < queries.length; i++) {
Query q = qs.newQuery(queries[i]);
try {
results = (SelectResults) q.execute();
res[i][0] = results;
} catch (Exception e) {
fail("Query execution failed for query " + queries[i] + " with exception: " + e);
}
if (i < 10) {
assertEquals("Query " + queries[i] + " should return 5 results and not " + results.size(), 5, results.size());
} else {
assertEquals("Query " + queries[i] + " should return 1 results and not " + results.size(), 1, results.size());
}
}
try {
qs.createIndex("status", "status", region.getFullPath());
qs.createIndex("position1secId", "pos1.secId", region.getFullPath());
qs.createIndex("secId", "pos.secId", region.getFullPath() + " pos, pos.secId");
} catch (Exception e) {
fail("Index creation failed");
}
for (int i = 0; i < queries.length; i++) {
Query q = qs.newQuery(queries[i]);
try {
results = (SelectResults) q.execute();
res[i][1] = results;
} catch (Exception e) {
fail("Query execution failed for query " + queries[i] + " with exception: " + e);
}
if (i < 10) {
assertEquals("Query " + queries[i] + " should return 5 results and not " + results.size(), 5, results.size());
} else {
assertEquals("Query " + queries[i] + " should return 1 results and not " + results.size(), 1, results.size());
}
}
CacheUtils.compareResultsOfWithAndWithoutIndex(res);
}
use of org.apache.geode.cache.query.data.Position in project geode by apache.
the class IumMultConditionJUnitTest method testComparisonBetnWithAndWithoutIndexCreation.
@Test
public void testComparisonBetnWithAndWithoutIndexCreation() throws Exception {
Region region = CacheUtils.createRegion("pos", Portfolio.class);
for (int i = 0; i < 4; i++) {
region.put("" + i, new Portfolio(i));
}
QueryService qs;
qs = CacheUtils.getQueryService();
String[] queries = { "SELECT DISTINCT * FROM /pos pf, positions.values pos where pf.status='active' and pos.secId= 'IBM' and ID = 0" };
SelectResults[][] sr = new SelectResults[queries.length][2];
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
sr[i][0] = (SelectResults) q.execute();
if (!observer.isIndexesUsed) {
CacheUtils.log("NO INDEX USED");
} else {
fail("Indexes used !!!!?");
}
// CacheUtils.log(Utils.printResult(r));
resType1 = (StructType) (sr[i][0]).getCollectionType().getElementType();
resSize1 = ((sr[i][0]).size());
// CacheUtils.log(resType1);
strg1 = resType1.getFieldNames();
// CacheUtils.log(strg1[0]);
// CacheUtils.log(strg1[1]);
set1 = ((sr[i][0]).asSet());
Iterator iter = set1.iterator();
while (iter.hasNext()) {
Struct stc1 = (Struct) iter.next();
valPf1 = stc1.get(strg1[0]);
valPos1 = stc1.get(strg1[1]);
isActive1 = ((Portfolio) stc1.get(strg1[0])).isActive();
// CacheUtils.log(isActive1);
// CacheUtils.log(valPf1);
// CacheUtils.log(valPos1);
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
// Create an Index on status and execute the same query again.
qs = CacheUtils.getQueryService();
qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "pf.status", "/pos pf, pf.positions.values pos");
// Index index2 = (Index)qs.createIndex("secIdIndex",
// IndexType.FUNCTIONAL,"pos.secId","/pos pf, pf.positions.values pos");
qs.createIndex("IDIndex", IndexType.FUNCTIONAL, "pf.ID", "/pos pf, pf.positions.values pos");
String[] queries2 = { "SELECT DISTINCT * FROM /pos pf, positions.values pos where pf.status='active' and pos.secId= 'IBM' and ID = 0" };
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
QueryObserverImpl observer2 = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer2);
sr[i][1] = (SelectResults) q.execute();
if (observer2.isIndexesUsed == true) {
CacheUtils.log("YES INDEX IS USED!");
} else {
fail("Index NOT Used");
}
// CacheUtils.log(Utils.printResult(r2));
resType2 = (StructType) (sr[i][1]).getCollectionType().getElementType();
resSize2 = ((sr[i][1]).size());
// CacheUtils.log(resType2);
strg2 = resType2.getFieldNames();
// CacheUtils.log(strg2[0]);
// CacheUtils.log(strg2[1]);
set2 = ((sr[i][1]).asSet());
Iterator iter = set2.iterator();
while (iter.hasNext()) {
Struct stc2 = (Struct) iter.next();
valPf2 = stc2.get(strg2[0]);
valPos2 = stc2.get(strg2[1]);
isActive2 = ((Portfolio) stc2.get(strg2[0])).isActive();
// CacheUtils.log(valPf2);
// CacheUtils.log(valPos2);
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
// values of the iterator names used in the Query.
if ((resType1).equals(resType2)) {
CacheUtils.log("Both Search Results are of the same Type i.e.-->" + resType1);
} else {
fail("FAILED:Search result Type is different in both the cases");
}
if (resSize1 == resSize2 || resSize1 != 0) {
CacheUtils.log("Both Search Results are Non zero and are of Same Size i.e. Size= " + resSize1);
} else {
fail("FAILED:Search result Type is different in both the cases");
}
itert2 = set2.iterator();
itert1 = set1.iterator();
while (itert1.hasNext()) {
Struct stc2 = (Struct) itert2.next();
Struct stc1 = (Struct) itert1.next();
if (stc2.get(strg2[0]) != stc1.get(strg1[0]))
fail("FAILED: In both the Cases the first member of StructSet i.e. Portfolio are different. ");
if (stc2.get(strg2[1]) != stc1.get(strg1[1]))
fail("FAILED: In both the cases Positions are different");
if (!StringUtils.equals(((Position) stc2.get(strg2[1])).secId, ((Position) stc1.get(strg1[1])).secId))
fail("FAILED: In both the cases Positions secIds are different");
if (((Portfolio) stc2.get(strg2[0])).isActive() != ((Portfolio) stc1.get(strg1[0])).isActive())
fail("FAILED: Status of the Portfolios found are different");
if (((Portfolio) stc2.get(strg2[0])).getID() != ((Portfolio) stc1.get(strg1[0])).getID())
fail("FAILED: IDs of the Portfolios found are different");
}
CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
Aggregations