use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.
the class IndexWithSngleFrmAndMultCondQryJUnitTest method testIndexSkipping.
@Test
public void testIndexSkipping() throws Exception {
Region region = CacheUtils.createRegion("pos", Portfolio.class);
for (int i = 0; i < 10; i++) {
region.put("" + i, new Portfolio(i));
}
QueryService qs;
qs = CacheUtils.getQueryService();
String[] queries = { "SELECT DISTINCT * FROM /pos pf, positions.values pos where pf.ID > 0 and pf.ID < 3 and pf.status='active' and pos.secId != null " };
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("How could index be present when not created!?");
}
// CacheUtils.log(Utils.printResult(r));
resType1 = (StructType) ((SelectResults) sr[i][0]).getCollectionType().getElementType();
resSize1 = (((SelectResults) sr[i][0]).size());
CacheUtils.log(resType1);
strg1 = resType1.getFieldNames();
set1 = (((SelectResults) 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();
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
// Create an Index on status and execute the same query again.
qs = CacheUtils.getQueryService();
Index index1 = qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "pf.status", "/pos pf");
Index index2 = (Index) qs.createIndex("secIdIndex", IndexType.FUNCTIONAL, "pos.secId", "/pos pf, pf.positions.values pos");
Index index3 = qs.createIndex("IDIndex", IndexType.FUNCTIONAL, "pf.ID", "/pos pf");
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) {
fail("FAILED: Index NOT Used");
}
assertTrue(observer2.indexesUsed.size() < 2);
resType2 = (StructType) ((SelectResults) sr[i][1]).getCollectionType().getElementType();
resSize2 = (((SelectResults) sr[i][1]).size());
strg2 = resType2.getFieldNames();
set2 = (((SelectResults) 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();
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
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("Search Results size is Non Zero and equal in both cases i.e. Size= " + resSize1);
} else {
fail("FAILED:Search result size 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 IndexWithSngleFrmAndMultCondQryJUnitTest method testCorrectOrCondResultWithMultiIndexes.
/**
* Test verifies the result of query with 'OR' conditions when separate indexes are used for two
* comparisons of OR clause. Like, "select * from /portfolio where ID > 0 OR status = 'active'"
* and we have indexes on ID and status both. Both indexes must be used.
*/
@Test
public void testCorrectOrCondResultWithMultiIndexes() throws Exception {
Region region = CacheUtils.createRegion("pos", Portfolio.class);
for (int i = 0; i < 10; i++) {
region.put("" + i, new Portfolio(i));
}
for (int i = 10; i < 20; i++) {
Portfolio p = new Portfolio(i);
if (i % 2 == 0) {
p.status = null;
}
region.put("" + i, p);
}
QueryService qs;
qs = CacheUtils.getQueryService();
String[] queries = { "SELECT * FROM /pos pf, positions.values pos where pf.ID > 0 OR pf.status='active' OR pos.secId != 'IBM'", "SELECT * FROM /pos pf where pf.ID > 0 OR pf.status='active'", "SELECT * FROM /pos pf where pf.ID > 0 OR pf.status LIKE 'act%'", "SELECT DISTINCT * FROM /pos pf where pf.ID > 0 OR pf.status='active' ORDER BY pf.status desc LIMIT 10", "SELECT * FROM /pos pf where pf.ID > 0 OR pf.status IN SET('active', 'inactive')" };
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) {
fail("How could index be present when not created!?");
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
// Create an Index on status and execute the same query again.
qs = CacheUtils.getQueryService();
Index index1 = qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "pf.status", "/pos pf");
Index index2 = qs.createIndex("secIdIndex", IndexType.FUNCTIONAL, "pos.secId", "/pos pf, pf.positions.values pos");
Index index3 = qs.createIndex("IDIndex", IndexType.FUNCTIONAL, "pf.ID", "/pos pf");
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 && !queries[i].contains("LIKE")) {
fail("FAILED: Index NOT Used for query : " + q.getQueryString());
} else if (!queries[i].contains("LIKE")) {
assertTrue(observer2.indexesUsed.size() >= 2);
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.
the class IndexWithSngleFrmAndMultCondQryJUnitTest method testIndexUsageIfOneFieldIndexedAndMoreThanOneUnindexed_Bug38032.
/**
* BugTest for Bug # 38032 Test index usage on PR region & Local region if the where clause
* contains three conditions but only one condition is indexed & the total number of from clause
* iterators is 1.
*
*/
@Test
public void testIndexUsageIfOneFieldIndexedAndMoreThanOneUnindexed_Bug38032() throws Exception {
AttributesFactory af = new AttributesFactory();
af.setValueConstraint(Portfolio.class);
RegionAttributes ra = af.createRegionAttributes();
Region region = CacheUtils.getCache().createRegion("pos", ra);
for (int i = 0; i < 5; i++) {
region.put("" + i, new Portfolio(i));
}
// As default generation of Portfolio objects
// The status is active for key =0 & key =2 & key =4
// The description is XXXX for key =1, key =3
// Let us make explitly
// Set createTime as 5 for three Portfolio objects of key =0 & key =1 & key=2;
// Description as XXXX for key =2 only.
// Thus out of 4 objects created only 1 object ( key =2 ) will have
// description = XXXX , status = active & time = 5
((Portfolio) region.get("0")).setCreateTime(5);
((Portfolio) region.get("1")).setCreateTime(5);
((Portfolio) region.get("2")).setCreateTime(5);
((Portfolio) region.get("2")).description = "XXXX";
int numSatisfyingOurCond = 0;
for (int i = 0; i < 5; i++) {
Portfolio pf = (Portfolio) region.get("" + i);
if (pf.description != null && pf.description.equals("XXXX") && pf.getCreateTime() == 5 && pf.isActive()) {
++numSatisfyingOurCond;
}
}
assertEquals(1, numSatisfyingOurCond);
executeQuery_2(region, true);
region.destroyRegion();
CacheUtils.closeCache();
CacheUtils.restartCache();
af = new AttributesFactory();
af.setValueConstraint(Portfolio.class);
PartitionAttributesFactory pfa = new PartitionAttributesFactory();
pfa.setRedundantCopies(0);
pfa.setTotalNumBuckets(1);
af.setPartitionAttributes(pfa.create());
ra = af.createRegionAttributes();
region = CacheUtils.getCache().createRegion("pos", ra);
for (int i = 0; i < 5; i++) {
region.put("" + i, new Portfolio(i));
}
// As default generation of Portfolio objects
// The status is active for key =0 & key =2 & key =4
// The description is XXXX for key =1, key =3
// Let us make explitly
// Set createTime as 5 for three Portfolio objects of key =0 & key =1 & key=2;
// Description as XXXX for key =2 only.
// Thus out of 4 objects created only 1 object ( key =2 ) will have
// description = XXXX , status = active & time = 5
Portfolio x = (Portfolio) region.get("0");
x.setCreateTime(5);
region.put("0", x);
x = (Portfolio) region.get("1");
x.setCreateTime(5);
region.put("1", x);
x = (Portfolio) region.get("2");
x.setCreateTime(5);
x.description = "XXXX";
region.put("2", x);
numSatisfyingOurCond = 0;
for (int i = 0; i < 5; i++) {
Portfolio pf = (Portfolio) region.get("" + i);
if (pf.description != null && pf.description.equals("XXXX") && pf.getCreateTime() == 5 && pf.isActive()) {
++numSatisfyingOurCond;
}
}
assertEquals(1, numSatisfyingOurCond);
executeQuery_2(region, false);
region.destroyRegion();
}
use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.
the class IndexWithSngleFrmAndMultCondQryJUnitTest method testIndexUsageIfIndexesGreaterThanFieldsInQueryWhereClauseWithOneIterator.
/**
* Test index usage on PR region & Local region if the total number of indexes created are more
* than the fields used in the where clause of the query and the number of from clause iterators
* is One
*/
@Test
public void testIndexUsageIfIndexesGreaterThanFieldsInQueryWhereClauseWithOneIterator() throws Exception {
AttributesFactory af = new AttributesFactory();
af.setValueConstraint(Portfolio.class);
RegionAttributes ra = af.createRegionAttributes();
Region region = CacheUtils.getCache().createRegion("pos", ra);
for (int i = 0; i < 4; i++) {
region.put("" + i, new Portfolio(i));
}
executeQuery_1(region, true);
region.destroyRegion();
CacheUtils.closeCache();
CacheUtils.restartCache();
af = new AttributesFactory();
af.setValueConstraint(Portfolio.class);
PartitionAttributesFactory pfa = new PartitionAttributesFactory();
pfa.setRedundantCopies(0);
pfa.setTotalNumBuckets(1);
af.setPartitionAttributes(pfa.create());
ra = af.createRegionAttributes();
region = CacheUtils.getCache().createRegion("pos", ra);
for (int i = 0; i < 4; i++) {
Portfolio x = new Portfolio(i);
region.put("" + i, x);
}
executeQuery_1(region, false);
region.destroyRegion();
}
use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.
the class IndexCreationJUnitTest method testIndexInitializationForOverFlowRegions.
@Test
public void testIndexInitializationForOverFlowRegions() throws Exception {
InternalDistributedSystem.getAnyInstance().disconnect();
File file = new File("persistData0");
file.mkdir();
{
Properties props = new Properties();
props.setProperty(NAME, "test");
props.setProperty(MCAST_PORT, "0");
props.setProperty(STATISTIC_SAMPLING_ENABLED, "true");
props.setProperty(ENABLE_TIME_STATISTICS, "true");
props.setProperty(CACHE_XML_FILE, getClass().getResource("index-recovery-overflow.xml").toURI().getPath());
DistributedSystem ds = DistributedSystem.connect(props);
// Create the cache which causes the cache-xml-file to be parsed
Cache cache = CacheFactory.create(ds);
QueryService qs = cache.getQueryService();
Region region = cache.getRegion("mainReportRegion");
for (int i = 0; i < 100; i++) {
Portfolio pf = new Portfolio(i);
pf.setCreateTime(i);
region.put("" + i, pf);
}
IndexStatistics is1 = qs.getIndex(region, "status").getStatistics();
assertEquals(2, is1.getNumberOfKeys());
assertEquals(100, is1.getNumberOfValues());
IndexStatistics is2 = qs.getIndex(region, "ID").getStatistics();
assertEquals(100, is2.getNumberOfKeys());
assertEquals(100, is2.getNumberOfValues());
// verify that a query on the creation time works as expected
SelectResults results = (SelectResults) qs.newQuery("<trace>SELECT * FROM /mainReportRegion.entrySet mr Where mr.value.createTime > 1L and mr.value.createTime < 3L").execute();
assertEquals("OQL index results did not match", 1, results.size());
cache.close();
ds.disconnect();
}
{
Properties props = new Properties();
props.setProperty(NAME, "test");
props.setProperty(MCAST_PORT, "0");
props.setProperty(STATISTIC_SAMPLING_ENABLED, "true");
props.setProperty(ENABLE_TIME_STATISTICS, "true");
props.setProperty(CACHE_XML_FILE, getClass().getResource("index-recovery-overflow.xml").toURI().getPath());
DistributedSystem ds = DistributedSystem.connect(props);
Cache cache = CacheFactory.create(ds);
QueryService qs = cache.getQueryService();
Region region = cache.getRegion("mainReportRegion");
assertTrue("Index initialization time should not be 0.", ((LocalRegion) region).getCachePerfStats().getIndexInitializationTime() > 0);
IndexStatistics is1 = qs.getIndex(region, "status").getStatistics();
assertEquals(2, is1.getNumberOfKeys());
assertEquals(100, is1.getNumberOfValues());
IndexStatistics is2 = qs.getIndex(region, "ID").getStatistics();
assertEquals(100, is2.getNumberOfKeys());
assertEquals(100, is2.getNumberOfValues());
// verify that a query on the creation time works as expected
SelectResults results = (SelectResults) qs.newQuery("<trace>SELECT * FROM /mainReportRegion.entrySet mr Where mr.value.createTime > 1L and mr.value.createTime < 3L").execute();
assertEquals("OQL index results did not match", 1, results.size());
ds.disconnect();
FileUtils.deleteDirectory(file);
}
}
Aggregations