• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/router/db-4.8.30/examples_java/src/db/GettingStarted/
1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2004-2009 Oracle.  All rights reserved.
5 *
6 * $Id$
7 */
8
9// File: MyDbs.java
10
11package db.GettingStarted;
12
13import java.io.FileNotFoundException;
14
15import com.sleepycat.bind.serial.StoredClassCatalog;
16import com.sleepycat.bind.tuple.TupleBinding;
17import com.sleepycat.db.Database;
18import com.sleepycat.db.DatabaseConfig;
19import com.sleepycat.db.DatabaseException;
20import com.sleepycat.db.DatabaseType;
21import com.sleepycat.db.SecondaryConfig;
22import com.sleepycat.db.SecondaryDatabase;
23
24
25public class MyDbs {
26
27    // The databases that our application uses
28    private Database vendorDb = null;
29    private Database inventoryDb = null;
30    private Database classCatalogDb = null;
31    private SecondaryDatabase itemNameIndexDb = null;
32
33    private String vendordb = "VendorDB.db";
34    private String inventorydb = "InventoryDB.db";
35    private String classcatalogdb = "ClassCatalogDB.db";
36    private String itemnameindexdb = "ItemNameIndexDB.db";
37
38    // Needed for object serialization
39    private StoredClassCatalog classCatalog;
40
41    // Our constructor does nothing
42    public MyDbs() {}
43
44    // The setup() method opens all our databases
45    // for us.
46    public void setup(String databasesHome)
47        throws DatabaseException {
48
49        DatabaseConfig myDbConfig = new DatabaseConfig();
50        SecondaryConfig mySecConfig = new SecondaryConfig();
51
52        myDbConfig.setErrorStream(System.err);
53        mySecConfig.setErrorStream(System.err);
54        myDbConfig.setErrorPrefix("MyDbs");
55        mySecConfig.setErrorPrefix("MyDbs");
56        myDbConfig.setType(DatabaseType.BTREE);
57        mySecConfig.setType(DatabaseType.BTREE);
58        myDbConfig.setAllowCreate(true);
59        mySecConfig.setAllowCreate(true);
60
61        // Now open, or create and open, our databases
62        // Open the vendors and inventory databases
63        try {
64            vendordb = databasesHome + "/" + vendordb;
65            vendorDb = new Database(vendordb,
66                                    null,
67                                    myDbConfig);
68
69            inventorydb = databasesHome + "/" + inventorydb;
70            inventoryDb = new Database(inventorydb,
71                                        null,
72                                        myDbConfig);
73
74            // Open the class catalog db. This is used to
75            // optimize class serialization.
76            classcatalogdb = databasesHome + "/" + classcatalogdb;
77            classCatalogDb = new Database(classcatalogdb,
78                                          null,
79                                          myDbConfig);
80        } catch(FileNotFoundException fnfe) {
81            System.err.println("MyDbs: " + fnfe.toString());
82            System.exit(-1);
83        }
84
85        // Create our class catalog
86        classCatalog = new StoredClassCatalog(classCatalogDb);
87
88        // Need a tuple binding for the Inventory class.
89        // We use the InventoryBinding class
90        // that we implemented for this purpose.
91        TupleBinding inventoryBinding = new InventoryBinding();
92
93        // Open the secondary database. We use this to create a
94        // secondary index for the inventory database
95
96        // We want to maintain an index for the inventory entries based
97        // on the item name. So, instantiate the appropriate key creator
98        // and open a secondary database.
99        ItemNameKeyCreator keyCreator =
100            new ItemNameKeyCreator(new InventoryBinding());
101
102
103        // Set up additional secondary properties
104        // Need to allow duplicates for our secondary database
105        mySecConfig.setSortedDuplicates(true);
106        mySecConfig.setAllowPopulate(true); // Allow autopopulate
107        mySecConfig.setKeyCreator(keyCreator);
108
109        // Now open it
110        try {
111            itemnameindexdb = databasesHome + "/" + itemnameindexdb;
112            itemNameIndexDb = new SecondaryDatabase(itemnameindexdb,
113                                                    null,
114                                                    inventoryDb,
115                                                    mySecConfig);
116        } catch(FileNotFoundException fnfe) {
117            System.err.println("MyDbs: " + fnfe.toString());
118            System.exit(-1);
119        }
120    }
121
122   // getter methods
123    public Database getVendorDB() {
124        return vendorDb;
125    }
126
127    public Database getInventoryDB() {
128        return inventoryDb;
129    }
130
131    public SecondaryDatabase getNameIndexDB() {
132        return itemNameIndexDb;
133    }
134
135    public StoredClassCatalog getClassCatalog() {
136        return classCatalog;
137    }
138
139    // Close the databases
140    public void close() {
141        try {
142            if (itemNameIndexDb != null) {
143                itemNameIndexDb.close();
144            }
145
146            if (vendorDb != null) {
147                vendorDb.close();
148            }
149
150            if (inventoryDb != null) {
151                inventoryDb.close();
152            }
153
154            if (classCatalogDb != null) {
155                classCatalogDb.close();
156            }
157
158        } catch(DatabaseException dbe) {
159            System.err.println("Error closing MyDbs: " +
160                                dbe.toString());
161            System.exit(-1);
162        }
163    }
164}
165
166