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:32 bostic Exp $
00008  */
00009 
00010 package collections.ship.factory;
00011 
00012 import java.util.Iterator;
00013 import java.util.Set;
00014 
00015 import com.sleepycat.collections.StoredIterator;
00016 import com.sleepycat.collections.TransactionRunner;
00017 import com.sleepycat.collections.TransactionWorker;
00018 
00036 public class Sample {
00037 
00038     private SampleDatabase db;
00039     private SampleViews views;
00040 
00044     public static void main(String[] args) {
00045 
00046         System.out.println("\nRunning sample: " + Sample.class);
00047 
00048         // Parse the command line arguments.
00049         //
00050         String homeDir = "./tmp";
00051         for (int i = 0; i < args.length; i += 1) {
00052             if (args[i].equals("-h") && i < args.length - 1) {
00053                 i += 1;
00054                 homeDir = args[i];
00055             } else {
00056                 System.err.println("Usage:\n java " + Sample.class.getName() +
00057                                    "\n  [-h <home-directory>]");
00058                 System.exit(2);
00059             }
00060         }
00061 
00062         // Run the sample.
00063         //
00064         Sample sample = null;
00065         try {
00066             sample = new Sample(homeDir);
00067             sample.run();
00068         } catch (Exception e) {
00069             // If an exception reaches this point, the last transaction did not
00070             // complete.  If the exception is RunRecoveryException, follow
00071             // the Berkeley DB recovery procedures before running again.
00072             e.printStackTrace();
00073         } finally {
00074             if (sample != null) {
00075                 try {
00076                     // Always attempt to close the database cleanly.
00077                     sample.close();
00078                 } catch (Exception e) {
00079                     System.err.println("Exception during database close:");
00080                     e.printStackTrace();
00081                 }
00082             }
00083         }
00084     }
00085 
00089     private Sample(String homeDir)
00090         throws Exception {
00091 
00092         db = new SampleDatabase(homeDir);
00093         views = new SampleViews(db);
00094     }
00095 
00099     private void close()
00100         throws Exception {
00101 
00102         db.close();
00103     }
00104 
00111     private void run()
00112         throws Exception {
00113 
00114         TransactionRunner runner = new TransactionRunner(db.getEnvironment());
00115         runner.run(new PopulateDatabase());
00116         runner.run(new PrintDatabase());
00117     }
00118 
00122     private class PopulateDatabase implements TransactionWorker {
00123 
00124         public void doWork()
00125             throws Exception {
00126             addSuppliers();
00127             addParts();
00128             addShipments();
00129         }
00130     }
00131 
00140     private class PrintDatabase implements TransactionWorker {
00141 
00142         public void doWork()
00143             throws Exception {
00144             printValues("Parts",
00145                         views.getPartSet().iterator());
00146             printValues("Suppliers",
00147                         views.getSupplierSet().iterator());
00148             printValues("Suppliers for City Paris",
00149                         views.getSupplierByCityMap().duplicates(
00150                                             "Paris").iterator());
00151             printValues("Shipments",
00152                         views.getShipmentSet().iterator());
00153             printValues("Shipments for Part P1",
00154                         views.getShipmentByPartMap().duplicates(
00155                                             new PartKey("P1")).iterator());
00156             printValues("Shipments for Supplier S1",
00157                         views.getShipmentBySupplierMap().duplicates(
00158                                             new SupplierKey("S1")).iterator());
00159         }
00160     }
00161 
00166     private void addParts() {
00167 
00168         Set parts = views.getPartSet();
00169         if (parts.isEmpty()) {
00170             System.out.println("Adding Parts");
00171             parts.add(new Part("P1", "Nut", "Red",
00172                                new Weight(12.0, Weight.GRAMS), "London"));
00173             parts.add(new Part("P2", "Bolt", "Green",
00174                                new Weight(17.0, Weight.GRAMS), "Paris"));
00175             parts.add(new Part("P3", "Screw", "Blue",
00176                                new Weight(17.0, Weight.GRAMS), "Rome"));
00177             parts.add(new Part("P4", "Screw", "Red",
00178                                new Weight(14.0, Weight.GRAMS), "London"));
00179             parts.add(new Part("P5", "Cam", "Blue",
00180                                new Weight(12.0, Weight.GRAMS), "Paris"));
00181             parts.add(new Part("P6", "Cog", "Red",
00182                                new Weight(19.0, Weight.GRAMS), "London"));
00183         }
00184     }
00185 
00190     private void addSuppliers() {
00191 
00192         Set suppliers = views.getSupplierSet();
00193         if (suppliers.isEmpty()) {
00194             System.out.println("Adding Suppliers");
00195             suppliers.add(new Supplier("S1", "Smith", 20, "London"));
00196             suppliers.add(new Supplier("S2", "Jones", 10, "Paris"));
00197             suppliers.add(new Supplier("S3", "Blake", 30, "Paris"));
00198             suppliers.add(new Supplier("S4", "Clark", 20, "London"));
00199             suppliers.add(new Supplier("S5", "Adams", 30, "Athens"));
00200         }
00201     }
00202 
00207     private void addShipments() {
00208 
00209         Set shipments = views.getShipmentSet();
00210         if (shipments.isEmpty()) {
00211             System.out.println("Adding Shipments");
00212             shipments.add(new Shipment("P1", "S1", 300));
00213             shipments.add(new Shipment("P2", "S1", 200));
00214             shipments.add(new Shipment("P3", "S1", 400));
00215             shipments.add(new Shipment("P4", "S1", 200));
00216             shipments.add(new Shipment("P5", "S1", 100));
00217             shipments.add(new Shipment("P6", "S1", 100));
00218             shipments.add(new Shipment("P1", "S2", 300));
00219             shipments.add(new Shipment("P2", "S2", 400));
00220             shipments.add(new Shipment("P2", "S3", 200));
00221             shipments.add(new Shipment("P2", "S4", 200));
00222             shipments.add(new Shipment("P4", "S4", 300));
00223             shipments.add(new Shipment("P5", "S4", 400));
00224         }
00225     }
00226 
00234     private void printValues(String label, Iterator iterator) {
00235 
00236         System.out.println("\n--- " + label + " ---");
00237         try {
00238             while (iterator.hasNext()) {
00239                 System.out.println(iterator.next().toString());
00240             }
00241         } finally {
00242             // IMPORTANT: Use StoredIterator to close all database
00243             // iterators.  If java.util.Iterator is in hand, you can safely
00244             // close it by calling StoredIterator.close(Iterator).
00245             StoredIterator.close(iterator);
00246         }
00247     }
00248 }

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