use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class LikePredicateJUnitTest method likePercentageTerminated_4.
/**
* Tests a simple % terminated like predicate with an AND condition
*
* @throws Exception
*/
private void likePercentageTerminated_4(boolean useBindPrm) throws Exception {
Cache cache = CacheUtils.getCache();
AttributesFactory attributesFactory = new AttributesFactory();
RegionAttributes regionAttributes = attributesFactory.create();
Region region = cache.createRegion("pos", regionAttributes);
String base = "abc";
String pattern = base;
// so we will get string like abcdcdcdcdcdc
for (int i = 1; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pattern += "dc";
pf.status = pattern;
region.put(new Integer(i), pf);
}
base = "abd";
pattern = base;
// so we will get string like abddcdcdcd
for (int i = 201; i < 400; ++i) {
Portfolio pf = new Portfolio(i);
pattern += "dc";
pf.status = pattern;
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 + " AND ps.ID > 2 AND ps.ID < 150");
if (useBindPrm) {
results = (SelectResults) q.execute(new Object[] { "abc%" });
} else {
results = (SelectResults) q.execute();
}
ResultsBag bag = new ResultsBag(null);
for (int i = 3; i < 150; ++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");
QueryObserver old = 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() {
assertTrue(indexCalled[0]);
assertFalse(indexCalled[1]);
}
});
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() {
// Only one indexed condition should be called
boolean indexInvoked = false;
for (int i = 0; i < indexCalled.length; ++i) {
indexInvoked = indexInvoked || indexCalled[i];
}
assertTrue(indexInvoked);
}
});
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.QueryService in project geode by apache.
the class LikePredicateJUnitTest method regexMetaChar.
/**
* Tests for regular expression meta chars. This has no special meaning with Like.
*
* @throws Exception
*/
private void regexMetaChar(boolean useBindPrms) throws Exception {
Cache cache = CacheUtils.getCache();
AttributesFactory attributesFactory = new AttributesFactory();
RegionAttributes regionAttributes = attributesFactory.create();
Region region = cache.createRegion("pos", regionAttributes);
String[] values = new String[] { "active", "act**ve", "ac+t+ve", "?act?ve", "act)ve^", "|+act(ve", "act*+|ve", "^+act.ve+^", "act[]ve", "act][ve", "act^[a-z]ve", "act/ve", "inactive", "acxtxve", "ac(tiv)e", "act()ive", "act{}ive", "act{ive" };
// Add values to region.
for (int i = 0; i < values.length; i++) {
region.put(new Integer(i), values[i]);
}
// Add % and _ with escape char.
region.put(new Integer(values.length + 1), "act%+ive");
region.put(new Integer(values.length + 2), "act_+ive");
QueryService qs = cache.getQueryService();
Query q;
SelectResults results;
for (int i = 0; i < values.length; i++) {
if (!useBindPrms) {
q = qs.newQuery("select p from /pos.values p where p like '" + values[i] + "'");
results = (SelectResults) q.execute();
} else {
q = qs.newQuery("select p from /pos.values p where p like $1");
results = (SelectResults) q.execute(new Object[] { values[i] });
}
List r = results.asList();
if (r.size() != 1 || !r.get(0).equals(values[i])) {
fail("Unexpected result. expected :" + values[i] + " for the like predicate: " + values[i] + " found : " + (r.size() == 1 ? r.get(0) : "Result size not equal to 1"));
}
}
// Create Index
qs.createIndex("p", IndexType.FUNCTIONAL, "p", "/pos.values p");
for (int i = 0; i < values.length; i++) {
if (!useBindPrms) {
q = qs.newQuery("select p from /pos.values p where p like '" + values[i] + "'");
results = (SelectResults) q.execute();
} else {
q = qs.newQuery("select p from /pos.values p where p like $1");
results = (SelectResults) q.execute(new Object[] { values[i] });
}
List r = results.asList();
if (r.size() != 1 || !r.get(0).equals(values[i])) {
fail("Unexpected result. expected :" + values[i] + " for the like predicate: " + values[i] + " found : " + (r.size() == 1 ? r.get(0) : "Result size not equal to 1"));
}
}
}
use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class LikePredicateJUnitTest method equalityForm_1.
/**
* Tests simple non % or non _ terminated string which in effect means equality
*
* @throws Exception
*/
private void equalityForm_1(boolean useBindPrms) 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 (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);
bag.add(region.get(new Integer(1)));
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() {
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);
QueryObserverHolder.setInstance(old);
}
use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class LikePredicateJUnitTest method testMultipleWhereClausesWithIndexes.
@Test
public void testMultipleWhereClausesWithIndexes() throws Exception {
Cache cache = CacheUtils.getCache();
AttributesFactory attributesFactory = new AttributesFactory();
RegionAttributes regionAttributes = attributesFactory.create();
Region region = cache.createRegion("pos", regionAttributes);
QueryService qs = cache.getQueryService();
Query q;
String[] queries = new String[] { " SELECT status, pkid FROM /pos WHERE status like 'inactive' and pkid like '1' ", " SELECT status, pkid FROM /pos WHERE status like 'active' or pkid like '1' ", " SELECT status, pkid FROM /pos WHERE status like 'in%' and pkid like '1' ", " SELECT status FROM /pos WHERE status like 'in%' or pkid like '1'", " SELECT pkid FROM /pos WHERE status like 'inact%' and pkid like '1%' ", " SELECT * FROM /pos WHERE status like 'inact%' or pkid like '1%' ", " SELECT * FROM /pos WHERE status like 'inactiv_' or pkid like '1%' ", " SELECT status, pkid FROM /pos WHERE status like '_nactive' or pkid like '1%' ", " SELECT * FROM /pos WHERE status like '_nac%ive' or pkid like '1%' ", " SELECT * FROM /pos WHERE status like 'in_ctive' or pkid like '1%' ", " SELECT * FROM /pos WHERE status like 'in_ctive' or pkid like '1_' ", " SELECT status, pkid FROM /pos WHERE status like '%ctive' and pkid like '1%' ", " SELECT * FROM /pos WHERE NOT (status like 'active' or pkid like '1') ", " SELECT * FROM /pos WHERE NOT (status like 'active' or pkid like '1%') ", " SELECT * FROM /pos WHERE NOT (status like 'acti%' and pkid like '1%') ", " SELECT * FROM /pos WHERE NOT (status like 'acti%' or pkid like '1%') ", " SELECT * FROM /pos WHERE NOT (status like 'active' and pkid like '1%') ", " SELECT * FROM /pos WHERE NOT (status like 'active' or pkid like '1%') ", " SELECT * FROM /pos WHERE NOT (status like 'active' or pkid like '11' or pkid like '1') ", " SELECT * FROM /pos WHERE status like '%' and pkid like '1%' ", " SELECT * FROM /pos WHERE pkid like '_'", " SELECT * FROM /pos WHERE status like '.*tive' ", " SELECT * FROM /pos WHERE pkid like '1+' ", " SELECT * FROM /pos WHERE unicodeṤtring like 'ṤṶẐ' ", " SELECT * FROM /pos WHERE unicodeṤtring like 'ṤṶ%' ", " SELECT * FROM /pos p, p.positions.values v WHERE v.secId like 'I%' ", " SELECT * FROM /pos p, p.positions.values v WHERE v.secId like '%L' ", " SELECT * FROM /pos p, p.positions.values v WHERE v.secId like 'A%L' " };
SelectResults[][] sr = new SelectResults[queries.length][2];
for (int i = 0; i < 20; i++) {
Portfolio pf = new Portfolio(i);
region.put(new Integer(i), pf);
}
for (int i = 0; i < queries.length; i++) {
q = qs.newQuery(queries[i]);
sr[i][0] = (SelectResults) q.execute();
}
// Create index.
qs.createIndex("pkidIndex", "pkid", "/pos");
qs.createIndex("statusIndex", "status", "/pos");
qs.createIndex("unicodeṤtringIndex", "unicodeṤtring", "/pos");
qs.createIndex("secIdIndex", "v.secId", "/pos p, p.positions.values v");
for (int i = 0; i < queries.length; i++) {
q = qs.newQuery(queries[i]);
try {
sr[i][1] = (SelectResults) q.execute();
} catch (Exception e) {
fail("Query execution failed for: " + queries[i]);
}
}
// compare results.
CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
SelectResults results = null;
q = qs.newQuery("SELECT status FROM /pos WHERE status like 'inactive' and pkid like '1' ");
results = (SelectResults) q.execute();
if (results.size() != 1) {
fail("Unexpected result. expected :" + 1 + " found : " + results.size());
}
q = qs.newQuery(" SELECT pkid FROM /pos WHERE status like 'active' or pkid like '1' ");
results = (SelectResults) q.execute();
if (results.size() != 11) {
fail("Unexpected result. expected :" + 11 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE status like 'in%' and pkid like '1' ");
results = (SelectResults) q.execute();
if (results.size() != 1) {
fail("Unexpected result. expected :" + 1 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE status like 'in%' or pkid like '1' ");
results = (SelectResults) q.execute();
if (results.size() != 10) {
fail("Unexpected result. expected :" + 10 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE status like 'inact%' and pkid like '1%' ");
results = (SelectResults) q.execute();
if (results.size() != 6) {
fail("Unexpected result. expected :" + 6 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE status like 'inact%' or pkid like '1%' ");
results = (SelectResults) q.execute();
if (results.size() != 15) {
fail("Unexpected result. expected :" + 15 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE status like 'inactiv_' or pkid like '1%' ");
results = (SelectResults) q.execute();
if (results.size() != 15) {
fail("Unexpected result. expected :" + 15 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE status like '_nactive' or pkid like '1%' ");
results = (SelectResults) q.execute();
if (results.size() != 15) {
fail("Unexpected result. expected :" + 15 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE status like '_nac%ive' or pkid like '1%' ");
results = (SelectResults) q.execute();
if (results.size() != 15) {
fail("Unexpected result. expected :" + 15 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE status like 'in_ctive' or pkid like '1%' ");
results = (SelectResults) q.execute();
if (results.size() != 15) {
fail("Unexpected result. expected :" + 15 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE status like 'in_ctive' or pkid like '1_' ");
results = (SelectResults) q.execute();
if (results.size() != 15) {
fail("Unexpected result. expected :" + 15 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE status like '%ctive' and pkid like '1%' ");
results = (SelectResults) q.execute();
if (results.size() != 11) {
fail("Unexpected result. expected :" + 11 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE NOT (status like 'active' or pkid like '1') ");
results = (SelectResults) q.execute();
if (results.size() != 9) {
fail("Unexpected result. expected :" + 9 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE NOT (status like 'active' or pkid like '1%') ");
results = (SelectResults) q.execute();
if (results.size() != 4) {
fail("Unexpected result. expected :" + 4 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE NOT (status like 'acti%' and pkid like '1%') ");
results = (SelectResults) q.execute();
if (results.size() != 15) {
fail("Unexpected result. expected :" + 15 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE NOT (status like 'acti%' or pkid like '1%') ");
results = (SelectResults) q.execute();
if (results.size() != 4) {
fail("Unexpected result. expected :" + 4 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE NOT (status like 'active' and pkid like '1%') ");
results = (SelectResults) q.execute();
if (results.size() != 15) {
fail("Unexpected result. expected :" + 15 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE NOT (status like 'active' or pkid like '1%') ");
results = (SelectResults) q.execute();
if (results.size() != 4) {
fail("Unexpected result. expected :" + 4 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE NOT (status like 'active' or pkid like '11' or pkid like '1') ");
results = (SelectResults) q.execute();
if (results.size() != 8) {
fail("Unexpected result. expected :" + 8 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE status like '%' and pkid like '1%' ");
results = (SelectResults) q.execute();
if (results.size() != 11) {
fail("Unexpected result. expected :" + 11 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE pkid like '_' ");
results = (SelectResults) q.execute();
if (results.size() != 10) {
fail("Unexpected result. expected :" + 10 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE pkid like '.*tive' ");
results = (SelectResults) q.execute();
if (results.size() != 0) {
fail("Unexpected result. expected :" + 0 + " found : " + results.size());
}
q = qs.newQuery(" SELECT * FROM /pos WHERE pkid like '1+' ");
results = (SelectResults) q.execute();
if (results.size() != 0) {
fail("Unexpected result. expected :" + 0 + " found : " + results.size());
}
}
use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class LikePredicateJUnitTest method removeIndexDuringQuery.
private void removeIndexDuringQuery(boolean isPr) throws Exception {
String regionName = "exampleRegion";
String name = "/" + regionName;
Cache cache = CacheUtils.getCache();
Region r1 = null;
if (isPr) {
r1 = cache.createRegionFactory(RegionShortcut.PARTITION).create(regionName);
} else {
r1 = cache.createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
}
QueryService qs = cache.getQueryService();
qs.createIndex("status", "status", name);
assertEquals(cache.getQueryService().getIndexes().size(), 1);
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexCalled = false;
public void afterIndexLookup(Collection results) {
indexCalled = true;
}
public void endQuery() {
assertTrue(indexCalled);
}
});
// set the test hook
IndexManager.testHook = new LikeQueryIndexTestHook();
for (int i = 0; i < 10; i++) {
r1.put("key-" + i, new Portfolio(i));
}
SelectResults[][] rs = new SelectResults[1][2];
String query = "select distinct * from " + name + " where status like 'act%'";
rs[0][0] = (SelectResults) cache.getQueryService().newQuery(query).execute();
assertEquals(5, rs[0][0].size());
// wait for remove to complete
ThreadUtils.join(LikeQueryIndexTestHook.th, 60 * 1000);
// The index should have been removed by now
assertEquals(0, cache.getQueryService().getIndexes().size());
// remove the test hook
IndexManager.testHook = null;
// create the same index again and execute the query
qs.createIndex("status", "status", name);
rs[0][1] = (SelectResults) cache.getQueryService().newQuery(query).execute();
assertEquals(5, rs[0][1].size());
CacheUtils.compareResultsOfWithAndWithoutIndex(rs);
QueryObserverHolder.setInstance(old);
}
Aggregations