1 8 9 package collections.ship.index; 10 11 import java.io.FileNotFoundException ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 15 import com.sleepycat.collections.TransactionRunner; 16 import com.sleepycat.collections.TransactionWorker; 17 import com.sleepycat.je.DatabaseException; 18 19 35 public class Sample { 36 37 private SampleDatabase db; 38 private SampleViews views; 39 40 43 public static void main(String [] args) { 44 45 System.out.println("\nRunning sample: " + Sample.class); 46 47 String homeDir = "./tmp"; 50 for (int i = 0; i < args.length; i += 1) { 51 if (args[i].equals("-h") && i < args.length - 1) { 52 i += 1; 53 homeDir = args[i]; 54 } else { 55 System.err.println("Usage:\n java " + Sample.class.getName() + 56 "\n [-h <home-directory>]"); 57 System.exit(2); 58 } 59 } 60 61 Sample sample = null; 64 try { 65 sample = new Sample(homeDir); 66 sample.run(); 67 } catch (Exception e) { 68 e.printStackTrace(); 72 } finally { 73 if (sample != null) { 74 try { 75 sample.close(); 77 } catch (Exception e) { 78 System.err.println("Exception during database close:"); 79 e.printStackTrace(); 80 } 81 } 82 } 83 } 84 85 88 private Sample(String homeDir) 89 throws DatabaseException, FileNotFoundException { 90 91 db = new SampleDatabase(homeDir); 92 views = new SampleViews(db); 93 } 94 95 98 private void close() 99 throws DatabaseException { 100 101 db.close(); 102 } 103 104 110 private void run() 111 throws Exception { 112 113 TransactionRunner runner = new TransactionRunner(db.getEnvironment()); 114 runner.run(new PopulateDatabase()); 115 runner.run(new PrintDatabase()); 116 } 117 118 121 private class PopulateDatabase implements TransactionWorker { 122 123 public void doWork() 124 throws Exception { 125 addSuppliers(); 126 addParts(); 127 addShipments(); 128 } 129 } 130 131 138 private class PrintDatabase implements TransactionWorker { 139 140 public void doWork() 141 throws Exception { 142 printEntries("Parts", 143 views.getPartEntrySet().iterator()); 144 printEntries("Suppliers", 145 views.getSupplierEntrySet().iterator()); 146 printValues("Suppliers for City Paris", 147 views.getSupplierByCityMap().duplicates( 148 "Paris").iterator()); 149 printEntries("Shipments", 150 views.getShipmentEntrySet().iterator()); 151 printValues("Shipments for Part P1", 152 views.getShipmentByPartMap().duplicates( 153 new PartKey("P1")).iterator()); 154 printValues("Shipments for Supplier S1", 155 views.getShipmentBySupplierMap().duplicates( 156 new SupplierKey("S1")).iterator()); 157 } 158 } 159 160 164 private void addParts() { 165 166 Map parts = views.getPartMap(); 167 if (parts.isEmpty()) { 168 System.out.println("Adding Parts"); 169 parts.put(new PartKey("P1"), 170 new PartData("Nut", "Red", 171 new Weight(12.0, Weight.GRAMS), 172 "London")); 173 parts.put(new PartKey("P2"), 174 new PartData("Bolt", "Green", 175 new Weight(17.0, Weight.GRAMS), 176 "Paris")); 177 parts.put(new PartKey("P3"), 178 new PartData("Screw", "Blue", 179 new Weight(17.0, Weight.GRAMS), 180 "Rome")); 181 parts.put(new PartKey("P4"), 182 new PartData("Screw", "Red", 183 new Weight(14.0, Weight.GRAMS), 184 "London")); 185 parts.put(new PartKey("P5"), 186 new PartData("Cam", "Blue", 187 new Weight(12.0, Weight.GRAMS), 188 "Paris")); 189 parts.put(new PartKey("P6"), 190 new PartData("Cog", "Red", 191 new Weight(19.0, Weight.GRAMS), 192 "London")); 193 } 194 } 195 196 200 private void addSuppliers() { 201 202 Map suppliers = views.getSupplierMap(); 203 if (suppliers.isEmpty()) { 204 System.out.println("Adding Suppliers"); 205 suppliers.put(new SupplierKey("S1"), 206 new SupplierData("Smith", 20, "London")); 207 suppliers.put(new SupplierKey("S2"), 208 new SupplierData("Jones", 10, "Paris")); 209 suppliers.put(new SupplierKey("S3"), 210 new SupplierData("Blake", 30, "Paris")); 211 suppliers.put(new SupplierKey("S4"), 212 new SupplierData("Clark", 20, "London")); 213 suppliers.put(new SupplierKey("S5"), 214 new SupplierData("Adams", 30, "Athens")); 215 } 216 } 217 218 222 private void addShipments() { 223 224 Map shipments = views.getShipmentMap(); 225 if (shipments.isEmpty()) { 226 System.out.println("Adding Shipments"); 227 shipments.put(new ShipmentKey("P1", "S1"), 228 new ShipmentData(300)); 229 shipments.put(new ShipmentKey("P2", "S1"), 230 new ShipmentData(200)); 231 shipments.put(new ShipmentKey("P3", "S1"), 232 new ShipmentData(400)); 233 shipments.put(new ShipmentKey("P4", "S1"), 234 new ShipmentData(200)); 235 shipments.put(new ShipmentKey("P5", "S1"), 236 new ShipmentData(100)); 237 shipments.put(new ShipmentKey("P6", "S1"), 238 new ShipmentData(100)); 239 shipments.put(new ShipmentKey("P1", "S2"), 240 new ShipmentData(300)); 241 shipments.put(new ShipmentKey("P2", "S2"), 242 new ShipmentData(400)); 243 shipments.put(new ShipmentKey("P2", "S3"), 244 new ShipmentData(200)); 245 shipments.put(new ShipmentKey("P2", "S4"), 246 new ShipmentData(200)); 247 shipments.put(new ShipmentKey("P4", "S4"), 248 new ShipmentData(300)); 249 shipments.put(new ShipmentKey("P5", "S4"), 250 new ShipmentData(400)); 251 } 252 } 253 254 258 private void printEntries(String label, Iterator iterator) { 259 260 System.out.println("\n--- " + label + " ---"); 261 while (iterator.hasNext()) { 262 Map.Entry entry = (Map.Entry ) iterator.next(); 263 System.out.println(entry.getKey().toString()); 264 System.out.println(entry.getValue().toString()); 265 } 266 } 267 268 271 private void printValues(String label, Iterator iterator) { 272 273 System.out.println("\n--- " + label + " ---"); 274 while (iterator.hasNext()) { 275 System.out.println(iterator.next().toString()); 276 } 277 } 278 } 279 | Popular Tags |