This topic explains how to implement PdxSerializable in your domain object in VMware Tanzu GemFire.
For a domain object with source that you can modify, implement the PdxSerializable
interface in the object and use its methods to serialize and deserialize the object’s fields.
In your domain class, implement PdxSerializable
, importing the required org.apache.geode.pdx
classes.
For example:
import org.apache.geode.pdx.PdxReader;
import org.apache.geode.pdx.PdxSerializable;
import org.apache.geode.pdx.PdxWriter;
public class PortfolioPdx implements PdxSerializable {
...
If your domain class does not have a zero-arg constructor, create one for it.
For example:
public PortfolioPdx(){}
Program PdxSerializable.toData.
PdxWriter
write methods. Tanzu GemFire automatically provides PdxWriter
to the toData
method for PdxSerializable
objects.PdxWriter
markIdentifyField
method for each field you want to have Tanzu GemFire use to identify your object. Put this after the field’s write method. Tanzu GemFire uses this information to compare objects for operations like distinct queries. If you do not set as least one identity field, then the equals
and hashCode
methods will use all PDX fields to compare objects and consequently, will not perform as well. It is important that the fields used by your equals
and hashCode
implementations are the same fields that you mark as identity fields.For best performance, do fixed width fields first and then variable length fields.
Example toData
code:
// PortfolioPdx fields
private int id;
private String pkid;
private Map<String, PositionPdx> positions;
private String type;
private String status;
private String[] names;
private byte[] newVal;
private Date creationDate;
...
public void toData(PdxWriter writer)
{
writer.writeInt("id", id)
// The markIdentifyField call for a field must
// come after the field's write method
.markIdentityField("id")
.writeDate("creationDate", creationDate) //fixed length field
.writeString("pkid", pkid)
.writeObject("positions", positions)
.writeString("type", type)
.writeString("status", status)
.writeStringArray("names", names)
.writeByteArray("newVal", newVal)
}
Program PdxSerializable.fromData
to read your data fields from the serialized form into the object’s fields using the PdxReader
read methods.
Provide the same names that you did in toData
and call the read operations in the same order as you called the write operations in your toData
implementation.
Tanzu GemFire automatically provides PdxReader
to the fromData
method for PdxSerializable
objects.
Example fromData
code:
public void fromData(PdxReader reader)
{
id = reader.readInt("id");
creationDate = reader.readDate("creationDate");
pkid = reader.readString("pkid");
position1 = (PositionPdx)reader.readObject("position1");
position2 = (PositionPdx)reader.readObject("position2");
positions = (Map<String, PositionPdx>)reader.readObject("positions");
type = reader.readString("type");
status = reader.readString("status");
names = reader.readStringArray("names");
newVal = reader.readByteArray("newVal");
arrayNull = reader.readByteArray("arrayNull");
arrayZeroSize = reader.readByteArray("arrayZeroSize");
}
What to do next
PdxInstance
for selective object deserialization. See Programming Your Application to Use PdxInstances.