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

MyDbs.java

00001 // File: MyDbs.java
00002 
00003 package db.GettingStarted;
00004 
00005 import java.io.FileNotFoundException;
00006 
00007 import com.sleepycat.bind.serial.StoredClassCatalog;
00008 import com.sleepycat.bind.tuple.TupleBinding;
00009 import com.sleepycat.db.Database;
00010 import com.sleepycat.db.DatabaseConfig;
00011 import com.sleepycat.db.DatabaseException;
00012 import com.sleepycat.db.DatabaseType;
00013 import com.sleepycat.db.SecondaryConfig;
00014 import com.sleepycat.db.SecondaryDatabase;
00015 
00016 
00017 public class MyDbs {
00018 
00019     // The databases that our application uses
00020     private Database vendorDb = null;
00021     private Database inventoryDb = null;
00022     private Database classCatalogDb = null;
00023     private SecondaryDatabase itemNameIndexDb = null;
00024 
00025     private String vendordb = "VendorDB.db";
00026     private String inventorydb = "InventoryDB.db";
00027     private String classcatalogdb = "ClassCatalogDB.db";
00028     private String itemnameindexdb = "ItemNameIndexDB.db";
00029 
00030     // Needed for object serialization
00031     private StoredClassCatalog classCatalog;
00032 
00033     // Our constructor does nothing
00034     public MyDbs() {}
00035 
00036     // The setup() method opens all our databases
00037     // for us.
00038     public void setup(String databasesHome)
00039         throws DatabaseException {
00040 
00041         DatabaseConfig myDbConfig = new DatabaseConfig();
00042         SecondaryConfig mySecConfig = new SecondaryConfig();
00043 
00044         myDbConfig.setErrorStream(System.err);
00045         mySecConfig.setErrorStream(System.err);
00046         myDbConfig.setErrorPrefix("MyDbs");
00047         mySecConfig.setErrorPrefix("MyDbs");
00048         myDbConfig.setType(DatabaseType.BTREE);
00049         mySecConfig.setType(DatabaseType.BTREE);
00050         myDbConfig.setAllowCreate(true);
00051         mySecConfig.setAllowCreate(true);
00052 
00053         // Now open, or create and open, our databases
00054         // Open the vendors and inventory databases
00055         try {
00056             vendordb = databasesHome + "/" + vendordb;
00057             vendorDb = new Database(vendordb,
00058                                     null,
00059                                     myDbConfig);
00060 
00061             inventorydb = databasesHome + "/" + inventorydb;
00062             inventoryDb = new Database(inventorydb,
00063                                         null,
00064                                         myDbConfig);
00065 
00066             // Open the class catalog db. This is used to
00067             // optimize class serialization.
00068             classcatalogdb = databasesHome + "/" + classcatalogdb;
00069             classCatalogDb = new Database(classcatalogdb,
00070                                           null,
00071                                           myDbConfig);
00072         } catch(FileNotFoundException fnfe) {
00073             System.err.println("MyDbs: " + fnfe.toString());
00074             System.exit(-1);
00075         }
00076 
00077         // Create our class catalog
00078         classCatalog = new StoredClassCatalog(classCatalogDb);
00079 
00080         // Need a tuple binding for the Inventory class.
00081         // We use the InventoryBinding class
00082         // that we implemented for this purpose.
00083         TupleBinding inventoryBinding = new InventoryBinding();
00084 
00085         // Open the secondary database. We use this to create a
00086         // secondary index for the inventory database
00087 
00088         // We want to maintain an index for the inventory entries based
00089         // on the item name. So, instantiate the appropriate key creator
00090         // and open a secondary database.
00091         ItemNameKeyCreator keyCreator =
00092             new ItemNameKeyCreator(new InventoryBinding());
00093 
00094 
00095         // Set up additional secondary properties
00096         // Need to allow duplicates for our secondary database
00097         mySecConfig.setSortedDuplicates(true);
00098         mySecConfig.setAllowPopulate(true); // Allow autopopulate
00099         mySecConfig.setKeyCreator(keyCreator);
00100 
00101         // Now open it
00102         try {
00103             itemnameindexdb = databasesHome + "/" + itemnameindexdb;
00104             itemNameIndexDb = new SecondaryDatabase(itemnameindexdb,
00105                                                     null,
00106                                                     inventoryDb,
00107                                                     mySecConfig);
00108         } catch(FileNotFoundException fnfe) {
00109             System.err.println("MyDbs: " + fnfe.toString());
00110             System.exit(-1);
00111         }
00112     }
00113 
00114    // getter methods
00115     public Database getVendorDB() {
00116         return vendorDb;
00117     }
00118 
00119     public Database getInventoryDB() {
00120         return inventoryDb;
00121     }
00122 
00123     public SecondaryDatabase getNameIndexDB() {
00124         return itemNameIndexDb;
00125     }
00126 
00127     public StoredClassCatalog getClassCatalog() {
00128         return classCatalog;
00129     }
00130 
00131     // Close the databases
00132     public void close() {
00133         try {
00134             if (itemNameIndexDb != null) {
00135                 itemNameIndexDb.close();
00136             }
00137 
00138             if (vendorDb != null) {
00139                 vendorDb.close();
00140             }
00141 
00142             if (inventoryDb != null) {
00143                 inventoryDb.close();
00144             }
00145 
00146             if (classCatalogDb != null) {
00147                 classCatalogDb.close();
00148             }
00149 
00150         } catch(DatabaseException dbe) {
00151             System.err.println("Error closing MyDbs: " +
00152                                 dbe.toString());
00153             System.exit(-1);
00154         }
00155     }
00156 }
00157 

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