This is an automated email from the ASF dual-hosted git repository. bbender pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/geode-native.git
The following commit(s) were added to refs/heads/develop by this push: new ca93200 GEODE-4337: example cpp function execution (#389) ca93200 is described below commit ca932007309ce421b2728508071c80b7a9e60016 Author: Blake Bender <ekalbred...@hotmail.com> AuthorDate: Wed Nov 7 07:50:11 2018 -0800 GEODE-4337: example cpp function execution (#389) * Add new example directory. * Add simple function execution code to get a few values from region * Add new examples-only java source to tree * Install with new jar file --- examples/CMakeLists.txt | 5 + examples/CMakeLists.txt.in | 2 + examples/cpp/function-execution/main.cpp | 225 +++++++++------------ examples/cpp/function-execution/startserver.sh | 2 +- .../CMakeLists.txt} | 9 +- .../utilities/CMakeLists.txt.in | 15 +- examples/utilities/ExampleMultiGetFunction.java | 67 ++++++ .../startserver.sh | 2 +- tests/javaobject/CMakeLists.txt | 6 - 9 files changed, 181 insertions(+), 152 deletions(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 0566b6c..4393ca6 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -29,6 +29,9 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/CMakeLists.txt DESTINATION examples) install(FILES + ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/FindGeode.cmake + DESTINATION examples/cmake) +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cmake/Find${PRODUCT_NAME_NOSPACE}.cmake DESTINATION examples/cmake) @@ -36,3 +39,5 @@ add_subdirectory(cpp) if (BUILD_CLI) add_subdirectory(dotnet) endif() +add_subdirectory(utilities) + diff --git a/examples/CMakeLists.txt.in b/examples/CMakeLists.txt.in index f124022..6e363b6 100644 --- a/examples/CMakeLists.txt.in +++ b/examples/CMakeLists.txt.in @@ -19,3 +19,5 @@ project(examples LANGUAGES NONE) add_subdirectory(cpp) @DOTNET_SUB_DIRECTORY@ +add_subdirectory(utilities) + diff --git a/examples/cpp/function-execution/main.cpp b/examples/cpp/function-execution/main.cpp index 6b57734..e3f239f 100644 --- a/examples/cpp/function-execution/main.cpp +++ b/examples/cpp/function-execution/main.cpp @@ -16,17 +16,12 @@ */ /* - * The Execute Function QuickStart Example. - * * This example takes the following steps: * - * 1. Create a Geode Cache. - * 2. Create the example Region Programmatically. + * 1. Create a Geode Cache, Pool, and example Region Programmatically. * 3. Populate some objects on the Region. - * 4. Create Execute Objects - * 5. Execute Functions - * 6. Close the Cache. - * + * 4. Create Execute Object + * 5. Execute Function */ #include <iostream> #include <memory> @@ -38,147 +33,113 @@ #include <geode/FunctionService.hpp> #include <geode/CacheableString.hpp> -// Use the "geode" namespace. -using namespace apache::geode::client; +using apache::geode::client::Cache; +using apache::geode::client::CacheableArrayList; +using apache::geode::client::CacheableKey; +using apache::geode::client::CacheableString; +using apache::geode::client::CacheableVector; +using apache::geode::client::CacheFactory; +using apache::geode::client::Exception; +using apache::geode::client::FunctionService; +using apache::geode::client::Region; +using apache::geode::client::RegionShortcut; + +const auto getFuncIName = std::string("ExampleMultiGetFunction"); + +const int EXAMPLE_SERVER_PORT = 50505; + +const std::vector<std::string> keys = { + "KEY--1", + "KEY--2", + "KEY--3", +}; + +const std::vector<std::string> values = { + "VALUE--1", + "VALUE--2", + "VALUE--3" +}; + +Cache setupCache() { + return CacheFactory() + .set("log-level", "none") + .create(); +} -const auto getFuncIName = std::string("MultiGetFunctionI"); -const auto putFuncIName = std::string("MultiPutFunctionI"); -const auto getFuncName = std::string("MultiGetFunction"); +void createPool(const Cache& cache) { + auto pool = cache.getPoolManager() + .createFactory() + .addServer("localhost", EXAMPLE_SERVER_PORT) + .create("pool"); +} -const int EXAMPLE_ITEM_COUNT = 34; +std::shared_ptr<Region> createRegion(Cache& cache) { + auto regionFactory = cache.createRegionFactory(RegionShortcut::PROXY); + auto region = regionFactory.setPoolName("pool").create("partition_region"); -// The Execute Function QuickStart example. -int main(int argc, char** argv) { - try { - // Create CacheFactory using the settings from the geode.properties file by - // default. - auto cache = CacheFactory() - .set("log-level", "none") - .create(); - - std::cout << "Created CacheFactory\n"; - - auto pool = cache.getPoolManager() - .createFactory() - .setSubscriptionEnabled(true) - .addServer("localhost", 50505) - .addServer("localhost", 40404) - .create("pool"); - - // Create the example Region Programmatically - auto regionFactory = cache.createRegionFactory(RegionShortcut::PROXY); - auto regPtr0 = regionFactory.setPoolName("pool").create("partition_region"); - - std::cout << "Created the Region\n"; - - regPtr0->registerAllKeys(); - char buf[128]; - - auto resultList = CacheableVector::create(); - for (int i = 0; i < EXAMPLE_ITEM_COUNT; i++) { - sprintf(buf, "VALUE--%d", i); - auto value(CacheableString::create(buf)); - - sprintf(buf, "KEY--%d", i); - auto key = CacheableKey::create(buf); - regPtr0->put(key, value); - } + return region; +} - auto routingObj = CacheableVector::create(); - for (int i = 1; i < EXAMPLE_ITEM_COUNT; i+=2) { - sprintf(buf, "KEY--%d", i); - auto key = CacheableKey::create(buf); - routingObj->push_back(key); - } +void populateRegion(const std::shared_ptr<Region>& region) { + for (int i = 0; i < keys.size(); i++) { + region->put(keys[i], values[i]); + } +} - std::cout << "test data independent function with result on one server\n"; - auto exc = FunctionService::onServer(regPtr0->getRegionService()); - if(auto executeFunctionResult = exc.withArgs(routingObj).execute(getFuncIName)->getResult()) { - for (auto &arrayList: *executeFunctionResult) { - for (auto &cachedString: *std::dynamic_pointer_cast<CacheableArrayList>(arrayList)) { - resultList->push_back(cachedString); - } - } - sprintf(buf, "get: result count = %lu\n", resultList->size()); - std::cout << buf; - int i = 0; - for (auto &cachedString: *resultList) { - sprintf( - buf, "get result[%d]=%s\n", i, - std::dynamic_pointer_cast<CacheableString>(cachedString)->value().c_str()); - std::cout << buf; - ++i; +std::shared_ptr<CacheableVector> populateArguments() { + auto arguments = CacheableVector::create(); + for (int i = 0; i < keys.size(); i++) { + arguments->push_back(CacheableKey::create(keys[i])); + } + return arguments; +} + +std::vector<std::string> executeFunctionOnServer(const std::shared_ptr<Region> region, + const std::shared_ptr<CacheableVector> arguments) { + std::vector<std::string> resultList; + + auto functionService = FunctionService::onServer(region->getRegionService()); + if(auto executeFunctionResult = functionService.withArgs(arguments).execute(getFuncIName)->getResult()) { + for (auto &arrayList: *executeFunctionResult) { + for (auto &cachedString: *std::dynamic_pointer_cast<CacheableArrayList>(arrayList)) { + resultList.push_back(std::dynamic_pointer_cast<CacheableString>(cachedString)->value()); } - } else { - std::cout << "get executeFunctionResult is NULL\n"; } + } else { + std::cout << "get executeFunctionResult is NULL\n"; + } - std::cout << "test data independent function without result on one server\n"; + return resultList; +} - exc.withArgs(routingObj).execute(putFuncIName, std::chrono::milliseconds(15)); +void printResults(const std::vector<std::string>& resultList) { + std::cout << "Result count = " << resultList.size() << std::endl << std::endl; + int i = 0; + for (auto &cachedString: resultList) { + std::cout << "\tResult[" << i << "]=" << cachedString << std::endl; + ++i; + } +} - std::cout << "test data independent function with result on all servers\n"; +int main(int argc, char** argv) { + try { + auto cache = setupCache(); - exc = FunctionService::onServers(regPtr0->getRegionService()); - if(auto executeFunctionResult = exc.withArgs(routingObj).execute(getFuncIName)->getResult()) { - resultList->clear(); - for (auto &arrayList: *executeFunctionResult) { - for (auto &cachedString: *std::dynamic_pointer_cast<CacheableArrayList>(arrayList)) { - resultList->push_back(cachedString); - } - } - sprintf(buf, "get: result count = %lu\n", resultList->size()); - std::cout << buf; - int i = 0; - for (auto &cachedString: *resultList) { - sprintf( - buf, "get result[%d]=%s\n", i, - std::dynamic_pointer_cast<CacheableString>(cachedString)->value().c_str()); - std::cout << buf; - ++i; - } - } else { - std::cout << "get executeFunctionResult is NULL\n"; - } + createPool(cache); + auto region = createRegion(cache); - std::cout << "test data independent function without result on all servers\n"; - exc.withArgs(routingObj).execute(putFuncIName, std::chrono::milliseconds(15)); - std::cout << "test data dependent function with result\n"; - - auto args = CacheableBoolean::create(1); - exc = FunctionService::onRegion(regPtr0); - if(auto executeFunctionResult = exc.withFilter(routingObj) - .withArgs(args) - .execute(getFuncName) - ->getResult()) { - resultList->clear(); - std::cout << "Execute on Region: result count = " << executeFunctionResult->size() << '\n'; - for (auto &arrayList: *executeFunctionResult) { - for (auto &cachedString: *std::dynamic_pointer_cast<CacheableArrayList>(arrayList)) { - resultList->push_back(cachedString); - } - } - sprintf(buf, "Execute on Region: result count = %lu\n", resultList->size()); - std::cout << buf; - int i = 0; - for (auto &cachedString: *resultList) { - sprintf( - buf, "Execute on Region: result[%d]=%s\n", i, - std::dynamic_pointer_cast<CacheableString>(cachedString)->value().c_str()); - std::cout << buf; - ++i; - } - } else { - std::cout << "execute on region: executeFunctionResult is NULL\n"; - } + populateRegion(region); - return 0; + auto arguments = populateArguments(); + + auto resultList = executeFunctionOnServer(region, arguments); + + printResults(resultList); } - // An exception should not occur catch (const Exception& geodeExcp) { std::cerr << "Function Execution Geode Exception: " << geodeExcp.getMessage() << '\n'; - return 1; } } + diff --git a/examples/cpp/function-execution/startserver.sh b/examples/cpp/function-execution/startserver.sh index 24e2a9f..057c379 100755 --- a/examples/cpp/function-execution/startserver.sh +++ b/examples/cpp/function-execution/startserver.sh @@ -29,6 +29,6 @@ else fi fi -$GFSH_PATH -e "start locator --name=locator" -e "deploy --jar=../../javaobject.jar" -e "start server --name=the-server --server-port=40404" -e "create region --name=partition_region --type=PARTITION" -e "start server --name=the-second-server --server-port=50505" +$GFSH_PATH -e "start locator --name=locator" -e "deploy --jar=./example.jar" -e "start server --name=the-server --server-port=50505" -e "create region --name=partition_region --type=PARTITION" diff --git a/examples/CMakeLists.txt.in b/examples/utilities/CMakeLists.txt similarity index 76% copy from examples/CMakeLists.txt.in copy to examples/utilities/CMakeLists.txt index f124022..824a8ff 100644 --- a/examples/CMakeLists.txt.in +++ b/examples/utilities/CMakeLists.txt @@ -17,5 +17,10 @@ cmake_minimum_required(VERSION 3.10) project(examples LANGUAGES NONE) -add_subdirectory(cpp) -@DOTNET_SUB_DIRECTORY@ +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt.in ${CMAKE_CURRENT_BINARY_DIR}/CMakeLists.txt COPYONLY) + +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/ExampleMultiGetFunction.java + DESTINATION examples/utilities) + diff --git a/tests/javaobject/CMakeLists.txt b/examples/utilities/CMakeLists.txt.in similarity index 79% copy from tests/javaobject/CMakeLists.txt copy to examples/utilities/CMakeLists.txt.in index a6f61fd..48cdc1a 100644 --- a/tests/javaobject/CMakeLists.txt +++ b/examples/utilities/CMakeLists.txt.in @@ -13,7 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. cmake_minimum_required (VERSION 3.4) -project (javaobject) +project (example) + +set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../cmake) find_package(Geode REQUIRED) find_package(Java REQUIRED) @@ -22,15 +24,8 @@ include(UseJava) file(GLOB_RECURSE SOURCES "*.java") -add_jar(javaobject ${SOURCES} +add_jar(example ${SOURCES} INCLUDE_JARS ${Geode_CLASSPATH} + OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../cpp/function-execution ) -set(EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/javaobject.jar) - -install(DIRECTORY ${COMMON_INCLUDE_DIR} DESTINATION .) -install(FILES - ${EXPORT_FILE_NAME} - DESTINATION examples/ - ) - diff --git a/examples/utilities/ExampleMultiGetFunction.java b/examples/utilities/ExampleMultiGetFunction.java new file mode 100755 index 0000000..dcd689b --- /dev/null +++ b/examples/utilities/ExampleMultiGetFunction.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Properties; +import java.util.Vector; + +import org.apache.geode.cache.Cache; +import org.apache.geode.cache.CacheClosedException; +import org.apache.geode.cache.CacheFactory; +import org.apache.geode.cache.Declarable; +import org.apache.geode.cache.execute.FunctionAdapter; +import org.apache.geode.cache.execute.FunctionContext; +import org.apache.geode.cache.Region; +import org.apache.geode.cache.execute.ResultSender; + +public class ExampleMultiGetFunction extends FunctionAdapter implements Declarable{ + + public void execute(FunctionContext context) { + ArrayList vals = new ArrayList(); + if(context.getArguments() instanceof Vector ) { + Cache c = null; + try { + c = CacheFactory.getAnyInstance(); + } + catch (CacheClosedException ex) { + vals.add("NoCacheResult"); + context.getResultSender().lastResult(vals); + } + + Region region = c.getRegion("partition_region"); + Vector keys = (Vector)context.getArguments(); + System.out.println("Context.getArguments " + keys); + Iterator itr = keys.iterator(); + while (itr.hasNext()) { + Object k = itr.next(); + vals.add(region.get(k)); + System.out.println("vals " + vals); + } + } + context.getResultSender().lastResult(vals); + } + + public String getId() { + return "ExampleMultiGetFunction"; + } + + public void init(Properties arg0) { + } + +} diff --git a/examples/cpp/function-execution/startserver.sh b/examples/utilities/startserver.sh old mode 100755 new mode 100644 similarity index 85% copy from examples/cpp/function-execution/startserver.sh copy to examples/utilities/startserver.sh index 24e2a9f..8b19103 --- a/examples/cpp/function-execution/startserver.sh +++ b/examples/utilities/startserver.sh @@ -29,6 +29,6 @@ else fi fi -$GFSH_PATH -e "start locator --name=locator" -e "deploy --jar=../../javaobject.jar" -e "start server --name=the-server --server-port=40404" -e "create region --name=partition_region --type=PARTITION" -e "start server --name=the-second-server --server-port=50505" +$GFSH_PATH -e "start locator --name=locator" -e "deploy --jar=../../build/utilities/example.jar" -e "start server --name=the-server --server-port=50505" -e "create region --name=partition_region --type=PARTITION" diff --git a/tests/javaobject/CMakeLists.txt b/tests/javaobject/CMakeLists.txt index a6f61fd..f65806b 100644 --- a/tests/javaobject/CMakeLists.txt +++ b/tests/javaobject/CMakeLists.txt @@ -28,9 +28,3 @@ add_jar(javaobject ${SOURCES} set(EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/javaobject.jar) -install(DIRECTORY ${COMMON_INCLUDE_DIR} DESTINATION .) -install(FILES - ${EXPORT_FILE_NAME} - DESTINATION examples/ - ) -