Search in sources :

Example 16 with PartitionResolver

use of org.apache.geode.cache.PartitionResolver in project geode by apache.

the class PRFunctionExecutionDUnitTest method testBucketFilterOverride.

@Test
public void testBucketFilterOverride() throws Exception {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    final VM accessor = host.getVM(3);
    final VM datastore0 = host.getVM(0);
    final VM datastore1 = host.getVM(1);
    final VM datastore2 = host.getVM(2);
    getCache();
    accessor.invoke(new SerializableCallable("Create PR") {

        public Object call() throws Exception {
            PartitionResolver resolver = new BucketFilterPRResolver();
            RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 0, resolver);
            getCache().createRegion(rName, ra);
            return Boolean.TRUE;
        }
    });
    SerializableCallable dataStoreCreate = new SerializableCallable("Create PR with Function Factory") {

        public Object call() throws Exception {
            PartitionResolver resolver = new BucketFilterPRResolver();
            RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10, resolver);
            AttributesFactory raf = new AttributesFactory(ra);
            PartitionAttributesImpl pa = new PartitionAttributesImpl();
            pa.setAll(ra.getPartitionAttributes());
            raf.setPartitionAttributes(pa);
            getCache().createRegion(rName, raf.create());
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_BUCKET_FILTER);
            FunctionService.registerFunction(function);
            return Boolean.TRUE;
        }
    };
    datastore0.invoke(dataStoreCreate);
    datastore1.invoke(dataStoreCreate);
    datastore2.invoke(dataStoreCreate);
    Object o = accessor.invoke(new SerializableCallable("Create data") {

        public Object call() throws Exception {
            PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
            for (int i = 0; i < 50; ++i) {
                pr.put(i, i);
            }
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
    o = accessor.invoke(new SerializableCallable("Execute function with bucket filter override") {

        public Object call() throws Exception {
            PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_BUCKET_FILTER);
            FunctionService.registerFunction(function);
            InternalExecution dataSet = (InternalExecution) FunctionService.onRegion(pr);
            Set<Integer> bucketSet = new HashSet<Integer>();
            bucketSet.add(1);
            Set<Integer> keySet = new HashSet<Integer>();
            keySet.add(33);
            keySet.add(43);
            Set<Integer> expectedBucketSet = new HashSet<Integer>();
            expectedBucketSet.add(3);
            expectedBucketSet.add(4);
            ResultCollector<Integer, List<Integer>> rc = dataSet.withBucketFilter(bucketSet).withFilter(keySet).execute(function);
            List<Integer> results = rc.getResult();
            assertEquals(keySet.size(), results.size());
            for (Integer bucket : results) {
                expectedBucketSet.remove(bucket);
            }
            assertTrue(expectedBucketSet.isEmpty());
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
}
Also used : TestFunction(org.apache.geode.internal.cache.functions.TestFunction) RegionAttributes(org.apache.geode.cache.RegionAttributes) Host(org.apache.geode.test.dunit.Host) PartitionResolver(org.apache.geode.cache.PartitionResolver) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionException(org.apache.geode.cache.execute.FunctionException) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) PartitionAttributesImpl(org.apache.geode.internal.cache.PartitionAttributesImpl) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Aggregations

PartitionResolver (org.apache.geode.cache.PartitionResolver)16 FixedPartitionResolver (org.apache.geode.cache.FixedPartitionResolver)5 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)5 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)5 PartitionAttributesImpl (org.apache.geode.internal.cache.PartitionAttributesImpl)4 ArrayList (java.util.ArrayList)3 EntryOperation (org.apache.geode.cache.EntryOperation)3 Region (org.apache.geode.cache.Region)3 EntryOperationImpl (org.apache.geode.internal.cache.EntryOperationImpl)3 VM (org.apache.geode.test.dunit.VM)3 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 List (java.util.List)2 AttributesFactory (org.apache.geode.cache.AttributesFactory)2 Cache (org.apache.geode.cache.Cache)2 FixedPartitionAttributes (org.apache.geode.cache.FixedPartitionAttributes)2 RegionAttributes (org.apache.geode.cache.RegionAttributes)2 Function (org.apache.geode.cache.execute.Function)2