###

Berkeley DB Reference Guide:
  Java API Tutorial - Tuple

### Tuple - Creating tuple key bindings

Serial bindings were used in prior examples as key bindings, and keys were stored as serialized objects. In this example, a tuple binding is used for each key since keys will be stored as tuples. Because keys are no longer stored as serialized objects, the PartKey, SupplierKey and ShipmentKey classes no longer implement the Serializable interface (this was the only change to these classes and is not shown below).

For the Part key, Supplier key, and Shipment key, the SampleViews class was changed in this example to create a custom TupleBinding instead of a SerialBinding . The custom tuple key binding classes are defined further below.

>     import com.sleepycat.bdb.bind.tuple.TupleBinding;
>     ...
>     public class SampleViews
>     {
>         ...
>         public SampleViews(SampleDatabase db)
>         {
>             DataBinding partKeyBinding =
>                 new PartKeyBinding(db.getPartKeyFormat());
>             EntityBinding partValueBinding =
>                 new PartBinding(db.getPartKeyFormat(), db.getPartValueFormat());
>             DataBinding supplierKeyBinding =
>                 new SupplierKeyBinding(db.getSupplierKeyFormat());
>             EntityBinding supplierValueBinding =
>                 new SupplierBinding(db.getSupplierKeyFormat(),
>                                     db.getSupplierValueFormat());
>             DataBinding shipmentKeyBinding =
>                 new ShipmentKeyBinding(db.getShipmentKeyFormat());
>             EntityBinding shipmentValueBinding =
>                 new ShipmentBinding(db.getShipmentKeyFormat(),
>                                     db.getShipmentValueFormat());
>             DataBinding cityKeyBinding =
>                 TupleBinding.getPrimitiveBinding(String.class,
>                                                  db.getCityKeyFormat());
>             ...
>         }
>     }

For the City key, however, a custom binding class is not needed because the key class is a primitive Java type, String . For any primitive Java type, a tuple binding may be created using the TupleBinding.getPrimitiveBinding static method.

The custom key binding classes, PartKeyBinding, SupplierKeyBinding and ShipmentKeyBinding, are defined by extending the TupleBinding class. The TupleBinding abstract class implements the DataBinding interface, and is used for one-to-one bindings between tuples and objects. Each binding class implements two methods for converting between tuples and objects. Tuple fields are read using the TupleInput parameter and written using the TupleOutput parameter.

>     import com.sleepycat.bdb.bind.tuple.TupleBinding;
>     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 PartKeyBinding extends TupleBinding
>         {
>             private PartKeyBinding(TupleFormat format)
>             {
>                 super(format);
>             }

>             public Object dataToObject(TupleInput input)
>                 throws IOException
>             {
>                 String number = input.readString();
>                 return new PartKey(number);
>             }

>             public void objectToData(Object object, TupleOutput output)
>                 throws IOException
>             {
>                 PartKey key = (PartKey) object;
>                 output.writeString(key.getNumber());
>             }
>         }
>         ...
>         private static class SupplierKeyBinding extends TupleBinding
>         {
>             private SupplierKeyBinding(TupleFormat format)
>             {
>                 super(format);
>             }

>             public Object dataToObject(TupleInput input)
>                 throws IOException
>             {
>                 String number = input.readString();
>                 return new SupplierKey(number);
>             }

>             public void objectToData(Object object, TupleOutput output)
>                 throws IOException
>             {
>                 SupplierKey key = (SupplierKey) object;
>                 output.writeString(key.getNumber());
>             }
>         }
>         ...
>         private static class ShipmentKeyBinding extends TupleBinding
>         {
>             private ShipmentKeyBinding(TupleFormat format)
>             {
>                 super(format);
>             }

>             public Object dataToObject(TupleInput input)
>                 throws IOException
>             {
>                 String partNumber = input.readString();
>                 String supplierNumber = input.readString();
>                 return new ShipmentKey(partNumber, supplierNumber);
>             }

>             public void objectToData(Object object, TupleOutput output)
>                 throws IOException
>             {
>                 ShipmentKey key = (ShipmentKey) object;
>                 output.writeString(key.getPartNumber());
>                 output.writeString(key.getSupplierNumber());
>             }
>         }
>     }

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