Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Berkeley DB Java Edition version 3.0.12
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.sleepycat.je
Class SecondaryDatabase

    java.lang.Object
      com.sleepycat.je.Database
          com.sleepycat.je.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` or `SecondaryMultiKeyCreator` 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.

Method Summary
`void` | `close()` Closes a secondary database and dis-associates it from its primary database.
`OperationStatus` | `delete(Transaction txn, DatabaseEntry key)` Deletes the primary key/data pair associated with the specified secondary key.
`OperationStatus` | `get(Transaction txn, DatabaseEntry key, DatabaseEntry pKey, DatabaseEntry data, LockMode lockMode)` Retrieves the key/data pair with the given key.
`OperationStatus` | `get(Transaction txn, DatabaseEntry key, DatabaseEntry data, LockMode lockMode)` Retrieves the key/data pair with the given key.
`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` | `getSearchBoth(Transaction txn, DatabaseEntry key, DatabaseEntry data, LockMode lockMode)` This operation is not allowed with this method signature.
`SecondaryConfig` | `getSecondaryConfig()` Returns a copy of the secondary configuration of this database.
`JoinCursor` | `join(Cursor[] cursors, JoinConfig config)` This operation is not allowed on a secondary database.
`SecondaryCursor` | `openSecondaryCursor(Transaction txn, CursorConfig cursorConfig)` Obtain a cursor on a database, returning a `SecondaryCursor`.
`OperationStatus` | `put(Transaction txn, DatabaseEntry key, DatabaseEntry data)` This operation is not allowed on a secondary database.
`OperationStatus` | `putNoDupData(Transaction txn, DatabaseEntry key, DatabaseEntry data)` This operation is not allowed on a secondary database.
`OperationStatus` | `putNoOverwrite(Transaction txn, DatabaseEntry key, DatabaseEntry data)` This operation is not allowed on a secondary database.
`int` | `truncate(Transaction txn, boolean countRecords)` This operation is not allowed on a secondary database.

Methods inherited from class com.sleepycat.je.Database
`getConfig, getDatabaseName, getEnvironment, getSecondaryDatabases, getStats, openCursor, openSequence, preload, preload, preload, removeSequence, sync, verify`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Method Detail

### 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; otherwise, `OperationStatus.SUCCESS`.

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

    `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; otherwise, `OperationStatus.SUCCESS`.

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

    `IllegalArgumentException` - if an invalid parameter was specified.

    `DatabaseException` - if a failure occurs.

### getPrimaryDatabase

    public Database getPrimaryDatabase()
                                throws DatabaseException

  Returns the primary database associated with this secondary database.

  Returns:
    the primary database associated with this secondary database.
  Throws:
    `DatabaseException`

### openSecondaryCursor

    public SecondaryCursor openSecondaryCursor(Transaction txn,
                                               CursorConfig cursorConfig)
                                        throws DatabaseException

  Obtain a cursor on a database, returning a `SecondaryCursor`. Calling this method is the equivalent of calling `Database.openCursor(com.sleepycat.je.Transaction, com.sleepycat.je.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.

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

  Returns:
    A secondary database cursor.

  Throws:
    `DatabaseException` - if a failure occurs.

### close

    public void close()
               throws DatabaseException

  Closes a secondary database and dis-associates it from its primary database. A secondary database should be closed before closing its associated primary database.

  Flush any cached database information to disk and discard the database handle.

  The database handle should not be closed while any other handle that refers to it is not yet closed; for example, database handles should not be closed while cursor handles into the database remain open, or transactions that include operations on the database have not yet been committed or aborted. Specifically, this includes `Cursor` and `Transaction` handles.

  When multiple threads are using the `Database` handle concurrently, only a single thread may call this method.

  The database handle may not be accessed again after this method is called, regardless of the method's success or failure.

  When called on a database that is the primary database for a secondary index, the primary database should be closed only after all secondary indices which reference it have been closed.

  Overrides:
    `close` in class `Database`
  Throws:
    `DatabaseException` - if a failure occurs.

### delete

    public OperationStatus delete(Transaction txn,
                                  DatabaseEntry key)
                           throws DatabaseException

  Deletes the primary key/data pair associated with the specified secondary key. In the presence of duplicate key values, all primary records associated with the designated secondary key will be deleted.

  When the primary records are deleted, their associated secondary records are deleted as if `Database.delete(com.sleepycat.je.Transaction, com.sleepycat.je.DatabaseEntry)` were called. This includes, but is not limited to, the secondary record referenced by the given key.

  Overrides:
    `delete` in class `Database`
  Parameters:
    `txn` - For a transactional database, an explicit transaction may be specified, or null may be specified to use auto-commit. For a non-transactional database, null must be specified.

    `key` - the key `DatabaseEntry` operated on.

  Returns:
    The method will return `OperationStatus.NOTFOUND` if the specified key is not found in the database; otherwise the method will return `OperationStatus.SUCCESS`.

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

    `DatabaseException` - if a failure occurs.

### get

    public OperationStatus get(Transaction txn,
                               DatabaseEntry key,
                               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.

  Overrides:
    `get` in class `Database`
  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.

    `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; otherwise, `OperationStatus.SUCCESS`.

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

    `IllegalArgumentException` - if an invalid parameter was specified.

    `DatabaseException` - if a failure occurs.

### getSearchBoth

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

  This operation is not allowed with this method signature. `UnsupportedOperationException` will always be thrown by this method. The corresponding method with the `pKey` parameter should be used instead.

  Overrides:
    `getSearchBoth` in class `Database`
  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 key used as input. It must be initialized with a non-null byte array by the caller.
    `data` - the data used as input. It must be initialized with a non-null byte array by the caller.

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

  Returns:
    `OperationStatus.NOTFOUND` if no matching key/data pair is found; otherwise, `OperationStatus.SUCCESS`.

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

    `DatabaseException` - if a failure occurs.

### put

    public OperationStatus put(Transaction txn,
                               DatabaseEntry key,
                               DatabaseEntry data)
                        throws DatabaseException

  This operation is not allowed on a secondary database. `UnsupportedOperationException` will always be thrown by this method. The corresponding method on the primary database should be used instead.

  Overrides:
    `put` in class `Database`
  Parameters:
    `txn` - For a transactional database, an explicit transaction may be specified, or null may be specified to use auto-commit. For a non-transactional database, null must be specified.

    `key` - the key `DatabaseEntry` operated on.

    `data` - the data `DatabaseEntry` stored.

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

    `DatabaseException` - if a failure occurs.

### putNoOverwrite

    public OperationStatus putNoOverwrite(Transaction txn,
                                          DatabaseEntry key,
                                          DatabaseEntry data)
                                   throws DatabaseException

  This operation is not allowed on a secondary database. `UnsupportedOperationException` will always be thrown by this method. The corresponding method on the primary database should be used instead.

  Overrides:
    `putNoOverwrite` in class `Database`
  Parameters:
    `txn` - For a transactional database, an explicit transaction may be specified, or null may be specified to use auto-commit. For a non-transactional database, null must be specified.

    `key` - the key `DatabaseEntry` operated on.

    `data` - the data `DatabaseEntry` stored.

  Returns:
    If the key already appears in the database, this method will return `OperationStatus.KEYEXIST`.

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

    `DatabaseException` - if a failure occurs.

### putNoDupData

    public OperationStatus putNoDupData(Transaction txn,
                                        DatabaseEntry key,
                                        DatabaseEntry data)
                                 throws DatabaseException

  This operation is not allowed on a secondary database. `UnsupportedOperationException` will always be thrown by this method. The corresponding method on the primary database should be used instead.

  Overrides:
    `putNoDupData` in class `Database`
  Parameters:
    `txn` - For a transactional database, an explicit transaction may be specified, or null may be specified to use auto-commit. For a non-transactional database, null must be specified.

    `key` - the key `DatabaseEntry` operated on.

    `data` - the data `DatabaseEntry` stored.

  Returns:
    If the key/data pair already appears in the database, this method will return `OperationStatus.KEYEXIST`.

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

    `DatabaseException` - if a failure occurs.

### join

    public JoinCursor join(Cursor[] cursors,
                           JoinConfig config)
                    throws DatabaseException

  This operation is not allowed on a secondary database. `UnsupportedOperationException` will always be thrown by this method. The corresponding method on the primary database should be used instead.

  Overrides:
    `join` in class `Database`
  Parameters:
    `cursors` - an array of cursors associated with this primary database.

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

  Returns:
    a specialized cursor that returns the results of the equality join operation.

  Throws:
    `DatabaseException` - if a failure occurs.
  See Also:
    `JoinCursor`

### truncate

    public int truncate(Transaction txn,
                        boolean countRecords)
                 throws DatabaseException

  This operation is not allowed on a secondary database. `UnsupportedOperationException` will always be thrown by this method. The corresponding method on the primary database should be used instead.

  Overrides:
    `truncate` in class `Database`
  Parameters:
    `txn` - For a transactional database, an explicit transaction may be specified, or null may be specified to use auto-commit. For a non-transactional database, null must be specified.

    `countRecords` - If true, count and return the number of records discarded.

  Returns:
    The number of records discarded, or -1 if returnCount is false.

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

    `DatabaseException` - if a failure occurs.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Berkeley DB Java Edition version 3.0.12
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

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