Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

Sample.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2002-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: Sample.java,v 12.2 2005/06/16 20:22:24 bostic Exp $
00008  */
00009 
00010 package collections.ship.entity;
00011 
00012 import java.io.FileNotFoundException;
00013 import java.util.Iterator;
00014 import java.util.Set;
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         // Parse the command line arguments.
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         // Run the sample.
00064         //
00065         Sample sample = null;
00066         try {
00067             sample = new Sample(homeDir);
00068             sample.run();
00069         } catch (Exception e) {
00070             // If an exception reaches this point, the last transaction did not
00071             // complete.  If the exception is RunRecoveryException, follow
00072             // the Berkeley DB recovery procedures before running again.
00073             e.printStackTrace();
00074         } finally {
00075             if (sample != null) {
00076                 try {
00077                     // Always attempt to close the database cleanly.
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 
00144         public void doWork()
00145             throws Exception {
00146             printValues("Parts",
00147                         views.getPartSet().iterator());
00148             printValues("Suppliers",
00149                         views.getSupplierSet().iterator());
00150             printValues("Suppliers for City Paris",
00151                         views.getSupplierByCityMap().duplicates(
00152                                             "Paris").iterator());
00153             printValues("Shipments",
00154                         views.getShipmentSet().iterator());
00155             printValues("Shipments for Part P1",
00156                         views.getShipmentByPartMap().duplicates(
00157                                             new PartKey("P1")).iterator());
00158             printValues("Shipments for Supplier S1",
00159                         views.getShipmentBySupplierMap().duplicates(
00160                                             new SupplierKey("S1")).iterator());
00161         }
00162     }
00163 
00168     private void addParts() {
00169 
00170         Set parts = views.getPartSet();
00171         if (parts.isEmpty()) {
00172             System.out.println("Adding Parts");
00173             parts.add(new Part("P1", "Nut", "Red",
00174                                new Weight(12.0, Weight.GRAMS), "London"));
00175             parts.add(new Part("P2", "Bolt", "Green",
00176                                new Weight(17.0, Weight.GRAMS), "Paris"));
00177             parts.add(new Part("P3", "Screw", "Blue",
00178                                new Weight(17.0, Weight.GRAMS), "Rome"));
00179             parts.add(new Part("P4", "Screw", "Red",
00180                                new Weight(14.0, Weight.GRAMS), "London"));
00181             parts.add(new Part("P5", "Cam", "Blue",
00182                                new Weight(12.0, Weight.GRAMS), "Paris"));
00183             parts.add(new Part("P6", "Cog", "Red",
00184                                new Weight(19.0, Weight.GRAMS), "London"));
00185         }
00186     }
00187 
00192     private void addSuppliers() {
00193 
00194         Set suppliers = views.getSupplierSet();
00195         if (suppliers.isEmpty()) {
00196             System.out.println("Adding Suppliers");
00197             suppliers.add(new Supplier("S1", "Smith", 20, "London"));
00198             suppliers.add(new Supplier("S2", "Jones", 10, "Paris"));
00199             suppliers.add(new Supplier("S3", "Blake", 30, "Paris"));
00200             suppliers.add(new Supplier("S4", "Clark", 20, "London"));
00201             suppliers.add(new Supplier("S5", "Adams", 30, "Athens"));
00202         }
00203     }
00204 
00209     private void addShipments() {
00210 
00211         Set shipments = views.getShipmentSet();
00212         if (shipments.isEmpty()) {
00213             System.out.println("Adding Shipments");
00214             shipments.add(new Shipment("P1", "S1", 300));
00215             shipments.add(new Shipment("P2", "S1", 200));
00216             shipments.add(new Shipment("P3", "S1", 400));
00217             shipments.add(new Shipment("P4", "S1", 200));
00218             shipments.add(new Shipment("P5", "S1", 100));
00219             shipments.add(new Shipment("P6", "S1", 100));
00220             shipments.add(new Shipment("P1", "S2", 300));
00221             shipments.add(new Shipment("P2", "S2", 400));
00222             shipments.add(new Shipment("P2", "S3", 200));
00223             shipments.add(new Shipment("P2", "S4", 200));
00224             shipments.add(new Shipment("P4", "S4", 300));
00225             shipments.add(new Shipment("P5", "S4", 400));
00226         }
00227     }
00228 
00236     private void printValues(String label, Iterator iterator) {
00237 
00238         System.out.println("\n--- " + label + " ---");
00239         try {
00240             while (iterator.hasNext()) {
00241                 System.out.println(iterator.next().toString());
00242             }
00243         } finally {
00244             // IMPORTANT: Use StoredIterator to close all database
00245             // iterators.  If java.util.Iterator is in hand, you can safely
00246             // close it by calling StoredIterator.close(Iterator).
00247             StoredIterator.close(iterator);
00248         }
00249     }
00250 }

Generated on Sun Dec 25 12:14:26 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2