1 4 package je.gettingStarted; 5 6 import java.io.File ; 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 private Database vendorDb; 25 private Database inventoryDb; 26 private Database classCatalogDb; 27 private SecondaryDatabase itemNameIndexDb; 28 29 private StoredClassCatalog classCatalog; 31 32 public MyDbEnv() {} 34 35 public void setup(File envHome, boolean readOnly) 38 throws DatabaseException { 39 40 EnvironmentConfig myEnvConfig = new EnvironmentConfig(); 41 DatabaseConfig myDbConfig = new DatabaseConfig(); 42 SecondaryConfig mySecConfig = new SecondaryConfig(); 43 44 myEnvConfig.setReadOnly(readOnly); 47 myDbConfig.setReadOnly(readOnly); 48 mySecConfig.setReadOnly(readOnly); 49 50 myEnvConfig.setAllowCreate(!readOnly); 54 myDbConfig.setAllowCreate(!readOnly); 55 mySecConfig.setAllowCreate(!readOnly); 56 57 myEnvConfig.setTransactional(!readOnly); 59 myDbConfig.setTransactional(!readOnly); 60 mySecConfig.setTransactional(!readOnly); 61 62 myEnv = new Environment(envHome, myEnvConfig); 64 65 vendorDb = myEnv.openDatabase(null, 68 "VendorDB", 69 myDbConfig); 70 71 inventoryDb = myEnv.openDatabase(null, 72 "InventoryDB", 73 myDbConfig); 74 75 classCatalogDb = 78 myEnv.openDatabase(null, 79 "ClassCatalogDB", 80 myDbConfig); 81 82 classCatalog = new StoredClassCatalog(classCatalogDb); 84 85 TupleBinding inventoryBinding = new InventoryBinding(); 89 90 93 ItemNameKeyCreator keyCreator = 97 new ItemNameKeyCreator(new InventoryBinding()); 98 99 100 mySecConfig.setSortedDuplicates(true); 103 mySecConfig.setAllowPopulate(true); mySecConfig.setKeyCreator(keyCreator); 105 106 itemNameIndexDb = 108 myEnv.openSecondaryDatabase( 109 null, 110 "itemNameIndex", inventoryDb, mySecConfig); } 114 115 117 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 public void close() { 140 if (myEnv != null) { 141 try { 142 itemNameIndexDb.close(); 144 vendorDb.close(); 145 inventoryDb.close(); 146 classCatalogDb.close(); 147 148 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 |