###

Berkeley DB Reference Guide:
  Java API Tutorial - Tuple

### Tuple - Using tuples with key extractors

Key extractors were used in prior examples to extract index keys from value objects. The keys were returned as deserialized key objects, since the serial format was used for keys. In this example, the tuple format is used for keys and the key extractors return keys by writing information to a tuple. The differences between this example and the prior example are:

- The TupleSerialKeyExtractor base class is used instead of the SerialSerialKeyExtractor base class.
- For all keys, the TupleFormat class is used instead of the SerialFormat class.
- For all key input and output parameters, the TupleInput and TupleOutput classes are used instead of Object (representing a deserialized object).
- Instead of returning a key output object, these methods call tuple write methods such as TupleOutput.writeString .

In addition to writing key tuples, the ShipmentByPartExtractor and ShipmentBySupplierExtractor classes also read the key tuple of the primary key. This is because they extract the index key from fields in the Shipment's primary key. Instead of calling getter methods on the ShipmentKey object, as in prior examples, these methods call TupleInput.readString . The ShipmentKey consists of two string fields that are read in sequence.

The modified key extractors are shown below: SupplierByCityExtractor, ShipmentByPartExtractor and ShipmentBySupplierExtractor.

>     import com.sleepycat.bdb.bind.serial.TupleSerialKeyExtractor;
>     import com.sleepycat.bdb.bind.tuple.TupleFormat;
>     import com.sleepycat.bdb.bind.tuple.TupleInput;
>     import com.sleepycat.bdb.bind.tuple.TupleOutput;
>     ...
>     public class SampleDatabase
>     {
>         private static class SupplierByCityExtractor
>             extends TupleSerialKeyExtractor
>         {
>             private SupplierByCityExtractor(TupleFormat primaryKeyFormat,
>                                             SerialFormat valueFormat,
>                                             TupleFormat indexKeyFormat)
>             {
>                 super(primaryKeyFormat, valueFormat, indexKeyFormat);
>             }

>             public void extractIndexKey(TupleInput primaryKeyInput,
>                                         Object valueInput,
>                                         TupleOutput indexKeyOutput)
>                 throws IOException
>             {
>                 SupplierValue supplierValue = (SupplierValue) valueInput;
>                 indexKeyOutput.writeString(supplierValue.getCity());
>             }

>             public void clearIndexKey(Object valueInputOutput)
>                 throws IOException
>             {
>                 throw new UnsupportedOperationException();
>             }
>         }

>         private static class ShipmentByPartExtractor
>             extends TupleSerialKeyExtractor
>         {
>             private ShipmentByPartExtractor(TupleFormat primaryKeyFormat,
>                                             SerialFormat valueFormat,
>                                             TupleFormat indexKeyFormat)
>             {
>                 super(primaryKeyFormat, valueFormat, indexKeyFormat);
>             }

>             public void extractIndexKey(TupleInput primaryKeyInput,
>                                         Object valueInput,
>                                         TupleOutput indexKeyOutput)
>                 throws IOException
>             {
>                 String partNumber = primaryKeyInput.readString();
>                 // don't bother reading the supplierNumber
>                 indexKeyOutput.writeString(partNumber);
>             }

>             public void clearIndexKey(Object valueInputOutput)
>                 throws IOException
>             {
>                 throw new UnsupportedOperationException();
>             }
>         }

>         private static class ShipmentBySupplierExtractor
>             extends TupleSerialKeyExtractor
>         {
>             private ShipmentBySupplierExtractor(TupleFormat primaryKeyFormat,
>                                                 SerialFormat valueFormat,
>                                                 TupleFormat indexKeyFormat)
>             {
>                 super(primaryKeyFormat, valueFormat, indexKeyFormat);
>             }

>             public void extractIndexKey(TupleInput primaryKeyInput,
>                                         Object valueInput,
>                                         TupleOutput indexKeyOutput)
>                 throws IOException
>             {
>                 primaryKeyInput.readString(); // skip the partNumber
>                 String supplierNumber = primaryKeyInput.readString();
>                 indexKeyOutput.writeString(supplierNumber);
>             }

>             public void clearIndexKey(Object valueInputOutput)
>                 throws IOException
>             {
>                 throw new UnsupportedOperationException();
>             }
>         }
>     }

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