use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class PRQueryNumThreadsJUnitTest method testOrderByQuery.
@Test
public void testOrderByQuery() throws Exception {
Region region = PartitionedRegionTestHelper.createPartitionedRegion(regionName, "100", 0);
String[] values = new String[100];
for (int j = 0; j < 100; j++) {
values[j] = new String("" + j);
}
PRQueryProcessor.TEST_NUM_THREADS = 10;
try {
populateData(region, values);
String queryString = "Select distinct p from /" + region.getName() + " p order by p";
Query query = region.getCache().getQueryService().newQuery(queryString);
SelectResults sr = (SelectResults) query.execute();
Assert.assertTrue(sr.size() == 100);
} finally {
PRQueryProcessor.TEST_NUM_THREADS = 0;
region.close();
}
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class PRQueryNumThreadsJUnitTest method testQueryWithNullProjectionValue.
@Test
public void testQueryWithNullProjectionValue() throws Exception {
Region region = PartitionedRegionTestHelper.createPartitionedRegion(regionName, "100", 0);
int size = 10;
HashMap value = null;
for (int j = 0; j < size; j++) {
value = new HashMap();
value.put("account" + j, "account" + j);
region.put("" + j, value);
}
String queryString = "Select p.get('account') from /" + region.getName() + " p ";
Query query = region.getCache().getQueryService().newQuery(queryString);
SelectResults sr = (SelectResults) query.execute();
Assert.assertTrue(sr.size() == size);
PRQueryProcessor.TEST_NUM_THREADS = 10;
try {
queryString = "Select p.get('acc') from /" + region.getName() + " p ";
query = region.getCache().getQueryService().newQuery(queryString);
sr = (SelectResults) query.execute();
Assert.assertTrue(sr.size() == 10);
for (Object r : sr.asList()) {
if (r != null) {
fail("Expected null value, but found " + r);
}
}
} finally {
PRQueryProcessor.TEST_NUM_THREADS = 0;
region.close();
}
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class PRQueryNumThreadsJUnitTest method testQueryOnSingleDataStore.
/**
* Tests the execution of query on a PartitionedRegion created on a single data store. <br>
* 1. Creates a PR with redundancy=0 on a single VM. 2. Puts some test Objects in cache. 3. Fires
* queries on the data and verifies the result.
*
* @throws Exception
*/
@Test
public void testQueryOnSingleDataStore() throws Exception {
Region region = PartitionedRegionTestHelper.createPartitionedRegion(regionName, "100", 0);
PortfolioData[] portfolios = new PortfolioData[100];
for (int j = 0; j < 100; j++) {
portfolios[j] = new PortfolioData(j);
}
PRQueryProcessor.TEST_NUM_THREADS = 10;
try {
populateData(region, portfolios);
String queryString = "ID < 5";
SelectResults resSet = region.query(queryString);
Assert.assertTrue(resSet.size() == 5);
queryString = "ID > 5 and ID <=15";
resSet = region.query(queryString);
Assert.assertTrue(resSet.size() == 10);
} finally {
PRQueryProcessor.TEST_NUM_THREADS = 0;
region.close();
}
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class LocalRegion method query.
@Override
public SelectResults query(String predicate) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
if (predicate == null) {
throw new IllegalArgumentException("The input query predicate is null. A null predicate is not allowed.");
}
predicate = predicate.trim();
SelectResults results;
if (hasServerProxy()) {
// Trim whitespace
String queryString = constructRegionQueryString(predicate.trim());
try {
results = getServerProxy().query(queryString, null);
} catch (Exception e) {
Throwable cause = e.getCause();
if (cause == null) {
cause = e;
}
throw new QueryInvocationTargetException(e.getMessage(), cause);
}
} else {
// TODO: params size is always zero so this whole block is wasted
Object[] params = new Object[0];
QueryService qs = getGemFireCache().getLocalQueryService();
String queryStr = constructRegionQueryString(predicate.trim());
DefaultQuery query = (DefaultQuery) qs.newQuery(queryStr);
if (query.getRegionsInQuery(params).size() != 1) {
throw new QueryInvalidException("Prevent multiple region query from being executed through region.query()");
}
results = (SelectResults) query.execute(params);
}
return results;
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class DataCommandFunction method select.
@SuppressWarnings("rawtypes")
private DataCommandResult select(Object principal, String queryString) {
InternalCache cache = getCache();
AtomicInteger nestedObjectCount = new AtomicInteger(0);
if (StringUtils.isEmpty(queryString)) {
return DataCommandResult.createSelectInfoResult(null, null, -1, null, CliStrings.QUERY__MSG__QUERY_EMPTY, false);
}
QueryService qs = cache.getQueryService();
// TODO : Find out if is this optimised use. Can you have something equivalent of parsed
// queries with names where name can be retrieved to avoid parsing every-time
Query query = qs.newQuery(queryString);
DefaultQuery tracedQuery = (DefaultQuery) query;
WrappedIndexTrackingQueryObserver queryObserver = null;
String queryVerboseMsg = null;
long startTime = -1;
if (tracedQuery.isTraced()) {
startTime = NanoTimer.getTime();
queryObserver = new WrappedIndexTrackingQueryObserver();
QueryObserverHolder.setInstance(queryObserver);
}
List<SelectResultRow> list = new ArrayList<>();
try {
Object results = query.execute();
if (tracedQuery.isTraced()) {
queryVerboseMsg = getLogMessage(queryObserver, startTime, queryString);
queryObserver.reset2();
}
if (results instanceof SelectResults) {
select_SelectResults((SelectResults) results, principal, list, nestedObjectCount);
} else {
select_NonSelectResults(results, list);
}
return DataCommandResult.createSelectResult(queryString, list, queryVerboseMsg, null, null, true);
} catch (FunctionDomainException | GfJsonException | QueryInvocationTargetException | NameResolutionException | TypeMismatchException e) {
logger.warn(e.getMessage(), e);
return DataCommandResult.createSelectResult(queryString, null, queryVerboseMsg, e, e.getMessage(), false);
} finally {
if (queryObserver != null) {
QueryObserverHolder.reset();
}
}
}
Aggregations