This is an automated email from the ASF dual-hosted git repository. jbarrett 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 c32e6b1 Geode 4335: Custom PdxSerializable example (#201) c32e6b1 is described below commit c32e6b1bf3a0932827d5152afe0fdac1954d006a Author: mhansonp <mhan...@pivotal.io> AuthorDate: Fri Feb 2 13:17:27 2018 -0800 Geode 4335: Custom PdxSerializable example (#201) Signed-off-by: Ivan Godwin <igod...@pivotal.io> Signed-off-by: Ryan McMahon <mcmella...@gmail.com> --- examples/cpp/BUILDING.md | 15 +---- examples/cpp/cmake/Findgeode-native.cmake | 2 +- .../CMakeLists.txt} | 35 ++++++----- examples/cpp/customserializable/Order.cpp | 64 ++++++++++++++++++++ examples/cpp/customserializable/Order.hpp | 70 ++++++++++++++++++++++ examples/cpp/customserializable/main.cpp | 70 ++++++++++++++++++++++ .../startserver.sh} | 11 ++-- .../stopserver.sh | 7 +-- examples/cpp/put-get-remove/startserver.sh | 2 +- examples/cpp/put-get-remove/stopserver.sh | 2 +- 10 files changed, 236 insertions(+), 42 deletions(-) diff --git a/examples/cpp/BUILDING.md b/examples/cpp/BUILDING.md index 3267329..cf1c549 100644 --- a/examples/cpp/BUILDING.md +++ b/examples/cpp/BUILDING.md @@ -26,18 +26,11 @@ **Mac OS X / \*nix** $ cd <example> + $ export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:<install path>/geode-native/lib $ sh ./startserver.sh $ build/<example name> $ sh ./stopserver.sh -**Windows** - - $ cd <example> - $ PATH=%PATH%;C:\Program Files\geode-native\bin (or wherever you installed the geode-native client dll) - $ gfsh -e "start locator --name=locator" -e "start server --name=server" -e "create region --name=example_userinfo --type=PARTITION" - $ build\Debug\put-get-remove.exe - $ gfsh -e "connect" -e "stop server --name=server" -e "stop locator --name=locator" - ## Generator CMake uses a "generator" to produce configuration files for use by a variety of build tools, e.g., UNIX makefiles, Visual Studio projects. By default a system-specific generator is used by CMake during configuration. (Please see [the CMake documentation](https://cmake.org/documentation/) for further information.) However, in many cases there is a better choice. @@ -46,11 +39,5 @@ The default generator: $ cmake .. -### Windows Generator -The recommended generator on Windows is `Visual Studio 14 2015 Win64`: - - $ cmake -G "Visual Studio 14 2015 Win64" .. - - ### Building requirements Please see the documentation for building Geode Native. \ No newline at end of file diff --git a/examples/cpp/cmake/Findgeode-native.cmake b/examples/cpp/cmake/Findgeode-native.cmake index 634c12e..d4d7328 100644 --- a/examples/cpp/cmake/Findgeode-native.cmake +++ b/examples/cpp/cmake/Findgeode-native.cmake @@ -65,7 +65,7 @@ else() set(_GEODE_NATIVE_PATHS ( "/opt/local" "/usr/local" - "../../../../" + "${CMAKE_CURRENT_SOURCE_DIR}/../../../../" "C:/program files" )) endif() diff --git a/examples/cpp/put-get-remove/startserver.sh b/examples/cpp/customserializable/CMakeLists.txt old mode 100755 new mode 100644 similarity index 63% copy from examples/cpp/put-get-remove/startserver.sh copy to examples/cpp/customserializable/CMakeLists.txt index e657de2..dcc9d2d --- a/examples/cpp/put-get-remove/startserver.sh +++ b/examples/cpp/customserializable/CMakeLists.txt @@ -1,5 +1,3 @@ -#!/bin/env bash - # 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. @@ -15,18 +13,25 @@ # See the License for the specific language governing permissions and # limitations under the License. -GFSH_PATH="" -which gfsh 2> /dev/null +cmake_minimum_required(VERSION 3.5) + +project(customserializer) + +set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../cmake) +set(CMAKE_CXX_STANDARD 11) + +if(CMAKE_CXX_COMPILER_ID STREQUAL "SunPro") + add_compile_options(-m64) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m64") +endif() + +find_package(geode-native REQUIRED) -if [ $? -gt 0 ]; then - GFSH_PATH="gfsh" -else - if [ "$GEODE_HOME" == "" ]; then - echo "Could not find gfsh. Please set the GEODE_HOME path." - echo "e.g. export GEODE_HOME=<path to Geode>" - else - GFSH_PATH=$GEODE_HOME/bin/gfsh - fi -fi +add_executable(${PROJECT_NAME} + main.cpp + Order.cpp) -$GFSH_PATH -e "start locator --name=locator" -e "start server --name=server" -e "create region --name=example_userinfo --type=PARTITION" +target_link_libraries(${PROJECT_NAME} + PUBLIC + apache.geode::cpp +) diff --git a/examples/cpp/customserializable/Order.cpp b/examples/cpp/customserializable/Order.cpp new file mode 100644 index 0000000..d8fdd3e --- /dev/null +++ b/examples/cpp/customserializable/Order.cpp @@ -0,0 +1,64 @@ +/* + * 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. + */ + +#include "Order.hpp" + +#include <geode/PdxReader.hpp> +#include <geode/PdxWriter.hpp> + +namespace customserializable { + +void Order::fromData(PdxReader& pdxReader) { + order_id_ = static_cast<uint32_t>(pdxReader.readLong(ORDER_ID_KEY_)); + name_ = pdxReader.readString(NAME_KEY_); + quantity_ = static_cast<uint16_t>(pdxReader.readInt(QUANTITY_KEY_)); +} + +void Order::toData(PdxWriter& pdxWriter) const { + pdxWriter.writeLong(ORDER_ID_KEY_, order_id_); + pdxWriter.markIdentityField(ORDER_ID_KEY_); + + pdxWriter.writeString(NAME_KEY_, name_); + pdxWriter.markIdentityField(NAME_KEY_); + + pdxWriter.writeInt(QUANTITY_KEY_, quantity_); + pdxWriter.markIdentityField(QUANTITY_KEY_); +} + +std::string Order::toString() const { + return "OrderID: " + std::to_string(order_id_) + " Product Name: " + name_ + + " Quantity: " + std::to_string(quantity_); +} + +size_t Order::objectSize() const { + auto objectSize = sizeof(Order); + objectSize += name_.capacity(); + return objectSize; +} + +const std::string& Order::getClassName() const { + static const std::string CLASS_NAME = "com.example.Order"; + return CLASS_NAME; +} + +PdxSerializable* Order::createDeserializable() { return new Order(); } + +const std::string Order::ORDER_ID_KEY_ = "order_id"; +const std::string Order::NAME_KEY_ = "name"; +const std::string Order::QUANTITY_KEY_ = "quantity"; + +} // namespace customserializable diff --git a/examples/cpp/customserializable/Order.hpp b/examples/cpp/customserializable/Order.hpp new file mode 100644 index 0000000..054c772 --- /dev/null +++ b/examples/cpp/customserializable/Order.hpp @@ -0,0 +1,70 @@ +/* + * 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. + */ + +#pragma once + +#ifndef CUSTOMSERIALIZABLE_ORDER_H +#define CUSTOMSERIALIZABLE_ORDER_H + +#include <string> + +#include <geode/PdxSerializable.hpp> + +namespace customserializable { + +using namespace apache::geode::client; + +class Order : public PdxSerializable { + public: + inline Order() : Order(0, "", 0) {} + + inline Order(uint32_t order_id, std::string name, uint16_t quantity) + : order_id_(order_id), name_(std::move(name)), quantity_(quantity) {} + + ~Order() override = default; + + inline uint32_t getOrderId() const { return order_id_; } + + inline const std::string& getName() const { return name_; } + + inline uint16_t getQuantity() const { return quantity_; } + + void fromData(PdxReader& pdxReader) override; + + void toData(PdxWriter& pdxWriter) const override; + + std::string toString() const override; + + size_t objectSize() const override; + + const std::string& getClassName() const override; + + static PdxSerializable* createDeserializable(); + + private: + static const std::string ORDER_ID_KEY_; + static const std::string NAME_KEY_; + static const std::string QUANTITY_KEY_; + + uint32_t order_id_; + std::string name_; + uint16_t quantity_; +}; + +} // namespace customserializable + +#endif // CUSTOMSERIALIZABLE_ORDER_H diff --git a/examples/cpp/customserializable/main.cpp b/examples/cpp/customserializable/main.cpp new file mode 100644 index 0000000..f1446da --- /dev/null +++ b/examples/cpp/customserializable/main.cpp @@ -0,0 +1,70 @@ +/* + * 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. + */ + +#include <iostream> +#include <sstream> + +#include <geode/CacheFactory.hpp> +#include <geode/PdxWrapper.hpp> +#include <geode/PoolManager.hpp> + +#include "Order.hpp" + +using namespace apache::geode::client; +using namespace customserializable; + +int main(int argc, char** argv) { + auto cacheFactory = CacheFactory(); + cacheFactory.set("log-level", "none"); + auto cache = cacheFactory.create(); + + auto poolFactory = cache.getPoolManager().createFactory(); + poolFactory->addLocator("localhost", 10334); + auto pool = poolFactory->create("pool"); + + auto regionFactory = cache.createRegionFactory(RegionShortcut::PROXY); + auto region = regionFactory.setPoolName("pool").create("custom_orders"); + + cache.getTypeRegistry().registerPdxType(Order::createDeserializable); + + std::cout << "Create orders" << std::endl; + auto order1 = std::make_shared<Order>(1, "product x", 23); + auto order2 = std::make_shared<Order>(2, "product y", 37); + + std::cout << "Storing orders in the region" << std::endl; + region->put("Customer1", order1); + region->put("Customer2", order2); + + std::cout << "Getting the orders from the region" << std::endl; + + if (auto order1retrieved = + std::dynamic_pointer_cast<Order>(region->get("Customer1"))) { + std::cout << "OrderID: " << order1retrieved->getOrderId() << std::endl; + std::cout << "Product Name: " << order1retrieved->getName() << std::endl; + std::cout << "Quantity: " << order1retrieved->getQuantity() << std::endl; + } else { + std::cout << "Order 1 not found." << std::endl; + } + + if (auto order2retrieved = region->get("Customer2")) { + std::cout << order2retrieved->toString() << std::endl; + } else { + std::cout << "Order 2 not found." << std::endl; + } + + cache.close(); +} diff --git a/examples/cpp/put-get-remove/stopserver.sh b/examples/cpp/customserializable/startserver.sh similarity index 83% copy from examples/cpp/put-get-remove/stopserver.sh copy to examples/cpp/customserializable/startserver.sh index f04fab6..2c947ea 100755 --- a/examples/cpp/put-get-remove/stopserver.sh +++ b/examples/cpp/customserializable/startserver.sh @@ -1,5 +1,3 @@ -#!/bin/env bash - # 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. @@ -15,13 +13,14 @@ # See the License for the specific language governing permissions and # limitations under the License. +#!/bin/env bash GFSH_PATH="" which gfsh 2> /dev/null -if [ $? -gt 0 ]; then +if [ $? -eq 0 ]; then GFSH_PATH="gfsh" -else - if [ "$GEODE_HOME" == "" ]; then +else +if [ "$GEODE_HOME" == "" ]; then echo "Could not find gfsh. Please set the GEODE_HOME path." echo "e.g. export GEODE_HOME=<path to Geode>" else @@ -29,4 +28,4 @@ else fi fi -$GFSH_PATH -e "connect" -e "stop server --name=server" -e "stop locator --name=locator" +$GFSH_PATH -e "start locator --name=locator" -e "start server --name=server" -e "create region --name=custom_orders --type=PARTITION" diff --git a/examples/cpp/put-get-remove/stopserver.sh b/examples/cpp/customserializable/stopserver.sh similarity index 97% copy from examples/cpp/put-get-remove/stopserver.sh copy to examples/cpp/customserializable/stopserver.sh index f04fab6..f406a23 100755 --- a/examples/cpp/put-get-remove/stopserver.sh +++ b/examples/cpp/customserializable/stopserver.sh @@ -1,5 +1,3 @@ -#!/bin/env bash - # 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. @@ -15,12 +13,13 @@ # See the License for the specific language governing permissions and # limitations under the License. +#!/bin/env bash GFSH_PATH="" which gfsh 2> /dev/null -if [ $? -gt 0 ]; then +if [ $? -eq 0 ]; then GFSH_PATH="gfsh" -else +else if [ "$GEODE_HOME" == "" ]; then echo "Could not find gfsh. Please set the GEODE_HOME path." echo "e.g. export GEODE_HOME=<path to Geode>" diff --git a/examples/cpp/put-get-remove/startserver.sh b/examples/cpp/put-get-remove/startserver.sh index e657de2..70ff419 100755 --- a/examples/cpp/put-get-remove/startserver.sh +++ b/examples/cpp/put-get-remove/startserver.sh @@ -18,7 +18,7 @@ GFSH_PATH="" which gfsh 2> /dev/null -if [ $? -gt 0 ]; then +if [ $? -eq 0 ]; then GFSH_PATH="gfsh" else if [ "$GEODE_HOME" == "" ]; then diff --git a/examples/cpp/put-get-remove/stopserver.sh b/examples/cpp/put-get-remove/stopserver.sh index f04fab6..8c914a4 100755 --- a/examples/cpp/put-get-remove/stopserver.sh +++ b/examples/cpp/put-get-remove/stopserver.sh @@ -18,7 +18,7 @@ GFSH_PATH="" which gfsh 2> /dev/null -if [ $? -gt 0 ]; then +if [ $? -eq 0 ]; then GFSH_PATH="gfsh" else if [ "$GEODE_HOME" == "" ]; then -- To stop receiving notification emails like this one, please contact jbarr...@apache.org.