use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class Query651 method cmdExecute.
@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException, InterruptedException {
// Based on MessageType.DESTROY
// Added by gregp 10/18/05
serverConnection.setAsTrue(REQUIRES_RESPONSE);
serverConnection.setAsTrue(REQUIRES_CHUNKED_RESPONSE);
// Retrieve the data from the message parts
String queryString = clientMessage.getPart(0).getString();
long compiledQueryId = 0;
Object[] queryParams = null;
try {
if (clientMessage.getMessageType() == MessageType.QUERY_WITH_PARAMETERS) {
// Query with parameters supported from 6.6 onwards.
// Number of parameters.
int params = clientMessage.getPart(1).getInt();
// In case of native client there will be extra two parameters at 2 and 3 index.
int paramStartIndex = 2;
if (clientMessage.getNumberOfParts() > (1 + /* type */
1 + /* query string */
1 + /* params length */
params)) {
int timeout = clientMessage.getPart(3).getInt();
serverConnection.setRequestSpecificTimeout(timeout);
paramStartIndex = 4;
}
// Get the query execution parameters.
queryParams = new Object[params];
for (int i = 0; i < queryParams.length; i++) {
queryParams[i] = clientMessage.getPart(i + paramStartIndex).getObject();
}
} else {
// need to take care while adding new message
if (clientMessage.getNumberOfParts() == 3) {
int timeout = clientMessage.getPart(2).getInt();
serverConnection.setRequestSpecificTimeout(timeout);
}
}
} catch (ClassNotFoundException cne) {
throw new QueryInvalidException(cne.getMessage() + queryString);
}
if (logger.isDebugEnabled()) {
logger.debug("{}: Received query request from {} queryString: {}{}", serverConnection.getName(), serverConnection.getSocketString(), queryString, (queryParams != null ? (" with num query parameters :" + queryParams.length) : ""));
}
try {
// Create query
QueryService queryService = serverConnection.getCachedRegionHelper().getCache().getLocalQueryService();
org.apache.geode.cache.query.Query query = null;
if (queryParams != null) {
// Its a compiled query.
CacheClientNotifier ccn = serverConnection.getAcceptor().getCacheClientNotifier();
query = ccn.getCompiledQuery(queryString);
if (query == null) {
// This is first time the query is seen by this server.
query = queryService.newQuery(queryString);
ccn.addCompiledQuery((DefaultQuery) query);
}
ccn.getStats().incCompiledQueryUsedCount(1);
((DefaultQuery) query).setLastUsed(true);
} else {
query = queryService.newQuery(queryString);
}
Set regionNames = ((DefaultQuery) query).getRegionsInQuery(queryParams);
// Authorization check
QueryOperationContext queryContext = null;
AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
if (authzRequest != null) {
queryContext = authzRequest.queryAuthorize(queryString, regionNames, queryParams);
String newQueryString = queryContext.getQuery();
if (queryString != null && !queryString.equals(newQueryString)) {
query = queryService.newQuery(newQueryString);
queryString = newQueryString;
regionNames = queryContext.getRegionNames();
if (regionNames == null) {
regionNames = ((DefaultQuery) query).getRegionsInQuery(null);
}
}
}
processQueryUsingParams(clientMessage, query, queryString, regionNames, start, null, queryContext, serverConnection, true, queryParams);
} catch (QueryInvalidException e) {
throw new QueryInvalidException(e.getMessage() + queryString);
}
}
use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class CompiledInDUnitTest method verifyQuery.
void verifyQuery(final int numExpectedEntries, final String queryString, Object[] bindArguments) {
QueryService qs = getCache().getQueryService();
Query query = null;
SelectResults sr = null;
try {
query = qs.newQuery(queryString);
sr = (SelectResults) query.execute(bindArguments);
Iterator iterator = sr.iterator();
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail("Failed to execute query, " + ex.getMessage());
}
Assert.assertEquals(numExpectedEntries, sr.size());
}
use of org.apache.geode.cache.query.QueryService 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.QueryService in project geode by apache.
the class LikePredicateJUnitTest method executeInvalidQueryWithLike.
private void executeInvalidQueryWithLike(boolean useBindParam) {
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++) {
region.put("key-" + i, new Portfolio(i));
}
region.put("key-" + 11, new PortfolioModifiedStatus(11));
QueryService qs = cache.getQueryService();
Query[] q = new Query[4];
SelectResults[][] results = new SelectResults[2][2];
String predicate = "";
String predicate2 = "";
if (useBindParam) {
predicate = "$1";
predicate2 = "$1";
} else {
predicate = " '%nactive'";
predicate2 = " 'inactive'";
}
boolean exceptionThrown = false;
q[0] = qs.newQuery("SELECT distinct * FROM /pos ps WHERE ps.status like " + predicate);
q[1] = qs.newQuery("SELECT distinct * FROM /pos ps WHERE ps.status = " + predicate2);
q[2] = qs.newQuery("SELECT distinct * FROM /pos ps WHERE NOT (ps.status like " + predicate + ")");
q[3] = qs.newQuery("SELECT distinct * FROM /pos ps WHERE NOT (ps.status = " + predicate2 + ")");
try {
if (useBindParam) {
results[0][0] = (SelectResults) q[0].execute(new Object[] { "%nactive" });
results[0][1] = (SelectResults) q[1].execute(new Object[] { "inactive" });
results[1][0] = (SelectResults) q[2].execute(new Object[] { "%nactive" });
results[1][1] = (SelectResults) q[3].execute(new Object[] { "inactive" });
} else {
results[0][0] = (SelectResults) q[0].execute();
results[0][1] = (SelectResults) q[1].execute();
results[1][0] = (SelectResults) q[2].execute();
results[1][1] = (SelectResults) q[3].execute();
}
} catch (Exception e) {
exceptionThrown = true;
fail("Query execution failed " + e);
}
assertTrue(results[0][0].size() > 0);
assertTrue(CacheUtils.compareResultsOfWithAndWithoutIndex(results));
assertFalse("Query exception should not have been thrown", exceptionThrown);
try {
qs.createIndex("IDindex", "ps.ID", "/pos ps");
} catch (Exception e) {
fail("Index creation failed");
}
exceptionThrown = false;
try {
if (useBindParam) {
results[0][0] = (SelectResults) q[0].execute(new Object[] { "%nactive" });
results[0][1] = (SelectResults) q[1].execute(new Object[] { "inactive" });
results[1][0] = (SelectResults) q[2].execute(new Object[] { "%nactive" });
results[1][1] = (SelectResults) q[3].execute(new Object[] { "inactive" });
} else {
results[0][0] = (SelectResults) q[0].execute();
results[0][1] = (SelectResults) q[1].execute();
results[1][0] = (SelectResults) q[2].execute();
results[1][1] = (SelectResults) q[3].execute();
}
} catch (Exception e) {
exceptionThrown = true;
fail("Query execution failed " + e);
}
assertTrue(results[0][0].size() > 0);
assertTrue(CacheUtils.compareResultsOfWithAndWithoutIndex(results));
assertFalse("Query exception should not have been thrown", exceptionThrown);
}
use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class LikePredicateJUnitTest method likePercentageTerminated_1.
/**
* Tests simple % terminated pattern with atleast one preceding character
*
* @throws Exception
*/
private void likePercentageTerminated_1(boolean useBindParam) 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 (useBindParam) {
predicate = "$1";
} else {
predicate = " 'abc%'";
}
q = qs.newQuery("SELECT distinct * FROM /pos ps WHERE ps.status like " + predicate);
if (useBindParam) {
results = (SelectResults) q.execute(new Object[] { "abc%" });
} 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);
// 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 (useBindParam) {
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);
}
Aggregations