001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: Sample.java,v 1.20.2.2 2008/01/07 15:14:01 cwl Exp $
007: */
008:
009: package collections.ship.index;
010:
011: import java.io.FileNotFoundException;
012: import java.util.Iterator;
013: import java.util.Map;
014:
015: import com.sleepycat.collections.TransactionRunner;
016: import com.sleepycat.collections.TransactionWorker;
017: import com.sleepycat.je.DatabaseException;
018:
019: /**
020: * Sample is the main entry point for the sample program and may be run as
021: * follows:
022: *
023: * <pre>
024: * java collections.ship.index.Sample
025: * [-h <home-directory> ]
026: * </pre>
027: *
028: * <p> The default for the home directory is ./tmp -- the tmp subdirectory of
029: * the current directory where the sample is run. The home directory must exist
030: * before running the sample. To recreate the sample database from scratch,
031: * delete all files in the home directory before running the sample. </p>
032: *
033: * @author Mark Hayes
034: */
035: public class Sample {
036:
037: private SampleDatabase db;
038: private SampleViews views;
039:
040: /**
041: * Run the sample program.
042: */
043: public static void main(String[] args) {
044:
045: System.out.println("\nRunning sample: " + Sample.class);
046:
047: // Parse the command line arguments.
048: //
049: String homeDir = "./tmp";
050: for (int i = 0; i < args.length; i += 1) {
051: if (args[i].equals("-h") && i < args.length - 1) {
052: i += 1;
053: homeDir = args[i];
054: } else {
055: System.err.println("Usage:\n java "
056: + Sample.class.getName()
057: + "\n [-h <home-directory>]");
058: System.exit(2);
059: }
060: }
061:
062: // Run the sample.
063: //
064: Sample sample = null;
065: try {
066: sample = new Sample(homeDir);
067: sample.run();
068: } catch (Exception e) {
069: // If an exception reaches this point, the last transaction did not
070: // complete. If the exception is RunRecoveryException, follow
071: // the Berkeley DB recovery procedures before running again.
072: e.printStackTrace();
073: } finally {
074: if (sample != null) {
075: try {
076: // Always attempt to close the database cleanly.
077: sample.close();
078: } catch (Exception e) {
079: System.err
080: .println("Exception during database close:");
081: e.printStackTrace();
082: }
083: }
084: }
085: }
086:
087: /**
088: * Open the database and views.
089: */
090: private Sample(String homeDir) throws DatabaseException,
091: FileNotFoundException {
092:
093: db = new SampleDatabase(homeDir);
094: views = new SampleViews(db);
095: }
096:
097: /**
098: * Close the database cleanly.
099: */
100: private void close() throws DatabaseException {
101:
102: db.close();
103: }
104:
105: /**
106: * Run two transactions to populate and print the database. A
107: * TransactionRunner is used to ensure consistent handling of transactions,
108: * including deadlock retries. But the best transaction handling mechanism
109: * to use depends on the application.
110: */
111: private void run() throws Exception {
112:
113: TransactionRunner runner = new TransactionRunner(db
114: .getEnvironment());
115: runner.run(new PopulateDatabase());
116: runner.run(new PrintDatabase());
117: }
118:
119: /**
120: * Populate the database in a single transaction.
121: */
122: private class PopulateDatabase implements TransactionWorker {
123:
124: public void doWork() throws Exception {
125: addSuppliers();
126: addParts();
127: addShipments();
128: }
129: }
130:
131: /**
132: * Print the database in a single transaction. All entities are printed
133: * and the indices are used to print the entities for certain keys.
134: *
135: * <p> Note the use of special iterator() methods. These are used here
136: * with indices to find the shipments for certain keys.</p>
137: */
138: private class PrintDatabase implements TransactionWorker {
139:
140: public void doWork() throws Exception {
141: printEntries("Parts", views.getPartEntrySet().iterator());
142: printEntries("Suppliers", views.getSupplierEntrySet()
143: .iterator());
144: printValues("Suppliers for City Paris", views
145: .getSupplierByCityMap().duplicates("Paris")
146: .iterator());
147: printEntries("Shipments", views.getShipmentEntrySet()
148: .iterator());
149: printValues("Shipments for Part P1", views
150: .getShipmentByPartMap().duplicates(
151: new PartKey("P1")).iterator());
152: printValues("Shipments for Supplier S1", views
153: .getShipmentBySupplierMap().duplicates(
154: new SupplierKey("S1")).iterator());
155: }
156: }
157:
158: /**
159: * Populate the part entities in the database. If the part map is not
160: * empty, assume that this has already been done.
161: */
162: private void addParts() {
163:
164: Map parts = views.getPartMap();
165: if (parts.isEmpty()) {
166: System.out.println("Adding Parts");
167: parts.put(new PartKey("P1"), new PartData("Nut", "Red",
168: new Weight(12.0, Weight.GRAMS), "London"));
169: parts.put(new PartKey("P2"), new PartData("Bolt", "Green",
170: new Weight(17.0, Weight.GRAMS), "Paris"));
171: parts.put(new PartKey("P3"), new PartData("Screw", "Blue",
172: new Weight(17.0, Weight.GRAMS), "Rome"));
173: parts.put(new PartKey("P4"), new PartData("Screw", "Red",
174: new Weight(14.0, Weight.GRAMS), "London"));
175: parts.put(new PartKey("P5"), new PartData("Cam", "Blue",
176: new Weight(12.0, Weight.GRAMS), "Paris"));
177: parts.put(new PartKey("P6"), new PartData("Cog", "Red",
178: new Weight(19.0, Weight.GRAMS), "London"));
179: }
180: }
181:
182: /**
183: * Populate the supplier entities in the database. If the supplier map is
184: * not empty, assume that this has already been done.
185: */
186: private void addSuppliers() {
187:
188: Map suppliers = views.getSupplierMap();
189: if (suppliers.isEmpty()) {
190: System.out.println("Adding Suppliers");
191: suppliers.put(new SupplierKey("S1"), new SupplierData(
192: "Smith", 20, "London"));
193: suppliers.put(new SupplierKey("S2"), new SupplierData(
194: "Jones", 10, "Paris"));
195: suppliers.put(new SupplierKey("S3"), new SupplierData(
196: "Blake", 30, "Paris"));
197: suppliers.put(new SupplierKey("S4"), new SupplierData(
198: "Clark", 20, "London"));
199: suppliers.put(new SupplierKey("S5"), new SupplierData(
200: "Adams", 30, "Athens"));
201: }
202: }
203:
204: /**
205: * Populate the shipment entities in the database. If the shipment map
206: * is not empty, assume that this has already been done.
207: */
208: private void addShipments() {
209:
210: Map shipments = views.getShipmentMap();
211: if (shipments.isEmpty()) {
212: System.out.println("Adding Shipments");
213: shipments.put(new ShipmentKey("P1", "S1"),
214: new ShipmentData(300));
215: shipments.put(new ShipmentKey("P2", "S1"),
216: new ShipmentData(200));
217: shipments.put(new ShipmentKey("P3", "S1"),
218: new ShipmentData(400));
219: shipments.put(new ShipmentKey("P4", "S1"),
220: new ShipmentData(200));
221: shipments.put(new ShipmentKey("P5", "S1"),
222: new ShipmentData(100));
223: shipments.put(new ShipmentKey("P6", "S1"),
224: new ShipmentData(100));
225: shipments.put(new ShipmentKey("P1", "S2"),
226: new ShipmentData(300));
227: shipments.put(new ShipmentKey("P2", "S2"),
228: new ShipmentData(400));
229: shipments.put(new ShipmentKey("P2", "S3"),
230: new ShipmentData(200));
231: shipments.put(new ShipmentKey("P2", "S4"),
232: new ShipmentData(200));
233: shipments.put(new ShipmentKey("P4", "S4"),
234: new ShipmentData(300));
235: shipments.put(new ShipmentKey("P5", "S4"),
236: new ShipmentData(400));
237: }
238: }
239:
240: /**
241: * Print the key/value objects returned by an iterator of Map.Entry
242: * objects.
243: */
244: private void printEntries(String label, Iterator iterator) {
245:
246: System.out.println("\n--- " + label + " ---");
247: while (iterator.hasNext()) {
248: Map.Entry entry = (Map.Entry) iterator.next();
249: System.out.println(entry.getKey().toString());
250: System.out.println(entry.getValue().toString());
251: }
252: }
253:
254: /**
255: * Print the objects returned by an iterator of value objects.
256: */
257: private void printValues(String label, Iterator iterator) {
258:
259: System.out.println("\n--- " + label + " ---");
260: while (iterator.hasNext()) {
261: System.out.println(iterator.next().toString());
262: }
263: }
264: }
|