use of org.apache.geode.internal.cache.LocalDataSet in project geode by apache.
the class PartitionRegionHelper method getLocalPrimaryData.
/**
* Given a partitioned Region return a Region providing read access to primary copy of the data
* which is limited to the local heap, writes using this Region have no constraints and behave the
* same as a partitioned Region.<br>
*
* @param r a partitioned region
* @throws IllegalStateException if the provided region is something other than a
* {@linkplain DataPolicy#PARTITION partitioned Region}
* @return a Region for efficient reads
* @since GemFire 6.5
*/
public static <K, V> Region<K, V> getLocalPrimaryData(final Region<K, V> r) {
if (isPartitionedRegion(r)) {
PartitionedRegion pr = (PartitionedRegion) r;
final Set<Integer> buckets;
if (pr.getDataStore() != null) {
buckets = pr.getDataStore().getAllLocalPrimaryBucketIds();
} else {
buckets = Collections.emptySet();
}
return new LocalDataSet(pr, buckets);
} else if (r instanceof LocalDataSet) {
return r;
} else {
throw new IllegalArgumentException(LocalizedStrings.PartitionManager_REGION_0_IS_NOT_A_PARTITIONED_REGION.toLocalizedString(r.getFullPath()));
}
}
use of org.apache.geode.internal.cache.LocalDataSet in project geode by apache.
the class QueryDataFunction method selectWithType.
private QueryDataFunctionResult selectWithType(final FunctionContext context, String queryString, final boolean showMember, final String regionName, final int limit, final int queryResultSetLimit, final int queryCollectionsDepth) throws Exception {
InternalCache cache = getCache();
Function localQueryFunc = new LocalQueryFunction("LocalQueryFunction", regionName, showMember).setOptimizeForWrite(true);
queryString = applyLimitClause(queryString, limit, queryResultSetLimit);
try {
TypedJson result = new TypedJson(queryCollectionsDepth);
Region region = cache.getRegion(regionName);
if (region == null) {
throw new Exception(ManagementStrings.QUERY__MSG__REGIONS_NOT_FOUND_ON_MEMBER.toLocalizedString(regionName, cache.getDistributedSystem().getDistributedMember().getId()));
}
Object results = null;
boolean noDataFound = true;
if (region.getAttributes().getDataPolicy() == DataPolicy.NORMAL) {
QueryService queryService = cache.getQueryService();
Query query = queryService.newQuery(queryString);
results = query.execute();
} else {
ResultCollector rcollector;
PartitionedRegion parRegion = PartitionedRegionHelper.getPartitionedRegion(regionName, cache);
if (parRegion != null && showMember) {
if (parRegion.isDataStore()) {
Set<BucketRegion> localPrimaryBucketRegions = parRegion.getDataStore().getAllLocalPrimaryBucketRegions();
Set<Integer> localPrimaryBucketSet = new HashSet<>();
for (BucketRegion bRegion : localPrimaryBucketRegions) {
localPrimaryBucketSet.add(bRegion.getId());
}
LocalDataSet lds = new LocalDataSet(parRegion, localPrimaryBucketSet);
DefaultQuery query = (DefaultQuery) cache.getQueryService().newQuery(queryString);
results = (SelectResults) lds.executeQuery(query, null, localPrimaryBucketSet);
}
} else {
rcollector = FunctionService.onRegion(cache.getRegion(regionName)).setArguments(queryString).execute(localQueryFunc);
results = rcollector.getResult();
}
}
if (results != null && results instanceof SelectResults) {
SelectResults selectResults = (SelectResults) results;
for (Object object : selectResults) {
result.add(RESULT_KEY, object);
noDataFound = false;
}
} else if (results != null && results instanceof ArrayList) {
ArrayList listResults = (ArrayList) results;
ArrayList actualResult = (ArrayList) listResults.get(0);
for (Object object : actualResult) {
result.add(RESULT_KEY, object);
noDataFound = false;
}
}
if (!noDataFound && showMember) {
result.add(MEMBER_KEY, cache.getDistributedSystem().getDistributedMember().getId());
}
if (noDataFound) {
return new QueryDataFunctionResult(QUERY_EXEC_SUCCESS, BeanUtilFuncs.compress(new JsonisedErrorMessage(NO_DATA_FOUND).toString()));
}
return new QueryDataFunctionResult(QUERY_EXEC_SUCCESS, BeanUtilFuncs.compress(result.toString()));
} catch (Exception e) {
logger.warn(e.getMessage(), e);
throw e;
}
}
use of org.apache.geode.internal.cache.LocalDataSet in project geode by apache.
the class QueryWithBucketParameterIntegrationTest method setUp.
@Before
public void setUp() throws Exception {
String regionName = "pr1";
int totalBuckets = 40;
int numValues = 80;
CacheUtils.startCache();
Cache cache = CacheUtils.getCache();
PartitionAttributesFactory pAFactory = getPartitionAttributesFactoryWithPartitionResolver(totalBuckets);
RegionFactory rf = cache.createRegionFactory(RegionShortcut.PARTITION);
rf.setPartitionAttributes(pAFactory.create());
PartitionedRegion pr1 = (PartitionedRegion) rf.create(regionName);
populateRegion(pr1, numValues);
QueryService qs = pr1.getCache().getQueryService();
String query = "select distinct e1.value from /pr1 e1";
queryExecutor = (DefaultQuery) CacheUtils.getQueryService().newQuery(query);
Set<Integer> set = createAndPopulateSet(totalBuckets);
lds = new LocalDataSet(pr1, set);
}
use of org.apache.geode.internal.cache.LocalDataSet in project geode by apache.
the class BugJUnitTest method testEquijoinPRColocatedQuery_2.
@Test
public void testEquijoinPRColocatedQuery_2() throws Exception {
AttributesFactory factory = new AttributesFactory();
factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).setTotalNumBuckets(40).setPartitionResolver(new PartitionResolver() {
public String getName() {
return "blah";
}
public Serializable getRoutingObject(EntryOperation opDetails) {
return (Serializable) opDetails.getKey();
}
public void close() {
}
}).create());
PartitionedRegion pr1 = (PartitionedRegion) CacheUtils.getCache().createRegion("pr1", factory.create());
factory = new AttributesFactory();
factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).setTotalNumBuckets(40).setPartitionResolver(new PartitionResolver() {
public String getName() {
return "blah";
}
public Serializable getRoutingObject(EntryOperation opDetails) {
return (Serializable) opDetails.getKey();
}
public void close() {
}
}).setColocatedWith(pr1.getName()).create());
final PartitionedRegion pr2 = (PartitionedRegion) CacheUtils.getCache().createRegion("pr2", factory.create());
createAllNumPRAndEvenNumPR(pr1, pr2, 80);
Set<Integer> set = createAndPopulateSet(15);
LocalDataSet lds = new LocalDataSet(pr1, set);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
QueryService qs = pr1.getCache().getQueryService();
qs.createIndex("valueIndex", IndexType.FUNCTIONAL, "e.value", "/pr1.entries e");
qs.createIndex("valueIndex", IndexType.FUNCTIONAL, "e.value", "/pr2.entries e");
String query = "select distinct e1.key from /pr1.entries e1,/pr2.entries e2" + " where e1.value=e2.value";
DefaultQuery cury = (DefaultQuery) CacheUtils.getQueryService().newQuery(query);
SelectResults r = (SelectResults) lds.executeQuery(cury, null, set);
if (!observer.isIndexesUsed) {
fail("Indexes should have been used");
}
}
use of org.apache.geode.internal.cache.LocalDataSet in project geode by apache.
the class BugJUnitTest method testEquijoinPRColocatedQuery_1.
@Test
public void testEquijoinPRColocatedQuery_1() throws Exception {
AttributesFactory factory = new AttributesFactory();
factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).setTotalNumBuckets(40).setPartitionResolver(new PartitionResolver() {
public String getName() {
return "blah";
}
public Serializable getRoutingObject(EntryOperation opDetails) {
return (Serializable) opDetails.getKey();
}
public void close() {
}
}).create());
PartitionedRegion pr1 = (PartitionedRegion) CacheUtils.getCache().createRegion("pr1", factory.create());
factory = new AttributesFactory();
factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).setTotalNumBuckets(40).setPartitionResolver(new PartitionResolver() {
public String getName() {
return "blah";
}
public Serializable getRoutingObject(EntryOperation opDetails) {
return (Serializable) opDetails.getKey();
}
public void close() {
}
}).setColocatedWith(pr1.getName()).create());
final PartitionedRegion pr2 = (PartitionedRegion) CacheUtils.getCache().createRegion("pr2", factory.create());
createAllNumPRAndEvenNumPR(pr1, pr2, 80);
Set<Integer> set = createAndPopulateSet(15);
LocalDataSet lds = new LocalDataSet(pr1, set);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
QueryService qs = pr1.getCache().getQueryService();
qs.createIndex("valueIndex", IndexType.FUNCTIONAL, "e.value", "/pr1 e");
qs.createIndex("valueIndex", IndexType.FUNCTIONAL, "e.value", "/pr2 e");
String query = "select distinct e1.value from /pr1 e1, " + "/pr2 e2" + " where e1.value=e2.value";
DefaultQuery cury = (DefaultQuery) CacheUtils.getQueryService().newQuery(query);
SelectResults r = (SelectResults) lds.executeQuery(cury, null, set);
if (!observer.isIndexesUsed) {
fail("Indexes should have been used");
}
}
Aggregations