001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: SampleDatabase.java,v 1.25.2.2 2008/01/07 15:14:01 cwl Exp $
007: */
008:
009: package collections.ship.factory;
010:
011: import java.io.File;
012: import java.io.FileNotFoundException;
013:
014: import com.sleepycat.bind.serial.StoredClassCatalog;
015: import com.sleepycat.collections.TupleSerialFactory;
016: import com.sleepycat.je.Database;
017: import com.sleepycat.je.DatabaseConfig;
018: import com.sleepycat.je.DatabaseException;
019: import com.sleepycat.je.Environment;
020: import com.sleepycat.je.EnvironmentConfig;
021: import com.sleepycat.je.ForeignKeyDeleteAction;
022: import com.sleepycat.je.SecondaryConfig;
023: import com.sleepycat.je.SecondaryDatabase;
024:
025: /**
026: * SampleDatabase defines the storage containers, indices and foreign keys
027: * for the sample database.
028: *
029: * @author Mark Hayes
030: */
031: public class SampleDatabase {
032:
033: private static final String CLASS_CATALOG = "java_class_catalog";
034: private static final String SUPPLIER_STORE = "supplier_store";
035: private static final String PART_STORE = "part_store";
036: private static final String SHIPMENT_STORE = "shipment_store";
037: private static final String SHIPMENT_PART_INDEX = "shipment_part_index";
038: private static final String SHIPMENT_SUPPLIER_INDEX = "shipment_supplier_index";
039: private static final String SUPPLIER_CITY_INDEX = "supplier_city_index";
040:
041: private Environment env;
042: private Database partDb;
043: private Database supplierDb;
044: private Database shipmentDb;
045: private SecondaryDatabase supplierByCityDb;
046: private SecondaryDatabase shipmentByPartDb;
047: private SecondaryDatabase shipmentBySupplierDb;
048: private StoredClassCatalog javaCatalog;
049: private TupleSerialFactory factory;
050:
051: /**
052: * Open all storage containers, indices, and catalogs.
053: */
054: public SampleDatabase(String homeDirectory)
055: throws DatabaseException, FileNotFoundException {
056:
057: // Open the Berkeley DB environment in transactional mode.
058: //
059: System.out.println("Opening environment in: " + homeDirectory);
060: EnvironmentConfig envConfig = new EnvironmentConfig();
061: envConfig.setTransactional(true);
062: envConfig.setAllowCreate(true);
063: env = new Environment(new File(homeDirectory), envConfig);
064:
065: // Set the Berkeley DB config for opening all stores.
066: //
067: DatabaseConfig dbConfig = new DatabaseConfig();
068: dbConfig.setTransactional(true);
069: dbConfig.setAllowCreate(true);
070:
071: // Create the Serial class catalog. This holds the serialized class
072: // format for all database records of serial format.
073: //
074: Database catalogDb = env.openDatabase(null, CLASS_CATALOG,
075: dbConfig);
076: javaCatalog = new StoredClassCatalog(catalogDb);
077:
078: // Use the TupleSerialDbFactory for a Serial/Tuple-based database
079: // where marshalling interfaces are used.
080: //
081: factory = new TupleSerialFactory(javaCatalog);
082:
083: // Open the Berkeley DB database for the part, supplier and shipment
084: // stores. The stores are opened with no duplicate keys allowed.
085: //
086: partDb = env.openDatabase(null, PART_STORE, dbConfig);
087:
088: supplierDb = env.openDatabase(null, SUPPLIER_STORE, dbConfig);
089:
090: shipmentDb = env.openDatabase(null, SHIPMENT_STORE, dbConfig);
091:
092: // Open the SecondaryDatabase for the city index of the supplier store,
093: // and for the part and supplier indices of the shipment store.
094: // Duplicate keys are allowed since more than one supplier may be in
095: // the same city, and more than one shipment may exist for the same
096: // supplier or part. A foreign key constraint is defined for the
097: // supplier and part indices to ensure that a shipment only refers to
098: // existing part and supplier keys. The CASCADE delete action means
099: // that shipments will be deleted if their associated part or supplier
100: // is deleted.
101: //
102: SecondaryConfig secConfig = new SecondaryConfig();
103: secConfig.setTransactional(true);
104: secConfig.setAllowCreate(true);
105: secConfig.setSortedDuplicates(true);
106:
107: secConfig.setKeyCreator(factory.getKeyCreator(Supplier.class,
108: Supplier.CITY_KEY));
109: supplierByCityDb = env.openSecondaryDatabase(null,
110: SUPPLIER_CITY_INDEX, supplierDb, secConfig);
111:
112: secConfig.setForeignKeyDatabase(partDb);
113: secConfig
114: .setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE);
115: secConfig.setKeyCreator(factory.getKeyCreator(Shipment.class,
116: Shipment.PART_KEY));
117: shipmentByPartDb = env.openSecondaryDatabase(null,
118: SHIPMENT_PART_INDEX, shipmentDb, secConfig);
119:
120: secConfig.setForeignKeyDatabase(supplierDb);
121: secConfig
122: .setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE);
123: secConfig.setKeyCreator(factory.getKeyCreator(Shipment.class,
124: Shipment.SUPPLIER_KEY));
125: shipmentBySupplierDb = env.openSecondaryDatabase(null,
126: SHIPMENT_SUPPLIER_INDEX, shipmentDb, secConfig);
127: }
128:
129: /**
130: * Return the tuple-serial factory.
131: */
132: public final TupleSerialFactory getFactory() {
133:
134: return factory;
135: }
136:
137: /**
138: * Return the storage environment for the database.
139: */
140: public final Environment getEnvironment() {
141:
142: return env;
143: }
144:
145: /**
146: * Return the class catalog.
147: */
148: public final StoredClassCatalog getClassCatalog() {
149:
150: return javaCatalog;
151: }
152:
153: /**
154: * Return the part storage container.
155: */
156: public final Database getPartDatabase() {
157:
158: return partDb;
159: }
160:
161: /**
162: * Return the supplier storage container.
163: */
164: public final Database getSupplierDatabase() {
165:
166: return supplierDb;
167: }
168:
169: /**
170: * Return the shipment storage container.
171: */
172: public final Database getShipmentDatabase() {
173:
174: return shipmentDb;
175: }
176:
177: /**
178: * Return the shipment-by-part index.
179: */
180: public final SecondaryDatabase getShipmentByPartDatabase() {
181:
182: return shipmentByPartDb;
183: }
184:
185: /**
186: * Return the shipment-by-supplier index.
187: */
188: public final SecondaryDatabase getShipmentBySupplierDatabase() {
189:
190: return shipmentBySupplierDb;
191: }
192:
193: /**
194: * Return the supplier-by-city index.
195: */
196: public final SecondaryDatabase getSupplierByCityDatabase() {
197:
198: return supplierByCityDb;
199: }
200:
201: /**
202: * Close all databases and the environment.
203: */
204: public void close() throws DatabaseException {
205:
206: // Close secondary databases, then primary databases.
207: supplierByCityDb.close();
208: shipmentByPartDb.close();
209: shipmentBySupplierDb.close();
210: partDb.close();
211: supplierDb.close();
212: shipmentDb.close();
213: // And don't forget to close the catalog and the environment.
214: javaCatalog.close();
215: env.close();
216: }
217: }
|