package com.sleepycat.examples.bdb.shipment.entity;
import com.sleepycat.bdb.TransactionRunner;
import com.sleepycat.bdb.TransactionWorker;
import com.sleepycat.bdb.collection.StoredIterator;
import com.sleepycat.db.DbException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
public class Sample {
private SampleDatabase db;
private SampleViews views;
public static void main(String[] args) {
System.out.println("\nRunning sample: " + Sample.class);
boolean runRecovery = true;
String homeDir = "./tmp";
for (int i = 0; i < args.length; i += 1) {
String arg = args[i];
if (args[i].equals("-h") && i < args.length - 1) {
i += 1;
homeDir = args[i];
} else if (args[i].equals("-multiprocess")) {
runRecovery = false;
} else {
System.err.println("Usage:\n java " + Sample.class.getName() +
"\n [-h <home-directory>] [-multiprocess]");
System.exit(2);
}
}
Sample sample = null;
try {
sample = new Sample(homeDir, runRecovery);
sample.run();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sample != null) {
try {
sample.close();
} catch (Exception e) {
System.err.println("Exception during database close:");
e.printStackTrace();
}
}
}
}
private Sample(String homeDir, boolean runRecovery)
throws DbException, FileNotFoundException {
db = new SampleDatabase(homeDir, runRecovery);
views = new SampleViews(db);
}
private void close()
throws DbException, IOException {
db.close();
}
private void run()
throws Exception {
TransactionRunner runner = new TransactionRunner(db.getEnvironment());
runner.run(new PopulateDatabase());
runner.run(new PrintDatabase());
}
private class PopulateDatabase implements TransactionWorker {
public void doWork()
throws Exception {
addSuppliers();
addParts();
addShipments();
}
}
private class PrintDatabase implements TransactionWorker {
public void doWork()
throws Exception {
printValues("Parts",
views.getPartSet().iterator());
printValues("Suppliers",
views.getSupplierSet().iterator());
printValues("Suppliers for City Paris",
views.getSupplierByCityMap().duplicates(
"Paris").iterator());
printValues("Shipments",
views.getShipmentSet().iterator());
printValues("Shipments for Part P1",
views.getShipmentByPartMap().duplicates(
new PartKey("P1")).iterator());
printValues("Shipments for Supplier S1",
views.getShipmentBySupplierMap().duplicates(
new SupplierKey("S1")).iterator());
}
}
private void addParts() {
Set parts = views.getPartSet();
if (parts.isEmpty()) {
System.out.println("Adding Parts");
parts.add(new Part("P1", "Nut", "Red",
new Weight(12.0, Weight.GRAMS), "London"));
parts.add(new Part("P2", "Bolt", "Green",
new Weight(17.0, Weight.GRAMS), "Paris"));
parts.add(new Part("P3", "Screw", "Blue",
new Weight(17.0, Weight.GRAMS), "Rome"));
parts.add(new Part("P4", "Screw", "Red",
new Weight(14.0, Weight.GRAMS), "London"));
parts.add(new Part("P5", "Cam", "Blue",
new Weight(12.0, Weight.GRAMS), "Paris"));
parts.add(new Part("P6", "Cog", "Red",
new Weight(19.0, Weight.GRAMS), "London"));
}
}
private void addSuppliers() {
Set suppliers = views.getSupplierSet();
if (suppliers.isEmpty()) {
System.out.println("Adding Suppliers");
suppliers.add(new Supplier("S1", "Smith", 20, "London"));
suppliers.add(new Supplier("S2", "Jones", 10, "Paris"));
suppliers.add(new Supplier("S3", "Blake", 30, "Paris"));
suppliers.add(new Supplier("S4", "Clark", 20, "London"));
suppliers.add(new Supplier("S5", "Adams", 30, "Athens"));
}
}
private void addShipments() {
Set shipments = views.getShipmentSet();
if (shipments.isEmpty()) {
System.out.println("Adding Shipments");
shipments.add(new Shipment("P1", "S1", 300));
shipments.add(new Shipment("P2", "S1", 200));
shipments.add(new Shipment("P3", "S1", 400));
shipments.add(new Shipment("P4", "S1", 200));
shipments.add(new Shipment("P5", "S1", 100));
shipments.add(new Shipment("P6", "S1", 100));
shipments.add(new Shipment("P1", "S2", 300));
shipments.add(new Shipment("P2", "S2", 400));
shipments.add(new Shipment("P2", "S3", 200));
shipments.add(new Shipment("P2", "S4", 200));
shipments.add(new Shipment("P4", "S4", 300));
shipments.add(new Shipment("P5", "S4", 400));
}
}
private void printValues(String label, Iterator iterator) {
System.out.println("\n--- " + label + " ---");
try {
while (iterator.hasNext()) {
System.out.println(iterator.next().toString());
}
} finally {
StoredIterator.close(iterator);
}
}
}