LATEST VERSION: 8.1.0 - CHANGELOG
Pivotal GemFire® v8.1

Using Automatic PDX Serialization

Using Automatic PDX Serialization

You can allow your native client C++ applications to automatically PDX serialize and deserialize domain objects without having to add any extra code by using the pdxautoserializer command line tool provided with the native client.

When using the native client C++ API, you can automatically serialize and deserialize domain objects without making any code changes to those objects or having to implement a PdxSerializer or PdxSerializable interface and their related fromData and toData methods. The GemFire native client includes a command-line utility, pdxautoserializer, that allows you to generate C++ code that will serialize your domain objects in the PDX format for you.

How to Use Automatic PDX Serialization

Prerequisites:
  • Understand generally how to configure the GemFire cache.
  • Understand how PDX serialization works and how to configure your application to use PdxSerializer.
The procedure below uses the following sample class:
class PortfolioPdx 
{
  private:
    int32_t id;
    char* pkid;
    PositionPdxPtr position1;
    PositionPdxPtr position2;
    CacheableHashMapPtr positions;
    char** names;    
    int8_t* newVal;
    CacheableDatePtr creationDate;
    int8_t* arrayNull;
    int8_t* arrayZeroSize;
  public:
    // CTOR
    // DTORS
    // Other Methods declarations

For each domain class you provide, all fields are considered for serialization except those defined as static or transient and those you explicitly exclude using macros.

  1. Inherit your class from gemfire::PdxSerializable.
    class PortfolioPdx : public PdxSerializable
  2. Add the following method declarations in the public part of the class.
    const char* getClassName() const;
    virtual void toData(gemfire::PdxWriterPtr pw) ;
    virtual void fromData(gemfire::PdxReaderPtr pr);
    static PdxSerializable* createDeserializable();
  3. In your pre-build environment (for example in your makefiles), call pdxautoserializer as follows:
    <GFCPP>/bin/pdxautoserializer.exe --outDir=<location to generate files> <SOURCE_DIR>/PortfolioPdx.hpp
  4. Include the generated file in your project and compile.
The following is an example of a generated file:
#include "PortfolioPdx.hpp"
#include <gfcpp/PdxWriter.hpp>
#include <gfcpp/PdxReader.hpp>
#include <gfcpp/PdxAutoSerializer.hpp>
namespace testobject
{
  void PortfolioPdx::toData(gemfire::PdxWriterPtr var)
  {
    gemfire::PdxAutoSerializable::writePdxObject(var, "id", id);
    gemfire::PdxAutoSerializable::writePdxObject(var, "pkid", pkid);
    gemfire::PdxAutoSerializable::writePdxObject(var, "position1", position1);
    gemfire::PdxAutoSerializable::writePdxObject(var, "position2", position2);
    gemfire::PdxAutoSerializable::writePdxObject(var, "positions", positions);
    gemfire::PdxAutoSerializable::writePdxObject(var, "status", status);
    gemfire::PdxAutoSerializable::writePdxObject(var, "creationDate", creationDate);
  }

  void PortfolioPdx::fromData(PdxReaderPtr var)
  {
    gemfire::PdxAutoSerializable::readPdxObject( var, "id", id);
    gemfire::PdxAutoSerializable::readPdxObject( var, "pkid", pkid);
    gemfire::PdxAutoSerializable::readPdxObject( var, "position1", position1);
    gemfire::PdxAutoSerializable::readPdxObject( var, "position2", position2);
    gemfire::PdxAutoSerializable::readPdxObject( var, "positions", positions);
    gemfire::PdxAutoSerializable::readPdxObject( var, "status", status);
    gemfire::PdxAutoSerializable::readPdxObject( var, "creationDate", creationDate);
  }
   const char* PortfolioPdx::getClassName()  const
  {
     return "PortfolioPdx";
  }
}

Handling Arrays

  1. Define the following macro in your header file:
    #define GFARRAYSIZE(x)
  2. Assuming that the following is the class member of type array:
    int8_t* newVal;
    Then define a new variable which sets the length of the array:
    int32_t newValSize;
  3. Tag the new variable with the GFARRAYSIZE macro as follows:
    GFARRAYSIZE(newVal) int32_t newValSize;

Using a Single Variable as Length for Multiple Arrays

You can use the GFARRAYSIZES to have single length for multiple arrays.

Define the GFARRAYSIZES macro as follows
#define GFARRAYSIZES(x)
The following is an example usage:
class ArrayOfSizes?
    {
    public:
    int32_t* array1;
    int32_t* array2;
    int32_t* array3;
    int32_t* array4;
    int32_t* array5;

    GFARRAYSIZE(array1) int32_t singleSize; 
    GFARRAYSIZES("array2,array3,array4,array5") int32_t SingleSizeToMultipleArrays?;
  };​

Excluding Member Variables from Serialization

  1. Define the following macro in your header file:
    #define GFEXCLUDE
  2. Tag your member variable with this macro:
    GFEXCLUDE char* type;

Marking Identity Fields

Identity fields are used when comparing objects using the hashCode and equals methods.
  1. Define the following macro in your header file.
    #define GFID(x)
  2. Assuming that the following is the class member you want to use as IdentityField:
    int8_t* newVal;
    Tag the member with the GFID macro as follows:
    GFID(newVal)int8_t* newVal;

Ignoring User Defined Keywords

You might have certain user defined keywords after the class name. Current C++ grammar does not support this. If you have some keywords user will have to ignore them by using GFIGNORE macro.

For example, consider the following class definition:
#ifdef _WIN32
#ifdef BUILD_TESTOBJECT
#define TESTOBJECT_EXPORT LIBEXP
#else
#define TESTOBJECT_EXPORT LIBIMP
#endif
#else
#define TESTOBJECT_EXPORT
#endif

namespace PdxAutoTests {
  class TESTOBJECT_EXPORT PdxAutoMegaType :  public PdxSerializable {
  }
Currently, the pdxautoserializer tool will fail to recognize TESTOBJECT_EXPORT. Change your class by adding GFIGNORE macro as follows:
#ifdef _WIN32
#ifdef BUILD_TESTOBJECT
#define TESTOBJECT_EXPORT LIBEXP
#else
#define TESTOBJECT_EXPORT LIBIMP
#endif
#else
#define TESTOBJECT_EXPORT
#endif

using namespace gemfire;

#define GFIGNORE(X) X
#define GFID

namespace PdxAutoTests {
  class GFIGNORE(TESTOBJECT_EXPORT) PdxAutoMegaType :  public PdxSerializable {

Additional Usage Information for the pdxautoserializer Tool

The pdxautoserializer tool takes classes as input and generates code that will serialize the class into the PDX format for you.

The pdxautoserializer tool is located in $GFCPP/bin where $GFCPP corresponds to the installation location of the native client.

Some additional notes about using the pdxautoserializer tool:
  • Any const type in the class members are ignored by the pdxserializer tool.
  • Generated files will have namespace in the file name.
To view the command-line help for the tool, type:
prompt> pdxautoserializer.exe --help

Help returns the following syntax and usage information:

Usage: pdxautoserializer.exe [OPTIONS] <resources e.g. header> ...

Resource name should be the path to the header containing the classes to be 
auto-serialized.

[OPTIONS] may be one of those given below.

SINGLE denotes that the option should be specified only once.
MULTIPLE denotes that the option can be specified more than once.
OPTIONAL denotes that the option may be skipped in which case the default 
for that shall be chosen.

--className=VALUE       Name of the class for which to generate auto-serialization code (MULTIPLE,OPTIONAL)
--classNameStr=VALUE    Name of the class in string (MULTIPLE,OPTIONAL)
--help                  This help message.
--outDir                The output directory of the generated files (SINGLE,OPTIONAL)
--suffix                The suffix of the generated filenames -- default is 'Serializable' (SINGLE,OPTIONAL)
--usage                 This usage message.

Examples:
pdxautoserializer -outDir=<DIR NAME> <RESOURCE>
pdxautoserializer -outDir=<DIR NAME> --className=<CLASSNAME1> --className=<CLASSNAME2> <RESOURCE>
pdxautoserializer -outDir=<DIR NAME> --classNameStr=<CLASSNAME1:User defined String> --classNameStr=<CLASSNAME:User defined String> <RESOURCE>

Helper Macros to be defined in Input Header File :
GFINCLUDE        for including a specific member for serialization
GFEXCLUDE        for excluding a specific member for serialization
GFID             for considering a member as Identify Field
GFARRAYSIZE      for specifying a array length member
GFIGNORE         for ignoring certain keywords
For more details refer to documentation on this utility.

Generating Automatic Code for Single Class

Many times there are multiple classes in a single header file. For example:
#ifndef HEADER_HEADER
#define HEADER_HEADER

class class1{
};
class class2{
};
class class3 : public PdxSerializable{
};
#endif
If you want to generate code for only one of the classes, then use the --className option. For example, if you only want to generate code for class3, then you would use the following command:
pdxautoserializer --outDir=<outDir> --className=class3

Choosing Your Own Suffix to identify the generated files.

The pdxserializer tool also provides the option to choose your own suffix for the generated C++ files. This can help you write less code in your makefiles. Here's an example command:
pdxautoserializer --outDir=<outDir> --className=CharTypes --suffix="generated"

Example of Using PDX Serialization in Your Application

CacheFactoryPtr cacheFactory = CacheFactory::createCacheFactory();
    // Create a GemFire Cache with the "clientPdxRemoteQuery.xml" Cache XML file.
    CachePtr cachePtr = cacheFactory->set("cache-xml-file", "XMLs\\clientPdxRemoteQuery.xml")->create();

    LOGINFO("Created the GemFire Cache");

    // Get the example Region from the Cache which is declared in the Cache XML file.
    RegionPtr regionPtr = cachePtr->getRegion( "Portfolios");

    LOGINFO( "Obtained the Region from the Cache");

    // Register our Serializable/Cacheable Query objects, viz. PortfolioPdx and PositionPdx.
    Serializable::registerPdxType(PortfolioPdx::createDeserializable);
    PortfolioPdxPtr port1Ptr(new PortfolioPdx(1 /*ID*/, 10 /*size*/));
    PortfolioPdxPtr port2Ptr(new PortfolioPdx(2 /*ID*/, 20 /*size*/));
    PortfolioPdxPtr port3Ptr(new PortfolioPdx(3 /*ID*/, 30 /*size*/));
    regionPtr->put("Key1", port1Ptr);
    regionPtr->put("Key2", port2Ptr);
    regionPtr->put("Key3", port3Ptr);
    
    // Get the QueryService from the Cache.
    QueryServicePtr qrySvcPtr = cachePtr->getQueryService( "examplePool");

    LOGINFO( "Got the QueryService from the Cache");

    // Execute a Query which returns a ResultSet.
    QueryPtr qryPtr = qrySvcPtr->newQuery("SELECT DISTINCT * FROM /Portfolios");
    SelectResultsPtr resultsPtr = qryPtr->execute();

    LOGINFO( "ResultSet Query returned %d rows", resultsPtr->size());