|
Entity bindings are similar to ordinary bindings in that they convert between Java objects and the stored data format of keys and values. In addition, entity bindings map between key/value pairs and entity objects. An ordinary binding is a one-to-one mapping, while an entity binding is a two-to-one mapping.
The partValueBinding, supplierValueBinding and shipmentValueBinding bindings are created below as entity bindings rather than (in the prior examples) serial bindings.
import com.sleepycat.bdb.bind.DataBinding; import com.sleepycat.bdb.bind.EntityBinding; import com.sleepycat.bdb.bind.serial.SerialBinding; import com.sleepycat.bdb.bind.serial.SerialSerialBinding;public class SampleViews { ... public SampleViews(SampleDatabase db) { DataBinding partKeyBinding = new SerialBinding(db.getPartKeyFormat()); EntityBinding partValueBinding = new PartBinding(db.getPartKeyFormat(), db.getPartValueFormat()); DataBinding supplierKeyBinding = new SerialBinding(db.getSupplierKeyFormat()); EntityBinding supplierValueBinding = new SupplierBinding(db.getSupplierKeyFormat(), db.getSupplierValueFormat()); DataBinding shipmentKeyBinding = new SerialBinding(db.getShipmentKeyFormat()); EntityBinding shipmentValueBinding = new ShipmentBinding(db.getShipmentKeyFormat(), db.getShipmentValueFormat()); DataBinding cityKeyBinding = new SerialBinding(db.getCityKeyFormat()); ... } }
The entity bindings will be used in the next section to construct stored map objects.
The PartBinding class is defined below.
public class SampleViews { ... private static class PartBinding extends SerialSerialBinding { private PartBinding(SerialFormat keyFormat, SerialFormat valueFormat) { super(keyFormat, valueFormat); }public Object dataToObject(Object keyInput, Object valueInput) throws IOException { PartKey key = (PartKey) keyInput; PartValue value = (PartValue) valueInput; return new Part(key.getNumber(), value.getName(), value.getColor(), value.getWeight(), value.getCity()); }
public Object objectToKey(Object object) throws IOException { Part part = (Part) object; return new PartKey(part.getNumber()); }
public Object objectToValue(Object object) throws IOException { Part part = (Part) object; return new PartValue(part.getName(), part.getColor(), part.getWeight(), part.getCity()); } } }
In general, an entity binding is any class that implements the EntityBinding interface, just as an ordinary binding is any class that implements the DataBinding interface. In the prior examples the built-in SerialBinding class (which implements DataBinding ) was used and no application-defined binding classes were needed.
In this example, application-defined binding classes are used that extend the SerialSerialBinding abstract base class. This base class implements EntityBinding and provides the conversions between key/value bytes and key/value objects, just as the SerialBinding class does. The application-defined entity class implements the abstract methods defined in the base class that map between key/value objects and entity objects.
Three abstract methods are implemented for each entity binding. The dataToObject method takes as input the key and value objects, which have been deserialized automatically by the base class. As output, it returns the combined Part entity.
The objectToKey and objectToValue methods take an entity object as input. As output they return the part key or value object that is extracted from the entity object. The key or value will then be serialized automatically by the base class.
The SupplierBinding and ShipmentBinding classes are very similar to the PartBinding class.
public class SampleViews { ... private static class SupplierBinding extends SerialSerialBinding { private SupplierBinding(SerialFormat keyFormat, SerialFormat valueFormat) { super(keyFormat, valueFormat); }public Object dataToObject(Object keyInput, Object valueInput) throws IOException { SupplierKey key = (SupplierKey) keyInput; SupplierValue value = (SupplierValue) valueInput; return new Supplier(key.getNumber(), value.getName(), value.getStatus(), value.getCity()); }
public Object objectToKey(Object object) throws IOException { Supplier supplier = (Supplier) object; return new SupplierKey(supplier.getNumber()); }
public Object objectToValue(Object object) throws IOException { Supplier supplier = (Supplier) object; return new SupplierValue(supplier.getName(), supplier.getStatus(), supplier.getCity()); } }
private static class ShipmentBinding extends SerialSerialBinding { private ShipmentBinding(SerialFormat keyFormat, SerialFormat valueFormat) { super(keyFormat, valueFormat); }
public Object dataToObject(Object keyInput, Object valueInput) throws IOException { ShipmentKey key = (ShipmentKey) keyInput; ShipmentValue value = (ShipmentValue) valueInput; return new Shipment(key.getPartNumber(), key.getSupplierNumber(), value.getQuantity()); }
public Object objectToKey(Object object) throws IOException { Shipment shipment = (Shipment) object; return new ShipmentKey(shipment.getPartNumber(), shipment.getSupplierNumber()); }
public Object objectToValue(Object object) throws IOException { Shipment shipment = (Shipment) object; return new ShipmentValue(shipment.getQuantity()); } } }
Copyright (c) 1996-2003 Sleepycat Software, Inc. - All rights reserved.