1 8 9 package collections.ship.tuple; 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.StoredClassCatalog; 16 import com.sleepycat.bind.serial.TupleSerialKeyCreator; 17 import com.sleepycat.bind.tuple.TupleInput; 18 import com.sleepycat.bind.tuple.TupleOutput; 19 import com.sleepycat.je.Database; 20 import com.sleepycat.je.DatabaseConfig; 21 import com.sleepycat.je.DatabaseException; 22 import com.sleepycat.je.Environment; 23 import com.sleepycat.je.EnvironmentConfig; 24 import com.sleepycat.je.ForeignKeyDeleteAction; 25 import com.sleepycat.je.SecondaryConfig; 26 import com.sleepycat.je.SecondaryDatabase; 27 28 34 public class SampleDatabase { 35 36 private static final String CLASS_CATALOG = "java_class_catalog"; 37 private static final String SUPPLIER_STORE = "supplier_store"; 38 private static final String PART_STORE = "part_store"; 39 private static final String SHIPMENT_STORE = "shipment_store"; 40 private static final String SHIPMENT_PART_INDEX = "shipment_part_index"; 41 private static final String SHIPMENT_SUPPLIER_INDEX = 42 "shipment_supplier_index"; 43 private static final String SUPPLIER_CITY_INDEX = "supplier_city_index"; 44 45 private Environment env; 46 private Database partDb; 47 private Database supplierDb; 48 private Database shipmentDb; 49 private SecondaryDatabase supplierByCityDb; 50 private SecondaryDatabase shipmentByPartDb; 51 private SecondaryDatabase shipmentBySupplierDb; 52 private StoredClassCatalog javaCatalog; 53 54 57 public SampleDatabase(String homeDirectory) 58 throws DatabaseException, FileNotFoundException { 59 60 System.out.println("Opening environment in: " + homeDirectory); 63 EnvironmentConfig envConfig = new EnvironmentConfig(); 64 envConfig.setTransactional(true); 65 envConfig.setAllowCreate(true); 66 env = new Environment(new File (homeDirectory), envConfig); 67 68 DatabaseConfig dbConfig = new DatabaseConfig(); 71 dbConfig.setTransactional(true); 72 dbConfig.setAllowCreate(true); 73 74 Database catalogDb = env.openDatabase(null, CLASS_CATALOG, dbConfig); 78 javaCatalog = new StoredClassCatalog(catalogDb); 79 80 partDb = env.openDatabase(null, PART_STORE, dbConfig); 84 85 supplierDb = env.openDatabase(null, SUPPLIER_STORE, dbConfig); 86 87 shipmentDb = env.openDatabase(null, SHIPMENT_STORE, dbConfig); 88 89 SecondaryConfig secConfig = new SecondaryConfig(); 100 secConfig.setTransactional(true); 101 secConfig.setAllowCreate(true); 102 secConfig.setSortedDuplicates(true); 103 104 secConfig.setKeyCreator(new SupplierByCityKeyCreator(javaCatalog, 105 SupplierData.class)); 106 supplierByCityDb = env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX, 107 supplierDb, secConfig); 108 109 secConfig.setForeignKeyDatabase(partDb); 110 secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE); 111 secConfig.setKeyCreator(new ShipmentByPartKeyCreator(javaCatalog, 112 ShipmentData.class)); 113 shipmentByPartDb = env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX, 114 shipmentDb, secConfig); 115 116 secConfig.setForeignKeyDatabase(supplierDb); 117 secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE); 118 secConfig.setKeyCreator(new ShipmentBySupplierKeyCreator(javaCatalog, 119 ShipmentData.class)); 120 shipmentBySupplierDb = env.openSecondaryDatabase(null, 121 SHIPMENT_SUPPLIER_INDEX, 122 shipmentDb, secConfig); 123 } 124 125 128 public final Environment getEnvironment() { 129 130 return env; 131 } 132 133 136 public final StoredClassCatalog getClassCatalog() { 137 138 return javaCatalog; 139 } 140 141 144 public final Database getPartDatabase() { 145 146 return partDb; 147 } 148 149 152 public final Database getSupplierDatabase() { 153 154 return supplierDb; 155 } 156 157 160 public final Database getShipmentDatabase() { 161 162 return shipmentDb; 163 } 164 165 168 public final SecondaryDatabase getShipmentByPartDatabase() { 169 170 return shipmentByPartDb; 171 } 172 173 176 public final SecondaryDatabase getShipmentBySupplierDatabase() { 177 178 return shipmentBySupplierDb; 179 } 180 181 184 public final SecondaryDatabase getSupplierByCityDatabase() { 185 186 return supplierByCityDb; 187 } 188 189 192 public void close() 193 throws DatabaseException { 194 195 supplierByCityDb.close(); 197 shipmentByPartDb.close(); 198 shipmentBySupplierDb.close(); 199 partDb.close(); 200 supplierDb.close(); 201 shipmentDb.close(); 202 javaCatalog.close(); 204 env.close(); 205 } 206 207 213 private static class SupplierByCityKeyCreator 214 extends TupleSerialKeyCreator { 215 216 221 private SupplierByCityKeyCreator(ClassCatalog catalog, 222 Class valueClass) { 223 224 super(catalog, valueClass); 225 } 226 227 231 public boolean createSecondaryKey(TupleInput primaryKeyInput, 232 Object valueInput, 233 TupleOutput indexKeyOutput) { 234 235 SupplierData supplierData = (SupplierData) valueInput; 236 String city = supplierData.getCity(); 237 if (city != null) { 238 indexKeyOutput.writeString(supplierData.getCity()); 239 return true; 240 } else { 241 return false; 242 } 243 } 244 } 245 246 252 private static class ShipmentByPartKeyCreator 253 extends TupleSerialKeyCreator { 254 255 260 private ShipmentByPartKeyCreator(ClassCatalog catalog, 261 Class valueClass) { 262 super(catalog, valueClass); 263 } 264 265 269 public boolean createSecondaryKey(TupleInput primaryKeyInput, 270 Object valueInput, 271 TupleOutput indexKeyOutput) { 272 273 String partNumber = primaryKeyInput.readString(); 274 indexKeyOutput.writeString(partNumber); 276 return true; 277 } 278 } 279 280 286 private static class ShipmentBySupplierKeyCreator 287 extends TupleSerialKeyCreator { 288 289 294 private ShipmentBySupplierKeyCreator(ClassCatalog catalog, 295 Class valueClass) { 296 super(catalog, valueClass); 297 } 298 299 304 public boolean createSecondaryKey(TupleInput primaryKeyInput, 305 Object valueInput, 306 TupleOutput indexKeyOutput) { 307 308 primaryKeyInput.readString(); String supplierNumber = primaryKeyInput.readString(); 310 indexKeyOutput.writeString(supplierNumber); 311 return true; 312 } 313 } 314 } 315 | Popular Tags |