je/examples/collections/ship/basic/SampleDatabase.java
2021-06-06 13:46:45 -04:00

133 lines
3.8 KiB
Java
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*-
* Copyright (C) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
*
* This file was distributed by Oracle as part of a version of Oracle Berkeley
* DB Java Edition made available at:
*
* http://www.oracle.com/technetwork/database/database-technologies/berkeleydb/downloads/index.html
*
* Please see the LICENSE file included in the top-level directory of the
* appropriate version of Oracle Berkeley DB Java Edition for a copy of the
* license and additional information.
*/
package collections.ship.basic;
import java.io.File;
import com.sleepycat.bind.serial.StoredClassCatalog;
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;
/**
* 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 Environment env;
private Database partDb;
private Database supplierDb;
private Database shipmentDb;
private StoredClassCatalog javaCatalog;
/**
* Open all storage containers, indices, and catalogs.
*/
public SampleDatabase(String homeDirectory)
throws DatabaseException {
// 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);
}
/**
* 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;
}
/**
* Close all databases and the environment.
*/
public void close()
throws DatabaseException {
partDb.close();
supplierDb.close();
shipmentDb.close();
// And don't forget to close the catalog and the environment.
javaCatalog.close();
env.close();
}
}