blob: 714980c7f54ef750484dee7339a29f7c6757de7e [file] [log] [blame]
---
title: PdxSerializable Example
---
<!--
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.
-->
## Order.cpp:
Front matter:
#include "Order.hpp"
`PdxReader` & `PdxWriter` are used for `fromData()` & `toData()`:
#include <geode/PdxReader.hpp>
#include <geode/PdxWriter.hpp>
namespace customserializable {
- Must provide `fromData()` and `toData()`.
- Specify the keys in the same order in `fromData()` and `toData()`. Use `PdxReader` and `PdxWriter`:
```
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_);
}
```
Optionally override and add detailed methods as needed. In this example, we override `toString()` and implement `size()` and `classname()`.
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;
}
Provide a constructor in a parameter-less wrapper that can be called to create a prototypical instance (called `createDeserializable()` here).
std::shared_ptr<PdxSerializable> Order::createDeserializable() {
return std::make_shared<Order>(0, "", 0);
}
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
## main.cpp
Front matter. You will need `TypeRegistry` to register your PdxSerializable class. Bring in the customary includes for I/O and cache, pool, and region creation.
#include <iostream>
#include <sstream>
#include <geode/CacheFactory.hpp>
#include <geode/PoolManager.hpp>
#include <geode/RegionFactory.hpp>
#include <geode/RegionShortcut.hpp>
#include <geode/TypeRegistry.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();
}