00001
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
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
00031 private StoredClassCatalog classCatalog;
00032
00033
00034 public MyDbs() {}
00035
00036
00037
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
00054
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
00067
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
00078 classCatalog = new StoredClassCatalog(classCatalogDb);
00079
00080
00081
00082
00083 TupleBinding inventoryBinding = new InventoryBinding();
00084
00085
00086
00087
00088
00089
00090
00091 ItemNameKeyCreator keyCreator =
00092 new ItemNameKeyCreator(new InventoryBinding());
00093
00094
00095
00096
00097 mySecConfig.setSortedDuplicates(true);
00098 mySecConfig.setAllowPopulate(true);
00099 mySecConfig.setKeyCreator(keyCreator);
00100
00101
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
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
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