1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002-2009 Oracle. All rights reserved.
*
* $Id$
*/
package collections.ship.basic;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.Map;
import com.sleepycat.collections.TransactionRunner;
import com.sleepycat.collections.TransactionWorker;
import com.sleepycat.db.DatabaseException;
/**
* Sample is the main entry point for the sample program and may be run as
* follows:
*
* <pre>
* java collections.ship.basic.Sample
* [-h <home-directory> ]
* </pre>
*
* <p> The default for the home directory is ./tmp -- the tmp subdirectory of
* the current directory where the sample is run. The home directory must exist
* before running the sample. To recreate the sample database from scratch,
* delete all files in the home directory before running the sample. </p>
*
* @author Mark Hayes
*/
public class Sample {
private SampleDatabase db;
private SampleViews views;
/**
* Run the sample program.
*/
public static void main(String[] args) {
System.out.println("\nRunning sample: " + Sample.class);
// Parse the command line arguments.
//
String homeDir = "./tmp";
for (int i = 0; i < args.length; i += 1) {
if (args[i].equals("-h") && i < args.length - 1) {
i += 1;
homeDir = args[i];
} else {
System.err.println("Usage:\n java " + Sample.class.getName() +
"\n [-h <home-directory>]");
System.exit(2);
}
}
// Run the sample.
//
Sample sample = null;
try {
sample = new Sample(homeDir);
sample.run();
} catch (Exception e) {
// If an exception reaches this point, the last transaction did not
// complete. If the exception is RunRecoveryException, follow
// the Berkeley DB recovery procedures before running again.
e.printStackTrace();
} finally {
if (sample != null) {
try {
// Always attempt to close the database cleanly.
sample.close();
} catch (Exception e) {
System.err.println("Exception during database close:");
e.printStackTrace();
}
}
}
}
/**
* Open the database and views.
*/
private Sample(String homeDir)
throws DatabaseException, FileNotFoundException {
db = new SampleDatabase(homeDir);
views = new SampleViews(db);
}
/**
* Close the database cleanly.
*/
private void close()
throws DatabaseException {
db.close();
}
/**
* Run two transactions to populate and print the database. A
* TransactionRunner is used to ensure consistent handling of transactions,
* including deadlock retries. But the best transaction handling mechanism
* to use depends on the application.
*/
private void run()
throws Exception {
TransactionRunner runner = new TransactionRunner(db.getEnvironment());
runner.run(new PopulateDatabase());
runner.run(new PrintDatabase());
}
/**
* Populate the database in a single transaction.
*/
private class PopulateDatabase implements TransactionWorker {
public void doWork()
throws Exception {
addSuppliers();
addParts();
addShipments();
}
}
/**
* Print the database in a single transaction. All entities are printed.
*/
private class PrintDatabase implements TransactionWorker {
public void doWork()
throws Exception {
printEntries("Parts",
views.getPartEntrySet().iterator());
printEntries("Suppliers",
views.getSupplierEntrySet().iterator());
printEntries("Shipments",
views.getShipmentEntrySet().iterator());
}
}
/**
* Populate the part entities in the database. If the part map is not
* empty, assume that this has already been done.
*/
private void addParts() {
Map parts = views.getPartMap();
if (parts.isEmpty()) {
System.out.println("Adding Parts");
parts.put(new PartKey("P1"),
new PartData("Nut", "Red",
new Weight(12.0, Weight.GRAMS),
"London"));
parts.put(new PartKey("P2"),
new PartData("Bolt", "Green",
new Weight(17.0, Weight.GRAMS),
"Paris"));
parts.put(new PartKey("P3"),
new PartData("Screw", "Blue",
new Weight(17.0, Weight.GRAMS),
"Rome"));
parts.put(new PartKey("P4"),
new PartData("Screw", "Red",
new Weight(14.0, Weight.GRAMS),
"London"));
parts.put(new PartKey("P5"),
new PartData("Cam", "Blue",
new Weight(12.0, Weight.GRAMS),
"Paris"));
parts.put(new PartKey("P6"),
new PartData("Cog", "Red",
new Weight(19.0, Weight.GRAMS),
"London"));
}
}
/**
* Populate the supplier entities in the database. If the supplier map is
* not empty, assume that this has already been done.
*/
private void addSuppliers() {
Map suppliers = views.getSupplierMap();
if (suppliers.isEmpty()) {
System.out.println("Adding Suppliers");
suppliers.put(new SupplierKey("S1"),
new SupplierData("Smith", 20, "London"));
suppliers.put(new SupplierKey("S2"),
new SupplierData("Jones", 10, "Paris"));
suppliers.put(new SupplierKey("S3"),
new SupplierData("Blake", 30, "Paris"));
suppliers.put(new SupplierKey("S4"),
new SupplierData("Clark", 20, "London"));
suppliers.put(new SupplierKey("S5"),
new SupplierData("Adams", 30, "Athens"));
}
}
/**
* Populate the shipment entities in the database. If the shipment map
* is not empty, assume that this has already been done.
*/
private void addShipments() {
Map shipments = views.getShipmentMap();
if (shipments.isEmpty()) {
System.out.println("Adding Shipments");
shipments.put(new ShipmentKey("P1", "S1"),
new ShipmentData(300));
shipments.put(new ShipmentKey("P2", "S1"),
new ShipmentData(200));
shipments.put(new ShipmentKey("P3", "S1"),
new ShipmentData(400));
shipments.put(new ShipmentKey("P4", "S1"),
new ShipmentData(200));
shipments.put(new ShipmentKey("P5", "S1"),
new ShipmentData(100));
shipments.put(new ShipmentKey("P6", "S1"),
new ShipmentData(100));
shipments.put(new ShipmentKey("P1", "S2"),
new ShipmentData(300));
shipments.put(new ShipmentKey("P2", "S2"),
new ShipmentData(400));
shipments.put(new ShipmentKey("P2", "S3"),
new ShipmentData(200));
shipments.put(new ShipmentKey("P2", "S4"),
new ShipmentData(200));
shipments.put(new ShipmentKey("P4", "S4"),
new ShipmentData(300));
shipments.put(new ShipmentKey("P5", "S4"),
new ShipmentData(400));
}
}
/**
* Print the key/value objects returned by an iterator of Map.Entry
* objects.
*/
private void printEntries(String label, Iterator iterator) {
System.out.println("\n--- " + label + " ---");
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
System.out.println(entry.getKey().toString());
System.out.println(entry.getValue().toString());
}
}
}
|