SampleDatabase.java [plain text]
package com.sleepycat.examples.bdb.shipment.index;
import com.sleepycat.bdb.bind.KeyExtractor;
import com.sleepycat.bdb.bind.serial.SerialFormat;
import com.sleepycat.bdb.bind.serial.SerialSerialKeyExtractor;
import com.sleepycat.bdb.ForeignKeyIndex;
import com.sleepycat.bdb.DataIndex;
import com.sleepycat.bdb.DataStore;
import com.sleepycat.bdb.StoredClassCatalog;
import com.sleepycat.db.Db;
import com.sleepycat.db.DbEnv;
import com.sleepycat.db.DbException;
import java.io.FileNotFoundException;
import java.io.IOException;
public class SampleDatabase {
private static final String CLASS_CATALOG = "java_class_catalog";
private static final String SUPPLIER_STORE = "supplier_store";
private static final String PART_STORE = "part_store";
private static final String SHIPMENT_STORE = "shipment_store";
private static final String SHIPMENT_PART_INDEX = "shipment_part_index";
private static final String SHIPMENT_SUPPLIER_INDEX =
"shipment_supplier_index";
private static final String SUPPLIER_CITY_INDEX = "supplier_city_index";
private DbEnv env;
private DataStore supplierStore;
private DataStore partStore;
private DataStore shipmentStore;
private DataIndex supplierByCityIndex;
private ForeignKeyIndex shipmentByPartIndex;
private ForeignKeyIndex shipmentBySupplierIndex;
private StoredClassCatalog javaCatalog;
private SerialFormat partKeyFormat;
private SerialFormat partValueFormat;
private SerialFormat supplierKeyFormat;
private SerialFormat supplierValueFormat;
private SerialFormat shipmentKeyFormat;
private SerialFormat shipmentValueFormat;
private SerialFormat cityKeyFormat;
public SampleDatabase(String homeDirectory, boolean runRecovery)
throws DbException, FileNotFoundException {
int envFlags = Db.DB_INIT_TXN | Db.DB_INIT_LOCK | Db.DB_INIT_MPOOL |
Db.DB_CREATE;
if (runRecovery) envFlags |= Db.DB_RECOVER;
env = new DbEnv(0);
System.out.println("Opening environment in: " + homeDirectory);
env.open(homeDirectory, envFlags, 0);
int flags = Db.DB_CREATE | Db.DB_AUTO_COMMIT;
javaCatalog = new StoredClassCatalog(env, CLASS_CATALOG, null, flags);
partKeyFormat = new SerialFormat(javaCatalog, PartKey.class);
partValueFormat = new SerialFormat(javaCatalog, PartValue.class);
supplierKeyFormat = new SerialFormat(javaCatalog, SupplierKey.class);
supplierValueFormat =
new SerialFormat(javaCatalog, SupplierValue.class);
shipmentKeyFormat = new SerialFormat(javaCatalog, ShipmentKey.class);
shipmentValueFormat =
new SerialFormat(javaCatalog, ShipmentValue.class);
cityKeyFormat = new SerialFormat(javaCatalog, String.class);
Db partDb = new Db(env, 0);
partDb.open(null, PART_STORE, null, Db.DB_BTREE, flags, 0);
partStore = new DataStore(partDb, partKeyFormat, partValueFormat,
null);
Db supplierDb = new Db(env, 0);
supplierDb.open(null, SUPPLIER_STORE, null, Db.DB_BTREE, flags, 0);
supplierStore = new DataStore(supplierDb, supplierKeyFormat,
supplierValueFormat, null);
Db shipmentDb = new Db(env, 0);
shipmentDb.open(null, SHIPMENT_STORE, null, Db.DB_BTREE, flags, 0);
shipmentStore = new DataStore(shipmentDb, shipmentKeyFormat,
shipmentValueFormat, null);
KeyExtractor cityExtractor = new SupplierByCityExtractor(
supplierKeyFormat,
supplierValueFormat,
cityKeyFormat);
KeyExtractor partExtractor = new ShipmentByPartExtractor(
shipmentKeyFormat,
shipmentValueFormat,
partKeyFormat);
KeyExtractor supplierExtractor = new ShipmentBySupplierExtractor(
shipmentKeyFormat,
shipmentValueFormat,
supplierKeyFormat);
Db cityIndexDb = new Db(env, 0);
cityIndexDb.setFlags(Db.DB_DUPSORT);
cityIndexDb.open(null, SUPPLIER_CITY_INDEX, null,
Db.DB_BTREE, flags, 0);
supplierByCityIndex = new DataIndex(supplierStore, cityIndexDb,
cityKeyFormat, cityExtractor);
Db partIndexDb = new Db(env, 0);
partIndexDb.setFlags(Db.DB_DUPSORT);
partIndexDb.open(null, SHIPMENT_PART_INDEX, null,
Db.DB_BTREE, flags, 0);
shipmentByPartIndex = new ForeignKeyIndex(shipmentStore, partIndexDb,
partExtractor, partStore,
ForeignKeyIndex.ON_DELETE_CASCADE);
Db supplierIndexDb = new Db(env, 0);
supplierIndexDb.setFlags(Db.DB_DUPSORT);
supplierIndexDb.open(null, SHIPMENT_SUPPLIER_INDEX, null,
Db.DB_BTREE, flags, 0);
shipmentBySupplierIndex = new ForeignKeyIndex(shipmentStore,
supplierIndexDb,
supplierExtractor, supplierStore,
ForeignKeyIndex.ON_DELETE_CASCADE);
}
public final DbEnv getEnvironment() {
return env;
}
public final SerialFormat getPartKeyFormat() {
return partKeyFormat;
}
public final SerialFormat getPartValueFormat() {
return partValueFormat;
}
public final SerialFormat getSupplierKeyFormat() {
return supplierKeyFormat;
}
public final SerialFormat getSupplierValueFormat() {
return supplierValueFormat;
}
public final SerialFormat getShipmentKeyFormat() {
return shipmentKeyFormat;
}
public final SerialFormat getShipmentValueFormat() {
return shipmentValueFormat;
}
public final SerialFormat getCityKeyFormat() {
return cityKeyFormat;
}
public final DataStore getPartStore() {
return partStore;
}
public final DataStore getSupplierStore() {
return supplierStore;
}
public final DataStore getShipmentStore() {
return shipmentStore;
}
public final ForeignKeyIndex getShipmentByPartIndex() {
return shipmentByPartIndex;
}
public final ForeignKeyIndex getShipmentBySupplierIndex() {
return shipmentBySupplierIndex;
}
public final DataIndex getSupplierByCityIndex() {
return supplierByCityIndex;
}
public void close()
throws DbException, IOException {
partStore.close();
supplierStore.close();
shipmentStore.close();
javaCatalog.close();
env.close(0);
}
private static class SupplierByCityExtractor
extends SerialSerialKeyExtractor {
private SupplierByCityExtractor(SerialFormat primaryKeyFormat,
SerialFormat valueFormat,
SerialFormat indexKeyFormat) {
super(primaryKeyFormat, valueFormat, indexKeyFormat);
}
public Object extractIndexKey(Object primaryKeyInput,
Object valueInput)
throws IOException {
SupplierValue supplierValue = (SupplierValue) valueInput;
return supplierValue.getCity();
}
public Object clearIndexKey(Object valueInputOutput)
throws IOException {
throw new UnsupportedOperationException();
}
}
private static class ShipmentByPartExtractor
extends SerialSerialKeyExtractor {
private ShipmentByPartExtractor(SerialFormat primaryKeyFormat,
SerialFormat valueFormat,
SerialFormat indexKeyFormat) {
super(primaryKeyFormat, valueFormat, indexKeyFormat);
}
public Object extractIndexKey(Object primaryKeyInput,
Object valueInput)
throws IOException {
ShipmentKey shipmentKey = (ShipmentKey) primaryKeyInput;
return new PartKey(shipmentKey.getPartNumber());
}
public Object clearIndexKey(Object valueInputOutput)
throws IOException {
throw new UnsupportedOperationException();
}
}
private static class ShipmentBySupplierExtractor
extends SerialSerialKeyExtractor {
private ShipmentBySupplierExtractor(SerialFormat primaryKeyFormat,
SerialFormat valueFormat,
SerialFormat indexKeyFormat) {
super(primaryKeyFormat, valueFormat, indexKeyFormat);
}
public Object extractIndexKey(Object primaryKeyInput,
Object valueInput)
throws IOException {
ShipmentKey shipmentKey = (ShipmentKey) primaryKeyInput;
return new SupplierKey(shipmentKey.getSupplierNumber());
}
public Object clearIndexKey(Object valueInputOutput)
throws IOException {
throw new UnsupportedOperationException();
}
}
}