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:41 bostic Exp $
00008  */
00009 
00010 package collections.ship.sentity;
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 SupplierByCityKeyCreator(javaCatalog,
00111                                                              Supplier.class));
00112         supplierByCityDb = env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX,
00113                                                      null,
00114                                                      supplierDb,
00115                                                      secConfig);
00116 
00117         secConfig.setKeyCreator(new ShipmentByPartKeyCreator(javaCatalog,
00118                                                              Shipment.class));
00119         shipmentByPartDb = env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX,
00120                                                      null,
00121                                                      shipmentDb,
00122                                                      secConfig);
00123 
00124         secConfig.setKeyCreator(new ShipmentBySupplierKeyCreator(javaCatalog,
00125                                                               Shipment.class));
00126         shipmentBySupplierDb = env.openSecondaryDatabase(null,
00127                                                      SHIPMENT_SUPPLIER_INDEX,
00128                                                      null,
00129                                                      shipmentDb,
00130                                                      secConfig);
00131     }
00132 
00136     public final Environment getEnvironment() {
00137 
00138         return env;
00139     }
00140 
00144     public final StoredClassCatalog getClassCatalog() {
00145 
00146         return javaCatalog;
00147     }
00148 
00152     public final Database getPartDatabase() {
00153 
00154         return partDb;
00155     }
00156 
00160     public final Database getSupplierDatabase() {
00161 
00162         return supplierDb;
00163     }
00164 
00168     public final Database getShipmentDatabase() {
00169 
00170         return shipmentDb;
00171     }
00172 
00176     public final SecondaryDatabase getShipmentByPartDatabase() {
00177 
00178         return shipmentByPartDb;
00179     }
00180 
00184     public final SecondaryDatabase getShipmentBySupplierDatabase() {
00185 
00186         return shipmentBySupplierDb;
00187     }
00188 
00192     public final SecondaryDatabase getSupplierByCityDatabase() {
00193 
00194         return supplierByCityDb;
00195     }
00196 
00200     public void close()
00201         throws DatabaseException {
00202 
00203         // Close secondary databases, then primary databases.
00204         supplierByCityDb.close();
00205         shipmentByPartDb.close();
00206         shipmentBySupplierDb.close();
00207         partDb.close();
00208         supplierDb.close();
00209         shipmentDb.close();
00210         // And don't forget to close the catalog and the environment.
00211         javaCatalog.close();
00212         env.close();
00213     }
00214 
00221     private static class SupplierByCityKeyCreator
00222         extends TupleSerialKeyCreator {
00223 
00229         private SupplierByCityKeyCreator(ClassCatalog catalog,
00230                                          Class valueClass) {
00231 
00232             super(catalog, valueClass);
00233         }
00234 
00239         public boolean createSecondaryKey(TupleInput primaryKeyInput,
00240                                           Object valueInput,
00241                                           TupleOutput indexKeyOutput) {
00242 
00243             Supplier supplier = (Supplier) valueInput;
00244             String city = supplier.getCity();
00245             if (city != null) {
00246                 indexKeyOutput.writeString(supplier.getCity());
00247                 return true;
00248             } else {
00249                 return false;
00250             }
00251         }
00252     }
00253 
00260     private static class ShipmentByPartKeyCreator
00261         extends TupleSerialKeyCreator {
00262 
00268         private ShipmentByPartKeyCreator(ClassCatalog catalog,
00269                                          Class valueClass) {
00270             super(catalog, valueClass);
00271         }
00272 
00277         public boolean createSecondaryKey(TupleInput primaryKeyInput,
00278                                           Object valueInput,
00279                                           TupleOutput indexKeyOutput) {
00280 
00281             String partNumber = primaryKeyInput.readString();
00282             // don't bother reading the supplierNumber
00283             indexKeyOutput.writeString(partNumber);
00284             return true;
00285         }
00286     }
00287 
00294     private static class ShipmentBySupplierKeyCreator
00295         extends TupleSerialKeyCreator {
00296 
00302         private ShipmentBySupplierKeyCreator(ClassCatalog catalog,
00303                                              Class valueClass) {
00304             super(catalog, valueClass);
00305         }
00306 
00312         public boolean createSecondaryKey(TupleInput primaryKeyInput,
00313                                           Object valueInput,
00314                                           TupleOutput indexKeyOutput) {
00315 
00316             primaryKeyInput.readString(); // skip the partNumber
00317             String supplierNumber = primaryKeyInput.readString();
00318             indexKeyOutput.writeString(supplierNumber);
00319             return true;
00320         }
00321     }
00322 }

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