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:44 bostic Exp $
00008  */
00009 
00010 package collections.ship.tuple;
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.  For keys, a
00045         // one-to-one binding is implemented with EntryBinding classes to bind
00046         // the stored tuple entry to a key Object.
00047         //
00048         ClassCatalog catalog = db.getClassCatalog();
00049         EntryBinding partKeyBinding =
00050             new PartKeyBinding();
00051         EntityBinding partDataBinding =
00052             new PartBinding(catalog, PartData.class);
00053         EntryBinding supplierKeyBinding =
00054             new SupplierKeyBinding();
00055         EntityBinding supplierDataBinding =
00056             new SupplierBinding(catalog, SupplierData.class);
00057         EntryBinding shipmentKeyBinding =
00058             new ShipmentKeyBinding();
00059         EntityBinding shipmentDataBinding =
00060             new ShipmentBinding(catalog, ShipmentData.class);
00061         EntryBinding cityKeyBinding =
00062             TupleBinding.getPrimitiveBinding(String.class);
00063 
00064         // Create map views for all stores and indices.
00065         // StoredSortedMap is used since the stores and indices are ordered
00066         // (they use the DB_BTREE access method).
00067         //
00068         partMap =
00069             new StoredSortedMap(db.getPartDatabase(),
00070                                 partKeyBinding, partDataBinding, true);
00071         supplierMap =
00072             new StoredSortedMap(db.getSupplierDatabase(),
00073                                 supplierKeyBinding, supplierDataBinding, true);
00074         shipmentMap =
00075             new StoredSortedMap(db.getShipmentDatabase(),
00076                                 shipmentKeyBinding, shipmentDataBinding, true);
00077         shipmentByPartMap =
00078             new StoredSortedMap(db.getShipmentByPartDatabase(),
00079                                 partKeyBinding, shipmentDataBinding, true);
00080         shipmentBySupplierMap =
00081             new StoredSortedMap(db.getShipmentBySupplierDatabase(),
00082                                 supplierKeyBinding, shipmentDataBinding, true);
00083         supplierByCityMap =
00084             new StoredSortedMap(db.getSupplierByCityDatabase(),
00085                                 cityKeyBinding, supplierDataBinding, true);
00086     }
00087 
00088     // The views returned below can be accessed using the java.util.Map or
00089     // java.util.Set interfaces, or using the StoredSortedMap and
00090     // StoredValueSet classes, which provide additional methods.  The entity
00091     // sets could be obtained directly from the Map.values() method but
00092     // convenience methods are provided here to return them in order to avoid
00093     // down-casting elsewhere.
00094 
00098     public StoredSortedMap getPartMap() {
00099 
00100         return partMap;
00101     }
00102 
00106     public StoredSortedMap getSupplierMap() {
00107 
00108         return supplierMap;
00109     }
00110 
00114     public StoredSortedMap getShipmentMap() {
00115 
00116         return shipmentMap;
00117     }
00118 
00122     public StoredSortedValueSet getPartSet() {
00123 
00124         return (StoredSortedValueSet) partMap.values();
00125     }
00126 
00130     public StoredSortedValueSet getSupplierSet() {
00131 
00132         return (StoredSortedValueSet) supplierMap.values();
00133     }
00134 
00138     public StoredSortedValueSet getShipmentSet() {
00139 
00140         return (StoredSortedValueSet) shipmentMap.values();
00141     }
00142 
00146     public StoredSortedMap getShipmentByPartMap() {
00147 
00148         return shipmentByPartMap;
00149     }
00150 
00154     public StoredSortedMap getShipmentBySupplierMap() {
00155 
00156         return shipmentBySupplierMap;
00157     }
00158 
00162     public final StoredSortedMap getSupplierByCityMap() {
00163 
00164         return supplierByCityMap;
00165     }
00166 
00171     private static class PartKeyBinding extends TupleBinding {
00172 
00176         private PartKeyBinding() {
00177         }
00178 
00182         public Object entryToObject(TupleInput input) {
00183 
00184             String number = input.readString();
00185             return new PartKey(number);
00186         }
00187 
00191         public void objectToEntry(Object object, TupleOutput output) {
00192 
00193             PartKey key = (PartKey) object;
00194             output.writeString(key.getNumber());
00195         }
00196     }
00197 
00202     private static class PartBinding extends TupleSerialBinding {
00203 
00207         private PartBinding(ClassCatalog classCatalog, Class dataClass) {
00208 
00209             super(classCatalog, dataClass);
00210         }
00211 
00215         public Object entryToObject(TupleInput keyInput, Object dataInput) {
00216 
00217             String number = keyInput.readString();
00218             PartData data = (PartData) dataInput;
00219             return new Part(number, data.getName(), data.getColor(),
00220                             data.getWeight(), data.getCity());
00221         }
00222 
00226         public void objectToKey(Object object, TupleOutput output) {
00227 
00228             Part part = (Part) object;
00229             output.writeString(part.getNumber());
00230         }
00231 
00235         public Object objectToData(Object object) {
00236 
00237             Part part = (Part) object;
00238             return new PartData(part.getName(), part.getColor(),
00239                                  part.getWeight(), part.getCity());
00240         }
00241     }
00242 
00247     private static class SupplierKeyBinding extends TupleBinding {
00248 
00252         private SupplierKeyBinding() {
00253         }
00254 
00258         public Object entryToObject(TupleInput input) {
00259 
00260             String number = input.readString();
00261             return new SupplierKey(number);
00262         }
00263 
00267         public void objectToEntry(Object object, TupleOutput output) {
00268 
00269             SupplierKey key = (SupplierKey) object;
00270             output.writeString(key.getNumber());
00271         }
00272     }
00273 
00278     private static class SupplierBinding extends TupleSerialBinding {
00279 
00283         private SupplierBinding(ClassCatalog classCatalog, Class dataClass) {
00284 
00285             super(classCatalog, dataClass);
00286         }
00287 
00291         public Object entryToObject(TupleInput keyInput, Object dataInput) {
00292 
00293             String number = keyInput.readString();
00294             SupplierData data = (SupplierData) dataInput;
00295             return new Supplier(number, data.getName(),
00296                                 data.getStatus(), data.getCity());
00297         }
00298 
00302         public void objectToKey(Object object, TupleOutput output) {
00303 
00304             Supplier supplier = (Supplier) object;
00305             output.writeString(supplier.getNumber());
00306         }
00307 
00311         public Object objectToData(Object object) {
00312 
00313             Supplier supplier = (Supplier) object;
00314             return new SupplierData(supplier.getName(), supplier.getStatus(),
00315                                      supplier.getCity());
00316         }
00317     }
00318 
00323     private static class ShipmentKeyBinding extends TupleBinding {
00324 
00328         private ShipmentKeyBinding() {
00329         }
00330 
00334         public Object entryToObject(TupleInput input) {
00335 
00336             String partNumber = input.readString();
00337             String supplierNumber = input.readString();
00338             return new ShipmentKey(partNumber, supplierNumber);
00339         }
00340 
00344         public void objectToEntry(Object object, TupleOutput output) {
00345 
00346             ShipmentKey key = (ShipmentKey) object;
00347             output.writeString(key.getPartNumber());
00348             output.writeString(key.getSupplierNumber());
00349         }
00350     }
00351 
00356     private static class ShipmentBinding extends TupleSerialBinding {
00357 
00361         private ShipmentBinding(ClassCatalog classCatalog, Class dataClass) {
00362 
00363             super(classCatalog, dataClass);
00364         }
00365 
00369         public Object entryToObject(TupleInput keyInput, Object dataInput) {
00370 
00371             String partNumber = keyInput.readString();
00372             String supplierNumber = keyInput.readString();
00373             ShipmentData data = (ShipmentData) dataInput;
00374             return new Shipment(partNumber, supplierNumber,
00375                                 data.getQuantity());
00376         }
00377 
00381         public void objectToKey(Object object, TupleOutput output) {
00382 
00383             Shipment shipment = (Shipment) object;
00384             output.writeString(shipment.getPartNumber());
00385             output.writeString(shipment.getSupplierNumber());
00386         }
00387 
00391         public Object objectToData(Object object) {
00392 
00393             Shipment shipment = (Shipment) object;
00394             return new ShipmentData(shipment.getQuantity());
00395         }
00396     }
00397 }

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