KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > collections > ship > marshal > 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:01 bostic Exp $
7  */

8
9 package collections.ship.marshal;
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 MarshalledKeyCreator(javaCatalog,
105                                                          Supplier.class,
106                                                          Supplier.CITY_KEY));
107         supplierByCityDb = env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX,
108                                                      supplierDb, secConfig);
109
110         secConfig.setForeignKeyDatabase(partDb);
111         secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE);
112         secConfig.setKeyCreator(new MarshalledKeyCreator(javaCatalog,
113                                                          Shipment.class,
114                                                          Shipment.PART_KEY));
115         shipmentByPartDb = env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX,
116                                                      shipmentDb, secConfig);
117
118         secConfig.setForeignKeyDatabase(supplierDb);
119         secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE);
120         secConfig.setKeyCreator(new MarshalledKeyCreator(javaCatalog,
121                                                          Shipment.class,
122                                                      Shipment.SUPPLIER_KEY));
123         shipmentBySupplierDb = env.openSecondaryDatabase(null,
124                                                      SHIPMENT_SUPPLIER_INDEX,
125                                                      shipmentDb, secConfig);
126     }
127
128     /**
129      * Return the storage environment for the database.
130      */

131     public final Environment getEnvironment() {
132
133         return env;
134     }
135
136     /**
137      * Return the class catalog.
138      */

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

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

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

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

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

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

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

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

216     private static class MarshalledKeyCreator
217         extends TupleSerialKeyCreator {
218
219         private String JavaDoc keyName;
220
221         /**
222          * Construct the key creator.
223          * @param catalog is the class catalog.
224          * @param valueClass is the supplier value class.
225          * @param keyName is the key name passed to the marshalling methods.
226          */

227         private MarshalledKeyCreator(ClassCatalog catalog,
228                                      Class JavaDoc valueClass,
229                                      String JavaDoc keyName) {
230
231             super(catalog, valueClass);
232             this.keyName = keyName;
233         }
234
235         /**
236          * Extract the city key from a supplier key/value pair. The city key
237          * is stored in the supplier value, so the supplier key is not used.
238          */

239         public boolean createSecondaryKey(TupleInput primaryKeyInput,
240                                           Object JavaDoc valueInput,
241                                           TupleOutput indexKeyOutput) {
242
243             // the primary key is unmarshalled before marshalling the index
244
// key, to account for cases where the index key is composed of
245
// data elements from the primary key
246
MarshalledEntity entity = (MarshalledEntity) valueInput;
247             entity.unmarshalPrimaryKey(primaryKeyInput);
248             return entity.marshalSecondaryKey(keyName, indexKeyOutput);
249         }
250     }
251 }
252
Popular Tags