package db.GettingStarted;
import java.io.FileNotFoundException;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.db.Database;
import com.sleepycat.db.DatabaseConfig;
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.DatabaseType;
import com.sleepycat.db.SecondaryConfig;
import com.sleepycat.db.SecondaryDatabase;
public class MyDbs {
private Database vendorDb = null;
private Database inventoryDb = null;
private Database classCatalogDb = null;
private SecondaryDatabase itemNameIndexDb = null;
private String vendordb = "VendorDB.db";
private String inventorydb = "InventoryDB.db";
private String classcatalogdb = "ClassCatalogDB.db";
private String itemnameindexdb = "ItemNameIndexDB.db";
private StoredClassCatalog classCatalog;
public MyDbs() {}
public void setup(String databasesHome)
throws DatabaseException {
DatabaseConfig myDbConfig = new DatabaseConfig();
SecondaryConfig mySecConfig = new SecondaryConfig();
myDbConfig.setErrorStream(System.err);
mySecConfig.setErrorStream(System.err);
myDbConfig.setErrorPrefix("MyDbs");
mySecConfig.setErrorPrefix("MyDbs");
myDbConfig.setType(DatabaseType.BTREE);
mySecConfig.setType(DatabaseType.BTREE);
myDbConfig.setAllowCreate(true);
mySecConfig.setAllowCreate(true);
try {
vendordb = databasesHome + "/" + vendordb;
vendorDb = new Database(vendordb,
null,
myDbConfig);
inventorydb = databasesHome + "/" + inventorydb;
inventoryDb = new Database(inventorydb,
null,
myDbConfig);
classcatalogdb = databasesHome + "/" + classcatalogdb;
classCatalogDb = new Database(classcatalogdb,
null,
myDbConfig);
} catch(FileNotFoundException fnfe) {
System.err.println("MyDbs: " + fnfe.toString());
System.exit(-1);
}
classCatalog = new StoredClassCatalog(classCatalogDb);
TupleBinding inventoryBinding = new InventoryBinding();
ItemNameKeyCreator keyCreator =
new ItemNameKeyCreator(new InventoryBinding());
mySecConfig.setSortedDuplicates(true);
mySecConfig.setAllowPopulate(true); mySecConfig.setKeyCreator(keyCreator);
try {
itemnameindexdb = databasesHome + "/" + itemnameindexdb;
itemNameIndexDb = new SecondaryDatabase(itemnameindexdb,
null,
inventoryDb,
mySecConfig);
} catch(FileNotFoundException fnfe) {
System.err.println("MyDbs: " + fnfe.toString());
System.exit(-1);
}
}
public Database getVendorDB() {
return vendorDb;
}
public Database getInventoryDB() {
return inventoryDb;
}
public SecondaryDatabase getNameIndexDB() {
return itemNameIndexDb;
}
public StoredClassCatalog getClassCatalog() {
return classCatalog;
}
public void close() {
try {
if (itemNameIndexDb != null) {
itemNameIndexDb.close();
}
if (vendorDb != null) {
vendorDb.close();
}
if (inventoryDb != null) {
inventoryDb.close();
}
if (classCatalogDb != null) {
classCatalogDb.close();
}
} catch(DatabaseException dbe) {
System.err.println("Error closing MyDbs: " +
dbe.toString());
System.exit(-1);
}
}
}