Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

SampleDatabase.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2002-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: SampleDatabase.java,v 12.2 2005/06/16 20:22:32 bostic Exp $
00008  */
00009 
00010 package collections.ship.factory;
00011 
00012 import java.io.File;
00013 import java.io.FileNotFoundException;
00014 
00015 import com.sleepycat.bind.serial.StoredClassCatalog;
00016 import com.sleepycat.collections.TupleSerialFactory;
00017 import com.sleepycat.db.Database;
00018 import com.sleepycat.db.DatabaseConfig;
00019 import com.sleepycat.db.DatabaseException;
00020 import com.sleepycat.db.DatabaseType;
00021 import com.sleepycat.db.Environment;
00022 import com.sleepycat.db.EnvironmentConfig;
00023 import com.sleepycat.db.SecondaryConfig;
00024 import com.sleepycat.db.SecondaryDatabase;
00025 
00032 public class SampleDatabase {
00033 
00034     private static final String CLASS_CATALOG = "java_class_catalog";
00035     private static final String SUPPLIER_STORE = "supplier_store";
00036     private static final String PART_STORE = "part_store";
00037     private static final String SHIPMENT_STORE = "shipment_store";
00038     private static final String SHIPMENT_PART_INDEX = "shipment_part_index";
00039     private static final String SHIPMENT_SUPPLIER_INDEX =
00040                                     "shipment_supplier_index";
00041     private static final String SUPPLIER_CITY_INDEX = "supplier_city_index";
00042 
00043     private Environment env;
00044     private Database partDb;
00045     private Database supplierDb;
00046     private Database shipmentDb;
00047     private SecondaryDatabase supplierByCityDb;
00048     private SecondaryDatabase shipmentByPartDb;
00049     private SecondaryDatabase shipmentBySupplierDb;
00050     private StoredClassCatalog javaCatalog;
00051     private TupleSerialFactory factory;
00052 
00056     public SampleDatabase(String homeDirectory)
00057         throws DatabaseException, FileNotFoundException {
00058 
00059         // Open the Berkeley DB environment in transactional mode.
00060         //
00061         System.out.println("Opening environment in: " + homeDirectory);
00062         EnvironmentConfig envConfig = new EnvironmentConfig();
00063         envConfig.setTransactional(true);
00064         envConfig.setAllowCreate(true);
00065         envConfig.setInitializeCache(true);
00066         envConfig.setInitializeLocking(true);
00067         env = new Environment(new File(homeDirectory), envConfig);
00068 
00069         // Set the Berkeley DB config for opening all stores.
00070         //
00071         DatabaseConfig dbConfig = new DatabaseConfig();
00072         dbConfig.setTransactional(true);
00073         dbConfig.setAllowCreate(true);
00074         dbConfig.setType(DatabaseType.BTREE);
00075 
00076         // Create the Serial class catalog.  This holds the serialized class
00077         // format for all database records of serial format.
00078         //
00079         Database catalogDb = env.openDatabase(null, CLASS_CATALOG, null,
00080                                               dbConfig);
00081         javaCatalog = new StoredClassCatalog(catalogDb);
00082 
00083         // Use the TupleSerialDbFactory for a Serial/Tuple-based database
00084         // where marshalling interfaces are used.
00085         //
00086         factory = new TupleSerialFactory(javaCatalog);
00087 
00088         // Open the Berkeley DB database for the part, supplier and shipment
00089         // stores.  The stores are opened with no duplicate keys allowed.
00090         //
00091         partDb = env.openDatabase(null, PART_STORE, null, dbConfig);
00092 
00093         supplierDb = env.openDatabase(null, SUPPLIER_STORE, null, dbConfig);
00094 
00095         shipmentDb = env.openDatabase(null, SHIPMENT_STORE, null, dbConfig);
00096 
00097         // Open the SecondaryDatabase for the city index of the supplier store,
00098         // and for the part and supplier indices of the shipment store.
00099         // Duplicate keys are allowed since more than one supplier may be in
00100         // the same city, and more than one shipment may exist for the same
00101         // supplier or part.  A foreign key constraint is defined for the
00102         // supplier and part indices to ensure that a shipment only refers to
00103         // existing part and supplier keys.  The CASCADE delete action means
00104         // that shipments will be deleted if their associated part or supplier
00105         // is deleted.
00106         //
00107         SecondaryConfig secConfig = new SecondaryConfig();
00108         secConfig.setTransactional(true);
00109         secConfig.setAllowCreate(true);
00110         secConfig.setType(DatabaseType.BTREE);
00111         secConfig.setSortedDuplicates(true);
00112 
00113         secConfig.setKeyCreator(factory.getKeyCreator(Supplier.class,
00114                                                       Supplier.CITY_KEY));
00115         supplierByCityDb = env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX,
00116                                                      null,
00117                                                      supplierDb,
00118                                                      secConfig);
00119 
00120         secConfig.setKeyCreator(factory.getKeyCreator(Shipment.class,
00121                                                       Shipment.PART_KEY));
00122         shipmentByPartDb = env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX,
00123                                                      null,
00124                                                      shipmentDb,
00125                                                      secConfig);
00126 
00127         secConfig.setKeyCreator(factory.getKeyCreator(Shipment.class,
00128                                                       Shipment.SUPPLIER_KEY));
00129         shipmentBySupplierDb = env.openSecondaryDatabase(null,
00130                                                      SHIPMENT_SUPPLIER_INDEX,
00131                                                      null,
00132                                                      shipmentDb,
00133                                                      secConfig);
00134     }
00135 
00139     public final TupleSerialFactory getFactory() {
00140 
00141         return factory;
00142     }
00143 
00147     public final Environment getEnvironment() {
00148 
00149         return env;
00150     }
00151 
00155     public final StoredClassCatalog getClassCatalog() {
00156 
00157         return javaCatalog;
00158     }
00159 
00163     public final Database getPartDatabase() {
00164 
00165         return partDb;
00166     }
00167 
00171     public final Database getSupplierDatabase() {
00172 
00173         return supplierDb;
00174     }
00175 
00179     public final Database getShipmentDatabase() {
00180 
00181         return shipmentDb;
00182     }
00183 
00187     public final SecondaryDatabase getShipmentByPartDatabase() {
00188 
00189         return shipmentByPartDb;
00190     }
00191 
00195     public final SecondaryDatabase getShipmentBySupplierDatabase() {
00196 
00197         return shipmentBySupplierDb;
00198     }
00199 
00203     public final SecondaryDatabase getSupplierByCityDatabase() {
00204 
00205         return supplierByCityDb;
00206     }
00207 
00211     public void close()
00212         throws DatabaseException {
00213 
00214         // Close secondary databases, then primary databases.
00215         supplierByCityDb.close();
00216         shipmentByPartDb.close();
00217         shipmentBySupplierDb.close();
00218         partDb.close();
00219         supplierDb.close();
00220         shipmentDb.close();
00221         // And don't forget to close the catalog and the environment.
00222         javaCatalog.close();
00223         env.close();
00224     }
00225 }

Generated on Sun Dec 25 12:14:26 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2