DataSerializer プラグインを使用して、自動のオブジェクト・インフレーションを迂回して、既にシリアライズされたデータから手動で属性を取得できます。DataSerializer を使用して、データをシリアライズ形式で挿入したり更新したりすることもできます。 この使用法は、データの一部のみにアクセスする必要があるときや、システム間でデータを受け渡しする必要があるときに有用です。
このタスクは、MapSerializerPlugin プラグインと ValueSerializerPlugin プラグインで COPY_TO_BYTES_RAW コピー・モードを使用します。MapSerializer は、BackingMap インターフェースをポイントする、メインのプラグインです。これには、KeyDataSerializer と ValueDataSerializer という、ネストされた 2 つのプラグインが含まれます。製品はネストされたプラグインをサポートしないため、BaseMapSerializer は、ネストまたは接続されたプラグインを人工的にサポートします。したがって、OSGi コンテナーの中でこれらの API を使用する場合、MapSerializer が唯一のプロキシーになります。 サポートする参照をリフレッシュできるように BackingMap ライフサイクル・イベントも listen していない限り、必ずしもすべてのネストされたプラグインが、例えばローダーなどの他の従属プラグイン内にキャッシュしなければならないというわけではありません。
COPY_TO_BYTES_RAW が設定されると、すべての ObjectMap メソッドは SerializedValue オブジェクトを返し、ユーザーはシリアライズ形式または Java オブジェクト形式の値を取得することができます。
KeySerializerPlugin プラグインを使用したとき、キーを返すすべてのメソッド (MapIndexPlugin プラグインや Loader プラグインなど) は SerializedKey オブジェクトを返します。
データが既にシリアライズ形式であるとき、データは同じ SerializedKey オブジェクトおよび SerializedValue オブジェクトを使用して挿入されます。 データが byte[] 形式であるときは、適切なキーまたは値ラッパーを作成するために DataObjectKeyFactory ファクトリーおよび DataObjectValueFactory ファクトリーが使用されます。 これらのファクトリーは、BackingMap の SerializerAccessor から、または DataSerializer 実装内からアクセスできる DataObjectContext で使用可能です。
このトピック内の例は、以下のアクションを実行する方法を示しています。
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);
}
}