1 8 9 package collections.ship.index; 10 11 import java.io.File ; 12 import java.io.FileNotFoundException ; 13 14 import com.sleepycat.bind.serial.ClassCatalog; 15 import com.sleepycat.bind.serial.SerialSerialKeyCreator; 16 import com.sleepycat.bind.serial.StoredClassCatalog; 17 import com.sleepycat.je.Database; 18 import com.sleepycat.je.DatabaseConfig; 19 import com.sleepycat.je.DatabaseException; 20 import com.sleepycat.je.Environment; 21 import com.sleepycat.je.EnvironmentConfig; 22 import com.sleepycat.je.ForeignKeyDeleteAction; 23 import com.sleepycat.je.SecondaryConfig; 24 import com.sleepycat.je.SecondaryDatabase; 25 26 32 public class SampleDatabase { 33 34 private static final String CLASS_CATALOG = "java_class_catalog"; 35 private static final String SUPPLIER_STORE = "supplier_store"; 36 private static final String PART_STORE = "part_store"; 37 private static final String SHIPMENT_STORE = "shipment_store"; 38 private static final String SHIPMENT_PART_INDEX = "shipment_part_index"; 39 private static final String SHIPMENT_SUPPLIER_INDEX = 40 "shipment_supplier_index"; 41 private static final String SUPPLIER_CITY_INDEX = "supplier_city_index"; 42 43 private Environment env; 44 private Database partDb; 45 private Database supplierDb; 46 private Database shipmentDb; 47 private SecondaryDatabase supplierByCityDb; 48 private SecondaryDatabase shipmentByPartDb; 49 private SecondaryDatabase shipmentBySupplierDb; 50 private StoredClassCatalog javaCatalog; 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 partDb = env.openDatabase(null, PART_STORE, dbConfig); 82 83 supplierDb = env.openDatabase(null, SUPPLIER_STORE, dbConfig); 84 85 shipmentDb = env.openDatabase(null, SHIPMENT_STORE, dbConfig); 86 87 SecondaryConfig secConfig = new SecondaryConfig(); 98 secConfig.setTransactional(true); 99 secConfig.setAllowCreate(true); 100 secConfig.setSortedDuplicates(true); 101 102 secConfig.setKeyCreator( 103 new SupplierByCityKeyCreator(javaCatalog, 104 SupplierKey.class, 105 SupplierData.class, 106 String .class)); 107 supplierByCityDb = env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX, 108 supplierDb, secConfig); 109 110 secConfig.setForeignKeyDatabase(partDb); 111 secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE); 112 secConfig.setKeyCreator( 113 new ShipmentByPartKeyCreator(javaCatalog, 114 ShipmentKey.class, 115 ShipmentData.class, 116 PartKey.class)); 117 shipmentByPartDb = env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX, 118 shipmentDb, secConfig); 119 120 secConfig.setForeignKeyDatabase(supplierDb); 121 secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE); 122 secConfig.setKeyCreator( 123 new ShipmentBySupplierKeyCreator(javaCatalog, 124 ShipmentKey.class, 125 ShipmentData.class, 126 SupplierKey.class)); 127 shipmentBySupplierDb = env.openSecondaryDatabase(null, 128 SHIPMENT_SUPPLIER_INDEX, 129 shipmentDb, secConfig); 130 } 131 132 135 public final Environment getEnvironment() { 136 137 return env; 138 } 139 140 143 public final StoredClassCatalog getClassCatalog() { 144 145 return javaCatalog; 146 } 147 148 151 public final Database getPartDatabase() { 152 153 return partDb; 154 } 155 156 159 public final Database getSupplierDatabase() { 160 161 return supplierDb; 162 } 163 164 167 public final Database getShipmentDatabase() { 168 169 return shipmentDb; 170 } 171 172 175 public final SecondaryDatabase getShipmentByPartDatabase() { 176 177 return shipmentByPartDb; 178 } 179 180 183 public final SecondaryDatabase getShipmentBySupplierDatabase() { 184 185 return shipmentBySupplierDb; 186 } 187 188 191 public final SecondaryDatabase getSupplierByCityDatabase() { 192 193 return supplierByCityDb; 194 } 195 196 199 public void close() 200 throws DatabaseException { 201 202 supplierByCityDb.close(); 204 shipmentByPartDb.close(); 205 shipmentBySupplierDb.close(); 206 partDb.close(); 207 supplierDb.close(); 208 shipmentDb.close(); 209 javaCatalog.close(); 211 env.close(); 212 } 213 214 220 private static class SupplierByCityKeyCreator 221 extends SerialSerialKeyCreator { 222 223 230 private SupplierByCityKeyCreator(ClassCatalog catalog, 231 Class primaryKeyClass, 232 Class valueClass, 233 Class indexKeyClass) { 234 235 super(catalog, primaryKeyClass, valueClass, indexKeyClass); 236 } 237 238 242 public Object createSecondaryKey(Object primaryKeyInput, 243 Object valueInput) { 244 245 SupplierData supplierData = (SupplierData) valueInput; 246 return supplierData.getCity(); 247 } 248 } 249 250 256 private static class ShipmentByPartKeyCreator 257 extends SerialSerialKeyCreator { 258 259 266 private ShipmentByPartKeyCreator(ClassCatalog catalog, 267 Class primaryKeyClass, 268 Class valueClass, 269 Class indexKeyClass) { 270 271 super(catalog, primaryKeyClass, valueClass, indexKeyClass); 272 } 273 274 278 public Object createSecondaryKey(Object primaryKeyInput, 279 Object valueInput) { 280 281 ShipmentKey shipmentKey = (ShipmentKey) primaryKeyInput; 282 return new PartKey(shipmentKey.getPartNumber()); 283 } 284 } 285 286 292 private static class ShipmentBySupplierKeyCreator 293 extends SerialSerialKeyCreator { 294 295 302 private ShipmentBySupplierKeyCreator(ClassCatalog catalog, 303 Class primaryKeyClass, 304 Class valueClass, 305 Class indexKeyClass) { 306 307 super(catalog, primaryKeyClass, valueClass, indexKeyClass); 308 } 309 310 315 public Object createSecondaryKey(Object primaryKeyInput, 316 Object valueInput) { 317 318 ShipmentKey shipmentKey = (ShipmentKey) primaryKeyInput; 319 return new SupplierKey(shipmentKey.getSupplierNumber()); 320 } 321 } 322 } 323 | Popular Tags |