1// File: MyDbs.java 2 3package db.GettingStarted; 4 5import java.io.FileNotFoundException; 6 7import com.sleepycat.bind.serial.StoredClassCatalog; 8import com.sleepycat.bind.tuple.TupleBinding; 9import com.sleepycat.db.Database; 10import com.sleepycat.db.DatabaseConfig; 11import com.sleepycat.db.DatabaseException; 12import com.sleepycat.db.DatabaseType; 13import com.sleepycat.db.SecondaryConfig; 14import com.sleepycat.db.SecondaryDatabase; 15 16 17public class MyDbs { 18 19 // The databases that our application uses 20 private Database vendorDb = null; 21 private Database inventoryDb = null; 22 private Database classCatalogDb = null; 23 private SecondaryDatabase itemNameIndexDb = null; 24 25 private String vendordb = "VendorDB.db"; 26 private String inventorydb = "InventoryDB.db"; 27 private String classcatalogdb = "ClassCatalogDB.db"; 28 private String itemnameindexdb = "ItemNameIndexDB.db"; 29 30 // Needed for object serialization 31 private StoredClassCatalog classCatalog; 32 33 // Our constructor does nothing 34 public MyDbs() {} 35 36 // The setup() method opens all our databases 37 // for us. 38 public void setup(String databasesHome) 39 throws DatabaseException { 40 41 DatabaseConfig myDbConfig = new DatabaseConfig(); 42 SecondaryConfig mySecConfig = new SecondaryConfig(); 43 44 myDbConfig.setErrorStream(System.err); 45 mySecConfig.setErrorStream(System.err); 46 myDbConfig.setErrorPrefix("MyDbs"); 47 mySecConfig.setErrorPrefix("MyDbs"); 48 myDbConfig.setType(DatabaseType.BTREE); 49 mySecConfig.setType(DatabaseType.BTREE); 50 myDbConfig.setAllowCreate(true); 51 mySecConfig.setAllowCreate(true); 52 53 // Now open, or create and open, our databases 54 // Open the vendors and inventory databases 55 try { 56 vendordb = databasesHome + "/" + vendordb; 57 vendorDb = new Database(vendordb, 58 null, 59 myDbConfig); 60 61 inventorydb = databasesHome + "/" + inventorydb; 62 inventoryDb = new Database(inventorydb, 63 null, 64 myDbConfig); 65 66 // Open the class catalog db. This is used to 67 // optimize class serialization. 68 classcatalogdb = databasesHome + "/" + classcatalogdb; 69 classCatalogDb = new Database(classcatalogdb, 70 null, 71 myDbConfig); 72 } catch(FileNotFoundException fnfe) { 73 System.err.println("MyDbs: " + fnfe.toString()); 74 System.exit(-1); 75 } 76 77 // Create our class catalog 78 classCatalog = new StoredClassCatalog(classCatalogDb); 79 80 // Need a tuple binding for the Inventory class. 81 // We use the InventoryBinding class 82 // that we implemented for this purpose. 83 TupleBinding inventoryBinding = new InventoryBinding(); 84 85 // Open the secondary database. We use this to create a 86 // secondary index for the inventory database 87 88 // We want to maintain an index for the inventory entries based 89 // on the item name. So, instantiate the appropriate key creator 90 // and open a secondary database. 91 ItemNameKeyCreator keyCreator = 92 new ItemNameKeyCreator(new InventoryBinding()); 93 94 95 // Set up additional secondary properties 96 // Need to allow duplicates for our secondary database 97 mySecConfig.setSortedDuplicates(true); 98 mySecConfig.setAllowPopulate(true); // Allow autopopulate 99 mySecConfig.setKeyCreator(keyCreator); 100 101 // Now open it 102 try { 103 itemnameindexdb = databasesHome + "/" + itemnameindexdb; 104 itemNameIndexDb = new SecondaryDatabase(itemnameindexdb, 105 null, 106 inventoryDb, 107 mySecConfig); 108 } catch(FileNotFoundException fnfe) { 109 System.err.println("MyDbs: " + fnfe.toString()); 110 System.exit(-1); 111 } 112 } 113 114 // getter methods 115 public Database getVendorDB() { 116 return vendorDb; 117 } 118 119 public Database getInventoryDB() { 120 return inventoryDb; 121 } 122 123 public SecondaryDatabase getNameIndexDB() { 124 return itemNameIndexDb; 125 } 126 127 public StoredClassCatalog getClassCatalog() { 128 return classCatalog; 129 } 130 131 // Close the databases 132 public void close() { 133 try { 134 if (itemNameIndexDb != null) { 135 itemNameIndexDb.close(); 136 } 137 138 if (vendorDb != null) { 139 vendorDb.close(); 140 } 141 142 if (inventoryDb != null) { 143 inventoryDb.close(); 144 } 145 146 if (classCatalogDb != null) { 147 classCatalogDb.close(); 148 } 149 150 } catch(DatabaseException dbe) { 151 System.err.println("Error closing MyDbs: " + 152 dbe.toString()); 153 System.exit(-1); 154 } 155 } 156} 157 158