use of org.apache.geode.cache.query.data.Portfolio 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);
}
use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.
the class LikePredicateJUnitTest method testLikeWithOtherIndexedField.
/**
* Query with index on other fields.
*
* @throws Exception
*/
@Test
public void testLikeWithOtherIndexedField() throws Exception {
Cache cache = CacheUtils.getCache();
AttributesFactory attributesFactory = new AttributesFactory();
RegionAttributes regionAttributes = attributesFactory.create();
Region region = cache.createRegion("pos", regionAttributes);
String base = "abc";
for (int i = 1; i <= 10; i++) {
Portfolio pf = new Portfolio(i);
pf.pkid = "1";
if ((i % 4) == 0) {
pf.status = base;
} else if ((i <= 2)) {
pf.pkid = "2";
}
region.put(new Integer(i), pf);
}
QueryService qs = cache.getQueryService();
Query q;
SelectResults results;
SelectResults expectedResults;
int expectedResultSize = 2;
q = qs.newQuery(" SELECT * FROM /pos ps WHERE ps.status like '%b%'");
results = (SelectResults) q.execute();
if (results.size() != expectedResultSize) {
fail("Unexpected result. expected :" + expectedResultSize + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos ps WHERE ps.status like '%b%' or ps.pkid = '2' ");
results = (SelectResults) q.execute();
if (results.size() != (expectedResultSize * 2)) {
fail("Unexpected result. expected :" + (expectedResultSize * 2) + " found : " + results.size());
}
// Query to be compared with indexed results.
q = qs.newQuery(" SELECT * FROM /pos ps WHERE ps.status like '%b%' and ps.pkid = '1' ");
expectedResults = (SelectResults) q.execute();
if (expectedResults.size() != expectedResultSize) {
fail("Unexpected result. expected :" + expectedResultSize + " found : " + expectedResults.size());
}
// Create Index
qs.createIndex("pkid", IndexType.FUNCTIONAL, "ps.pkid", "/pos ps");
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexCalled = false;
public void afterIndexLookup(Collection results) {
indexCalled = true;
}
public void endQuery() {
assertTrue(indexCalled);
}
});
results = (SelectResults) q.execute();
SelectResults[][] rs = new SelectResults[][] { { results, expectedResults } };
// rs[0][1] = expectedResults;
if (results.size() != expectedResultSize) {
fail("Unexpected result. expected :" + expectedResultSize + " found : " + results.size());
}
// compare results.
CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
q = qs.newQuery(" SELECT * FROM /pos ps WHERE ps.status like '%b%' or ps.pkid = '2' ");
results = (SelectResults) q.execute();
if (results.size() != (expectedResultSize * 2)) {
fail("Unexpected result. expected :" + (expectedResultSize * 2) + " found : " + results.size());
}
QueryObserverHolder.setInstance(old);
}
use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.
the class LikePredicateJUnitTest method likePercentageTerminated_3.
/**
* Tests a simple % terminated like predicate with an OR condition
*
* @throws Exception
*/
private void likePercentageTerminated_3(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);
}
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 (useBindPrm) {
predicate = "$1";
} else {
predicate = " 'abc%'";
}
q = qs.newQuery("SELECT distinct * FROM /pos ps WHERE ps.status like " + predicate + " OR ps.ID > 6");
if (useBindPrm) {
results = (SelectResults) q.execute(new Object[] { "abc%" });
} else {
results = (SelectResults) q.execute();
}
ResultsBag bag = new ResultsBag(null);
for (int i = 1; i < 11; ++i) {
if (i != 6) {
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() {
assertFalse(indexCalled);
}
});
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() {
for (int i = 0; i < indexCalled.length; ++i) {
assertTrue(indexCalled[i]);
}
}
});
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);
}
use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.
the class LikePredicateJUnitTest method equalityForm_2.
/**
* Tests simple \% or \ _ terminated string which in effect means equality
*
* @throws Exception
*/
private void equalityForm_2(boolean useBindPrms) throws Exception {
Cache cache = CacheUtils.getCache();
AttributesFactory attributesFactory = new AttributesFactory();
RegionAttributes regionAttributes = attributesFactory.create();
Region region = cache.createRegion("pos", regionAttributes);
String str = "d_";
String base = "abc";
for (int i = 1; i < 6; ++i) {
Portfolio pf = new Portfolio(i);
pf.status = base + str;
region.put(new Integer(i), pf);
}
base = "abc";
str = "d%";
for (int i = 6; i < 11; ++i) {
Portfolio pf = new Portfolio(i);
pf.status = base + str;
region.put(new Integer(i), pf);
}
QueryService qs = cache.getQueryService();
Query q, q1;
SelectResults results;
SelectResults expectedResults;
String predicate = "";
if (useBindPrms) {
predicate = "$1";
} else {
predicate = " 'abcd\\_'";
}
q = qs.newQuery("SELECT distinct * FROM /pos ps WHERE ps.status like " + predicate);
if (useBindPrms) {
results = (SelectResults) q.execute(new Object[] { "abcd\\_" });
} 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);
predicate = "";
if (useBindPrms) {
predicate = "$1";
} else {
predicate = " 'abcd\\%'";
}
q1 = qs.newQuery("SELECT distinct * FROM /pos ps WHERE ps.status like " + predicate);
if (useBindPrms) {
results = (SelectResults) q1.execute(new Object[] { "abcd\\%" });
} else {
results = (SelectResults) q1.execute();
}
bag = new ResultsBag(null);
for (int i = 6; i < 11; ++i) {
bag.add(region.get(new Integer(i)));
}
SelectResults expectedResults1 = new ResultsCollectionWrapper(new ObjectTypeImpl(Object.class), bag.asSet());
SelectResults[][] rs1 = new SelectResults[][] { { results, expectedResults1 } };
CacheUtils.compareResultsOfWithAndWithoutIndex(rs1, 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 (useBindPrms) {
results = (SelectResults) q.execute(new Object[] { "abcd\\_" });
} else {
results = (SelectResults) q.execute();
}
rs[0][0] = results;
rs[0][1] = expectedResults;
CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
if (useBindPrms) {
results = (SelectResults) q1.execute(new Object[] { "abcd\\%" });
} else {
results = (SelectResults) q1.execute();
}
rs1[0][0] = results;
rs1[0][1] = expectedResults1;
CacheUtils.compareResultsOfWithAndWithoutIndex(rs1, this);
QueryObserverHolder.setInstance(old);
}
use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.
the class LikePredicateJUnitTest method testLikeWithOtherIndexedField2.
/**
* Query with index on other fields.
*
* @throws Exception
*/
@Test
public void testLikeWithOtherIndexedField2() throws Exception {
Cache cache = CacheUtils.getCache();
AttributesFactory attributesFactory = new AttributesFactory();
RegionAttributes regionAttributes = attributesFactory.create();
Region region = cache.createRegion("pos", regionAttributes);
int size = 10;
String base = "abc";
for (int i = 1; i <= size; i++) {
Portfolio pf = new Portfolio(i);
pf.pkid = "1";
if ((i % 4) == 0) {
pf.status = base;
} else if ((i <= 2)) {
pf.pkid = "2";
}
region.put(new Integer(i), pf);
}
QueryService qs = cache.getQueryService();
Query q;
SelectResults results;
SelectResults expectedResults;
int expectedResultSize = 2;
q = qs.newQuery(" SELECT * FROM /pos ps WHERE ps.status like '%b%'");
results = (SelectResults) q.execute();
if (results.size() != expectedResultSize) {
fail("Unexpected result. expected :" + expectedResultSize + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos ps WHERE ps.status like '%b%' or ps.pkid = '2' ");
results = (SelectResults) q.execute();
if (results.size() != (expectedResultSize * 2)) {
fail("Unexpected result. expected :" + (expectedResultSize * 2) + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos ps WHERE ps.status like '%b%' or ps.ID > 0 ");
results = (SelectResults) q.execute();
if (results.size() != size) {
fail("Unexpected result. expected :" + size + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos ps WHERE ps.status like '%b%' or ps.ID > 4 ");
results = (SelectResults) q.execute();
if (results.size() != 7) {
fail("Unexpected result. expected :" + 7 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos ps WHERE ps.status like '%b%' and ps.ID > 3 ");
results = (SelectResults) q.execute();
if (results.size() != 2) {
fail("Unexpected result. expected :" + 2 + " found : " + results.size());
}
// Query to be compared with indexed results.
q = qs.newQuery(" SELECT * FROM /pos ps WHERE ps.status like '%b%' and ps.pkid = '1' ");
expectedResults = (SelectResults) q.execute();
if (expectedResults.size() != expectedResultSize) {
fail("Unexpected result. expected :" + expectedResultSize + " found : " + expectedResults.size());
}
// Create Index
qs.createIndex("pkid", IndexType.FUNCTIONAL, "ps.pkid", "/pos ps");
qs.createIndex("status", IndexType.FUNCTIONAL, "ps.status", "/pos ps");
qs.createIndex("id", IndexType.FUNCTIONAL, "ps.ID", "/pos ps");
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexCalled = false;
public void afterIndexLookup(Collection results) {
indexCalled = true;
}
public void endQuery() {
assertTrue(indexCalled);
}
});
results = (SelectResults) q.execute();
SelectResults[][] rs = new SelectResults[][] { { results, expectedResults } };
// rs[0][1] = expectedResults;
if (results.size() != expectedResultSize) {
fail("Unexpected result. expected :" + expectedResultSize + " found : " + results.size());
}
// compare results.
CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
q = qs.newQuery(" SELECT * FROM /pos ps WHERE ps.status like '_b_' or ps.pkid = '2' ");
results = (SelectResults) q.execute();
if (results.size() != (expectedResultSize * 2)) {
fail("Unexpected result. expected :" + (expectedResultSize * 2) + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos ps WHERE ps.status like '%b%' or ps.ID > 0 ");
results = (SelectResults) q.execute();
if (results.size() != size) {
fail("Unexpected result. expected :" + size + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos ps WHERE ps.status like '%b%' or ps.ID > 4 ");
results = (SelectResults) q.execute();
if (results.size() != 7) {
fail("Unexpected result. expected :" + 7 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos ps WHERE ps.status like '%b%' and ps.ID > 3 ");
results = (SelectResults) q.execute();
if (results.size() != 2) {
fail("Unexpected result. expected :" + 2 + " found : " + results.size());
}
QueryObserverHolder.setInstance(old);
}
Aggregations