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:44 bostic Exp $
00008  */
00009 
00010 package collections.ship.tuple;
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         public void doWork()
00144             throws Exception {
00145             printValues("Parts",
00146                         views.getPartSet().iterator());
00147             printValues("Suppliers",
00148                         views.getSupplierSet().iterator());
00149             printValues("Suppliers for City Paris",
00150                         views.getSupplierByCityMap().duplicates(
00151                                             "Paris").iterator());
00152             printValues("Shipments",
00153                         views.getShipmentSet().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         Set parts = views.getPartSet();
00170         if (parts.isEmpty()) {
00171             System.out.println("Adding Parts");
00172             parts.add(new Part("P1", "Nut", "Red",
00173                                new Weight(12.0, Weight.GRAMS), "London"));
00174             parts.add(new Part("P2", "Bolt", "Green",
00175                                new Weight(17.0, Weight.GRAMS), "Paris"));
00176             parts.add(new Part("P3", "Screw", "Blue",
00177                                new Weight(17.0, Weight.GRAMS), "Rome"));
00178             parts.add(new Part("P4", "Screw", "Red",
00179                                new Weight(14.0, Weight.GRAMS), "London"));
00180             parts.add(new Part("P5", "Cam", "Blue",
00181                                new Weight(12.0, Weight.GRAMS), "Paris"));
00182             parts.add(new Part("P6", "Cog", "Red",
00183                                new Weight(19.0, Weight.GRAMS), "London"));
00184         }
00185     }
00186 
00191     private void addSuppliers() {
00192 
00193         Set suppliers = views.getSupplierSet();
00194         if (suppliers.isEmpty()) {
00195             System.out.println("Adding Suppliers");
00196             suppliers.add(new Supplier("S1", "Smith", 20, "London"));
00197             suppliers.add(new Supplier("S2", "Jones", 10, "Paris"));
00198             suppliers.add(new Supplier("S3", "Blake", 30, "Paris"));
00199             suppliers.add(new Supplier("S4", "Clark", 20, "London"));
00200             suppliers.add(new Supplier("S5", "Adams", 30, "Athens"));
00201         }
00202     }
00203 
00208     private void addShipments() {
00209 
00210         Set shipments = views.getShipmentSet();
00211         if (shipments.isEmpty()) {
00212             System.out.println("Adding Shipments");
00213             shipments.add(new Shipment("P1", "S1", 300));
00214             shipments.add(new Shipment("P2", "S1", 200));
00215             shipments.add(new Shipment("P3", "S1", 400));
00216             shipments.add(new Shipment("P4", "S1", 200));
00217             shipments.add(new Shipment("P5", "S1", 100));
00218             shipments.add(new Shipment("P6", "S1", 100));
00219             shipments.add(new Shipment("P1", "S2", 300));
00220             shipments.add(new Shipment("P2", "S2", 400));
00221             shipments.add(new Shipment("P2", "S3", 200));
00222             shipments.add(new Shipment("P2", "S4", 200));
00223             shipments.add(new Shipment("P4", "S4", 300));
00224             shipments.add(new Shipment("P5", "S4", 400));
00225         }
00226     }
00227 
00235     private void printValues(String label, Iterator iterator) {
00236 
00237         System.out.println("\n--- " + label + " ---");
00238         try {
00239             while (iterator.hasNext()) {
00240                 System.out.println(iterator.next().toString());
00241             }
00242         } finally {
00243             // IMPORTANT: Use StoredIterator to close all database
00244             // iterators.  If java.util.Iterator is in hand, you can safely
00245             // close it by calling StoredIterator.close(Iterator).
00246             StoredIterator.close(iterator);
00247         }
00248     }
00249 }

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