KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > collections > ship > index > 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.26 2006/10/30 21:13:59 bostic Exp $
7  */

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

32 public class SampleDatabase {
33
34     private static final String JavaDoc CLASS_CATALOG = "java_class_catalog";
35     private static final String JavaDoc SUPPLIER_STORE = "supplier_store";
36     private static final String JavaDoc PART_STORE = "part_store";
37     private static final String JavaDoc SHIPMENT_STORE = "shipment_store";
38     private static final String JavaDoc SHIPMENT_PART_INDEX = "shipment_part_index";
39     private static final String JavaDoc SHIPMENT_SUPPLIER_INDEX =
40     "shipment_supplier_index";
41     private static final String JavaDoc SUPPLIER_CITY_INDEX = "supplier_city_index";
42
43     private Environment env;
44     private Database partDb;
45     private Database supplierDb;
46     private Database shipmentDb;
47     private SecondaryDatabase supplierByCityDb;
48     private SecondaryDatabase shipmentByPartDb;
49     private SecondaryDatabase shipmentBySupplierDb;
50     private StoredClassCatalog javaCatalog;
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         // Open the Berkeley DB database for the part, supplier and shipment
79
// stores. The stores are opened with no duplicate keys allowed.
80
//
81
partDb = env.openDatabase(null, PART_STORE, dbConfig);
82
83         supplierDb = env.openDatabase(null, SUPPLIER_STORE, dbConfig);
84
85         shipmentDb = env.openDatabase(null, SHIPMENT_STORE, dbConfig);
86         
87         // Open the SecondaryDatabase for the city index of the supplier store,
88
// and for the part and supplier indices of the shipment store.
89
// Duplicate keys are allowed since more than one supplier may be in
90
// the same city, and more than one shipment may exist for the same
91
// supplier or part. A foreign key constraint is defined for the
92
// supplier and part indices to ensure that a shipment only refers to
93
// existing part and supplier keys. The CASCADE delete action means
94
// that shipments will be deleted if their associated part or supplier
95
// is deleted.
96
//
97
SecondaryConfig secConfig = new SecondaryConfig();
98         secConfig.setTransactional(true);
99         secConfig.setAllowCreate(true);
100         secConfig.setSortedDuplicates(true);
101
102         secConfig.setKeyCreator(
103             new SupplierByCityKeyCreator(javaCatalog,
104                                          SupplierKey.class,
105                                          SupplierData.class,
106                                          String JavaDoc.class));
107         supplierByCityDb = env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX,
108                                                      supplierDb, secConfig);
109
110         secConfig.setForeignKeyDatabase(partDb);
111         secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE);
112         secConfig.setKeyCreator(
113             new ShipmentByPartKeyCreator(javaCatalog,
114                                          ShipmentKey.class,
115                                          ShipmentData.class,
116                                          PartKey.class));
117         shipmentByPartDb = env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX,
118                                                      shipmentDb, secConfig);
119
120         secConfig.setForeignKeyDatabase(supplierDb);
121         secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE);
122         secConfig.setKeyCreator(
123             new ShipmentBySupplierKeyCreator(javaCatalog,
124                                              ShipmentKey.class,
125                                              ShipmentData.class,
126                                              SupplierKey.class));
127         shipmentBySupplierDb = env.openSecondaryDatabase(null,
128                                                      SHIPMENT_SUPPLIER_INDEX,
129                                                      shipmentDb, secConfig);
130     }
131
132     /**
133      * Return the storage environment for the database.
134      */

135     public final Environment getEnvironment() {
136
137         return env;
138     }
139
140     /**
141      * Return the class catalog.
142      */

143     public final StoredClassCatalog getClassCatalog() {
144
145         return javaCatalog;
146     }
147
148     /**
149      * Return the part storage container.
150      */

151     public final Database getPartDatabase() {
152
153         return partDb;
154     }
155
156     /**
157      * Return the supplier storage container.
158      */

159     public final Database getSupplierDatabase() {
160
161         return supplierDb;
162     }
163
164     /**
165      * Return the shipment storage container.
166      */

167     public final Database getShipmentDatabase() {
168
169         return shipmentDb;
170     }
171
172     /**
173      * Return the shipment-by-part index.
174      */

175     public final SecondaryDatabase getShipmentByPartDatabase() {
176
177         return shipmentByPartDb;
178     }
179
180     /**
181      * Return the shipment-by-supplier index.
182      */

183     public final SecondaryDatabase getShipmentBySupplierDatabase() {
184
185         return shipmentBySupplierDb;
186     }
187
188     /**
189      * Return the supplier-by-city index.
190      */

191     public final SecondaryDatabase getSupplierByCityDatabase() {
192
193         return supplierByCityDb;
194     }
195
196     /**
197      * Close all stores (closing a store automatically closes its indices).
198      */

199     public void close()
200         throws DatabaseException {
201
202         // Close secondary databases, then primary databases.
203
supplierByCityDb.close();
204         shipmentByPartDb.close();
205         shipmentBySupplierDb.close();
206         partDb.close();
207         supplierDb.close();
208         shipmentDb.close();
209         // And don't forget to close the catalog and the environment.
210
javaCatalog.close();
211         env.close();
212     }
213
214     /**
215      * The SecondaryKeyCreator for the SupplierByCity index. This is an
216      * extension of the abstract class SerialSerialKeyCreator, which implements
217      * SecondaryKeyCreator for the case where the data keys and value are all
218      * of the serial format.
219      */

220     private static class SupplierByCityKeyCreator
221         extends SerialSerialKeyCreator {
222
223         /**
224          * Construct the city key extractor.
225          * @param catalog is the class catalog.
226          * @param primaryKeyClass is the supplier key class.
227          * @param valueClass is the supplier value class.
228          * @param indexKeyClass is the city key class.
229          */

230         private SupplierByCityKeyCreator(ClassCatalog catalog,
231                                          Class JavaDoc primaryKeyClass,
232                                          Class JavaDoc valueClass,
233                                          Class JavaDoc indexKeyClass) {
234
235             super(catalog, primaryKeyClass, valueClass, indexKeyClass);
236         }
237
238         /**
239          * Extract the city key from a supplier key/value pair. The city key
240          * is stored in the supplier value, so the supplier key is not used.
241          */

242         public Object JavaDoc createSecondaryKey(Object JavaDoc primaryKeyInput,
243                                          Object JavaDoc valueInput) {
244
245             SupplierData supplierData = (SupplierData) valueInput;
246             return supplierData.getCity();
247         }
248     }
249
250     /**
251      * The SecondaryKeyCreator for the ShipmentByPart index. This is an
252      * extension of the abstract class SerialSerialKeyCreator, which implements
253      * SecondaryKeyCreator for the case where the data keys and value are all
254      * of the serial format.
255      */

256     private static class ShipmentByPartKeyCreator
257         extends SerialSerialKeyCreator {
258
259         /**
260          * Construct the part key extractor.
261          * @param catalog is the class catalog.
262          * @param primaryKeyClass is the shipment key class.
263          * @param valueClass is the shipment value class.
264          * @param indexKeyClass is the part key class.
265          */

266         private ShipmentByPartKeyCreator(ClassCatalog catalog,
267                                          Class JavaDoc primaryKeyClass,
268                                          Class JavaDoc valueClass,
269                                          Class JavaDoc indexKeyClass) {
270
271             super(catalog, primaryKeyClass, valueClass, indexKeyClass);
272         }
273
274         /**
275          * Extract the part key from a shipment key/value pair. The part key
276          * is stored in the shipment key, so the shipment value is not used.
277          */

278         public Object JavaDoc createSecondaryKey(Object JavaDoc primaryKeyInput,
279                                          Object JavaDoc valueInput) {
280
281             ShipmentKey shipmentKey = (ShipmentKey) primaryKeyInput;
282             return new PartKey(shipmentKey.getPartNumber());
283         }
284     }
285
286     /**
287      * The SecondaryKeyCreator for the ShipmentBySupplier index. This is an
288      * extension of the abstract class SerialSerialKeyCreator, which implements
289      * SecondaryKeyCreator for the case where the data keys and value are all
290      * of the serial format.
291      */

292     private static class ShipmentBySupplierKeyCreator
293         extends SerialSerialKeyCreator {
294
295         /**
296          * Construct the supplier key extractor.
297          * @param catalog is the class catalog.
298          * @param primaryKeyClass is the shipment key class.
299          * @param valueClass is the shipment value class.
300          * @param indexKeyClass is the supplier key class.
301          */

302         private ShipmentBySupplierKeyCreator(ClassCatalog catalog,
303                                              Class JavaDoc primaryKeyClass,
304                                              Class JavaDoc valueClass,
305                                              Class JavaDoc indexKeyClass) {
306
307             super(catalog, primaryKeyClass, valueClass, indexKeyClass);
308         }
309
310         /**
311          * Extract the supplier key from a shipment key/value pair. The part
312          * key is stored in the shipment key, so the shipment value is not
313          * used.
314          */

315         public Object JavaDoc createSecondaryKey(Object JavaDoc primaryKeyInput,
316                                          Object JavaDoc valueInput) {
317
318             ShipmentKey shipmentKey = (ShipmentKey) primaryKeyInput;
319             return new SupplierKey(shipmentKey.getSupplierNumber());
320         }
321     }
322 }
323
Popular Tags