SecondaryKeyCreator (Oracle - Berkeley DB Java Edition API)
Date: 2014-12-16

com.sleepycat.je

## Interface SecondaryKeyCreator

- All Known Implementing Classes:
    SerialSerialKeyCreator, TupleSerialKeyCreator, TupleSerialMarshalledKeyCreator, TupleTupleKeyCreator, TupleTupleMarshalledKeyCreator

      public interface SecondaryKeyCreator

  The interface implemented for extracting single-valued secondary keys from primary records.

  The key creator object is specified by calling `SecondaryConfig.setKeyCreator`. The secondary database configuration is specified when calling `Environment.openSecondaryDatabase`.

  For example:

           class MyKeyCreator implements SecondaryKeyCreator {
               public boolean createSecondaryKey(SecondaryDatabase secondary,
                                                   DatabaseEntry key,
                                                   DatabaseEntry data,
                                                   DatabaseEntry result) {
                   //
                   // DO HERE: Extract the secondary key from the primary key and
                   // data, and set the secondary key into the result parameter.
                   //
                   return true;
               }
           }
           ...
           SecondaryConfig secConfig = new SecondaryConfig();
           secConfig.setKeyCreator(new MyKeyCreator());
           // Now pass secConfig to Environment.openSecondaryDatabase

  Use this interface when zero or one secondary key is present in a single primary record, in other words, for many-to-one and one-to-one relationships. When more than one secondary key may be present (for many-to-many and one-to-many relationships), use the `SecondaryMultiKeyCreator` interface instead. The table below summarizes how to create all four variations of relationships.

  Relationship | Interface | Duplicates | Example
  --- | --- | --- | ---
  One-to-one | `SecondaryKeyCreator` | No | A person record with a unique social security number key.
  Many-to-one | `SecondaryKeyCreator` | Yes | A person record with a non-unique employer key.
  One-to-many | `SecondaryMultiKeyCreator` | No | A person record with multiple unique email address keys.
  Many-to-many | `SecondaryMultiKeyCreator` | Yes | A person record with multiple non-unique organization keys.

  To configure a database for duplicates. pass true to `DatabaseConfig.setSortedDuplicates(boolean)`.

  WARNING: Key creator instances are shared by multiple threads and key creator methods are called without any special synchronization. Therefore, key creators must be thread safe. In general no shared state should be used and any caching of computed values must be done with proper synchronization.

- - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `createSecondaryKey(SecondaryDatabase secondary, DatabaseEntry key, DatabaseEntry data, DatabaseEntry result)` Creates a secondary key entry, given a primary key and data entry.

- - ### Method Detail

    - #### createSecondaryKey

          boolean createSecondaryKey(SecondaryDatabase secondary,
                                   DatabaseEntry key,
                                   DatabaseEntry data,
                                   DatabaseEntry result)

      Creates a secondary key entry, given a primary key and data entry.

      A secondary key may be derived from the primary key, primary data, or a combination of the primary key and data. For secondary keys that are optional, the key creator method may return false and the key/data pair will not be indexed. To ensure the integrity of a secondary database the key creator method must always return the same result for a given set of input parameters.

      A `RuntimeException` may be thrown by this method if an error occurs attempting to create the secondary key. This exception will be thrown by the API method currently in progress, for example, a `put` method. However, this will cause the write operation to be incomplete. When databases are not configured to be transactional, caution should be used to avoid integrity problems. See Special considerations for using Secondary Databases with and without Transactions.

      Parameters:
        `secondary` - the database to which the secondary key will be added. This parameter is passed for informational purposes but is not commonly used. This parameter is always non-null.
        `key` - the primary key entry. This parameter must not be modified by this method. This parameter is always non-null.
        `data` - the primary data entry. This parameter must not be modified by this method. If `SecondaryConfig#setExtractFromPrimaryKeyOnly` is configured as `true`, the `data` param may be either null or non-null, and the implementation is expected to ignore it; otherwise, this parameter is always non-null.
        `result` - the secondary key created by this method. This parameter is always non-null.
      Returns:
        true if a key was created, or false to indicate that the key is not present.

Copyright (c) 2004, 2014 Oracle and/or its affiliates. All rights reserved.