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:34 bostic Exp $
00008  */
00009 
00010 package collections.ship.index;
00011 
00012 import java.io.File;
00013 import java.io.FileNotFoundException;
00014 
00015 import com.sleepycat.bind.serial.ClassCatalog;
00016 import com.sleepycat.bind.serial.SerialSerialKeyCreator;
00017 import com.sleepycat.bind.serial.StoredClassCatalog;
00018 import com.sleepycat.db.Database;
00019 import com.sleepycat.db.DatabaseConfig;
00020 import com.sleepycat.db.DatabaseException;
00021 import com.sleepycat.db.DatabaseType;
00022 import com.sleepycat.db.Environment;
00023 import com.sleepycat.db.EnvironmentConfig;
00024 import com.sleepycat.db.SecondaryConfig;
00025 import com.sleepycat.db.SecondaryDatabase;
00026 
00033 public class SampleDatabase {
00034 
00035     private static final String CLASS_CATALOG = "java_class_catalog";
00036     private static final String SUPPLIER_STORE = "supplier_store";
00037     private static final String PART_STORE = "part_store";
00038     private static final String SHIPMENT_STORE = "shipment_store";
00039     private static final String SHIPMENT_PART_INDEX = "shipment_part_index";
00040     private static final String SHIPMENT_SUPPLIER_INDEX =
00041         "shipment_supplier_index";
00042     private static final String SUPPLIER_CITY_INDEX = "supplier_city_index";
00043 
00044     private Environment env;
00045     private Database partDb;
00046     private Database supplierDb;
00047     private Database shipmentDb;
00048     private SecondaryDatabase supplierByCityDb;
00049     private SecondaryDatabase shipmentByPartDb;
00050     private SecondaryDatabase shipmentBySupplierDb;
00051     private StoredClassCatalog javaCatalog;
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         // Open the Berkeley DB database for the part, supplier and shipment
00084         // stores.  The stores are opened with no duplicate keys allowed.
00085         //
00086         partDb = env.openDatabase(null, PART_STORE, null, dbConfig);
00087 
00088         supplierDb = env.openDatabase(null, SUPPLIER_STORE, null, dbConfig);
00089 
00090         shipmentDb = env.openDatabase(null, SHIPMENT_STORE, null, dbConfig);
00091 
00092         // Open the SecondaryDatabase for the city index of the supplier store,
00093         // and for the part and supplier indices of the shipment store.
00094         // Duplicate keys are allowed since more than one supplier may be in
00095         // the same city, and more than one shipment may exist for the same
00096         // supplier or part.  A foreign key constraint is defined for the
00097         // supplier and part indices to ensure that a shipment only refers to
00098         // existing part and supplier keys.  The CASCADE delete action means
00099         // that shipments will be deleted if their associated part or supplier
00100         // is deleted.
00101         //
00102         SecondaryConfig secConfig = new SecondaryConfig();
00103         secConfig.setTransactional(true);
00104         secConfig.setAllowCreate(true);
00105         secConfig.setType(DatabaseType.BTREE);
00106         secConfig.setSortedDuplicates(true);
00107 
00108         secConfig.setKeyCreator(
00109             new SupplierByCityKeyCreator(javaCatalog,
00110                                          SupplierKey.class,
00111                                          SupplierData.class,
00112                                          String.class));
00113         supplierByCityDb = env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX,
00114                                                      null,
00115                                                      supplierDb,
00116                                                      secConfig);
00117 
00118         secConfig.setKeyCreator(
00119             new ShipmentByPartKeyCreator(javaCatalog,
00120                                          ShipmentKey.class,
00121                                          ShipmentData.class,
00122                                          PartKey.class));
00123         shipmentByPartDb = env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX,
00124                                                      null,
00125                                                      shipmentDb,
00126                                                      secConfig);
00127 
00128         secConfig.setKeyCreator(
00129             new ShipmentBySupplierKeyCreator(javaCatalog,
00130                                              ShipmentKey.class,
00131                                              ShipmentData.class,
00132                                              SupplierKey.class));
00133         shipmentBySupplierDb = env.openSecondaryDatabase(null,
00134                                                      SHIPMENT_SUPPLIER_INDEX,
00135                                                      null,
00136                                                      shipmentDb,
00137                                                      secConfig);
00138     }
00139 
00143     public final Environment getEnvironment() {
00144 
00145         return env;
00146     }
00147 
00151     public final StoredClassCatalog getClassCatalog() {
00152 
00153         return javaCatalog;
00154     }
00155 
00159     public final Database getPartDatabase() {
00160 
00161         return partDb;
00162     }
00163 
00167     public final Database getSupplierDatabase() {
00168 
00169         return supplierDb;
00170     }
00171 
00175     public final Database getShipmentDatabase() {
00176 
00177         return shipmentDb;
00178     }
00179 
00183     public final SecondaryDatabase getShipmentByPartDatabase() {
00184 
00185         return shipmentByPartDb;
00186     }
00187 
00191     public final SecondaryDatabase getShipmentBySupplierDatabase() {
00192 
00193         return shipmentBySupplierDb;
00194     }
00195 
00199     public final SecondaryDatabase getSupplierByCityDatabase() {
00200 
00201         return supplierByCityDb;
00202     }
00203 
00207     public void close()
00208         throws DatabaseException {
00209 
00210         // Close secondary databases, then primary databases.
00211         supplierByCityDb.close();
00212         shipmentByPartDb.close();
00213         shipmentBySupplierDb.close();
00214         partDb.close();
00215         supplierDb.close();
00216         shipmentDb.close();
00217         // And don't forget to close the catalog and the environment.
00218         javaCatalog.close();
00219         env.close();
00220     }
00221 
00228     private static class SupplierByCityKeyCreator
00229         extends SerialSerialKeyCreator {
00230 
00238         private SupplierByCityKeyCreator(ClassCatalog catalog,
00239                                          Class primaryKeyClass,
00240                                          Class valueClass,
00241                                          Class indexKeyClass) {
00242 
00243             super(catalog, primaryKeyClass, valueClass, indexKeyClass);
00244         }
00245 
00250         public Object createSecondaryKey(Object primaryKeyInput,
00251                                          Object valueInput) {
00252 
00253             SupplierData supplierData = (SupplierData) valueInput;
00254             return supplierData.getCity();
00255         }
00256     }
00257 
00264     private static class ShipmentByPartKeyCreator
00265         extends SerialSerialKeyCreator {
00266 
00274         private ShipmentByPartKeyCreator(ClassCatalog catalog,
00275                                          Class primaryKeyClass,
00276                                          Class valueClass,
00277                                          Class indexKeyClass) {
00278 
00279             super(catalog, primaryKeyClass, valueClass, indexKeyClass);
00280         }
00281 
00286         public Object createSecondaryKey(Object primaryKeyInput,
00287                                          Object valueInput) {
00288 
00289             ShipmentKey shipmentKey = (ShipmentKey) primaryKeyInput;
00290             return new PartKey(shipmentKey.getPartNumber());
00291         }
00292     }
00293 
00300     private static class ShipmentBySupplierKeyCreator
00301         extends SerialSerialKeyCreator {
00302 
00310         private ShipmentBySupplierKeyCreator(ClassCatalog catalog,
00311                                              Class primaryKeyClass,
00312                                              Class valueClass,
00313                                              Class indexKeyClass) {
00314 
00315             super(catalog, primaryKeyClass, valueClass, indexKeyClass);
00316         }
00317 
00323         public Object createSecondaryKey(Object primaryKeyInput,
00324                                          Object valueInput) {
00325 
00326             ShipmentKey shipmentKey = (ShipmentKey) primaryKeyInput;
00327             return new SupplierKey(shipmentKey.getSupplierNumber());
00328         }
00329     }
00330 }

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