use of org.apache.geode.cache.RegionAttributes 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.RegionAttributes 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.RegionAttributes 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.RegionAttributes 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);
}
use of org.apache.geode.cache.RegionAttributes 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();
}
Aggregations