1 8 9 package collections.ship.factory; 10 11 import java.io.File ; 12 import java.io.FileNotFoundException ; 13 14 import com.sleepycat.bind.serial.StoredClassCatalog; 15 import com.sleepycat.collections.TupleSerialFactory; 16 import com.sleepycat.je.Database; 17 import com.sleepycat.je.DatabaseConfig; 18 import com.sleepycat.je.DatabaseException; 19 import com.sleepycat.je.Environment; 20 import com.sleepycat.je.EnvironmentConfig; 21 import com.sleepycat.je.ForeignKeyDeleteAction; 22 import com.sleepycat.je.SecondaryConfig; 23 import com.sleepycat.je.SecondaryDatabase; 24 25 31 public class SampleDatabase { 32 33 private static final String CLASS_CATALOG = "java_class_catalog"; 34 private static final String SUPPLIER_STORE = "supplier_store"; 35 private static final String PART_STORE = "part_store"; 36 private static final String SHIPMENT_STORE = "shipment_store"; 37 private static final String SHIPMENT_PART_INDEX = "shipment_part_index"; 38 private static final String SHIPMENT_SUPPLIER_INDEX = 39 "shipment_supplier_index"; 40 private static final String SUPPLIER_CITY_INDEX = "supplier_city_index"; 41 42 private Environment env; 43 private Database partDb; 44 private Database supplierDb; 45 private Database shipmentDb; 46 private SecondaryDatabase supplierByCityDb; 47 private SecondaryDatabase shipmentByPartDb; 48 private SecondaryDatabase shipmentBySupplierDb; 49 private StoredClassCatalog javaCatalog; 50 private TupleSerialFactory factory; 51 52 55 public SampleDatabase(String homeDirectory) 56 throws DatabaseException, FileNotFoundException { 57 58 System.out.println("Opening environment in: " + homeDirectory); 61 EnvironmentConfig envConfig = new EnvironmentConfig(); 62 envConfig.setTransactional(true); 63 envConfig.setAllowCreate(true); 64 env = new Environment(new File (homeDirectory), envConfig); 65 66 DatabaseConfig dbConfig = new DatabaseConfig(); 69 dbConfig.setTransactional(true); 70 dbConfig.setAllowCreate(true); 71 72 Database catalogDb = env.openDatabase(null, CLASS_CATALOG, dbConfig); 76 javaCatalog = new StoredClassCatalog(catalogDb); 77 78 factory = new TupleSerialFactory(javaCatalog); 82 83 partDb = env.openDatabase(null, PART_STORE, dbConfig); 87 88 supplierDb = env.openDatabase(null, SUPPLIER_STORE, dbConfig); 89 90 shipmentDb = env.openDatabase(null, SHIPMENT_STORE, dbConfig); 91 92 SecondaryConfig secConfig = new SecondaryConfig(); 103 secConfig.setTransactional(true); 104 secConfig.setAllowCreate(true); 105 secConfig.setSortedDuplicates(true); 106 107 secConfig.setKeyCreator(factory.getKeyCreator(Supplier.class, 108 Supplier.CITY_KEY)); 109 supplierByCityDb = env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX, 110 supplierDb, secConfig); 111 112 secConfig.setForeignKeyDatabase(partDb); 113 secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE); 114 secConfig.setKeyCreator(factory.getKeyCreator(Shipment.class, 115 Shipment.PART_KEY)); 116 shipmentByPartDb = env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX, 117 shipmentDb, secConfig); 118 119 secConfig.setForeignKeyDatabase(supplierDb); 120 secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE); 121 secConfig.setKeyCreator(factory.getKeyCreator(Shipment.class, 122 Shipment.SUPPLIER_KEY)); 123 shipmentBySupplierDb = env.openSecondaryDatabase(null, 124 SHIPMENT_SUPPLIER_INDEX, 125 shipmentDb, secConfig); 126 } 127 128 131 public final TupleSerialFactory getFactory() { 132 133 return factory; 134 } 135 136 139 public final Environment getEnvironment() { 140 141 return env; 142 } 143 144 147 public final StoredClassCatalog getClassCatalog() { 148 149 return javaCatalog; 150 } 151 152 155 public final Database getPartDatabase() { 156 157 return partDb; 158 } 159 160 163 public final Database getSupplierDatabase() { 164 165 return supplierDb; 166 } 167 168 171 public final Database getShipmentDatabase() { 172 173 return shipmentDb; 174 } 175 176 179 public final SecondaryDatabase getShipmentByPartDatabase() { 180 181 return shipmentByPartDb; 182 } 183 184 187 public final SecondaryDatabase getShipmentBySupplierDatabase() { 188 189 return shipmentBySupplierDb; 190 } 191 192 195 public final SecondaryDatabase getSupplierByCityDatabase() { 196 197 return supplierByCityDb; 198 } 199 200 203 public void close() 204 throws DatabaseException { 205 206 supplierByCityDb.close(); 208 shipmentByPartDb.close(); 209 shipmentBySupplierDb.close(); 210 partDb.close(); 211 supplierDb.close(); 212 shipmentDb.close(); 213 javaCatalog.close(); 215 env.close(); 216 } 217 } 218 | Popular Tags |