package com.sleepycat.examples.bdb.shipment.marshal;
import com.sleepycat.bdb.bind.DataBinding;
import com.sleepycat.bdb.bind.EntityBinding;
import com.sleepycat.bdb.bind.serial.TupleSerialBinding;
import com.sleepycat.bdb.bind.serial.SerialFormat;
import com.sleepycat.bdb.bind.tuple.TupleFormat;
import com.sleepycat.bdb.bind.tuple.TupleInput;
import com.sleepycat.bdb.bind.tuple.TupleOutput;
import com.sleepycat.bdb.bind.tuple.TupleBinding;
import com.sleepycat.bdb.collection.StoredSortedValueSet;
import com.sleepycat.bdb.collection.StoredSortedMap;
import com.sleepycat.bdb.util.IOExceptionWrapper;
import java.io.IOException;
public class SampleViews {
private StoredSortedMap partMap;
private StoredSortedMap supplierMap;
private StoredSortedMap shipmentMap;
private StoredSortedMap shipmentByPartMap;
private StoredSortedMap shipmentBySupplierMap;
private StoredSortedMap supplierByCityMap;
public SampleViews(SampleDatabase db) {
DataBinding partKeyBinding =
new MarshalledKeyBinding(db.getPartKeyFormat(), PartKey.class);
EntityBinding partValueBinding =
new MarshalledEntityBinding(db.getPartKeyFormat(),
db.getPartValueFormat(),
Part.class);
DataBinding supplierKeyBinding =
new MarshalledKeyBinding(db.getSupplierKeyFormat(),
SupplierKey.class);
EntityBinding supplierValueBinding =
new MarshalledEntityBinding(db.getSupplierKeyFormat(),
db.getSupplierValueFormat(),
Supplier.class);
DataBinding shipmentKeyBinding =
new MarshalledKeyBinding(db.getShipmentKeyFormat(),
ShipmentKey.class);
EntityBinding shipmentValueBinding =
new MarshalledEntityBinding(db.getShipmentKeyFormat(),
db.getShipmentValueFormat(),
Shipment.class);
DataBinding cityKeyBinding =
TupleBinding.getPrimitiveBinding(String.class,
db.getCityKeyFormat());
partMap =
new StoredSortedMap(db.getPartStore(),
partKeyBinding, partValueBinding, true);
supplierMap =
new StoredSortedMap(db.getSupplierStore(),
supplierKeyBinding, supplierValueBinding, true);
shipmentMap =
new StoredSortedMap(db.getShipmentStore(),
shipmentKeyBinding, shipmentValueBinding, true);
shipmentByPartMap =
new StoredSortedMap(db.getShipmentByPartIndex(),
partKeyBinding, shipmentValueBinding, true);
shipmentBySupplierMap =
new StoredSortedMap(db.getShipmentBySupplierIndex(),
supplierKeyBinding, shipmentValueBinding, true);
supplierByCityMap =
new StoredSortedMap(db.getSupplierByCityIndex(),
cityKeyBinding, supplierValueBinding, true);
}
public StoredSortedMap getPartMap() {
return partMap;
}
public StoredSortedMap getSupplierMap() {
return supplierMap;
}
public StoredSortedMap getShipmentMap() {
return shipmentMap;
}
public StoredSortedValueSet getPartSet() {
return (StoredSortedValueSet) partMap.values();
}
public StoredSortedValueSet getSupplierSet() {
return (StoredSortedValueSet) supplierMap.values();
}
public StoredSortedValueSet getShipmentSet() {
return (StoredSortedValueSet) shipmentMap.values();
}
public StoredSortedMap getShipmentByPartMap() {
return shipmentByPartMap;
}
public StoredSortedMap getShipmentBySupplierMap() {
return shipmentBySupplierMap;
}
public final StoredSortedMap getSupplierByCityMap() {
return supplierByCityMap;
}
private static class MarshalledKeyBinding extends TupleBinding {
private Class keyClass;
private MarshalledKeyBinding(TupleFormat format, Class keyClass) {
super(format);
if (!MarshalledKey.class.isAssignableFrom(keyClass)) {
throw new IllegalArgumentException(keyClass.toString() +
" does not implement MarshalledKey");
}
this.keyClass = keyClass;
}
public Object dataToObject(TupleInput input)
throws IOException {
try {
MarshalledKey key = (MarshalledKey) keyClass.newInstance();
key.unmarshalKey(input);
return key;
} catch (IllegalAccessException e) {
throw new IOExceptionWrapper(e);
} catch (InstantiationException e) {
throw new IOExceptionWrapper(e);
}
}
public void objectToData(Object object, TupleOutput output)
throws IOException {
MarshalledKey key = (MarshalledKey) object;
key.marshalKey(output);
}
}
private static class MarshalledEntityBinding extends TupleSerialBinding {
private Class entityClass;
private MarshalledEntityBinding(TupleFormat keyFormat,
SerialFormat valueFormat,
Class entityClass) {
super(keyFormat, valueFormat);
if (!MarshalledEntity.class.isAssignableFrom(entityClass)) {
throw new IllegalArgumentException(entityClass.toString() +
" does not implement MarshalledEntity");
}
this.entityClass = entityClass;
}
public Object dataToObject(TupleInput tupleInput, Object javaInput)
throws IOException {
MarshalledEntity entity = (MarshalledEntity) javaInput;
entity.unmarshalPrimaryKey(tupleInput);
return entity;
}
public void objectToKey(Object object, TupleOutput output)
throws IOException {
MarshalledEntity entity = (MarshalledEntity) object;
entity.marshalPrimaryKey(output);
}
public Object objectToValue(Object object)
throws IOException {
return object;
}
}
}