[Java programming language only]

Avoiding object inflation when updating and retrieving cache data

You can use the DataSerializer plug-ins to bypass automatic object inflation and manually retrieve attributes from data that has already been serialized. You can also use the DataSerializer to insert and update data in its serialized form. This usage can be useful when only part of the data needs to be accessed or when the data needs to be passed between systems.

About this task

This task uses the COPY_TO_BYTES_RAW copy mode with the MapSerializerPlugin and ValueSerializerPlugin plug-ins. The MapSerializer is the main plug-in point to the BackingMap interface. It includes two nested plug-ins, the KeyDataSerializer and ValueDataSerializer. Since the product does not support nested plug-ins, the BaseMapSerializer supports nested or wired plug-ins artificially. Therefore, when you use these APIs in the OSGi container, the MapSerializer is the only proxy. All nested plug-ins must not be cached within other dependent plug-ins, such as a loader, unless it also listens for BackingMap life cycle events, so that it can refresh its supporting references.

When COPY_TO_BYTES_RAW is set, all ObjectMap methods return SerializedValue objects, allowing the user to retrieve the serialized form or the Java object form of the value.

When using a KeySerializerPlugin plug-in, all methods that return keys, such as the MapIndexPlugin or Loader plug-ins return SerializedKey objects.

When the data is already in serialized form, the data is inserted using the same SerializedKey and SerializedValue objects. When the data is in byte[] format, the DataObjectKeyFactory and DataObjectValueFactory factories are used to create the appropriate key or value wrapper. The factories are available on the DataObjectContext, which can be accessed from the SerializerAccessor for the BackingMap, or from within the DataSerializer implementation.

The example in this topic demonstrates how to complete the following actions:

Procedure

  1. Use the DataSerializer plug-ins to serialize and inflate data objects.
  2. Retrieve serialized values.
  3. Retrieve individual attributes from a serialized value.
  4. Insert pre-serialized keys and values.

Example

Use this example to update and retrieve cache data:
import java.io.IOException;
import com.ibm.websphere.objectgrid.CopyMode;
import com.ibm.websphere.objectgrid.ObjectGrid;
import com.ibm.websphere.objectgrid.ObjectGridException;
import com.ibm.websphere.objectgrid.ObjectMap;
import com.ibm.websphere.objectgrid.Session;
import com.ibm.websphere.objectgrid.io.XsDataOutputStream;
import com.ibm.websphere.objectgrid.plugins.io.SerializerAccessor;
import com.ibm.websphere.objectgrid.plugins.io.ValueSerializerPlugin;
import com.ibm.websphere.objectgrid.plugins.io.dataobject.DataObjectContext;
import com.ibm.websphere.objectgrid.plugins.io.dataobject.SerializedKey;
import com.ibm.websphere.objectgrid.plugins.io.dataobject.SerializedValue;

    /**
     * Use the DataSerializer to serialize an Order key.
     */
    public byte[] serializeOrderKey(ObjectGrid grid, String key)
            throws IOException {
        SerializerAccessor sa = grid.getMap("Order").getSerializerAccessor();
        DataObjectContext dftObjCtx = sa.getDefaultContext();
        XsDataOutputStream out = dftObjCtx.getDataStreamManager()
                .createOutputStream();
        sa.getMapSerializerPlugin().getKeySerializerPlugin()
                .serializeDataObject(sa.getDefaultContext(), key, out);
        return out.toByteArray();
    }

    /**
     * Use the DataSerializer to serialize an Order value.
     */
    public byte[] serializeOrderValue(ObjectGrid grid, Order value)
            throws IOException {
        SerializerAccessor sa = grid.getMap("Order").getSerializerAccessor();
        DataObjectContext dftObjCtx = sa.getDefaultContext();
        XsDataOutputStream out = dftObjCtx.getDataStreamManager()
                .createOutputStream();
        sa.getMapSerializerPlugin().getValueSerializerPlugin()
                .serializeDataObject(sa.getDefaultContext(), value, out);
        return out.toByteArray();
    }

    /**
     * Retrieve a single Order in serialized form.
     */
    public byte[] fetchOrderRAWBytes(Session session, String key)
            throws ObjectGridException {
        ObjectMap map = session.getMap("Order");

        // Override the CopyMode to retrieve the serialized form of the value.
        // This process affects all API methods from this point on for the life
        // of the Session.
        map.setCopyMode(CopyMode.COPY_TO_BYTES_RAW, null);
        SerializedValue serValue = (SerializedValue) map.get(key);

        if (serValue == null)
            return null;

        // Retrieve the byte array and return it to the caller.
        return serValue.getInputStream().toByteArray();
    }

    /**
     * Retrieve one or more attributes from the Order without inflating the
     * Order object.
     */
    public Object[] fetchOrderAttribute(Session session, String key,
            String... attributes) throws ObjectGridException, IOException {
        ObjectMap map = session.getMap("Order");

        // Override the CopyMode to retrieve the serialized form of the value.
        // This process affects all API methods from this point on for the life
        // of the Session.
        map.setCopyMode(CopyMode.COPY_TO_BYTES_RAW, null);
        SerializedValue serValue = (SerializedValue) map.get(key);

        if (serValue == null)
            return null;

        // Retrieve a single attribute from the byte buffer.
        ValueSerializerPlugin valSer = session.getObjectGrid()
                .getMap(map.getName()).getSerializerAccessor()
                .getMapSerializerPlugin().getValueSerializerPlugin();
        Object attrCtx = valSer.getAttributeContexts(attributes);
        return valSer.inflateDataObjectAttributes(serValue.getContext(),
                serValue.getInputStream(), attrCtx);
    }

    /**
     * Inserts a pre-serialized key and value into the Order map.
     */
    public void insertRAWOrder(Session session, byte[] key, byte[] value)
            throws ObjectGridException {
        ObjectMap map = session.getMap("Order");

        // Get a referece to the default DataObjectContext for the map.
        DataObjectContext dftDtaObjCtx = session.getObjectGrid()
                .getMap(map.getName()).getSerializerAccessor()
                .getDefaultContext();

        // Wrap the key and value in a SerializedKey and SerializedValue
        // wrapper.
        SerializedKey serKey = dftDtaObjCtx.getKeyFactory().createKey(key);
        SerializedValue serValue = dftDtaObjCtx.getValueFactory().createValue(
                value);

        // Insert the serialized form of the key and value.
        map.insert(serKey, serValue);
    }
}