### Berkeley DB Reference Guide: Java API Tutorial - Tuple |

### Tuple - Creating tuple-serial entity bindings

In the prior example serial keys and serial values were used, and the SerialSerialBinding base class was used for entity bindings. In this example, tuple keys and serial values are used and therefore the TupleSerialBinding base class is used for entity bindings.

As with any entity binding, a key and value is converted to an entity in the TupleSerialBinding.dataToObject method, and from an entity to a key and value in the TupleSerialBinding.objectToKey and TupleSerialBinding.objectToValue methods. But since keys are stored as tuples, not as serialized objects, key fields are read and written using the TupleInput and TupleOutput parameters.

The SampleViews class contains the modified entity binding classes that were defined in the prior example: PartBinding, SupplierBinding and ShipmentBinding.

>     import com.sleepycat.bdb.bind.serial.TupleSerialBinding;
>     import com.sleepycat.bdb.bind.tuple.TupleFormat;
>     import com.sleepycat.bdb.bind.tuple.TupleInput;
>     import com.sleepycat.bdb.bind.tuple.TupleOutput;
>     ...
>     public class SampleViews
>     {
>         ...
>         private static class PartBinding extends TupleSerialBinding
>         {
>             private PartBinding(TupleFormat keyFormat,
>                                 SerialFormat valueFormat)
>             {
>                 super(keyFormat, valueFormat);
>             }

>             public Object dataToObject(TupleInput keyInput, Object valueInput)
>                 throws IOException
>             {
>                 String number = keyInput.readString();
>                 PartValue value = (PartValue) valueInput;
>                 return new Part(number, value.getName(), value.getColor(),
>                                 value.getWeight(), value.getCity());
>             }

>             public void objectToKey(Object object, TupleOutput output)
>                 throws IOException
>             {
>                 Part part = (Part) object;
>                 output.writeString(part.getNumber());
>             }

>             public Object objectToValue(Object object)
>                 throws IOException
>             {
>                 Part part = (Part) object;
>                 return new PartValue(part.getName(), part.getColor(),
>                                      part.getWeight(), part.getCity());
>             }
>         }

>         private static class SupplierBinding extends TupleSerialBinding
>         {
>             private SupplierBinding(TupleFormat keyFormat,
>                                     SerialFormat valueFormat)
>             {
>                 super(keyFormat, valueFormat);
>             }

>             public Object dataToObject(TupleInput keyInput, Object valueInput)
>                 throws IOException
>             {
>                 String number = keyInput.readString();
>                 SupplierValue value = (SupplierValue) valueInput;
>                 return new Supplier(number, value.getName(),
>                                     value.getStatus(), value.getCity());
>             }

>             public void objectToKey(Object object, TupleOutput output)
>                 throws IOException
>             {
>                 Supplier supplier = (Supplier) object;
>                 output.writeString(supplier.getNumber());
>             }

>             public Object objectToValue(Object object)
>                 throws IOException
>             {
>                 Supplier supplier = (Supplier) object;
>                 return new SupplierValue(supplier.getName(), supplier.getStatus(),
>                                          supplier.getCity());
>             }
>         }

>         private static class ShipmentBinding extends TupleSerialBinding
>         {
>             private ShipmentBinding(TupleFormat keyFormat,
>                                     SerialFormat valueFormat)
>             {
>                 super(keyFormat, valueFormat);
>             }

>             public Object dataToObject(TupleInput keyInput, Object valueInput)
>                 throws IOException
>             {
>                 String partNumber = keyInput.readString();
>                 String supplierNumber = keyInput.readString();
>                 ShipmentValue value = (ShipmentValue) valueInput;
>                 return new Shipment(partNumber, supplierNumber,
>                                     value.getQuantity());
>             }

>             public void objectToKey(Object object, TupleOutput output)
>                 throws IOException
>             {
>                 Shipment shipment = (Shipment) object;
>                 output.writeString(shipment.getPartNumber());
>                 output.writeString(shipment.getSupplierNumber());
>             }

>             public Object objectToValue(Object object)
>                 throws IOException
>             {
>                 Shipment shipment = (Shipment) object;
>                 return new ShipmentValue(shipment.getQuantity());
>             }
>         }
>     }

Copyright (c) 1996-2003 Sleepycat Software, Inc. - All rights reserved.