1 8 9 package collections.ship.marshal; 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 MarshalledKeyCreator(javaCatalog, 105 Supplier.class, 106 Supplier.CITY_KEY)); 107 supplierByCityDb = env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX, 108 supplierDb, secConfig); 109 110 secConfig.setForeignKeyDatabase(partDb); 111 secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE); 112 secConfig.setKeyCreator(new MarshalledKeyCreator(javaCatalog, 113 Shipment.class, 114 Shipment.PART_KEY)); 115 shipmentByPartDb = env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX, 116 shipmentDb, secConfig); 117 118 secConfig.setForeignKeyDatabase(supplierDb); 119 secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE); 120 secConfig.setKeyCreator(new MarshalledKeyCreator(javaCatalog, 121 Shipment.class, 122 Shipment.SUPPLIER_KEY)); 123 shipmentBySupplierDb = env.openSecondaryDatabase(null, 124 SHIPMENT_SUPPLIER_INDEX, 125 shipmentDb, secConfig); 126 } 127 128 131 public final Environment getEnvironment() { 132 133 return env; 134 } 135 136 139 public final StoredClassCatalog getClassCatalog() { 140 141 return javaCatalog; 142 } 143 144 147 public final Database getPartDatabase() { 148 149 return partDb; 150 } 151 152 155 public final Database getSupplierDatabase() { 156 157 return supplierDb; 158 } 159 160 163 public final Database getShipmentDatabase() { 164 165 return shipmentDb; 166 } 167 168 171 public final SecondaryDatabase getShipmentByPartDatabase() { 172 173 return shipmentByPartDb; 174 } 175 176 179 public final SecondaryDatabase getShipmentBySupplierDatabase() { 180 181 return shipmentBySupplierDb; 182 } 183 184 187 public final SecondaryDatabase getSupplierByCityDatabase() { 188 189 return supplierByCityDb; 190 } 191 192 195 public void close() 196 throws DatabaseException { 197 198 supplierByCityDb.close(); 200 shipmentByPartDb.close(); 201 shipmentBySupplierDb.close(); 202 partDb.close(); 203 supplierDb.close(); 204 shipmentDb.close(); 205 javaCatalog.close(); 207 env.close(); 208 } 209 210 216 private static class MarshalledKeyCreator 217 extends TupleSerialKeyCreator { 218 219 private String keyName; 220 221 227 private MarshalledKeyCreator(ClassCatalog catalog, 228 Class valueClass, 229 String keyName) { 230 231 super(catalog, valueClass); 232 this.keyName = keyName; 233 } 234 235 239 public boolean createSecondaryKey(TupleInput primaryKeyInput, 240 Object valueInput, 241 TupleOutput indexKeyOutput) { 242 243 MarshalledEntity entity = (MarshalledEntity) valueInput; 247 entity.unmarshalPrimaryKey(primaryKeyInput); 248 return entity.marshalSecondaryKey(keyName, indexKeyOutput); 249 } 250 } 251 } 252 | Popular Tags |