###

Berkeley DB Reference Guide:
  Java API Tutorial - Tuple

### Tuple - Compact ordered keys

Java API tuples are sequences of primitive Java data types, for example, integers and strings. The tuple format is a binary format for tuples that can be used to store keys and/or values.

Tuples are useful as keys because they have a meaningful sort order, while serialized objects do not. This is because the binary data for a tuple is written in such a way that its raw byte ordering provides a useful sort order. For example, strings in tuples are written with a null terminator rather than with a leading length.

Tuples are useful as keys or values when reducing the record size to a minimum is important. A tuple is significantly smaller than an equivalent serialized object. However, unlike serialized objects, tuples cannot contain complex data types and are not easily extended except by adding fields at the end of the tuple.

Whenever a tuple format is used, except when the key or value class is a Java primitive class, a tuple binding class must be implemented to map between the Java object and the tuple fields. Because of this extra requirement, and because tuples are not easily extended, a useful technique shown in this example is to use tuples for keys and serialized objects for values. This provides compact ordered keys but still allows arbitrary Java objects as values, and avoids implementing a tuple binding for each value class.

The example program illustrates:

- Using the tuple format
- Using tuples with key extractors
- Creating tuple key bindings
- Creating tuple-serial entity bindings
- Using sorted collections

Compare this example to the prior Entity example and you'll see that the Sample class has not changed. When changing a database format, while new bindings are needed to map key and value objects to the new format, the application using the objects often does not need to be modified.

The complete source of the final version of the example program is included in the Berkeley DB distribution.

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