KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > collections > ship > tuple > 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.24 2006/10/30 21:14:03 bostic Exp $
7  */

8
9 package collections.ship.tuple;
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.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 /**
29  * SampleDatabase defines the storage containers, indices and foreign keys
30  * for the sample database.
31  *
32  * @author Mark Hayes
33  */

34 public class SampleDatabase {
35
36     private static final String JavaDoc CLASS_CATALOG = "java_class_catalog";
37     private static final String JavaDoc SUPPLIER_STORE = "supplier_store";
38     private static final String JavaDoc PART_STORE = "part_store";
39     private static final String JavaDoc SHIPMENT_STORE = "shipment_store";
40     private static final String JavaDoc SHIPMENT_PART_INDEX = "shipment_part_index";
41     private static final String JavaDoc SHIPMENT_SUPPLIER_INDEX =
42     "shipment_supplier_index";
43     private static final String JavaDoc 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     /**
55      * Open all storage containers, indices, and catalogs.
56      */

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

128     public final Environment getEnvironment() {
129
130         return env;
131     }
132
133     /**
134      * Return the class catalog.
135      */

136     public final StoredClassCatalog getClassCatalog() {
137
138         return javaCatalog;
139     }
140
141     /**
142      * Return the part storage container.
143      */

144     public final Database getPartDatabase() {
145
146         return partDb;
147     }
148
149     /**
150      * Return the supplier storage container.
151      */

152     public final Database getSupplierDatabase() {
153
154         return supplierDb;
155     }
156
157     /**
158      * Return the shipment storage container.
159      */

160     public final Database getShipmentDatabase() {
161
162         return shipmentDb;
163     }
164
165     /**
166      * Return the shipment-by-part index.
167      */

168     public final SecondaryDatabase getShipmentByPartDatabase() {
169
170         return shipmentByPartDb;
171     }
172
173     /**
174      * Return the shipment-by-supplier index.
175      */

176     public final SecondaryDatabase getShipmentBySupplierDatabase() {
177
178         return shipmentBySupplierDb;
179     }
180
181     /**
182      * Return the supplier-by-city index.
183      */

184     public final SecondaryDatabase getSupplierByCityDatabase() {
185
186         return supplierByCityDb;
187     }
188
189     /**
190      * Close all stores (closing a store automatically closes its indices).
191      */

192     public void close()
193         throws DatabaseException {
194
195         // Close secondary databases, then primary databases.
196
supplierByCityDb.close();
197         shipmentByPartDb.close();
198         shipmentBySupplierDb.close();
199         partDb.close();
200         supplierDb.close();
201         shipmentDb.close();
202         // And don't forget to close the catalog and the environment.
203
javaCatalog.close();
204         env.close();
205     }
206
207     /**
208      * The SecondaryKeyCreator for the SupplierByCity index. This is an
209      * extension of the abstract class TupleSerialKeyCreator, which implements
210      * SecondaryKeyCreator for the case where the data keys are of the format
211      * TupleFormat and the data values are of the format SerialFormat.
212      */

213     private static class SupplierByCityKeyCreator
214         extends TupleSerialKeyCreator {
215
216         /**
217          * Construct the city key extractor.
218          * @param catalog is the class catalog.
219          * @param valueClass is the supplier value class.
220          */

221         private SupplierByCityKeyCreator(ClassCatalog catalog,
222                                          Class JavaDoc valueClass) {
223
224             super(catalog, valueClass);
225         }
226
227         /**
228          * Extract the city key from a supplier key/value pair. The city key
229          * is stored in the supplier value, so the supplier key is not used.
230          */

231         public boolean createSecondaryKey(TupleInput primaryKeyInput,
232                                           Object JavaDoc valueInput,
233                                           TupleOutput indexKeyOutput) {
234
235             SupplierData supplierData = (SupplierData) valueInput;
236             String JavaDoc city = supplierData.getCity();
237             if (city != null) {
238                 indexKeyOutput.writeString(supplierData.getCity());
239                 return true;
240             } else {
241                 return false;
242             }
243         }
244     }
245
246     /**
247      * The SecondaryKeyCreator for the ShipmentByPart index. This is an
248      * extension of the abstract class TupleSerialKeyCreator, which implements
249      * SecondaryKeyCreator for the case where the data keys are of the format
250      * TupleFormat and the data values are of the format SerialFormat.
251      */

252     private static class ShipmentByPartKeyCreator
253         extends TupleSerialKeyCreator {
254
255         /**
256          * Construct the part key extractor.
257          * @param catalog is the class catalog.
258          * @param valueClass is the shipment value class.
259          */

260         private ShipmentByPartKeyCreator(ClassCatalog catalog,
261                                          Class JavaDoc valueClass) {
262             super(catalog, valueClass);
263         }
264
265         /**
266          * Extract the part key from a shipment key/value pair. The part key
267          * is stored in the shipment key, so the shipment value is not used.
268          */

269         public boolean createSecondaryKey(TupleInput primaryKeyInput,
270                                           Object JavaDoc valueInput,
271                                           TupleOutput indexKeyOutput) {
272
273             String JavaDoc partNumber = primaryKeyInput.readString();
274             // don't bother reading the supplierNumber
275
indexKeyOutput.writeString(partNumber);
276             return true;
277         }
278     }
279
280     /**
281      * The SecondaryKeyCreator for the ShipmentBySupplier index. This is an
282      * extension of the abstract class TupleSerialKeyCreator, which implements
283      * SecondaryKeyCreator for the case where the data keys are of the format
284      * TupleFormat and the data values are of the format SerialFormat.
285      */

286     private static class ShipmentBySupplierKeyCreator
287         extends TupleSerialKeyCreator {
288
289         /**
290          * Construct the supplier key extractor.
291          * @param catalog is the class catalog.
292          * @param valueClass is the shipment value class.
293          */

294         private ShipmentBySupplierKeyCreator(ClassCatalog catalog,
295                                              Class JavaDoc valueClass) {
296             super(catalog, valueClass);
297         }
298
299         /**
300          * Extract the supplier key from a shipment key/value pair. The
301          * supplier key is stored in the shipment key, so the shipment value is
302          * not used.
303          */

304         public boolean createSecondaryKey(TupleInput primaryKeyInput,
305                                           Object JavaDoc valueInput,
306                                           TupleOutput indexKeyOutput) {
307
308             primaryKeyInput.readString(); // skip the partNumber
309
String JavaDoc supplierNumber = primaryKeyInput.readString();
310             indexKeyOutput.writeString(supplierNumber);
311             return true;
312         }
313     }
314 }
315
Popular Tags