Package com.sleepycat.db

## Class SecondaryDatabase

- java.lang.Object
- - com.sleepycat.db.Database
  - - com.sleepycat.db.SecondaryDatabase

-     public class SecondaryDatabase
      extends Database

  A secondary database handle.

  Secondary databases are opened with `Environment.openSecondaryDatabase` and are always associated with a single primary database. The distinguishing characteristics of a secondary database are:

  - Records are automatically added to a secondary database when records are added, modified and deleted in the primary database. Direct calls to `put()` methods on a secondary database are prohibited.
  - The `delete` method of a secondary database will delete the primary record and as well as all its associated secondary records.
  - Calls to all `get()` methods will return the data from the associated primary database.
  - Additional `get()` method signatures are provided to return the primary key in an additional `pKey` parameter.
  - Calls to `openCursor` will return a `SecondaryCursor`, which itself has `get()` methods that return the data of the primary database and additional `get()` method signatures for returning the primary key.
  - The `openSecondaryCursor` method is provided to return a `SecondaryCursor` that doesn't require casting.

  Before opening or creating a secondary database you must implement the `SecondaryKeyCreator` interface.

  For example, to create a secondary database that supports duplicates:

          Database primaryDb; // The primary database must already be open.
          SecondaryKeyCreator keyCreator; // Your key creator implementation.
          SecondaryConfig secConfig = new SecondaryConfig();
          secConfig.setAllowCreate(true);
          secConfig.setSortedDuplicates(true);
          secConfig.setKeyCreator(keyCreator);
          SecondaryDatabase newDb = env.openSecondaryDatabase(transaction,
                                                              "myDatabaseName",
                                                              primaryDb,
                                                              secConfig)

  If a primary database is to be associated with one or more secondary databases, it may not be configured for duplicates.

  Note that the associations between primary and secondary databases are not stored persistently. Whenever a primary database is opened for write access by the application, the appropriate associated secondary databases should also be opened by the application. This is necessary to ensure data integrity when changes are made to the primary database.

- - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `SecondaryDatabase​(java.lang.String fileName, java.lang.String databaseName, Database primaryDatabase, SecondaryConfig config)` | Open a database.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `OperationStatus` | `get​(Transaction txn, DatabaseEntry key, DatabaseEntry pKey, DatabaseEntry data, LockMode lockMode)` | Retrieves the key/data pair with the given key.
    `DatabaseConfig` | `getConfig()` | Return this Database object's configuration.
    `Database` | `getPrimaryDatabase()` | Returns the primary database associated with this secondary database.
    `OperationStatus` | `getSearchBoth​(Transaction txn, DatabaseEntry key, DatabaseEntry pKey, DatabaseEntry data, LockMode lockMode)` | Retrieves the key/data pair with the specified secondary and primary key, that is, both the primary and secondary key items must match.
    `OperationStatus` | `getSearchRecordNumber​(Transaction txn, DatabaseEntry key, DatabaseEntry pKey, DatabaseEntry data, LockMode lockMode)` | Retrieves the key/data pair associated with the specific numbered record of the database.
    `SecondaryConfig` | `getSecondaryConfig()` | Returns a copy of the secondary configuration of this database.
    `Cursor` | `openCursor​(Transaction txn, CursorConfig config)` | Return a cursor into the database.
    `SecondaryCursor` | `openSecondaryCursor​(Transaction txn, CursorConfig config)` | Obtain a cursor on a database, returning a `SecondaryCursor`.

    - ### Methods inherited from class com.sleepycat.db.Database

      `append, close, close, compact, consume, convert, delete, deleteMultiple, deleteMultipleKey, exists, get, getCacheFile, getDatabaseFile, getDatabaseName, getEnvironment, getKeyRange, getSearchBoth, getSearchRecordNumber, getSlices, getStats, join, openSequence, printStats, put, putMultiple, putMultipleKey, putNoDupData, putNoOverwrite, remove, removeSequence, rename, setConfig, setMsgfile, sliceLookup, sortMultipleKeyAndData, sortMultipleKeyData, sortMultipleKeyOrData, sync, truncate, upgrade, verify`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### SecondaryDatabase

          public SecondaryDatabase​(java.lang.String fileName,
                                   java.lang.String databaseName,
                                   Database primaryDatabase,
                                   SecondaryConfig config)
                            throws DatabaseException,
                                   java.io.FileNotFoundException

      Open a database.

      The database is represented by the file and database parameters.

      The currently supported database file formats (or access methods) are Btree, Hash, Queue, and Recno. The Btree format is a representation of a sorted, balanced tree structure. The Hash format is an extensible, dynamic hashing scheme. The Queue format supports fast access to fixed-length records accessed sequentially or by logical record number. The Recno format supports fixed- or variable-length records, accessed sequentially or by logical record number, and optionally backed by a flat text file.

      Storage and retrieval are based on key/data pairs; see `DatabaseEntry` for more information.

      Opening a database is a relatively expensive operation, and maintaining a set of open databases will normally be preferable to repeatedly opening and closing the database for each new query.

      In-memory databases never intended to be preserved on disk may be created by setting both the fileName and databaseName parameters to null. Note that in-memory databases can only ever be shared by sharing the single database handle that created them, in circumstances where doing so is safe. The environment variable `TMPDIR` may be used as a directory in which to create temporary backing files.

      Parameters:
        `fileName` - The name of an underlying file that will be used to back the database. On Windows platforms, this argument will be interpreted as a UTF-8 string, which is equivalent to ASCII for Latin characters.

        `databaseName` - An optional parameter that allows applications to have multiple databases in a single file. Although no databaseName parameter needs to be specified, it is an error to attempt to open a second database in a physical file that was not initially created using a databaseName parameter. Further, the databaseName parameter is not supported by the Queue format.

        `primaryDatabase` - a database handle for the primary database that is to be indexed.

        `config` - The secondary database open attributes. If null, default attributes are used.
      Throws:
        `DatabaseException` - if a failure occurs.
        `java.io.FileNotFoundException` - if the database file does not exist

  - ### Method Detail

    - #### openCursor

          public Cursor openCursor​(Transaction txn,
                                   CursorConfig config)
                            throws DatabaseException

      Return a cursor into the database.

      Overrides:
        `openCursor` in class `Database`
      Parameters:
        `txn` - To use a cursor for writing to a transactional database, an explicit transaction must be specified. For read-only access to a transactional database, the transaction may be null. For a non-transactional database, the transaction must be null.

        To transaction-protect cursor operations, cursors must be opened and closed within the context of a transaction, and the txn parameter specifies the transaction context in which the cursor will be used.

        `config` - The cursor attributes. If null, default attributes are used.

      Returns:
        A database cursor.

      Throws:
        `DatabaseException` - if a failure occurs.

    - #### openSecondaryCursor

          public SecondaryCursor openSecondaryCursor​(Transaction txn,
                                                     CursorConfig config)
                                              throws DatabaseException

      Obtain a cursor on a database, returning a `SecondaryCursor`. Calling this method is the equivalent of calling `openCursor(com.sleepycat.db.Transaction, com.sleepycat.db.CursorConfig)` and casting the result to `SecondaryCursor`.

      Parameters:
        `txn` - To use a cursor for writing to a transactional database, an explicit transaction must be specified. For read-only access to a transactional database, the transaction may be null. For a non-transactional database, the transaction must be null.

        To transaction-protect cursor operations, cursors must be opened and closed within the context of a transaction, and the txn parameter specifies the transaction context in which the cursor will be used.

        `config` - The cursor attributes. If null, default attributes are used.

      Returns:
        A secondary database cursor.

      Throws:
        `DatabaseException` - if a failure occurs.

    - #### getPrimaryDatabase

          public Database getPrimaryDatabase()

      Returns the primary database associated with this secondary database.

      Returns:
        the primary database associated with this secondary database.

    - #### getConfig

          public DatabaseConfig getConfig()
                                   throws DatabaseException

      Return this Database object's configuration.

      This may differ from the configuration used to open this object if the database existed previously.

      Overrides:
        `getConfig` in class `Database`
      Returns:
        This Database object's configuration.

      Throws:
        `DatabaseException` - if a failure occurs.

    - #### getSecondaryConfig

          public SecondaryConfig getSecondaryConfig()
                                             throws DatabaseException

      Returns a copy of the secondary configuration of this database.

      Returns:
        a copy of the secondary configuration of this database.

      Throws:
        `DatabaseException` - if a failure occurs.

    - #### get

          public OperationStatus get​(Transaction txn,
                                     DatabaseEntry key,
                                     DatabaseEntry pKey,
                                     DatabaseEntry data,
                                     LockMode lockMode)
                              throws DatabaseException

      Retrieves the key/data pair with the given key. If the matching key has duplicate values, the first data item in the set of duplicates is returned. Retrieval of duplicates requires the use of `Cursor` operations.

      Parameters:
        `txn` - For a transactional database, an explicit transaction may be specified to transaction-protect the operation, or null may be specified to perform the operation without transaction protection. For a non-transactional database, null must be specified.

        `key` - the secondary key used as input. It must be initialized with a non-null byte array by the caller.

        `pKey` - the primary key returned as output. Its byte array does not need to be initialized by the caller.

        `data` - the primary data returned as output. Its byte array does not need to be initialized by the caller.

        `lockMode` - the locking attributes; if null, default attributes are used.

      Returns:
        `OperationStatus.NOTFOUND` if no matching key/data pair is found; `OperationStatus.KEYEMPTY` if the database is a Queue or Recno database and the specified key exists, but was never explicitly created by the application or was later deleted; otherwise, `OperationStatus.SUCCESS`.

      Throws:
        `DeadlockException` - if the operation was selected to resolve a deadlock.

        `java.lang.IllegalArgumentException` - if an invalid parameter was specified.

        `DatabaseException` - if a failure occurs.

    - #### getSearchBoth

          public OperationStatus getSearchBoth​(Transaction txn,
                                               DatabaseEntry key,
                                               DatabaseEntry pKey,
                                               DatabaseEntry data,
                                               LockMode lockMode)
                                        throws DatabaseException

      Retrieves the key/data pair with the specified secondary and primary key, that is, both the primary and secondary key items must match.

      Parameters:
        `txn` - For a transactional database, an explicit transaction may be specified to transaction-protect the operation, or null may be specified to perform the operation without transaction protection. For a non-transactional database, null must be specified.
        `key` - the secondary key used as input. It must be initialized with a non-null byte array by the caller.
        `pKey` - the primary key used as input. It must be initialized with a non-null byte array by the caller.
        `data` - the primary data returned as output. Its byte array does not need to be initialized by the caller.

        `lockMode` - the locking attributes; if null, default attributes are used.

      Returns:
        `OperationStatus.NOTFOUND` if no matching key/data pair is found; `OperationStatus.KEYEMPTY` if the database is a Queue or Recno database and the specified key exists, but was never explicitly created by the application or was later deleted; otherwise, `OperationStatus.SUCCESS`.

      Throws:
        `DeadlockException` - if the operation was selected to resolve a deadlock.

        `java.lang.IllegalArgumentException` - if an invalid parameter was specified.

        `DatabaseException` - if a failure occurs.

    - #### getSearchRecordNumber

          public OperationStatus getSearchRecordNumber​(Transaction txn,
                                                       DatabaseEntry key,
                                                       DatabaseEntry pKey,
                                                       DatabaseEntry data,
                                                       LockMode lockMode)
                                                throws DatabaseException

      Retrieves the key/data pair associated with the specific numbered record of the database.

      The data field of the specified key must be a byte array containing a record number, as described in `DatabaseEntry`. This determines the record to be retrieved.

      For this method to be called, the underlying database must be of type Btree, and it must have been configured to support record numbers.

      If this method fails for any reason, the position of the cursor will be unchanged.

      Parameters:
        `txn` - For a transactional database, an explicit transaction may be specified to transaction-protect the operation, or null may be specified to perform the operation without transaction protection. For a non-transactional database, null must be specified.
        `key` - the secondary key returned as output. Its byte array does not need to be initialized by the caller.
        `pKey` - the primary key returned as output. Its byte array does not need to be initialized by the caller.
        `data` - the primary data returned as output. Multiple results can be retrieved by passing an object that is a subclass of `MultipleEntry`, otherwise its byte array does not need to be initialized by the caller.
        `lockMode` - the locking attributes; if null, default attributes are used.
      Returns:
        `OperationStatus.NOTFOUND` if no matching key/data pair is found; `OperationStatus.KEYEMPTY` if the database is a Queue or Recno database and the specified key exists, but was never explicitly created by the application or was later deleted; otherwise, `OperationStatus.SUCCESS`.
      Throws:
        `java.lang.NullPointerException` - if a DatabaseEntry parameter is null or does not contain a required non-null byte array.

        `DeadlockException` - if the operation was selected to resolve a deadlock.

        `java.lang.IllegalArgumentException` - if an invalid parameter was specified.

        `DatabaseException` - if a failure occurs.

Copyright (c) 1996, 2020 Oracle and/or its affiliates. All rights reserved.