2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 2002, 2012 Oracle and/or its affiliates. All rights reserved.
9 package collections.ship.sentity;
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;
22 * SampleViews defines the data bindings and collection views for the sample
27 public class SampleViews {
29 private StoredSortedMap partMap;
30 private StoredSortedMap supplierMap;
31 private StoredSortedMap shipmentMap;
32 private StoredSortedMap shipmentByPartMap;
33 private StoredSortedMap shipmentBySupplierMap;
34 private StoredSortedMap supplierByCityMap;
37 * Create the data bindings and collection views.
39 public SampleViews(SampleDatabase db) {
41 // Create the data bindings.
42 // In this sample, EntityBinding classes are used to bind the stored
43 // key/data entry pair to a combined data object; a "tricky" binding
44 // that uses transient fields is used--see PartBinding, etc, for
45 // details. For keys, a one-to-one binding is implemented with
46 // EntryBinding classes to bind the stored tuple entry to a key Object.
48 ClassCatalog catalog = db.getClassCatalog();
49 EntryBinding partKeyBinding =
51 EntityBinding partDataBinding =
52 new PartBinding(catalog, Part.class);
53 EntryBinding supplierKeyBinding =
54 new SupplierKeyBinding();
55 EntityBinding supplierDataBinding =
56 new SupplierBinding(catalog, Supplier.class);
57 EntryBinding shipmentKeyBinding =
58 new ShipmentKeyBinding();
59 EntityBinding shipmentDataBinding =
60 new ShipmentBinding(catalog, Shipment.class);
61 EntryBinding cityKeyBinding =
62 TupleBinding.getPrimitiveBinding(String.class);
64 // Create map views for all stores and indices.
65 // StoredSortedMap is used since the stores and indices are ordered
66 // (they use the DB_BTREE access method).
69 new StoredSortedMap(db.getPartDatabase(),
70 partKeyBinding, partDataBinding, true);
72 new StoredSortedMap(db.getSupplierDatabase(),
73 supplierKeyBinding, supplierDataBinding, true);
75 new StoredSortedMap(db.getShipmentDatabase(),
76 shipmentKeyBinding, shipmentDataBinding, true);
78 new StoredSortedMap(db.getShipmentByPartDatabase(),
79 partKeyBinding, shipmentDataBinding, true);
80 shipmentBySupplierMap =
81 new StoredSortedMap(db.getShipmentBySupplierDatabase(),
82 supplierKeyBinding, shipmentDataBinding, true);
84 new StoredSortedMap(db.getSupplierByCityDatabase(),
85 cityKeyBinding, supplierDataBinding, true);
88 // The views returned below can be accessed using the java.util.Map or
89 // java.util.Set interfaces, or using the StoredSortedMap and
90 // StoredValueSet classes, which provide additional methods. The entity
91 // sets could be obtained directly from the Map.values() method but
92 // convenience methods are provided here to return them in order to avoid
93 // down-casting elsewhere.
96 * Return a map view of the part storage container.
98 public StoredSortedMap getPartMap() {
104 * Return a map view of the supplier storage container.
106 public StoredSortedMap getSupplierMap() {
112 * Return a map view of the shipment storage container.
114 public StoredSortedMap getShipmentMap() {
120 * Return an entity set view of the part storage container.
122 public StoredSortedValueSet getPartSet() {
124 return (StoredSortedValueSet) partMap.values();
128 * Return an entity set view of the supplier storage container.
130 public StoredSortedValueSet getSupplierSet() {
132 return (StoredSortedValueSet) supplierMap.values();
136 * Return an entity set view of the shipment storage container.
138 public StoredSortedValueSet getShipmentSet() {
140 return (StoredSortedValueSet) shipmentMap.values();
144 * Return a map view of the shipment-by-part index.
146 public StoredSortedMap getShipmentByPartMap() {
148 return shipmentByPartMap;
152 * Return a map view of the shipment-by-supplier index.
154 public StoredSortedMap getShipmentBySupplierMap() {
156 return shipmentBySupplierMap;
160 * Return a map view of the supplier-by-city index.
162 public final StoredSortedMap getSupplierByCityMap() {
164 return supplierByCityMap;
168 * PartKeyBinding is used to bind the stored key tuple entry for a part to
169 * a key object representation.
171 private static class PartKeyBinding extends TupleBinding {
174 * Construct the binding object.
176 private PartKeyBinding() {
180 * Create the key object from the stored key tuple entry.
182 public Object entryToObject(TupleInput input) {
184 String number = input.readString();
185 return new PartKey(number);
189 * Create the stored key tuple entry from the key object.
191 public void objectToEntry(Object object, TupleOutput output) {
193 PartKey key = (PartKey) object;
194 output.writeString(key.getNumber());
199 * PartBinding is used to bind the stored key/data entry pair for a part
200 * to a combined data object (entity).
202 * <p> The binding is "tricky" in that it uses the Part class for both the
203 * stored data entry and the combined entity object. To do this, Part's
204 * key field(s) are transient and are set by the binding after the data
205 * object has been deserialized. This avoids the use of a PartData class
208 private static class PartBinding extends TupleSerialBinding {
211 * Construct the binding object.
213 private PartBinding(ClassCatalog classCatalog, Class dataClass) {
215 super(classCatalog, dataClass);
219 * Create the entity by combining the stored key and data.
220 * This "tricky" binding returns the stored data as the entity, but
221 * first it sets the transient key fields from the stored key.
223 public Object entryToObject(TupleInput keyInput, Object dataInput) {
225 String number = keyInput.readString();
226 Part part = (Part) dataInput;
232 * Create the stored key from the entity.
234 public void objectToKey(Object object, TupleOutput output) {
236 Part part = (Part) object;
237 output.writeString(part.getNumber());
241 * Return the entity as the stored data. There is nothing to do here
242 * since the entity's key fields are transient.
244 public Object objectToData(Object object) {
251 * SupplierKeyBinding is used to bind the stored key tuple entry for a
252 * supplier to a key object representation.
254 private static class SupplierKeyBinding extends TupleBinding {
257 * Construct the binding object.
259 private SupplierKeyBinding() {
263 * Create the key object from the stored key tuple entry.
265 public Object entryToObject(TupleInput input) {
267 String number = input.readString();
268 return new SupplierKey(number);
272 * Create the stored key tuple entry from the key object.
274 public void objectToEntry(Object object, TupleOutput output) {
276 SupplierKey key = (SupplierKey) object;
277 output.writeString(key.getNumber());
282 * SupplierBinding is used to bind the stored key/data entry pair for a
283 * supplier to a combined data object (entity).
285 * <p> The binding is "tricky" in that it uses the Supplier class for both
286 * the stored data entry and the combined entity object. To do this,
287 * Supplier's key field(s) are transient and are set by the binding after
288 * the data object has been deserialized. This avoids the use of a
289 * SupplierData class completely. </p>
291 private static class SupplierBinding extends TupleSerialBinding {
294 * Construct the binding object.
296 private SupplierBinding(ClassCatalog classCatalog, Class dataClass) {
298 super(classCatalog, dataClass);
302 * Create the entity by combining the stored key and data.
303 * This "tricky" binding returns the stored data as the entity, but
304 * first it sets the transient key fields from the stored key.
306 public Object entryToObject(TupleInput keyInput, Object dataInput) {
308 String number = keyInput.readString();
309 Supplier supplier = (Supplier) dataInput;
310 supplier.setKey(number);
315 * Create the stored key from the entity.
317 public void objectToKey(Object object, TupleOutput output) {
319 Supplier supplier = (Supplier) object;
320 output.writeString(supplier.getNumber());
324 * Return the entity as the stored data. There is nothing to do here
325 * since the entity's key fields are transient.
327 public Object objectToData(Object object) {
334 * ShipmentKeyBinding is used to bind the stored key tuple entry for a
335 * shipment to a key object representation.
337 private static class ShipmentKeyBinding extends TupleBinding {
340 * Construct the binding object.
342 private ShipmentKeyBinding() {
346 * Create the key object from the stored key tuple entry.
348 public Object entryToObject(TupleInput input) {
350 String partNumber = input.readString();
351 String supplierNumber = input.readString();
352 return new ShipmentKey(partNumber, supplierNumber);
356 * Create the stored key tuple entry from the key object.
358 public void objectToEntry(Object object, TupleOutput output) {
360 ShipmentKey key = (ShipmentKey) object;
361 output.writeString(key.getPartNumber());
362 output.writeString(key.getSupplierNumber());
367 * ShipmentBinding is used to bind the stored key/data entry pair for a
368 * shipment to a combined data object (entity).
370 * <p> The binding is "tricky" in that it uses the Shipment class for both
371 * the stored data entry and the combined entity object. To do this,
372 * Shipment's key field(s) are transient and are set by the binding after
373 * the data object has been deserialized. This avoids the use of a
374 * ShipmentData class completely. </p>
376 private static class ShipmentBinding extends TupleSerialBinding {
379 * Construct the binding object.
381 private ShipmentBinding(ClassCatalog classCatalog, Class dataClass) {
383 super(classCatalog, dataClass);
387 * Create the entity by combining the stored key and data.
388 * This "tricky" binding returns the stored data as the entity, but
389 * first it sets the transient key fields from the stored key.
391 public Object entryToObject(TupleInput keyInput, Object dataInput) {
393 String partNumber = keyInput.readString();
394 String supplierNumber = keyInput.readString();
395 Shipment shipment = (Shipment) dataInput;
396 shipment.setKey(partNumber, supplierNumber);
401 * Create the stored key from the entity.
403 public void objectToKey(Object object, TupleOutput output) {
405 Shipment shipment = (Shipment) object;
406 output.writeString(shipment.getPartNumber());
407 output.writeString(shipment.getSupplierNumber());
411 * Return the entity as the stored data. There is nothing to do here
412 * since the entity's key fields are transient.
414 public Object objectToData(Object object) {