package com.sleepycat.examples.bdb.shipment.tuple;
import com.sleepycat.bdb.bind.DataBinding;
import com.sleepycat.bdb.bind.EntityBinding;
import com.sleepycat.bdb.bind.serial.SerialFormat;
import com.sleepycat.bdb.bind.serial.TupleSerialBinding;
import com.sleepycat.bdb.bind.tuple.TupleBinding;
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.collection.StoredSortedValueSet;
import com.sleepycat.bdb.collection.StoredSortedMap;
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 PartKeyBinding(db.getPartKeyFormat());
EntityBinding partValueBinding =
new PartBinding(db.getPartKeyFormat(), db.getPartValueFormat());
DataBinding supplierKeyBinding =
new SupplierKeyBinding(db.getSupplierKeyFormat());
EntityBinding supplierValueBinding =
new SupplierBinding(db.getSupplierKeyFormat(),
db.getSupplierValueFormat());
DataBinding shipmentKeyBinding =
new ShipmentKeyBinding(db.getShipmentKeyFormat());
EntityBinding shipmentValueBinding =
new ShipmentBinding(db.getShipmentKeyFormat(),
db.getShipmentValueFormat());
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 PartKeyBinding extends TupleBinding {
private PartKeyBinding(TupleFormat format) {
super(format);
}
public Object dataToObject(TupleInput input)
throws IOException {
String number = input.readString();
return new PartKey(number);
}
public void objectToData(Object object, TupleOutput output)
throws IOException {
PartKey key = (PartKey) object;
output.writeString(key.getNumber());
}
}
private static class PartBinding extends TupleSerialBinding {
private PartBinding(TupleFormat keyFormat,
SerialFormat valueFormat) {
super(keyFormat, valueFormat);
}
public Object dataToObject(TupleInput keyInput, Object valueInput)
throws IOException {
String number = keyInput.readString();
PartValue value = (PartValue) valueInput;
return new Part(number, value.getName(), value.getColor(),
value.getWeight(), value.getCity());
}
public void objectToKey(Object object, TupleOutput output)
throws IOException {
Part part = (Part) object;
output.writeString(part.getNumber());
}
public Object objectToValue(Object object)
throws IOException {
Part part = (Part) object;
return new PartValue(part.getName(), part.getColor(),
part.getWeight(), part.getCity());
}
}
private static class SupplierKeyBinding extends TupleBinding {
private SupplierKeyBinding(TupleFormat format) {
super(format);
}
public Object dataToObject(TupleInput input)
throws IOException {
String number = input.readString();
return new SupplierKey(number);
}
public void objectToData(Object object, TupleOutput output)
throws IOException {
SupplierKey key = (SupplierKey) object;
output.writeString(key.getNumber());
}
}
private static class SupplierBinding extends TupleSerialBinding {
private SupplierBinding(TupleFormat keyFormat,
SerialFormat valueFormat) {
super(keyFormat, valueFormat);
}
public Object dataToObject(TupleInput keyInput, Object valueInput)
throws IOException {
String number = keyInput.readString();
SupplierValue value = (SupplierValue) valueInput;
return new Supplier(number, value.getName(),
value.getStatus(), value.getCity());
}
public void objectToKey(Object object, TupleOutput output)
throws IOException {
Supplier supplier = (Supplier) object;
output.writeString(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 ShipmentKeyBinding extends TupleBinding {
private ShipmentKeyBinding(TupleFormat format) {
super(format);
}
public Object dataToObject(TupleInput input)
throws IOException {
String partNumber = input.readString();
String supplierNumber = input.readString();
return new ShipmentKey(partNumber, supplierNumber);
}
public void objectToData(Object object, TupleOutput output)
throws IOException {
ShipmentKey key = (ShipmentKey) object;
output.writeString(key.getPartNumber());
output.writeString(key.getSupplierNumber());
}
}
private static class ShipmentBinding extends TupleSerialBinding {
private ShipmentBinding(TupleFormat keyFormat,
SerialFormat valueFormat) {
super(keyFormat, valueFormat);
}
public Object dataToObject(TupleInput keyInput, Object valueInput)
throws IOException {
String partNumber = keyInput.readString();
String supplierNumber = keyInput.readString();
ShipmentValue value = (ShipmentValue) valueInput;
return new Shipment(partNumber, supplierNumber,
value.getQuantity());
}
public void objectToKey(Object object, TupleOutput output)
throws IOException {
Shipment shipment = (Shipment) object;
output.writeString(shipment.getPartNumber());
output.writeString(shipment.getSupplierNumber());
}
public Object objectToValue(Object object)
throws IOException {
Shipment shipment = (Shipment) object;
return new ShipmentValue(shipment.getQuantity());
}
}
}