00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package collections.ship.index;
00011
00012 import java.io.FileNotFoundException;
00013 import java.util.Iterator;
00014 import java.util.Map;
00015
00016 import com.sleepycat.collections.StoredIterator;
00017 import com.sleepycat.collections.TransactionRunner;
00018 import com.sleepycat.collections.TransactionWorker;
00019 import com.sleepycat.db.DatabaseException;
00020
00037 public class Sample {
00038
00039 private SampleDatabase db;
00040 private SampleViews views;
00041
00045 public static void main(String[] args) {
00046
00047 System.out.println("\nRunning sample: " + Sample.class);
00048
00049
00050
00051 String homeDir = "./tmp";
00052 for (int i = 0; i < args.length; i += 1) {
00053 if (args[i].equals("-h") && i < args.length - 1) {
00054 i += 1;
00055 homeDir = args[i];
00056 } else {
00057 System.err.println("Usage:\n java " + Sample.class.getName() +
00058 "\n [-h <home-directory>]");
00059 System.exit(2);
00060 }
00061 }
00062
00063
00064
00065 Sample sample = null;
00066 try {
00067 sample = new Sample(homeDir);
00068 sample.run();
00069 } catch (Exception e) {
00070
00071
00072
00073 e.printStackTrace();
00074 } finally {
00075 if (sample != null) {
00076 try {
00077
00078 sample.close();
00079 } catch (Exception e) {
00080 System.err.println("Exception during database close:");
00081 e.printStackTrace();
00082 }
00083 }
00084 }
00085 }
00086
00090 private Sample(String homeDir)
00091 throws DatabaseException, FileNotFoundException {
00092
00093 db = new SampleDatabase(homeDir);
00094 views = new SampleViews(db);
00095 }
00096
00100 private void close()
00101 throws DatabaseException {
00102
00103 db.close();
00104 }
00105
00112 private void run()
00113 throws Exception {
00114
00115 TransactionRunner runner = new TransactionRunner(db.getEnvironment());
00116 runner.run(new PopulateDatabase());
00117 runner.run(new PrintDatabase());
00118 }
00119
00123 private class PopulateDatabase implements TransactionWorker {
00124
00125 public void doWork()
00126 throws Exception {
00127 addSuppliers();
00128 addParts();
00129 addShipments();
00130 }
00131 }
00132
00141 private class PrintDatabase implements TransactionWorker {
00142
00143 public void doWork()
00144 throws Exception {
00145 printEntries("Parts",
00146 views.getPartEntrySet().iterator());
00147 printEntries("Suppliers",
00148 views.getSupplierEntrySet().iterator());
00149 printValues("Suppliers for City Paris",
00150 views.getSupplierByCityMap().duplicates(
00151 "Paris").iterator());
00152 printEntries("Shipments",
00153 views.getShipmentEntrySet().iterator());
00154 printValues("Shipments for Part P1",
00155 views.getShipmentByPartMap().duplicates(
00156 new PartKey("P1")).iterator());
00157 printValues("Shipments for Supplier S1",
00158 views.getShipmentBySupplierMap().duplicates(
00159 new SupplierKey("S1")).iterator());
00160 }
00161 }
00162
00167 private void addParts() {
00168
00169 Map parts = views.getPartMap();
00170 if (parts.isEmpty()) {
00171 System.out.println("Adding Parts");
00172 parts.put(new PartKey("P1"),
00173 new PartData("Nut", "Red",
00174 new Weight(12.0, Weight.GRAMS),
00175 "London"));
00176 parts.put(new PartKey("P2"),
00177 new PartData("Bolt", "Green",
00178 new Weight(17.0, Weight.GRAMS),
00179 "Paris"));
00180 parts.put(new PartKey("P3"),
00181 new PartData("Screw", "Blue",
00182 new Weight(17.0, Weight.GRAMS),
00183 "Rome"));
00184 parts.put(new PartKey("P4"),
00185 new PartData("Screw", "Red",
00186 new Weight(14.0, Weight.GRAMS),
00187 "London"));
00188 parts.put(new PartKey("P5"),
00189 new PartData("Cam", "Blue",
00190 new Weight(12.0, Weight.GRAMS),
00191 "Paris"));
00192 parts.put(new PartKey("P6"),
00193 new PartData("Cog", "Red",
00194 new Weight(19.0, Weight.GRAMS),
00195 "London"));
00196 }
00197 }
00198
00203 private void addSuppliers() {
00204
00205 Map suppliers = views.getSupplierMap();
00206 if (suppliers.isEmpty()) {
00207 System.out.println("Adding Suppliers");
00208 suppliers.put(new SupplierKey("S1"),
00209 new SupplierData("Smith", 20, "London"));
00210 suppliers.put(new SupplierKey("S2"),
00211 new SupplierData("Jones", 10, "Paris"));
00212 suppliers.put(new SupplierKey("S3"),
00213 new SupplierData("Blake", 30, "Paris"));
00214 suppliers.put(new SupplierKey("S4"),
00215 new SupplierData("Clark", 20, "London"));
00216 suppliers.put(new SupplierKey("S5"),
00217 new SupplierData("Adams", 30, "Athens"));
00218 }
00219 }
00220
00225 private void addShipments() {
00226
00227 Map shipments = views.getShipmentMap();
00228 if (shipments.isEmpty()) {
00229 System.out.println("Adding Shipments");
00230 shipments.put(new ShipmentKey("P1", "S1"),
00231 new ShipmentData(300));
00232 shipments.put(new ShipmentKey("P2", "S1"),
00233 new ShipmentData(200));
00234 shipments.put(new ShipmentKey("P3", "S1"),
00235 new ShipmentData(400));
00236 shipments.put(new ShipmentKey("P4", "S1"),
00237 new ShipmentData(200));
00238 shipments.put(new ShipmentKey("P5", "S1"),
00239 new ShipmentData(100));
00240 shipments.put(new ShipmentKey("P6", "S1"),
00241 new ShipmentData(100));
00242 shipments.put(new ShipmentKey("P1", "S2"),
00243 new ShipmentData(300));
00244 shipments.put(new ShipmentKey("P2", "S2"),
00245 new ShipmentData(400));
00246 shipments.put(new ShipmentKey("P2", "S3"),
00247 new ShipmentData(200));
00248 shipments.put(new ShipmentKey("P2", "S4"),
00249 new ShipmentData(200));
00250 shipments.put(new ShipmentKey("P4", "S4"),
00251 new ShipmentData(300));
00252 shipments.put(new ShipmentKey("P5", "S4"),
00253 new ShipmentData(400));
00254 }
00255 }
00256
00265 private void printEntries(String label, Iterator iterator) {
00266
00267 System.out.println("\n--- " + label + " ---");
00268 try {
00269 while (iterator.hasNext()) {
00270 Map.Entry entry = (Map.Entry) iterator.next();
00271 System.out.println(entry.getKey().toString());
00272 System.out.println(entry.getValue().toString());
00273 }
00274 } finally {
00275
00276
00277
00278 StoredIterator.close(iterator);
00279 }
00280 }
00281
00289 private void printValues(String label, Iterator iterator) {
00290
00291 System.out.println("\n--- " + label + " ---");
00292 try {
00293 while (iterator.hasNext()) {
00294 System.out.println(iterator.next().toString());
00295 }
00296 } finally {
00297
00298
00299
00300 StoredIterator.close(iterator);
00301 }
00302 }
00303 }