Search in sources :

Example 16 with RegionFunctionContext

use of org.apache.geode.cache.execute.RegionFunctionContext in project geode by apache.

the class TestFunction method execute9.

public void execute9(FunctionContext context) {
    if (context instanceof RegionFunctionContext) {
        RegionFunctionContext rfContext = (RegionFunctionContext) context;
        rfContext.getDataSet().getCache().getLogger().info("Executing function :  TestFunction9.execute " + rfContext);
        if (rfContext.getArguments() instanceof Boolean) {
            rfContext.getResultSender().lastResult((Serializable) rfContext.getArguments());
        } else if (rfContext.getArguments() instanceof String) {
            String key = (String) rfContext.getArguments();
            if (key.equals("TestingTimeOut")) {
                // PRFunctionExecutionDUnitTest#testRemoteMultiKeyExecution_timeout
                try {
                    synchronized (this) {
                        this.wait(2000);
                    }
                } catch (InterruptedException e) {
                    rfContext.getDataSet().getCache().getLogger().warning("Got Exception : Thread Interrupted" + e);
                }
            }
            if (context instanceof RegionFunctionContext) {
                RegionFunctionContext prContext = (RegionFunctionContext) context;
                if (PartitionRegionHelper.isPartitionedRegion(prContext.getDataSet())) {
                    rfContext.getResultSender().lastResult((Serializable) PartitionRegionHelper.getLocalDataForContext(prContext).get(key));
                }
            }
        } else if (rfContext.getArguments() instanceof Set) {
            Set origKeys = (Set) rfContext.getArguments();
            ArrayList vals = new ArrayList();
            for (Iterator i = origKeys.iterator(); i.hasNext(); ) {
                Object val = null;
                if (context instanceof RegionFunctionContext) {
                    RegionFunctionContext prContext = (RegionFunctionContext) context;
                    val = PartitionRegionHelper.getLocalDataForContext(prContext).get(i.next());
                } else {
                    val = rfContext.getDataSet().get(i.next());
                }
                if (i.hasNext())
                    rfContext.getResultSender().sendResult((Serializable) val);
                else
                    rfContext.getResultSender().lastResult((Serializable) val);
                if (val != null) {
                    vals.add(val);
                }
            }
        } else if (rfContext.getArguments() instanceof HashMap) {
            HashMap putData = (HashMap) rfContext.getArguments();
            for (Iterator i = putData.entrySet().iterator(); i.hasNext(); ) {
                Map.Entry me = (Map.Entry) i.next();
                rfContext.getDataSet().put(me.getKey(), me.getValue());
            }
            rfContext.getResultSender().lastResult(Boolean.TRUE);
        } else {
            rfContext.getResultSender().lastResult(Boolean.TRUE);
        }
    } else {
    }
    context.getResultSender().lastResult("ABCD");
}
Also used : Serializable(java.io.Serializable) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with RegionFunctionContext

use of org.apache.geode.cache.execute.RegionFunctionContext in project geode by apache.

the class TestFunction method execute3.

public void execute3(FunctionContext context) {
    if (context instanceof RegionFunctionContext) {
        RegionFunctionContext prContext = (RegionFunctionContext) context;
        prContext.getDataSet().getCache().getLogger().info("Executing function : TestFunction3.execute " + prContext);
        if (prContext.getArguments() instanceof Set) {
            Set origKeys = (Set) prContext.getArguments();
            ArrayList vals = new ArrayList();
            for (Iterator i = origKeys.iterator(); i.hasNext(); ) {
                Object val = PartitionRegionHelper.getLocalDataForContext(prContext).get(i.next());
                if (val != null) {
                    vals.add(val);
                }
            }
            prContext.getResultSender().lastResult(vals);
        /* return vals; */
        } else if (prContext.getFilter() != null) {
            Set origKeys = prContext.getFilter();
            ArrayList vals = new ArrayList();
            for (Iterator i = origKeys.iterator(); i.hasNext(); ) {
                Object val = PartitionRegionHelper.getLocalDataForContext(prContext).get(i.next());
                if (val != null) {
                    vals.add(val);
                }
            }
            /* return vals; */
            // prContext.getResultSender().sendResult(vals);
            prContext.getResultSender().lastResult(vals);
        } else {
            /* return Boolean.FALSE; */
            // prContext.getResultSender().sendResult(Boolean.FALSE);
            prContext.getResultSender().lastResult(Boolean.FALSE);
        }
    } else {
        /* return Boolean.FALSE; */
        // context.getResultSender().sendResult(Boolean.FALSE);
        context.getResultSender().lastResult(Boolean.FALSE);
    }
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext)

Example 18 with RegionFunctionContext

use of org.apache.geode.cache.execute.RegionFunctionContext in project geode by apache.

the class RemoteTransactionDUnitTest method testNestedTxFunction.

@Test
public void testNestedTxFunction() {
    Host host = Host.getHost(0);
    VM accessor = host.getVM(0);
    VM datastore1 = host.getVM(1);
    VM datastore2 = host.getVM(2);
    initAccessorAndDataStore(accessor, datastore1, datastore2, 0);
    class NestedTxFunction2 extends FunctionAdapter {

        static final String id = "NestedTXFunction2";

        @Override
        public void execute(FunctionContext context) {
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            assertNotNull(mgr.getTXState());
            try {
                mgr.commit();
                fail("expected exceptio not thrown");
            } catch (UnsupportedOperationInTransactionException e) {
            }
            context.getResultSender().lastResult(Boolean.TRUE);
        }

        @Override
        public String getId() {
            return id;
        }
    }
    class NestedTxFunction extends FunctionAdapter {

        static final String id = "NestedTXFunction";

        @Override
        public void execute(FunctionContext context) {
            Region r = null;
            if (context instanceof RegionFunctionContext) {
                r = PartitionRegionHelper.getLocalDataForContext((RegionFunctionContext) context);
            } else {
                r = getGemfireCache().getRegion(CUSTOMER);
            }
            assertNotNull(getGemfireCache().getTxManager().getTXState());
            PartitionedRegion pr = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
            Set filter = new HashSet();
            filter.add(expectedCustId);
            LogWriterUtils.getLogWriter().info("SWAP:inside NestedTxFunc calling func2:");
            r.put(expectedCustId, expectedCustomer);
            FunctionService.onRegion(pr).withFilter(filter).execute(new NestedTxFunction2()).getResult();
            assertNotNull(getGemfireCache().getTxManager().getTXState());
            context.getResultSender().lastResult(Boolean.TRUE);
        }

        @Override
        public boolean optimizeForWrite() {
            return true;
        }

        @Override
        public String getId() {
            return id;
        }
    }
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            PartitionedRegion pr = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
            mgr.begin();
            Set filter = new HashSet();
            filter.add(expectedCustId);
            FunctionService.onRegion(pr).withFilter(filter).execute(new NestedTxFunction()).getResult();
            assertNotNull(getGemfireCache().getTxManager().getTXState());
            mgr.commit();
            assertEquals(expectedCustomer, pr.get(expectedCustId));
            return null;
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Host(org.apache.geode.test.dunit.Host) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) NamingException(javax.naming.NamingException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) RollbackException(javax.transaction.RollbackException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) CommitConflictException(org.apache.geode.cache.CommitConflictException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Example 19 with RegionFunctionContext

use of org.apache.geode.cache.execute.RegionFunctionContext in project geode by apache.

the class LuceneGetPageFunction method execute.

@Override
public void execute(FunctionContext context) {
    try {
        RegionFunctionContext ctx = (RegionFunctionContext) context;
        Region region = PartitionRegionHelper.getLocalDataForContext(ctx);
        Set<?> keys = ctx.getFilter();
        List<PageEntry> results = new PageResults(keys.size());
        for (Object key : keys) {
            PageEntry entry = getEntry(region, key);
            if (entry != null) {
                results.add(entry);
            }
        }
        ctx.getResultSender().lastResult(results);
    } catch (CacheClosedException | PrimaryBucketException e) {
        logger.debug("Exception during lucene query function", e);
        throw new InternalFunctionInvocationTargetException(e);
    }
}
Also used : InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) Region(org.apache.geode.cache.Region) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) CacheClosedException(org.apache.geode.cache.CacheClosedException) PrimaryBucketException(org.apache.geode.internal.cache.PrimaryBucketException)

Example 20 with RegionFunctionContext

use of org.apache.geode.cache.execute.RegionFunctionContext in project geode by apache.

the class RegionSizeFunction method execute.

public void execute(FunctionContext context) {
    RegionFunctionContext rfc = (RegionFunctionContext) context;
    context.getResultSender().lastResult(rfc.getDataSet().size());
}
Also used : RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext)

Aggregations

RegionFunctionContext (org.apache.geode.cache.execute.RegionFunctionContext)38 Region (org.apache.geode.cache.Region)23 ArrayList (java.util.ArrayList)21 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)17 Set (java.util.Set)15 Iterator (java.util.Iterator)14 FunctionContext (org.apache.geode.cache.execute.FunctionContext)12 FunctionAdapter (org.apache.geode.cache.execute.FunctionAdapter)10 HashSet (java.util.HashSet)8 Serializable (java.io.Serializable)6 Map (java.util.Map)6 FunctionException (org.apache.geode.cache.execute.FunctionException)6 IgnoredException (org.apache.geode.test.dunit.IgnoredException)6 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)5 List (java.util.List)5 CacheClosedException (org.apache.geode.cache.CacheClosedException)5 Execution (org.apache.geode.cache.execute.Execution)5 ResultCollector (org.apache.geode.cache.execute.ResultCollector)5