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:41 bostic Exp $
00008  */
00009 
00010 package collections.ship.sentity;
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 
00038 public class Sample {
00039 
00040     private SampleDatabase db;
00041     private SampleViews views;
00042 
00046     public static void main(String[] args) {
00047 
00048         System.out.println("\nRunning sample: " + Sample.class);
00049 
00050         // Parse the command line arguments.
00051         //
00052         String homeDir = "./tmp";
00053         for (int i = 0; i < args.length; i += 1) {
00054             if (args[i].equals("-h") && i < args.length - 1) {
00055                 i += 1;
00056                 homeDir = args[i];
00057             } else {
00058                 System.err.println("Usage:\n java " + Sample.class.getName() +
00059                                    "\n  [-h <home-directory>]");
00060                 System.exit(2);
00061             }
00062         }
00063 
00064         // Run the sample.
00065         //
00066         Sample sample = null;
00067         try {
00068             sample = new Sample(homeDir);
00069             sample.run();
00070         } catch (Exception e) {
00071             // If an exception reaches this point, the last transaction did not
00072             // complete.  If the exception is RunRecoveryException, follow
00073             // the Berkeley DB recovery procedures before running again.
00074             e.printStackTrace();
00075         } finally {
00076             if (sample != null) {
00077                 try {
00078                     // Always attempt to close the database cleanly.
00079                     sample.close();
00080                 } catch (Exception e) {
00081                     System.err.println("Exception during database close:");
00082                     e.printStackTrace();
00083                 }
00084             }
00085         }
00086     }
00087 
00091     private Sample(String homeDir)
00092         throws DatabaseException, FileNotFoundException {
00093 
00094         db = new SampleDatabase(homeDir);
00095         views = new SampleViews(db);
00096     }
00097 
00101     private void close()
00102         throws DatabaseException {
00103 
00104         db.close();
00105     }
00106 
00113     private void run()
00114         throws Exception {
00115 
00116         TransactionRunner runner = new TransactionRunner(db.getEnvironment());
00117         runner.run(new PopulateDatabase());
00118         runner.run(new PrintDatabase());
00119     }
00120 
00124     private class PopulateDatabase implements TransactionWorker {
00125 
00126         public void doWork()
00127             throws Exception {
00128             addSuppliers();
00129             addParts();
00130             addShipments();
00131         }
00132     }
00133 
00142     private class PrintDatabase implements TransactionWorker {
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