00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package collections.ship.factory;
00011
00012 import java.io.File;
00013 import java.io.FileNotFoundException;
00014
00015 import com.sleepycat.bind.serial.StoredClassCatalog;
00016 import com.sleepycat.collections.TupleSerialFactory;
00017 import com.sleepycat.db.Database;
00018 import com.sleepycat.db.DatabaseConfig;
00019 import com.sleepycat.db.DatabaseException;
00020 import com.sleepycat.db.DatabaseType;
00021 import com.sleepycat.db.Environment;
00022 import com.sleepycat.db.EnvironmentConfig;
00023 import com.sleepycat.db.SecondaryConfig;
00024 import com.sleepycat.db.SecondaryDatabase;
00025
00032 public class SampleDatabase {
00033
00034 private static final String CLASS_CATALOG = "java_class_catalog";
00035 private static final String SUPPLIER_STORE = "supplier_store";
00036 private static final String PART_STORE = "part_store";
00037 private static final String SHIPMENT_STORE = "shipment_store";
00038 private static final String SHIPMENT_PART_INDEX = "shipment_part_index";
00039 private static final String SHIPMENT_SUPPLIER_INDEX =
00040 "shipment_supplier_index";
00041 private static final String SUPPLIER_CITY_INDEX = "supplier_city_index";
00042
00043 private Environment env;
00044 private Database partDb;
00045 private Database supplierDb;
00046 private Database shipmentDb;
00047 private SecondaryDatabase supplierByCityDb;
00048 private SecondaryDatabase shipmentByPartDb;
00049 private SecondaryDatabase shipmentBySupplierDb;
00050 private StoredClassCatalog javaCatalog;
00051 private TupleSerialFactory factory;
00052
00056 public SampleDatabase(String homeDirectory)
00057 throws DatabaseException, FileNotFoundException {
00058
00059
00060
00061 System.out.println("Opening environment in: " + homeDirectory);
00062 EnvironmentConfig envConfig = new EnvironmentConfig();
00063 envConfig.setTransactional(true);
00064 envConfig.setAllowCreate(true);
00065 envConfig.setInitializeCache(true);
00066 envConfig.setInitializeLocking(true);
00067 env = new Environment(new File(homeDirectory), envConfig);
00068
00069
00070
00071 DatabaseConfig dbConfig = new DatabaseConfig();
00072 dbConfig.setTransactional(true);
00073 dbConfig.setAllowCreate(true);
00074 dbConfig.setType(DatabaseType.BTREE);
00075
00076
00077
00078
00079 Database catalogDb = env.openDatabase(null, CLASS_CATALOG, null,
00080 dbConfig);
00081 javaCatalog = new StoredClassCatalog(catalogDb);
00082
00083
00084
00085
00086 factory = new TupleSerialFactory(javaCatalog);
00087
00088
00089
00090
00091 partDb = env.openDatabase(null, PART_STORE, null, dbConfig);
00092
00093 supplierDb = env.openDatabase(null, SUPPLIER_STORE, null, dbConfig);
00094
00095 shipmentDb = env.openDatabase(null, SHIPMENT_STORE, null, dbConfig);
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 SecondaryConfig secConfig = new SecondaryConfig();
00108 secConfig.setTransactional(true);
00109 secConfig.setAllowCreate(true);
00110 secConfig.setType(DatabaseType.BTREE);
00111 secConfig.setSortedDuplicates(true);
00112
00113 secConfig.setKeyCreator(factory.getKeyCreator(Supplier.class,
00114 Supplier.CITY_KEY));
00115 supplierByCityDb = env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX,
00116 null,
00117 supplierDb,
00118 secConfig);
00119
00120 secConfig.setKeyCreator(factory.getKeyCreator(Shipment.class,
00121 Shipment.PART_KEY));
00122 shipmentByPartDb = env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX,
00123 null,
00124 shipmentDb,
00125 secConfig);
00126
00127 secConfig.setKeyCreator(factory.getKeyCreator(Shipment.class,
00128 Shipment.SUPPLIER_KEY));
00129 shipmentBySupplierDb = env.openSecondaryDatabase(null,
00130 SHIPMENT_SUPPLIER_INDEX,
00131 null,
00132 shipmentDb,
00133 secConfig);
00134 }
00135
00139 public final TupleSerialFactory getFactory() {
00140
00141 return factory;
00142 }
00143
00147 public final Environment getEnvironment() {
00148
00149 return env;
00150 }
00151
00155 public final StoredClassCatalog getClassCatalog() {
00156
00157 return javaCatalog;
00158 }
00159
00163 public final Database getPartDatabase() {
00164
00165 return partDb;
00166 }
00167
00171 public final Database getSupplierDatabase() {
00172
00173 return supplierDb;
00174 }
00175
00179 public final Database getShipmentDatabase() {
00180
00181 return shipmentDb;
00182 }
00183
00187 public final SecondaryDatabase getShipmentByPartDatabase() {
00188
00189 return shipmentByPartDb;
00190 }
00191
00195 public final SecondaryDatabase getShipmentBySupplierDatabase() {
00196
00197 return shipmentBySupplierDb;
00198 }
00199
00203 public final SecondaryDatabase getSupplierByCityDatabase() {
00204
00205 return supplierByCityDb;
00206 }
00207
00211 public void close()
00212 throws DatabaseException {
00213
00214
00215 supplierByCityDb.close();
00216 shipmentByPartDb.close();
00217 shipmentBySupplierDb.close();
00218 partDb.close();
00219 supplierDb.close();
00220 shipmentDb.close();
00221
00222 javaCatalog.close();
00223 env.close();
00224 }
00225 }