Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

SampleViews.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: SampleViews.java,v 12.2 2005/06/16 20:22:41 bostic Exp $
00008  */
00009 
00010 package collections.ship.sentity;
00011 
00012 import com.sleepycat.bind.EntityBinding;
00013 import com.sleepycat.bind.EntryBinding;
00014 import com.sleepycat.bind.serial.ClassCatalog;
00015 import com.sleepycat.bind.serial.TupleSerialBinding;
00016 import com.sleepycat.bind.tuple.TupleBinding;
00017 import com.sleepycat.bind.tuple.TupleInput;
00018 import com.sleepycat.bind.tuple.TupleOutput;
00019 import com.sleepycat.collections.StoredSortedMap;
00020 import com.sleepycat.collections.StoredSortedValueSet;
00021 
00028 public class SampleViews {
00029 
00030     private StoredSortedMap partMap;
00031     private StoredSortedMap supplierMap;
00032     private StoredSortedMap shipmentMap;
00033     private StoredSortedMap shipmentByPartMap;
00034     private StoredSortedMap shipmentBySupplierMap;
00035     private StoredSortedMap supplierByCityMap;
00036 
00040     public SampleViews(SampleDatabase db) {
00041 
00042         // Create the data bindings.
00043         // In this sample, EntityBinding classes are used to bind the stored
00044         // key/data entry pair to a combined data object; a "tricky" binding
00045         // that uses transient fields is used--see PartBinding, etc, for
00046         // details.  For keys, a one-to-one binding is implemented with
00047         // EntryBinding classes to bind the stored tuple entry to a key Object.
00048         //
00049         ClassCatalog catalog = db.getClassCatalog();
00050         EntryBinding partKeyBinding =
00051             new PartKeyBinding();
00052         EntityBinding partDataBinding =
00053             new PartBinding(catalog, Part.class);
00054         EntryBinding supplierKeyBinding =
00055             new SupplierKeyBinding();
00056         EntityBinding supplierDataBinding =
00057             new SupplierBinding(catalog, Supplier.class);
00058         EntryBinding shipmentKeyBinding =
00059             new ShipmentKeyBinding();
00060         EntityBinding shipmentDataBinding =
00061             new ShipmentBinding(catalog, Shipment.class);
00062         EntryBinding cityKeyBinding =
00063             TupleBinding.getPrimitiveBinding(String.class);
00064 
00065         // Create map views for all stores and indices.
00066         // StoredSortedMap is used since the stores and indices are ordered
00067         // (they use the DB_BTREE access method).
00068         //
00069         partMap =
00070             new StoredSortedMap(db.getPartDatabase(),
00071                                 partKeyBinding, partDataBinding, true);
00072         supplierMap =
00073             new StoredSortedMap(db.getSupplierDatabase(),
00074                                 supplierKeyBinding, supplierDataBinding, true);
00075         shipmentMap =
00076             new StoredSortedMap(db.getShipmentDatabase(),
00077                                 shipmentKeyBinding, shipmentDataBinding, true);
00078         shipmentByPartMap =
00079             new StoredSortedMap(db.getShipmentByPartDatabase(),
00080                                 partKeyBinding, shipmentDataBinding, true);
00081         shipmentBySupplierMap =
00082             new StoredSortedMap(db.getShipmentBySupplierDatabase(),
00083                                 supplierKeyBinding, shipmentDataBinding, true);
00084         supplierByCityMap =
00085             new StoredSortedMap(db.getSupplierByCityDatabase(),
00086                                 cityKeyBinding, supplierDataBinding, true);
00087     }
00088 
00089     // The views returned below can be accessed using the java.util.Map or
00090     // java.util.Set interfaces, or using the StoredSortedMap and
00091     // StoredValueSet classes, which provide additional methods.  The entity
00092     // sets could be obtained directly from the Map.values() method but
00093     // convenience methods are provided here to return them in order to avoid
00094     // down-casting elsewhere.
00095 
00099     public StoredSortedMap getPartMap() {
00100 
00101         return partMap;
00102     }
00103 
00107     public StoredSortedMap getSupplierMap() {
00108 
00109         return supplierMap;
00110     }
00111 
00115     public StoredSortedMap getShipmentMap() {
00116 
00117         return shipmentMap;
00118     }
00119 
00123     public StoredSortedValueSet getPartSet() {
00124 
00125         return (StoredSortedValueSet) partMap.values();
00126     }
00127 
00131     public StoredSortedValueSet getSupplierSet() {
00132 
00133         return (StoredSortedValueSet) supplierMap.values();
00134     }
00135 
00139     public StoredSortedValueSet getShipmentSet() {
00140 
00141         return (StoredSortedValueSet) shipmentMap.values();
00142     }
00143 
00147     public StoredSortedMap getShipmentByPartMap() {
00148 
00149         return shipmentByPartMap;
00150     }
00151 
00155     public StoredSortedMap getShipmentBySupplierMap() {
00156 
00157         return shipmentBySupplierMap;
00158     }
00159 
00163     public final StoredSortedMap getSupplierByCityMap() {
00164 
00165         return supplierByCityMap;
00166     }
00167 
00172     private static class PartKeyBinding extends TupleBinding {
00173 
00177         private PartKeyBinding() {
00178         }
00179 
00183         public Object entryToObject(TupleInput input) {
00184 
00185             String number = input.readString();
00186             return new PartKey(number);
00187         }
00188 
00192         public void objectToEntry(Object object, TupleOutput output) {
00193 
00194             PartKey key = (PartKey) object;
00195             output.writeString(key.getNumber());
00196         }
00197     }
00198 
00209     private static class PartBinding extends TupleSerialBinding {
00210 
00214         private PartBinding(ClassCatalog classCatalog, Class dataClass) {
00215 
00216             super(classCatalog, dataClass);
00217         }
00218 
00224         public Object entryToObject(TupleInput keyInput, Object dataInput) {
00225 
00226             String number = keyInput.readString();
00227             Part part = (Part) dataInput;
00228             part.setKey(number);
00229             return part;
00230         }
00231 
00235         public void objectToKey(Object object, TupleOutput output) {
00236 
00237             Part part = (Part) object;
00238             output.writeString(part.getNumber());
00239         }
00240 
00245         public Object objectToData(Object object) {
00246 
00247             return object;
00248         }
00249     }
00250 
00255     private static class SupplierKeyBinding extends TupleBinding {
00256 
00260         private SupplierKeyBinding() {
00261         }
00262 
00266         public Object entryToObject(TupleInput input) {
00267 
00268             String number = input.readString();
00269             return new SupplierKey(number);
00270         }
00271 
00275         public void objectToEntry(Object object, TupleOutput output) {
00276 
00277             SupplierKey key = (SupplierKey) object;
00278             output.writeString(key.getNumber());
00279         }
00280     }
00281 
00292     private static class SupplierBinding extends TupleSerialBinding {
00293 
00297         private SupplierBinding(ClassCatalog classCatalog, Class dataClass) {
00298 
00299             super(classCatalog, dataClass);
00300         }
00301 
00307         public Object entryToObject(TupleInput keyInput, Object dataInput) {
00308 
00309             String number = keyInput.readString();
00310             Supplier supplier = (Supplier) dataInput;
00311             supplier.setKey(number);
00312             return supplier;
00313         }
00314 
00318         public void objectToKey(Object object, TupleOutput output) {
00319 
00320             Supplier supplier = (Supplier) object;
00321             output.writeString(supplier.getNumber());
00322         }
00323 
00328         public Object objectToData(Object object) {
00329 
00330             return object;
00331         }
00332     }
00333 
00338     private static class ShipmentKeyBinding extends TupleBinding {
00339 
00343         private ShipmentKeyBinding() {
00344         }
00345 
00349         public Object entryToObject(TupleInput input) {
00350 
00351             String partNumber = input.readString();
00352             String supplierNumber = input.readString();
00353             return new ShipmentKey(partNumber, supplierNumber);
00354         }
00355 
00359         public void objectToEntry(Object object, TupleOutput output) {
00360 
00361             ShipmentKey key = (ShipmentKey) object;
00362             output.writeString(key.getPartNumber());
00363             output.writeString(key.getSupplierNumber());
00364         }
00365     }
00366 
00377     private static class ShipmentBinding extends TupleSerialBinding {
00378 
00382         private ShipmentBinding(ClassCatalog classCatalog, Class dataClass) {
00383 
00384             super(classCatalog, dataClass);
00385         }
00386 
00392         public Object entryToObject(TupleInput keyInput, Object dataInput) {
00393 
00394             String partNumber = keyInput.readString();
00395             String supplierNumber = keyInput.readString();
00396             Shipment shipment = (Shipment) dataInput;
00397             shipment.setKey(partNumber, supplierNumber);
00398             return shipment;
00399         }
00400 
00404         public void objectToKey(Object object, TupleOutput output) {
00405 
00406             Shipment shipment = (Shipment) object;
00407             output.writeString(shipment.getPartNumber());
00408             output.writeString(shipment.getSupplierNumber());
00409         }
00410 
00415         public Object objectToData(Object object) {
00416 
00417             return object;
00418         }
00419     }
00420 }

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