1 8 9 package collections.ship.marshal; 10 11 import com.sleepycat.bind.EntityBinding; 12 import com.sleepycat.bind.EntryBinding; 13 import com.sleepycat.bind.serial.ClassCatalog; 14 import com.sleepycat.bind.serial.TupleSerialBinding; 15 import com.sleepycat.bind.tuple.TupleBinding; 16 import com.sleepycat.bind.tuple.TupleInput; 17 import com.sleepycat.bind.tuple.TupleOutput; 18 import com.sleepycat.collections.StoredSortedMap; 19 import com.sleepycat.collections.StoredSortedValueSet; 20 import com.sleepycat.util.RuntimeExceptionWrapper; 21 22 28 public class SampleViews { 29 30 private StoredSortedMap partMap; 31 private StoredSortedMap supplierMap; 32 private StoredSortedMap shipmentMap; 33 private StoredSortedMap shipmentByPartMap; 34 private StoredSortedMap shipmentBySupplierMap; 35 private StoredSortedMap supplierByCityMap; 36 37 40 public SampleViews(SampleDatabase db) { 41 42 ClassCatalog catalog = db.getClassCatalog(); 50 EntryBinding partKeyBinding = 51 new MarshalledKeyBinding(PartKey.class); 52 EntityBinding partDataBinding = 53 new MarshalledEntityBinding(catalog, Part.class); 54 EntryBinding supplierKeyBinding = 55 new MarshalledKeyBinding(SupplierKey.class); 56 EntityBinding supplierDataBinding = 57 new MarshalledEntityBinding(catalog, Supplier.class); 58 EntryBinding shipmentKeyBinding = 59 new MarshalledKeyBinding(ShipmentKey.class); 60 EntityBinding shipmentDataBinding = 61 new MarshalledEntityBinding(catalog, Shipment.class); 62 EntryBinding cityKeyBinding = 63 TupleBinding.getPrimitiveBinding(String .class); 64 65 partMap = 70 new StoredSortedMap(db.getPartDatabase(), 71 partKeyBinding, partDataBinding, true); 72 supplierMap = 73 new StoredSortedMap(db.getSupplierDatabase(), 74 supplierKeyBinding, supplierDataBinding, true); 75 shipmentMap = 76 new StoredSortedMap(db.getShipmentDatabase(), 77 shipmentKeyBinding, shipmentDataBinding, true); 78 shipmentByPartMap = 79 new StoredSortedMap(db.getShipmentByPartDatabase(), 80 partKeyBinding, shipmentDataBinding, true); 81 shipmentBySupplierMap = 82 new StoredSortedMap(db.getShipmentBySupplierDatabase(), 83 supplierKeyBinding, shipmentDataBinding, true); 84 supplierByCityMap = 85 new StoredSortedMap(db.getSupplierByCityDatabase(), 86 cityKeyBinding, supplierDataBinding, true); 87 } 88 89 96 99 public StoredSortedMap getPartMap() { 100 101 return partMap; 102 } 103 104 107 public StoredSortedMap getSupplierMap() { 108 109 return supplierMap; 110 } 111 112 115 public StoredSortedMap getShipmentMap() { 116 117 return shipmentMap; 118 } 119 120 123 public StoredSortedValueSet getPartSet() { 124 125 return (StoredSortedValueSet) partMap.values(); 126 } 127 128 131 public StoredSortedValueSet getSupplierSet() { 132 133 return (StoredSortedValueSet) supplierMap.values(); 134 } 135 136 139 public StoredSortedValueSet getShipmentSet() { 140 141 return (StoredSortedValueSet) shipmentMap.values(); 142 } 143 144 147 public StoredSortedMap getShipmentByPartMap() { 148 149 return shipmentByPartMap; 150 } 151 152 155 public StoredSortedMap getShipmentBySupplierMap() { 156 157 return shipmentBySupplierMap; 158 } 159 160 163 public final StoredSortedMap getSupplierByCityMap() { 164 165 return supplierByCityMap; 166 } 167 168 173 private static class MarshalledKeyBinding extends TupleBinding { 174 175 private Class keyClass; 176 177 180 private MarshalledKeyBinding(Class keyClass) { 181 182 if (!MarshalledKey.class.isAssignableFrom(keyClass)) { 185 throw new IllegalArgumentException (keyClass.toString() + 186 " does not implement MarshalledKey"); 187 } 188 this.keyClass = keyClass; 189 } 190 191 194 public Object entryToObject(TupleInput input) { 195 196 try { 197 MarshalledKey key = (MarshalledKey) keyClass.newInstance(); 198 key.unmarshalKey(input); 199 return key; 200 } catch (IllegalAccessException e) { 201 throw new RuntimeExceptionWrapper(e); 202 } catch (InstantiationException e) { 203 throw new RuntimeExceptionWrapper(e); 204 } 205 } 206 207 210 public void objectToEntry(Object object, TupleOutput output) { 211 212 MarshalledKey key = (MarshalledKey) object; 213 key.marshalKey(output); 214 } 215 } 216 217 228 private static class MarshalledEntityBinding extends TupleSerialBinding { 229 230 233 private MarshalledEntityBinding(ClassCatalog classCatalog, 234 Class entityClass) { 235 236 super(classCatalog, entityClass); 237 238 if (!MarshalledEntity.class.isAssignableFrom(entityClass)) { 241 throw new IllegalArgumentException (entityClass.toString() + 242 " does not implement MarshalledEntity"); 243 } 244 } 245 246 251 public Object entryToObject(TupleInput tupleInput, Object javaInput) { 252 253 MarshalledEntity entity = (MarshalledEntity) javaInput; 254 entity.unmarshalPrimaryKey(tupleInput); 255 return entity; 256 } 257 258 261 public void objectToKey(Object object, TupleOutput output) { 262 263 MarshalledEntity entity = (MarshalledEntity) object; 264 entity.marshalPrimaryKey(output); 265 } 266 267 271 public Object objectToData(Object object) { 272 273 return object; 274 } 275 } 276 } 277 | Popular Tags |