Library Version 11.2.5.3

Using Tuples with Key Creators
Prev | Chapter 5. Using Tuples | Next

## Using Tuples with Key Creators

Key creators 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 creators return keys by writing information to a tuple. The differences between this example and the prior example are:

- The TupleSerialKeyCreator base class is used instead of the SerialSerialKeyCreator base 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 `ShipmentByPartKeyCreator` and `ShipmentBySupplierKeyCreator` 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 creators are shown below: `SupplierByCityKeyCreator`, `ShipmentByPartKeyCreator` and `ShipmentBySupplierKeyCreator`.

    import com.sleepycat.bind.serial.TupleSerialKeyCreator;
    import com.sleepycat.bind.tuple.TupleInput;
    import com.sleepycat.bind.tuple.TupleOutput;
    ...
    public class SampleDatabase
    {
        ...
        private static class SupplierByCityKeyCreator
            extends TupleSerialKeyCreator
        {
            private SupplierByCityKeyCreator(ClassCatalog catalog,
                                             Class valueClass)
            {
                super(catalog, valueClass);
            }

            public boolean createSecondaryKey(TupleInput primaryKeyInput,
                                              Object valueInput,
                                              TupleOutput indexKeyOutput)
            {
                SupplierData supplierData = (SupplierData) valueInput;
                String city = supplierData.getCity();
                if (city != null) {
                    indexKeyOutput.writeString(supplierData.getCity());
                    return true;
                } else {
                    return false;
                }
            }
        }

        private static class ShipmentByPartKeyCreator
            extends TupleSerialKeyCreator
        {
            private ShipmentByPartKeyCreator(ClassCatalog catalog,
                                             Class valueClass)
            {
                super(catalog, valueClass);
            }

            public boolean createSecondaryKey(TupleInput primaryKeyInput,
                                              Object valueInput,
                                              TupleOutput indexKeyOutput)
            {
                String partNumber = primaryKeyInput.readString();
                // don't bother reading the supplierNumber
                indexKeyOutput.writeString(partNumber);
                return true;
            }
        }

        private static class ShipmentBySupplierKeyCreator
            extends TupleSerialKeyCreator
        {
            private ShipmentBySupplierKeyCreator(ClassCatalog catalog,
                                                 Class valueClass)
            {
                super(catalog, valueClass);
            }

            public boolean createSecondaryKey(TupleInput primaryKeyInput,
                                              Object valueInput,
                                              TupleOutput indexKeyOutput)
            {
                primaryKeyInput.readString(); // skip the partNumber
                String supplierNumber = primaryKeyInput.readString();
                indexKeyOutput.writeString(supplierNumber);
                return true;
            }
        }
        ...
    }

Prev | Up | Next
Chapter 5. Using Tuples | Home | Creating Tuple Key Bindings