public class SecondaryDatabase extends Database
Secondary databases are opened with Environment.openSecondaryDatabase
and are
always associated with a single primary database. The distinguishing
characteristics of a secondary database are:
put()
methods on a secondary database are prohibited.delete
method of a secondary database will delete
the primary record and as well as all its associated secondary records.get()
methods will return the data from the
associated primary database.get()
method signatures are provided to return
the primary key in an additional pKey
parameter.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.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 | Description |
---|---|
SecondaryDatabase(java.lang.String fileName,
java.lang.String databaseName,
Database primaryDatabase,
SecondaryConfig config) |
Open a database.
|
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 . |
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
public SecondaryDatabase(java.lang.String fileName, java.lang.String databaseName, Database primaryDatabase, SecondaryConfig config) throws DatabaseException, java.io.FileNotFoundException
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.
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.DatabaseException
- if a failure occurs.java.io.FileNotFoundException
- if the database file does not existpublic Cursor openCursor(Transaction txn, CursorConfig config) throws DatabaseException
openCursor
in class Database
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.
DatabaseException
- if a failure occurs.public SecondaryCursor openSecondaryCursor(Transaction txn, CursorConfig config) throws DatabaseException
SecondaryCursor
.
Calling this method is the equivalent of calling openCursor(com.sleepycat.db.Transaction, com.sleepycat.db.CursorConfig)
and
casting the result to SecondaryCursor
.
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.
DatabaseException
- if a failure occurs.public Database getPrimaryDatabase()
public DatabaseConfig getConfig() throws DatabaseException
This may differ from the configuration used to open this object if the database existed previously.
getConfig
in class Database
DatabaseException
- if a failure occurs.public SecondaryConfig getSecondaryConfig() throws DatabaseException
DatabaseException
- if a failure occurs.public OperationStatus get(Transaction txn, DatabaseEntry key, DatabaseEntry pKey, DatabaseEntry data, LockMode lockMode) throws DatabaseException
Cursor
operations.
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.
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
.
DeadlockException
- if the operation was selected to resolve a
deadlock.
java.lang.IllegalArgumentException
- if an invalid parameter was specified.
DatabaseException
- if a failure occurs.public OperationStatus getSearchBoth(Transaction txn, DatabaseEntry key, DatabaseEntry pKey, DatabaseEntry data, LockMode lockMode) throws DatabaseException
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.
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
.
DeadlockException
- if the operation was selected to resolve a
deadlock.
java.lang.IllegalArgumentException
- if an invalid parameter was specified.
DatabaseException
- if a failure occurs.public OperationStatus getSearchRecordNumber(Transaction txn, DatabaseEntry key, DatabaseEntry pKey, DatabaseEntry data, LockMode lockMode) throws DatabaseException
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.
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.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
.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.