KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > je > gettingStarted > MyDbEnv


1 // file MyDbEnv.java
2
// $Id: MyDbEnv.java,v 1.6 2005/06/09 17:20:54 mark Exp $
3

4 package je.gettingStarted;
5
6 import java.io.File JavaDoc;
7
8 import com.sleepycat.bind.serial.StoredClassCatalog;
9 import com.sleepycat.bind.tuple.TupleBinding;
10 import com.sleepycat.je.Database;
11 import com.sleepycat.je.DatabaseConfig;
12 import com.sleepycat.je.DatabaseException;
13 import com.sleepycat.je.Environment;
14 import com.sleepycat.je.EnvironmentConfig;
15 import com.sleepycat.je.SecondaryConfig;
16 import com.sleepycat.je.SecondaryDatabase;
17
18
19 public class MyDbEnv {
20
21     private Environment myEnv;
22
23     // The databases that our application uses
24
private Database vendorDb;
25     private Database inventoryDb;
26     private Database classCatalogDb;
27     private SecondaryDatabase itemNameIndexDb;
28
29     // Needed for object serialization
30
private StoredClassCatalog classCatalog;
31
32     // Our constructor does nothing
33
public MyDbEnv() {}
34
35     // The setup() method opens all our databases and the environment
36
// for us.
37
public void setup(File JavaDoc envHome, boolean readOnly)
38         throws DatabaseException {
39
40         EnvironmentConfig myEnvConfig = new EnvironmentConfig();
41         DatabaseConfig myDbConfig = new DatabaseConfig();
42         SecondaryConfig mySecConfig = new SecondaryConfig();
43
44         // If the environment is read-only, then
45
// make the databases read-only too.
46
myEnvConfig.setReadOnly(readOnly);
47         myDbConfig.setReadOnly(readOnly);
48         mySecConfig.setReadOnly(readOnly);
49
50         // If the environment is opened for write, then we want to be
51
// able to create the environment and databases if
52
// they do not exist.
53
myEnvConfig.setAllowCreate(!readOnly);
54         myDbConfig.setAllowCreate(!readOnly);
55         mySecConfig.setAllowCreate(!readOnly);
56
57         // Allow transactions if we are writing to the database
58
myEnvConfig.setTransactional(!readOnly);
59         myDbConfig.setTransactional(!readOnly);
60         mySecConfig.setTransactional(!readOnly);
61
62         // Open the environment
63
myEnv = new Environment(envHome, myEnvConfig);
64
65         // Now open, or create and open, our databases
66
// Open the vendors and inventory databases
67
vendorDb = myEnv.openDatabase(null,
68                                       "VendorDB",
69                                        myDbConfig);
70
71         inventoryDb = myEnv.openDatabase(null,
72                                         "InventoryDB",
73                                          myDbConfig);
74         
75         // Open the class catalog db. This is used to
76
// optimize class serialization.
77
classCatalogDb =
78             myEnv.openDatabase(null,
79                                "ClassCatalogDB",
80                                myDbConfig);
81
82         // Create our class catalog
83
classCatalog = new StoredClassCatalog(classCatalogDb);
84
85         // Need a tuple binding for the Inventory class.
86
// We use the InventoryBinding class
87
// that we implemented for this purpose.
88
TupleBinding inventoryBinding = new InventoryBinding();
89                                                                                                                                   
90         // Open the secondary database. We use this to create a
91
// secondary index for the inventory database
92

93         // We want to maintain an index for the inventory entries based
94
// on the item name. So, instantiate the appropriate key creator
95
// and open a secondary database.
96
ItemNameKeyCreator keyCreator =
97             new ItemNameKeyCreator(new InventoryBinding());
98
99        
100         // Set up additional secondary properties
101
// Need to allow duplicates for our secondary database
102
mySecConfig.setSortedDuplicates(true);
103         mySecConfig.setAllowPopulate(true); // Allow autopopulate
104
mySecConfig.setKeyCreator(keyCreator);
105
106         // Now open it
107
itemNameIndexDb =
108             myEnv.openSecondaryDatabase(
109                     null,
110                     "itemNameIndex", // index name
111
inventoryDb, // the primary db that we're indexing
112
mySecConfig); // the secondary config
113
}
114
115    // getter methods
116

117     // Needed for things like beginning transactions
118
public Environment getEnv() {
119         return myEnv;
120     }
121         
122     public Database getVendorDB() {
123         return vendorDb;
124     }
125
126     public Database getInventoryDB() {
127         return inventoryDb;
128     }
129
130     public SecondaryDatabase getNameIndexDB() {
131         return itemNameIndexDb;
132     }
133
134     public StoredClassCatalog getClassCatalog() {
135         return classCatalog;
136     }
137
138     //Close the environment
139
public void close() {
140         if (myEnv != null) {
141             try {
142                 //Close the secondary before closing the primaries
143
itemNameIndexDb.close();
144                 vendorDb.close();
145                 inventoryDb.close();
146                 classCatalogDb.close();
147                
148                 // Finally, close the environment.
149
myEnv.close();
150             } catch(DatabaseException dbe) {
151                 System.err.println("Error closing MyDbEnv: " +
152                                     dbe.toString());
153                System.exit(-1);
154             }
155         }
156     }
157 }
158
159
Popular Tags