1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: SampleDatabase.java,v 1.27.2.2 2010/01/04 15:30:26 cwl Exp $
*/
package collections.ship.sentity;
import java.io.File;
import java.io.FileNotFoundException;
import com.sleepycat.bind.serial.ClassCatalog;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.bind.serial.TupleSerialKeyCreator;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.ForeignKeyDeleteAction;
import com.sleepycat.je.SecondaryConfig;
import com.sleepycat.je.SecondaryDatabase;
/**
* SampleDatabase defines the storage containers, indices and foreign keys
* for the sample database.
*
* @author Mark Hayes
*/
public class SampleDatabase {
private static final String CLASS_CATALOG = "java_class_catalog";
private static final String SUPPLIER_STORE = "supplier_store";
private static final String PART_STORE = "part_store";
private static final String SHIPMENT_STORE = "shipment_store";
private static final String SHIPMENT_PART_INDEX = "shipment_part_index";
private static final String SHIPMENT_SUPPLIER_INDEX =
"shipment_supplier_index";
private static final String SUPPLIER_CITY_INDEX = "supplier_city_index";
private Environment env;
private Database partDb;
private Database supplierDb;
private Database shipmentDb;
private SecondaryDatabase supplierByCityDb;
private SecondaryDatabase shipmentByPartDb;
private SecondaryDatabase shipmentBySupplierDb;
private StoredClassCatalog javaCatalog;
/**
* Open all storage containers, indices, and catalogs.
*/
public SampleDatabase(String homeDirectory)
throws DatabaseException, FileNotFoundException {
// Open the Berkeley DB environment in transactional mode.
//
System.out.println("Opening environment in: " + homeDirectory);
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
env = new Environment(new File(homeDirectory), envConfig);
// Set the Berkeley DB config for opening all stores.
//
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(true);
dbConfig.setAllowCreate(true);
// Create the Serial class catalog. This holds the serialized class
// format for all database records of serial format.
//
Database catalogDb = env.openDatabase(null, CLASS_CATALOG, dbConfig);
javaCatalog = new StoredClassCatalog(catalogDb);
// Open the Berkeley DB database for the part, supplier and shipment
// stores. The stores are opened with no duplicate keys allowed.
//
partDb = env.openDatabase(null, PART_STORE, dbConfig);
supplierDb = env.openDatabase(null, SUPPLIER_STORE, dbConfig);
shipmentDb = env.openDatabase(null, SHIPMENT_STORE, dbConfig);
// Open the SecondaryDatabase for the city index of the supplier store,
// and for the part and supplier indices of the shipment store.
// Duplicate keys are allowed since more than one supplier may be in
// the same city, and more than one shipment may exist for the same
// supplier or part. A foreign key constraint is defined for the
// supplier and part indices to ensure that a shipment only refers to
// existing part and supplier keys. The CASCADE delete action means
// that shipments will be deleted if their associated part or supplier
// is deleted.
//
SecondaryConfig secConfig = new SecondaryConfig();
secConfig.setTransactional(true);
secConfig.setAllowCreate(true);
secConfig.setSortedDuplicates(true);
secConfig.setKeyCreator(new SupplierByCityKeyCreator(javaCatalog,
Supplier.class));
supplierByCityDb = env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX,
supplierDb, secConfig);
secConfig.setForeignKeyDatabase(partDb);
secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE);
secConfig.setKeyCreator(new ShipmentByPartKeyCreator(javaCatalog,
Shipment.class));
shipmentByPartDb = env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX,
shipmentDb, secConfig);
secConfig.setForeignKeyDatabase(supplierDb);
secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE);
secConfig.setKeyCreator(new ShipmentBySupplierKeyCreator(javaCatalog,
Shipment.class));
shipmentBySupplierDb = env.openSecondaryDatabase(null,
SHIPMENT_SUPPLIER_INDEX,
shipmentDb, secConfig);
}
/**
* Return the storage environment for the database.
*/
public final Environment getEnvironment() {
return env;
}
/**
* Return the class catalog.
*/
public final StoredClassCatalog getClassCatalog() {
return javaCatalog;
}
/**
* Return the part storage container.
*/
public final Database getPartDatabase() {
return partDb;
}
/**
* Return the supplier storage container.
*/
public final Database getSupplierDatabase() {
return supplierDb;
}
/**
* Return the shipment storage container.
*/
public final Database getShipmentDatabase() {
return shipmentDb;
}
/**
* Return the shipment-by-part index.
*/
public final SecondaryDatabase getShipmentByPartDatabase() {
return shipmentByPartDb;
}
/**
* Return the shipment-by-supplier index.
*/
public final SecondaryDatabase getShipmentBySupplierDatabase() {
return shipmentBySupplierDb;
}
/**
* Return the supplier-by-city index.
*/
public final SecondaryDatabase getSupplierByCityDatabase() {
return supplierByCityDb;
}
/**
* Close all stores (closing a store automatically closes its indices).
*/
public void close()
throws DatabaseException {
// Close secondary databases, then primary databases.
supplierByCityDb.close();
shipmentByPartDb.close();
shipmentBySupplierDb.close();
partDb.close();
supplierDb.close();
shipmentDb.close();
// And don't forget to close the catalog and the environment.
javaCatalog.close();
env.close();
}
/**
* The SecondaryKeyCreator for the SupplierByCity index. This is an
* extension of the abstract class TupleSerialKeyCreator, which implements
* SecondaryKeyCreator for the case where the data keys are of the format
* TupleFormat and the data values are of the format SerialFormat.
*/
private static class SupplierByCityKeyCreator
extends TupleSerialKeyCreator {
/**
* Construct the city key extractor.
* @param catalog is the class catalog.
* @param valueClass is the supplier value class.
*/
private SupplierByCityKeyCreator(ClassCatalog catalog,
Class valueClass) {
super(catalog, valueClass);
}
/**
* Extract the city key from a supplier key/value pair. The city key
* is stored in the supplier value, so the supplier key is not used.
*/
public boolean createSecondaryKey(TupleInput primaryKeyInput,
Object valueInput,
TupleOutput indexKeyOutput) {
Supplier supplier = (Supplier) valueInput;
String city = supplier.getCity();
if (city != null) {
indexKeyOutput.writeString(supplier.getCity());
return true;
} else {
return false;
}
}
}
/**
* The SecondaryKeyCreator for the ShipmentByPart index. This is an
* extension of the abstract class TupleSerialKeyCreator, which implements
* SecondaryKeyCreator for the case where the data keys are of the format
* TupleFormat and the data values are of the format SerialFormat.
*/
private static class ShipmentByPartKeyCreator
extends TupleSerialKeyCreator {
/**
* Construct the part key extractor.
* @param catalog is the class catalog.
* @param valueClass is the shipment value class.
*/
private ShipmentByPartKeyCreator(ClassCatalog catalog,
Class valueClass) {
super(catalog, valueClass);
}
/**
* Extract the part key from a shipment key/value pair. The part key
* is stored in the shipment key, so the shipment value is not used.
*/
public boolean createSecondaryKey(TupleInput primaryKeyInput,
Object valueInput,
TupleOutput indexKeyOutput) {
String partNumber = primaryKeyInput.readString();
// don't bother reading the supplierNumber
indexKeyOutput.writeString(partNumber);
return true;
}
}
/**
* The SecondaryKeyCreator for the ShipmentBySupplier index. This is an
* extension of the abstract class TupleSerialKeyCreator, which implements
* SecondaryKeyCreator for the case where the data keys are of the format
* TupleFormat and the data values are of the format SerialFormat.
*/
private static class ShipmentBySupplierKeyCreator
extends TupleSerialKeyCreator {
/**
* Construct the supplier key extractor.
* @param catalog is the class catalog.
* @param valueClass is the shipment value class.
*/
private ShipmentBySupplierKeyCreator(ClassCatalog catalog,
Class valueClass) {
super(catalog, valueClass);
}
/**
* Extract the supplier key from a shipment key/value pair. The
* supplier key is stored in the shipment key, so the shipment value is
* not used.
*/
public boolean createSecondaryKey(TupleInput primaryKeyInput,
Object valueInput,
TupleOutput indexKeyOutput) {
primaryKeyInput.readString(); // skip the partNumber
String supplierNumber = primaryKeyInput.readString();
indexKeyOutput.writeString(supplierNumber);
return true;
}
}
}
|