1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002,2008 Oracle. All rights reserved. 5 * 6 * $Id: SampleDatabase.java,v 12.7 2008/01/08 20:58:28 bostic Exp $ 7 */ 8 9package collections.ship.basic; 10 11import java.io.File; 12import java.io.FileNotFoundException; 13 14import com.sleepycat.bind.serial.StoredClassCatalog; 15import com.sleepycat.db.Database; 16import com.sleepycat.db.DatabaseConfig; 17import com.sleepycat.db.DatabaseException; 18import com.sleepycat.db.DatabaseType; 19import com.sleepycat.db.Environment; 20import com.sleepycat.db.EnvironmentConfig; 21 22/** 23 * SampleDatabase defines the storage containers, indices and foreign keys 24 * for the sample database. 25 * 26 * @author Mark Hayes 27 */ 28public class SampleDatabase { 29 30 private static final String CLASS_CATALOG = "java_class_catalog"; 31 private static final String SUPPLIER_STORE = "supplier_store"; 32 private static final String PART_STORE = "part_store"; 33 private static final String SHIPMENT_STORE = "shipment_store"; 34 35 private Environment env; 36 private Database partDb; 37 private Database supplierDb; 38 private Database shipmentDb; 39 private StoredClassCatalog javaCatalog; 40 41 /** 42 * Open all storage containers, indices, and catalogs. 43 */ 44 public SampleDatabase(String homeDirectory) 45 throws DatabaseException, FileNotFoundException { 46 47 // Open the Berkeley DB environment in transactional mode. 48 // 49 System.out.println("Opening environment in: " + homeDirectory); 50 EnvironmentConfig envConfig = new EnvironmentConfig(); 51 envConfig.setTransactional(true); 52 envConfig.setAllowCreate(true); 53 envConfig.setInitializeCache(true); 54 envConfig.setInitializeLocking(true); 55 env = new Environment(new File(homeDirectory), envConfig); 56 57 // Set the Berkeley DB config for opening all stores. 58 // 59 DatabaseConfig dbConfig = new DatabaseConfig(); 60 dbConfig.setTransactional(true); 61 dbConfig.setAllowCreate(true); 62 dbConfig.setType(DatabaseType.BTREE); 63 64 // Create the Serial class catalog. This holds the serialized class 65 // format for all database records of serial format. 66 // 67 Database catalogDb = env.openDatabase(null, CLASS_CATALOG, null, 68 dbConfig); 69 javaCatalog = new StoredClassCatalog(catalogDb); 70 71 // Open the Berkeley DB database for the part, supplier and shipment 72 // stores. The stores are opened with no duplicate keys allowed. 73 // 74 partDb = env.openDatabase(null, PART_STORE, null, dbConfig); 75 76 supplierDb = env.openDatabase(null, SUPPLIER_STORE, null, dbConfig); 77 78 shipmentDb = env.openDatabase(null, SHIPMENT_STORE, null, dbConfig); 79 } 80 81 /** 82 * Return the storage environment for the database. 83 */ 84 public final Environment getEnvironment() { 85 86 return env; 87 } 88 89 /** 90 * Return the class catalog. 91 */ 92 public final StoredClassCatalog getClassCatalog() { 93 94 return javaCatalog; 95 } 96 97 /** 98 * Return the part storage container. 99 */ 100 public final Database getPartDatabase() { 101 102 return partDb; 103 } 104 105 /** 106 * Return the supplier storage container. 107 */ 108 public final Database getSupplierDatabase() { 109 110 return supplierDb; 111 } 112 113 /** 114 * Return the shipment storage container. 115 */ 116 public final Database getShipmentDatabase() { 117 118 return shipmentDb; 119 } 120 121 /** 122 * Close all databases and the environment. 123 */ 124 public void close() 125 throws DatabaseException { 126 127 partDb.close(); 128 supplierDb.close(); 129 shipmentDb.close(); 130 // And don't forget to close the catalog and the environment. 131 javaCatalog.close(); 132 env.close(); 133 } 134} 135