KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > collections > ship > factory > SampleDatabase


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: SampleDatabase.java,v 1.25 2006/10/30 21:13:59 bostic Exp $
7  */

8
9 package collections.ship.factory;
10
11 import java.io.File JavaDoc;
12 import java.io.FileNotFoundException JavaDoc;
13
14 import com.sleepycat.bind.serial.StoredClassCatalog;
15 import com.sleepycat.collections.TupleSerialFactory;
16 import com.sleepycat.je.Database;
17 import com.sleepycat.je.DatabaseConfig;
18 import com.sleepycat.je.DatabaseException;
19 import com.sleepycat.je.Environment;
20 import com.sleepycat.je.EnvironmentConfig;
21 import com.sleepycat.je.ForeignKeyDeleteAction;
22 import com.sleepycat.je.SecondaryConfig;
23 import com.sleepycat.je.SecondaryDatabase;
24
25 /**
26  * SampleDatabase defines the storage containers, indices and foreign keys
27  * for the sample database.
28  *
29  * @author Mark Hayes
30  */

31 public class SampleDatabase {
32
33     private static final String JavaDoc CLASS_CATALOG = "java_class_catalog";
34     private static final String JavaDoc SUPPLIER_STORE = "supplier_store";
35     private static final String JavaDoc PART_STORE = "part_store";
36     private static final String JavaDoc SHIPMENT_STORE = "shipment_store";
37     private static final String JavaDoc SHIPMENT_PART_INDEX = "shipment_part_index";
38     private static final String JavaDoc SHIPMENT_SUPPLIER_INDEX =
39                                     "shipment_supplier_index";
40     private static final String JavaDoc SUPPLIER_CITY_INDEX = "supplier_city_index";
41
42     private Environment env;
43     private Database partDb;
44     private Database supplierDb;
45     private Database shipmentDb;
46     private SecondaryDatabase supplierByCityDb;
47     private SecondaryDatabase shipmentByPartDb;
48     private SecondaryDatabase shipmentBySupplierDb;
49     private StoredClassCatalog javaCatalog;
50     private TupleSerialFactory factory;
51
52     /**
53      * Open all storage containers, indices, and catalogs.
54      */

55     public SampleDatabase(String JavaDoc homeDirectory)
56         throws DatabaseException, FileNotFoundException JavaDoc {
57
58         // Open the Berkeley DB environment in transactional mode.
59
//
60
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 JavaDoc(homeDirectory), envConfig);
65
66         // Set the Berkeley DB config for opening all stores.
67
//
68
DatabaseConfig dbConfig = new DatabaseConfig();
69         dbConfig.setTransactional(true);
70         dbConfig.setAllowCreate(true);
71
72         // Create the Serial class catalog. This holds the serialized class
73
// format for all database records of serial format.
74
//
75
Database catalogDb = env.openDatabase(null, CLASS_CATALOG, dbConfig);
76         javaCatalog = new StoredClassCatalog(catalogDb);
77
78         // Use the TupleSerialDbFactory for a Serial/Tuple-based database
79
// where marshalling interfaces are used.
80
//
81
factory = new TupleSerialFactory(javaCatalog);
82
83         // Open the Berkeley DB database for the part, supplier and shipment
84
// stores. The stores are opened with no duplicate keys allowed.
85
//
86
partDb = env.openDatabase(null, PART_STORE, dbConfig);
87
88         supplierDb = env.openDatabase(null, SUPPLIER_STORE, dbConfig);
89
90         shipmentDb = env.openDatabase(null, SHIPMENT_STORE, dbConfig);
91
92         // Open the SecondaryDatabase for the city index of the supplier store,
93
// and for the part and supplier indices of the shipment store.
94
// Duplicate keys are allowed since more than one supplier may be in
95
// the same city, and more than one shipment may exist for the same
96
// supplier or part. A foreign key constraint is defined for the
97
// supplier and part indices to ensure that a shipment only refers to
98
// existing part and supplier keys. The CASCADE delete action means
99
// that shipments will be deleted if their associated part or supplier
100
// is deleted.
101
//
102
SecondaryConfig secConfig = new SecondaryConfig();
103         secConfig.setTransactional(true);
104         secConfig.setAllowCreate(true);
105         secConfig.setSortedDuplicates(true);
106
107         secConfig.setKeyCreator(factory.getKeyCreator(Supplier.class,
108                                                       Supplier.CITY_KEY));
109         supplierByCityDb = env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX,
110                                                      supplierDb, secConfig);
111
112         secConfig.setForeignKeyDatabase(partDb);
113         secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE);
114         secConfig.setKeyCreator(factory.getKeyCreator(Shipment.class,
115                                                       Shipment.PART_KEY));
116         shipmentByPartDb = env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX,
117                                                      shipmentDb, secConfig);
118
119         secConfig.setForeignKeyDatabase(supplierDb);
120         secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE);
121         secConfig.setKeyCreator(factory.getKeyCreator(Shipment.class,
122                                                       Shipment.SUPPLIER_KEY));
123         shipmentBySupplierDb = env.openSecondaryDatabase(null,
124                                                      SHIPMENT_SUPPLIER_INDEX,
125                                                      shipmentDb, secConfig);
126     }
127
128     /**
129      * Return the tuple-serial factory.
130      */

131     public final TupleSerialFactory getFactory() {
132
133         return factory;
134     }
135
136     /**
137      * Return the storage environment for the database.
138      */

139     public final Environment getEnvironment() {
140
141         return env;
142     }
143
144     /**
145      * Return the class catalog.
146      */

147     public final StoredClassCatalog getClassCatalog() {
148
149         return javaCatalog;
150     }
151
152     /**
153      * Return the part storage container.
154      */

155     public final Database getPartDatabase() {
156
157         return partDb;
158     }
159
160     /**
161      * Return the supplier storage container.
162      */

163     public final Database getSupplierDatabase() {
164
165         return supplierDb;
166     }
167
168     /**
169      * Return the shipment storage container.
170      */

171     public final Database getShipmentDatabase() {
172
173         return shipmentDb;
174     }
175
176     /**
177      * Return the shipment-by-part index.
178      */

179     public final SecondaryDatabase getShipmentByPartDatabase() {
180
181         return shipmentByPartDb;
182     }
183
184     /**
185      * Return the shipment-by-supplier index.
186      */

187     public final SecondaryDatabase getShipmentBySupplierDatabase() {
188
189         return shipmentBySupplierDb;
190     }
191
192     /**
193      * Return the supplier-by-city index.
194      */

195     public final SecondaryDatabase getSupplierByCityDatabase() {
196
197         return supplierByCityDb;
198     }
199
200     /**
201      * Close all databases and the environment.
202      */

203     public void close()
204         throws DatabaseException {
205
206         // Close secondary databases, then primary databases.
207
supplierByCityDb.close();
208         shipmentByPartDb.close();
209         shipmentBySupplierDb.close();
210         partDb.close();
211         supplierDb.close();
212         shipmentDb.close();
213         // And don't forget to close the catalog and the environment.
214
javaCatalog.close();
215         env.close();
216     }
217 }
218
Popular Tags