// method and no duplicate keys allowed. The DB_BTREE method is used
// to provide ordered keys, since ordering is meaningful when the tuple
// data format is used. Duplicate keys are not allowed for any entity
// with indexes or foreign key relationships.
//
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);
// Create the KeyExtractor objects for the part and supplier
// indices of the shipment store. Each key extractor object defines
// its associated index, since it is responsible for mapping between
// the indexed value and the index key.
//
KeyExtractor cityExtractor = new SupplierByCityExtractor(
supplierKeyFormat,
supplierValueFormat,
cityKeyFormat);
KeyExtractor partExtractor = new ShipmentByPartExtractor(
shipmentKeyFormat,
shipmentValueFormat,
partKeyFormat);
KeyExtractor supplierExtractor = new ShipmentBySupplierExtractor(
shipmentKeyFormat,
shipmentValueFormat,
supplierKeyFormat);
// Open the Berkeley DB database, along with the associated
// ForeignKeyIndex, for the part and supplier indices of the shipment
// store.
// In this sample, the indices are opened with the DB_BTREE access
// method and sorted duplicate keys. The DB_BTREE method is used to
// provide ordered keys, since ordering is meaningful when the tuple
// data format is used. Duplicate keys are allowed since more than one
// shipment may exist for the same supplier or part. For indices, if
// duplicates are allowed they should always be sorted to allow for
// efficient joins.
//
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);