Implement the IGeodeSerializable Interface
To store your own data types in the cache, you implement the GemFire IGeodeSerializable
interface.
Examples follow the procedure.
Procedure
Implement the
ToData
function that serializes your data:void ToData(DataOutput output)
The
ToData
function is responsible for copying all of the data fields for the object to the object stream. TheDataOutput
class represents the output stream and provides methods for writing the primitives in a network byte order.Implement the
FromData
function that consumes a data input stream and repopulates the data fields for the object:void fromData (DataInput& input)
The
DataInput
class represents the input stream and provides methods for reading input elements. TheFromData
function must read the elements of the input stream in the same order that they were written byToData
.Implement the
ClassId
function to return an integer which is unique for your class (in the set of all of your user-defined classes).
Simple BankAccount Class
This example shows a simple class, BankAccount
, that encapsulates the two ints
, customerId
and accountId
:
public class BankAccount
{
private int m_customerId;
private int m_accountId;
public int Customer
{
get
{
return m_customerId;
}
}
public int Account
{
get
{
return m_accountId;
}
}
public BankAccount(int customer, int account)
{
m_customerId = customer;
m_accountId = account;
}
}
Implementing a Serializable Class
To make BankAccount
serializable, you implement the IGeodeSerializable
interface as shown in this example:
public class BankAccount : IGeodeSerializable
{
private int m_customerId;
private int m_accountId;
public int Customer
{
get
{
return m_customerId;
}
}
public int Account
{
get
{
return m_accountId;
}
}
public BankAccount(int customer, int account)
{
m_customerId = customer;
m_accountId = account;
}
// Our TypeFactoryMethod
public static IGeodeSerializable CreateInstance()
{
return new BankAccount(0, 0);
}
#region IGeodeSerializable Members
public void ToData(DataOutput output)
{
output.WriteInt32(m_customerId);
output.WriteInt32(m_accountId);
}
public void FromData(DataInput input)
{
m_customerId = input.ReadInt32();
m_accountId = input.ReadInt32();
return this;
}
public UInt32 ClassId
{
get
{
return 11;
}
}
public UInt32 ObjectSize
{
get
{
return (UInt32)(sizeof(Int32) + sizeof(Int32));
}
}
}