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:38 bostic Exp $
00008  */
00009 
00010 package collections.ship.marshal;
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.StoredClassCatalog;
00017 import com.sleepycat.bind.serial.TupleSerialKeyCreator;
00018 import com.sleepycat.bind.tuple.TupleInput;
00019 import com.sleepycat.bind.tuple.TupleOutput;
00020 import com.sleepycat.db.Database;
00021 import com.sleepycat.db.DatabaseConfig;
00022 import com.sleepycat.db.DatabaseException;
00023 import com.sleepycat.db.DatabaseType;
00024 import com.sleepycat.db.Environment;
00025 import com.sleepycat.db.EnvironmentConfig;
00026 import com.sleepycat.db.SecondaryConfig;
00027 import com.sleepycat.db.SecondaryDatabase;
00028 
00035 public class SampleDatabase {
00036 
00037     private static final String CLASS_CATALOG = "java_class_catalog";
00038     private static final String SUPPLIER_STORE = "supplier_store";
00039     private static final String PART_STORE = "part_store";
00040     private static final String SHIPMENT_STORE = "shipment_store";
00041     private static final String SHIPMENT_PART_INDEX = "shipment_part_index";
00042     private static final String SHIPMENT_SUPPLIER_INDEX =
00043         "shipment_supplier_index";
00044     private static final String SUPPLIER_CITY_INDEX = "supplier_city_index";
00045 
00046     private Environment env;
00047     private Database partDb;
00048     private Database supplierDb;
00049     private Database shipmentDb;
00050     private SecondaryDatabase supplierByCityDb;
00051     private SecondaryDatabase shipmentByPartDb;
00052     private SecondaryDatabase shipmentBySupplierDb;
00053     private StoredClassCatalog javaCatalog;
00054 
00058     public SampleDatabase(String homeDirectory)
00059         throws DatabaseException, FileNotFoundException {
00060 
00061         // Open the Berkeley DB environment in transactional mode.
00062         //
00063         System.out.println("Opening environment in: " + homeDirectory);
00064         EnvironmentConfig envConfig = new EnvironmentConfig();
00065         envConfig.setTransactional(true);
00066         envConfig.setAllowCreate(true);
00067         envConfig.setInitializeCache(true);
00068         envConfig.setInitializeLocking(true);
00069         env = new Environment(new File(homeDirectory), envConfig);
00070 
00071         // Set the Berkeley DB config for opening all stores.
00072         //
00073         DatabaseConfig dbConfig = new DatabaseConfig();
00074         dbConfig.setTransactional(true);
00075         dbConfig.setAllowCreate(true);
00076         dbConfig.setType(DatabaseType.BTREE);
00077 
00078         // Create the Serial class catalog.  This holds the serialized class
00079         // format for all database records of serial format.
00080         //
00081         Database catalogDb = env.openDatabase(null, CLASS_CATALOG, null,
00082                                               dbConfig);
00083         javaCatalog = new StoredClassCatalog(catalogDb);
00084 
00085         // Open the Berkeley DB database for the part, supplier and shipment
00086         // stores.  The stores are opened with no duplicate keys allowed.
00087         //
00088         partDb = env.openDatabase(null, PART_STORE, null, dbConfig);
00089 
00090         supplierDb = env.openDatabase(null, SUPPLIER_STORE, null, dbConfig);
00091 
00092         shipmentDb = env.openDatabase(null, SHIPMENT_STORE, null, dbConfig);
00093 
00094         // Open the SecondaryDatabase for the city index of the supplier store,
00095         // and for the part and supplier indices of the shipment store.
00096         // Duplicate keys are allowed since more than one supplier may be in
00097         // the same city, and more than one shipment may exist for the same
00098         // supplier or part.  A foreign key constraint is defined for the
00099         // supplier and part indices to ensure that a shipment only refers to
00100         // existing part and supplier keys.  The CASCADE delete action means
00101         // that shipments will be deleted if their associated part or supplier
00102         // is deleted.
00103         //
00104         SecondaryConfig secConfig = new SecondaryConfig();
00105         secConfig.setTransactional(true);
00106         secConfig.setAllowCreate(true);
00107         secConfig.setType(DatabaseType.BTREE);
00108         secConfig.setSortedDuplicates(true);
00109 
00110         secConfig.setKeyCreator(new MarshalledKeyCreator(javaCatalog,
00111                                                          Supplier.class,
00112                                                          Supplier.CITY_KEY));
00113         supplierByCityDb = env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX,
00114                                                      null,
00115                                                      supplierDb,
00116                                                      secConfig);
00117 
00118         secConfig.setKeyCreator(new MarshalledKeyCreator(javaCatalog,
00119                                                          Shipment.class,
00120                                                          Shipment.PART_KEY));
00121         shipmentByPartDb = env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX,
00122                                                      null,
00123                                                      shipmentDb,
00124                                                      secConfig);
00125 
00126         secConfig.setKeyCreator(new MarshalledKeyCreator(javaCatalog,
00127                                                          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 Environment getEnvironment() {
00140 
00141         return env;
00142     }
00143 
00147     public final StoredClassCatalog getClassCatalog() {
00148 
00149         return javaCatalog;
00150     }
00151 
00155     public final Database getPartDatabase() {
00156 
00157         return partDb;
00158     }
00159 
00163     public final Database getSupplierDatabase() {
00164 
00165         return supplierDb;
00166     }
00167 
00171     public final Database getShipmentDatabase() {
00172 
00173         return shipmentDb;
00174     }
00175 
00179     public final SecondaryDatabase getShipmentByPartDatabase() {
00180 
00181         return shipmentByPartDb;
00182     }
00183 
00187     public final SecondaryDatabase getShipmentBySupplierDatabase() {
00188 
00189         return shipmentBySupplierDb;
00190     }
00191 
00195     public final SecondaryDatabase getSupplierByCityDatabase() {
00196 
00197         return supplierByCityDb;
00198     }
00199 
00203     public void close()
00204         throws DatabaseException {
00205 
00206         // Close secondary databases, then primary databases.
00207         supplierByCityDb.close();
00208         shipmentByPartDb.close();
00209         shipmentBySupplierDb.close();
00210         partDb.close();
00211         supplierDb.close();
00212         shipmentDb.close();
00213         // And don't forget to close the catalog and the environment.
00214         javaCatalog.close();
00215         env.close();
00216     }
00217 
00224     private static class MarshalledKeyCreator
00225         extends TupleSerialKeyCreator {
00226 
00227         private String keyName;
00228 
00235         private MarshalledKeyCreator(ClassCatalog catalog,
00236                                      Class valueClass,
00237                                      String keyName) {
00238 
00239             super(catalog, valueClass);
00240             this.keyName = keyName;
00241         }
00242 
00247         public boolean createSecondaryKey(TupleInput primaryKeyInput,
00248                                           Object valueInput,
00249                                           TupleOutput indexKeyOutput) {
00250 
00251             // the primary key is unmarshalled before marshalling the index
00252             // key, to account for cases where the index key is composed of
00253             // data elements from the primary key
00254             MarshalledEnt entity = (MarshalledEnt) valueInput;
00255             entity.unmarshalPrimaryKey(primaryKeyInput);
00256             return entity.marshalSecondaryKey(keyName, indexKeyOutput);
00257         }
00258     }
00259 }

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