You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@geode.apache.org by GitBox <gi...@apache.org> on 2020/06/12 16:57:39 UTC

[GitHub] [geode-native] davebarnes97 commented on a change in pull request #617: Update C++ serialization example

davebarnes97 commented on a change in pull request #617:
URL: https://github.com/apache/geode-native/pull/617#discussion_r439536684



##########
File path: docs/geode-native-docs-cpp/serialization/cpp-serialization/pdxserializable-interface.html.md.erb
##########
@@ -27,88 +27,60 @@ A domain class should serialize and de-serialize all its member fields in the sa
 
 Use this procedure to program your domain object for PDX serialization using the `PdxSerializable` abstract class.
 
-1.  In your domain class, implement `PdxSerializable`. Example:
+1.  In your domain class, implement `PdxSerializable`. For example:
 
     ``` pre
-    class PdxObject: public PdxSerializable
+    class Order : public PdxSerializable {
     ```
 
-2.  Program the `toData` function to serialize your object as required by your application.
-    <br><br>
+2.  Program the `toData` method to serialize your object as required by your application. (See `markIdentityField` in a later step for an optimization that you can apply to this code sample.)
+
+    ``` cpp
+    void Order::toData(PdxWriter& pdxWriter) const {
+      pdxWriter.writeInt(ORDER_ID_KEY_, order_id_);
+      pdxWriter.writeString(NAME_KEY_, name_);
+      pdxWriter.writeShort(QUANTITY_KEY_, quantity_);
+    }
+    ```
+    
     If you also use PDX serialization in Java or .NET for the object, serialize the object in the same way for each language. Serialize the same fields in the same order and mark the same identity fields.
 
 3.  Program the `fromData` method to read your data fields from the serialized form into the object's fields.
-    <br><br>
-    In your `fromData` implementation, use the same name as you did in `toData` and call the read operations in the same order as you called the write operations in your `toData` implementation.
 
-4.  Optionally, program your domain object's `hashCode` and equality methods.
-    <br><br>
-    Use the `markIdentityField` method to indicate that the given field name should be included in `hashCode` and equality checks of this object on a server.
-    <br><br>
-    The fields that are marked as identity fields are used to generate the `hashCode` and equality methods of PdxInstance. Because of this, the identity fields should themselves either be primitives, or implement `hashCode` and equals.
-    <br><br>
-    If no fields are set as identity fields, then all fields will be used in `hashCode` and equality checks. The identity fields should make marked after they are written using a `write*` method.
-
-For example:
-
-``` cpp
-class PdxObject: public PdxSerializable {
+    ```cpp
+    void Order::fromData(PdxReader& pdxReader) {
+      order_id_ = pdxReader.readInt(ORDER_ID_KEY_);
+      name_ = pdxReader.readString(NAME_KEY_);
+      quantity_ = pdxReader.readShort(QUANTITY_KEY_);
+    }
+    ```
 
-private:
-    uint32_t m_id;
-    char* m_str;
+    In your `fromData` implementation, use the same name as you did in `toData` and call the read operations in the same order as you called the write operations in your `toData` implementation.
 
-public:
-    PdxObject(){};
-    PdxObject(uint32_t id, char* str);
-    virtual ~PdxObject();
+4.  Optionally, program your domain object's `hashCode` and equality methods. When you do so, you can optimize those methods by specifying the _identity fields_
+    to be used in comparisons. 
+    <br /><br />
+    If no fields are set as identity fields, then all fields will be used in `hashCode` and equality checks, so marking identity fields improves the efficiency
+    of hashing and equality operations. 
+    <br /><br />
+    Use the `markIdentityField` method to indicate that the given field name should be included in `hashCode` and equality checks of this object on a server.
+    It is important that the fields used by your equality method and `hashCode` implementations are the same fields that you mark as identity fields.
+    <br /><br />
+    Expanding the code sample from the description of the `toData` method, above:
 
-    uint32_t getID() {
-        return m_id;
-    }
+    ``` cpp
+    void Order::toData(PdxWriter& pdxWriter) const {
+      pdxWriter.writeInt(ORDER_ID_KEY_, order_id_);
+      pdxWriter.markIdentityField(ORDER_ID_KEY_);
 
-    char* getStr(){
-        return m_str;
-    }
+      pdxWriter.writeString(NAME_KEY_, name_);
+      pdxWriter.markIdentityField(NAME_KEY_);
 
-    virtual void toData(PdxWriterPtr pw) const;
-    virtual void fromData(PdxReaderPtr pr);
-    CacheableStringPtr toString() const;
-    virtual char* getClassName() const;
-    static Cacheable* createDeserializable() {
-        return new PdxObject();
+      pdxWriter.writeShort(QUANTITY_KEY_, quantity_);
+      pdxWriter.markIdentityField(QUANTITY_KEY_);
     }
-};
-
-PdxObject::PdxObject(uint32_t i, char* str) {
-    m_id = i;
-    m_str = str;
-}
-
-PdxObject::~PdxObject() {
-}
-
-void PdxObject::toData( PdxWriterPtr pw ) const {
-    pw->writeInt("id", m_id);
-       pw->markIdentityField("id");
-    pw->writeString("str", m_str);
-}
-
-void  PdxObject::fromData( PdxReaderPtr pr )
-{
-    m_id = pr->readInt("id");
-    m_str = pr->readString("str");
-}
-
-char* getClassName() const{
-{
-    return "com.example.PdxType";
-}
-
-CacheableStringPtr PdxObject::toString() const {
-       char idbuf[1024];
-       sprintf(idbuf,"PdxObject: [ ID=%d ]",m_id);
-       return CacheableString::create( idbuf );
-}
-```
-
+    ```
+    The fields that are marked as identity fields are used to generate the `hashCode` and equality
+    methods of PdxInstance. Because of this, the identity fields should themselves either be primitives,
+    or implement `hashCode` and equals.  The identity fields should be marked after they are written
+    using a `write*` method.

Review comment:
       Good suggestions, @karensmolermiller. I checked in a revision - please give it a second look.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org