PageRenderTime 28ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/SN-NG4.1/db4/examples_java/src/collections/ship/tuple/SampleDatabase.java

https://gitlab.com/OpenSourceMirror/sourcenav
Java | 321 lines | 161 code | 48 blank | 112 comment | 3 complexity | 1e6f96c24919d6b5f2a38a94cea1c22d MD5 | raw file
  1. /*-
  2. * See the file LICENSE for redistribution information.
  3. *
  4. * Copyright (c) 2002,2007 Oracle. All rights reserved.
  5. *
  6. * $Id: SampleDatabase.java,v 12.6 2007/05/17 15:15:36 bostic Exp $
  7. */
  8. package collections.ship.tuple;
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import com.sleepycat.bind.serial.ClassCatalog;
  12. import com.sleepycat.bind.serial.StoredClassCatalog;
  13. import com.sleepycat.bind.serial.TupleSerialKeyCreator;
  14. import com.sleepycat.bind.tuple.TupleInput;
  15. import com.sleepycat.bind.tuple.TupleOutput;
  16. import com.sleepycat.db.Database;
  17. import com.sleepycat.db.DatabaseConfig;
  18. import com.sleepycat.db.DatabaseException;
  19. import com.sleepycat.db.DatabaseType;
  20. import com.sleepycat.db.Environment;
  21. import com.sleepycat.db.EnvironmentConfig;
  22. import com.sleepycat.db.SecondaryConfig;
  23. import com.sleepycat.db.SecondaryDatabase;
  24. /**
  25. * SampleDatabase defines the storage containers, indices and foreign keys
  26. * for the sample database.
  27. *
  28. * @author Mark Hayes
  29. */
  30. public class SampleDatabase {
  31. private static final String CLASS_CATALOG = "java_class_catalog";
  32. private static final String SUPPLIER_STORE = "supplier_store";
  33. private static final String PART_STORE = "part_store";
  34. private static final String SHIPMENT_STORE = "shipment_store";
  35. private static final String SHIPMENT_PART_INDEX = "shipment_part_index";
  36. private static final String SHIPMENT_SUPPLIER_INDEX =
  37. "shipment_supplier_index";
  38. private static final String SUPPLIER_CITY_INDEX = "supplier_city_index";
  39. private Environment env;
  40. private Database partDb;
  41. private Database supplierDb;
  42. private Database shipmentDb;
  43. private SecondaryDatabase supplierByCityDb;
  44. private SecondaryDatabase shipmentByPartDb;
  45. private SecondaryDatabase shipmentBySupplierDb;
  46. private StoredClassCatalog javaCatalog;
  47. /**
  48. * Open all storage containers, indices, and catalogs.
  49. */
  50. public SampleDatabase(String homeDirectory)
  51. throws DatabaseException, FileNotFoundException {
  52. // Open the Berkeley DB environment in transactional mode.
  53. //
  54. System.out.println("Opening environment in: " + homeDirectory);
  55. EnvironmentConfig envConfig = new EnvironmentConfig();
  56. envConfig.setTransactional(true);
  57. envConfig.setAllowCreate(true);
  58. envConfig.setInitializeCache(true);
  59. envConfig.setInitializeLocking(true);
  60. env = new Environment(new File(homeDirectory), envConfig);
  61. // Set the Berkeley DB config for opening all stores.
  62. //
  63. DatabaseConfig dbConfig = new DatabaseConfig();
  64. dbConfig.setTransactional(true);
  65. dbConfig.setAllowCreate(true);
  66. dbConfig.setType(DatabaseType.BTREE);
  67. // Create the Serial class catalog. This holds the serialized class
  68. // format for all database records of serial format.
  69. //
  70. Database catalogDb = env.openDatabase(null, CLASS_CATALOG, null,
  71. dbConfig);
  72. javaCatalog = new StoredClassCatalog(catalogDb);
  73. // Open the Berkeley DB database for the part, supplier and shipment
  74. // stores. The stores are opened with no duplicate keys allowed.
  75. //
  76. partDb = env.openDatabase(null, PART_STORE, null, dbConfig);
  77. supplierDb = env.openDatabase(null, SUPPLIER_STORE, null, dbConfig);
  78. shipmentDb = env.openDatabase(null, SHIPMENT_STORE, null, dbConfig);
  79. // Open the SecondaryDatabase for the city index of the supplier store,
  80. // and for the part and supplier indices of the shipment store.
  81. // Duplicate keys are allowed since more than one supplier may be in
  82. // the same city, and more than one shipment may exist for the same
  83. // supplier or part. A foreign key constraint is defined for the
  84. // supplier and part indices to ensure that a shipment only refers to
  85. // existing part and supplier keys. The CASCADE delete action means
  86. // that shipments will be deleted if their associated part or supplier
  87. // is deleted.
  88. //
  89. SecondaryConfig secConfig = new SecondaryConfig();
  90. secConfig.setTransactional(true);
  91. secConfig.setAllowCreate(true);
  92. secConfig.setType(DatabaseType.BTREE);
  93. secConfig.setSortedDuplicates(true);
  94. secConfig.setKeyCreator(new SupplierByCityKeyCreator(javaCatalog,
  95. SupplierData.class));
  96. supplierByCityDb = env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX,
  97. null,
  98. supplierDb,
  99. secConfig);
  100. secConfig.setKeyCreator(new ShipmentByPartKeyCreator(javaCatalog,
  101. ShipmentData.class));
  102. shipmentByPartDb = env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX,
  103. null,
  104. shipmentDb,
  105. secConfig);
  106. secConfig.setKeyCreator(new ShipmentBySupplierKeyCreator(javaCatalog,
  107. ShipmentData.class));
  108. shipmentBySupplierDb = env.openSecondaryDatabase(null,
  109. SHIPMENT_SUPPLIER_INDEX,
  110. null,
  111. shipmentDb,
  112. secConfig);
  113. }
  114. /**
  115. * Return the storage environment for the database.
  116. */
  117. public final Environment getEnvironment() {
  118. return env;
  119. }
  120. /**
  121. * Return the class catalog.
  122. */
  123. public final StoredClassCatalog getClassCatalog() {
  124. return javaCatalog;
  125. }
  126. /**
  127. * Return the part storage container.
  128. */
  129. public final Database getPartDatabase() {
  130. return partDb;
  131. }
  132. /**
  133. * Return the supplier storage container.
  134. */
  135. public final Database getSupplierDatabase() {
  136. return supplierDb;
  137. }
  138. /**
  139. * Return the shipment storage container.
  140. */
  141. public final Database getShipmentDatabase() {
  142. return shipmentDb;
  143. }
  144. /**
  145. * Return the shipment-by-part index.
  146. */
  147. public final SecondaryDatabase getShipmentByPartDatabase() {
  148. return shipmentByPartDb;
  149. }
  150. /**
  151. * Return the shipment-by-supplier index.
  152. */
  153. public final SecondaryDatabase getShipmentBySupplierDatabase() {
  154. return shipmentBySupplierDb;
  155. }
  156. /**
  157. * Return the supplier-by-city index.
  158. */
  159. public final SecondaryDatabase getSupplierByCityDatabase() {
  160. return supplierByCityDb;
  161. }
  162. /**
  163. * Close all stores (closing a store automatically closes its indices).
  164. */
  165. public void close()
  166. throws DatabaseException {
  167. // Close secondary databases, then primary databases.
  168. supplierByCityDb.close();
  169. shipmentByPartDb.close();
  170. shipmentBySupplierDb.close();
  171. partDb.close();
  172. supplierDb.close();
  173. shipmentDb.close();
  174. // And don't forget to close the catalog and the environment.
  175. javaCatalog.close();
  176. env.close();
  177. }
  178. /**
  179. * The SecondaryKeyCreator for the SupplierByCity index. This is an
  180. * extension of the abstract class TupleSerialKeyCreator, which implements
  181. * SecondaryKeyCreator for the case where the data keys are of the format
  182. * TupleFormat and the data values are of the format SerialFormat.
  183. */
  184. private static class SupplierByCityKeyCreator
  185. extends TupleSerialKeyCreator {
  186. /**
  187. * Construct the city key extractor.
  188. * @param catalog is the class catalog.
  189. * @param valueClass is the supplier value class.
  190. */
  191. private SupplierByCityKeyCreator(ClassCatalog catalog,
  192. Class valueClass) {
  193. super(catalog, valueClass);
  194. }
  195. /**
  196. * Extract the city key from a supplier key/value pair. The city key
  197. * is stored in the supplier value, so the supplier key is not used.
  198. */
  199. public boolean createSecondaryKey(TupleInput primaryKeyInput,
  200. Object valueInput,
  201. TupleOutput indexKeyOutput) {
  202. SupplierData supplierData = (SupplierData) valueInput;
  203. String city = supplierData.getCity();
  204. if (city != null) {
  205. indexKeyOutput.writeString(supplierData.getCity());
  206. return true;
  207. } else {
  208. return false;
  209. }
  210. }
  211. }
  212. /**
  213. * The SecondaryKeyCreator for the ShipmentByPart index. This is an
  214. * extension of the abstract class TupleSerialKeyCreator, which implements
  215. * SecondaryKeyCreator for the case where the data keys are of the format
  216. * TupleFormat and the data values are of the format SerialFormat.
  217. */
  218. private static class ShipmentByPartKeyCreator
  219. extends TupleSerialKeyCreator {
  220. /**
  221. * Construct the part key extractor.
  222. * @param catalog is the class catalog.
  223. * @param valueClass is the shipment value class.
  224. */
  225. private ShipmentByPartKeyCreator(ClassCatalog catalog,
  226. Class valueClass) {
  227. super(catalog, valueClass);
  228. }
  229. /**
  230. * Extract the part key from a shipment key/value pair. The part key
  231. * is stored in the shipment key, so the shipment value is not used.
  232. */
  233. public boolean createSecondaryKey(TupleInput primaryKeyInput,
  234. Object valueInput,
  235. TupleOutput indexKeyOutput) {
  236. String partNumber = primaryKeyInput.readString();
  237. // don't bother reading the supplierNumber
  238. indexKeyOutput.writeString(partNumber);
  239. return true;
  240. }
  241. }
  242. /**
  243. * The SecondaryKeyCreator for the ShipmentBySupplier index. This is an
  244. * extension of the abstract class TupleSerialKeyCreator, which implements
  245. * SecondaryKeyCreator for the case where the data keys are of the format
  246. * TupleFormat and the data values are of the format SerialFormat.
  247. */
  248. private static class ShipmentBySupplierKeyCreator
  249. extends TupleSerialKeyCreator {
  250. /**
  251. * Construct the supplier key extractor.
  252. * @param catalog is the class catalog.
  253. * @param valueClass is the shipment value class.
  254. */
  255. private ShipmentBySupplierKeyCreator(ClassCatalog catalog,
  256. Class valueClass) {
  257. super(catalog, valueClass);
  258. }
  259. /**
  260. * Extract the supplier key from a shipment key/value pair. The
  261. * supplier key is stored in the shipment key, so the shipment value is
  262. * not used.
  263. */
  264. public boolean createSecondaryKey(TupleInput primaryKeyInput,
  265. Object valueInput,
  266. TupleOutput indexKeyOutput) {
  267. primaryKeyInput.readString(); // skip the partNumber
  268. String supplierNumber = primaryKeyInput.readString();
  269. indexKeyOutput.writeString(supplierNumber);
  270. return true;
  271. }
  272. }
  273. }