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:40 bostic Exp $
00008  */
00009 
00010 package collections.ship.marshal;
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 import com.sleepycat.util.RuntimeExceptionWrapper;
00022 
00029 public class SampleViews {
00030 
00031     private StoredSortedMap partMap;
00032     private StoredSortedMap supplierMap;
00033     private StoredSortedMap shipmentMap;
00034     private StoredSortedMap shipmentByPartMap;
00035     private StoredSortedMap shipmentBySupplierMap;
00036     private StoredSortedMap supplierByCityMap;
00037 
00041     public SampleViews(SampleDatabase db) {
00042 
00043         // Create the data bindings.
00044         // In this sample, EntityBinding classes are used to bind the stored
00045         // key/data entry pair to a combined data object; a "tricky" binding
00046         // that uses transient fields is used--see PartBinding, etc, for
00047         // details.  For keys, a one-to-one binding is implemented with
00048         // EntryBinding classes to bind the stored tuple entry to a key Object.
00049         //
00050         ClassCatalog catalog = db.getClassCatalog();
00051         EntryBinding partKeyBinding =
00052             new MarshalledKeyBinding(PartKey.class);
00053         EntityBinding partDataBinding =
00054             new MarshalledEntityBinding(catalog, Part.class);
00055         EntryBinding supplierKeyBinding =
00056             new MarshalledKeyBinding(SupplierKey.class);
00057         EntityBinding supplierDataBinding =
00058             new MarshalledEntityBinding(catalog, Supplier.class);
00059         EntryBinding shipmentKeyBinding =
00060             new MarshalledKeyBinding(ShipmentKey.class);
00061         EntityBinding shipmentDataBinding =
00062             new MarshalledEntityBinding(catalog, Shipment.class);
00063         EntryBinding cityKeyBinding =
00064             TupleBinding.getPrimitiveBinding(String.class);
00065 
00066         // Create map views for all stores and indices.
00067         // StoredSortedMap is used since the stores and indices are ordered
00068         // (they use the DB_BTREE access method).
00069         //
00070         partMap =
00071             new StoredSortedMap(db.getPartDatabase(),
00072                                 partKeyBinding, partDataBinding, true);
00073         supplierMap =
00074             new StoredSortedMap(db.getSupplierDatabase(),
00075                                 supplierKeyBinding, supplierDataBinding, true);
00076         shipmentMap =
00077             new StoredSortedMap(db.getShipmentDatabase(),
00078                                 shipmentKeyBinding, shipmentDataBinding, true);
00079         shipmentByPartMap =
00080             new StoredSortedMap(db.getShipmentByPartDatabase(),
00081                                 partKeyBinding, shipmentDataBinding, true);
00082         shipmentBySupplierMap =
00083             new StoredSortedMap(db.getShipmentBySupplierDatabase(),
00084                                 supplierKeyBinding, shipmentDataBinding, true);
00085         supplierByCityMap =
00086             new StoredSortedMap(db.getSupplierByCityDatabase(),
00087                                 cityKeyBinding, supplierDataBinding, true);
00088     }
00089 
00090     // The views returned below can be accessed using the java.util.Map or
00091     // java.util.Set interfaces, or using the StoredSortedMap and
00092     // StoredValueSet classes, which provide additional methods.  The entity
00093     // sets could be obtained directly from the Map.values() method but
00094     // convenience methods are provided here to return them in order to avoid
00095     // down-casting elsewhere.
00096 
00100     public StoredSortedMap getPartMap() {
00101 
00102         return partMap;
00103     }
00104 
00108     public StoredSortedMap getSupplierMap() {
00109 
00110         return supplierMap;
00111     }
00112 
00116     public StoredSortedMap getShipmentMap() {
00117 
00118         return shipmentMap;
00119     }
00120 
00124     public StoredSortedValueSet getPartSet() {
00125 
00126         return (StoredSortedValueSet) partMap.values();
00127     }
00128 
00132     public StoredSortedValueSet getSupplierSet() {
00133 
00134         return (StoredSortedValueSet) supplierMap.values();
00135     }
00136 
00140     public StoredSortedValueSet getShipmentSet() {
00141 
00142         return (StoredSortedValueSet) shipmentMap.values();
00143     }
00144 
00148     public StoredSortedMap getShipmentByPartMap() {
00149 
00150         return shipmentByPartMap;
00151     }
00152 
00156     public StoredSortedMap getShipmentBySupplierMap() {
00157 
00158         return shipmentBySupplierMap;
00159     }
00160 
00164     public final StoredSortedMap getSupplierByCityMap() {
00165 
00166         return supplierByCityMap;
00167     }
00168 
00174     private static class MarshalledKeyBinding extends TupleBinding {
00175 
00176         private Class keyClass;
00177 
00181         private MarshalledKeyBinding(Class keyClass) {
00182 
00183             // The key class will be used to instantiate the key object.
00184             //
00185             if (!MarshalledKey.class.isAssignableFrom(keyClass)) {
00186                 throw new IllegalArgumentException(keyClass.toString() +
00187                                        " does not implement MarshalledKey");
00188             }
00189             this.keyClass = keyClass;
00190         }
00191 
00195         public Object entryToObject(TupleInput input) {
00196 
00197             try {
00198                 MarshalledKey key = (MarshalledKey) keyClass.newInstance();
00199                 key.unmarshalKey(input);
00200                 return key;
00201             } catch (IllegalAccessException e) {
00202                 throw new RuntimeExceptionWrapper(e);
00203             } catch (InstantiationException e) {
00204                 throw new RuntimeExceptionWrapper(e);
00205             }
00206         }
00207 
00211         public void objectToEntry(Object object, TupleOutput output) {
00212 
00213             MarshalledKey key = (MarshalledKey) object;
00214             key.marshalKey(output);
00215         }
00216     }
00217 
00229     private static class MarshalledEntityBinding extends TupleSerialBinding {
00230 
00234         private MarshalledEntityBinding(ClassCatalog classCatalog,
00235                                         Class entityClass) {
00236 
00237             super(classCatalog, entityClass);
00238 
00239             // The entity class will be used to instantiate the entity object.
00240             //
00241             if (!MarshalledEnt.class.isAssignableFrom(entityClass)) {
00242                 throw new IllegalArgumentException(entityClass.toString() +
00243                                        " does not implement MarshalledEnt");
00244             }
00245         }
00246 
00252         public Object entryToObject(TupleInput tupleInput, Object javaInput) {
00253 
00254             MarshalledEnt entity = (MarshalledEnt) javaInput;
00255             entity.unmarshalPrimaryKey(tupleInput);
00256             return entity;
00257         }
00258 
00262         public void objectToKey(Object object, TupleOutput output) {
00263 
00264             MarshalledEnt entity = (MarshalledEnt) object;
00265             entity.marshalPrimaryKey(output);
00266         }
00267 
00272         public Object objectToData(Object object) {
00273 
00274             return object;
00275         }
00276     }
00277 }

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