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.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.
WARNING: 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. If the
secondary database is not opened, it will not be updated when the primary is
updated, and the references between the databases will become invalid.
(Note that this warning does not apply when using the DPL
, which does store secondary relationships
persistently.)
Normally, during a primary database write operation (insert, update or delete), all associated secondary databases are also updated. However, when an exception occurs during the write operation, the updates may be incomplete. If the databases are transactional, this is handled by aborting the transaction to undo the incomplete operation. If an auto-commit transaction is used (null is passed for the transaction), the transaction will be aborted automatically. If an explicit transaction is used, it must be aborted by the application caller after the exception is caught.
However, if the databases are non-transactional, integrity problems can
result when an exception occurs during the write operation. Because the
write operation is not made atomic by a transaction, references between the
databases will become invalid if the operation is incomplete. This results
in a SecondaryIntegrityException
when attempting to access the
databases later.
A secondary integrity problem is persistent; it cannot be resolved by reopening the databases or the environment. The only way to resolve the problem is to restore the environment from a valid backup, or, if the integrity of the primary database is assumed, to remove and recreate all secondary databases.
Therefore, secondary databases and indexes should always be used in conjunction with transactional databases and stores. Without transactions, it is the responsibility of the application to handle the results of the incomplete write operation or to take steps to prevent this situation from happening in the first place.
The following exceptions may be thrown during a write operation, and may cause an integrity problem in the absence of transactions.
SecondaryConstraintException
, see its subclasses for more
information.LockConflictException
, when more than one thread is accessing
the databases.EnvironmentFailureException
, if an unexpected or system failure
occurs.Error
or an unintended
RuntimeException
.Modifier and Type | Method and Description |
---|---|
void |
close()
Closes a secondary database and dis-associates it from its primary
database.
|
OperationStatus |
delete(Transaction txn,
DatabaseEntry key)
Deletes the record associated with the given secondary key.
|
OperationResult |
delete(Transaction txn,
DatabaseEntry key,
WriteOptions options)
Deletes the record associated with the given secondary key.
|
OperationResult |
get(Transaction txn,
DatabaseEntry key,
DatabaseEntry pKey,
DatabaseEntry data,
Get getType,
ReadOptions options)
Retrieves a record according to the specified
Get type. |
OperationStatus |
get(Transaction txn,
DatabaseEntry key,
DatabaseEntry pKey,
DatabaseEntry data,
LockMode lockMode)
Retrieves the key/data pair with the given key.
|
OperationResult |
get(Transaction txn,
DatabaseEntry key,
DatabaseEntry data,
Get getType,
ReadOptions options)
Moves the cursor to a record according to the specified
Get
type. |
OperationStatus |
get(Transaction txn,
DatabaseEntry key,
DatabaseEntry data,
LockMode lockMode)
Retrieves the key/data pair with the given key.
|
SecondaryConfig |
getConfig()
Returns a copy of the secondary configuration of this database.
|
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()
Deprecated.
As of JE 4.0.13, replaced by
getConfig() . |
java.util.List<SecondaryDatabase> |
getSecondaryDatabases()
Returns an empty list, since this database is itself a secondary
database.
|
JoinCursor |
join(Cursor[] cursors,
JoinConfig config)
This operation is not allowed on a secondary database.
|
SecondaryCursor |
openCursor(Transaction txn,
CursorConfig cursorConfig)
Obtain a cursor on a database, returning a
SecondaryCursor . |
SecondaryCursor |
openSecondaryCursor(Transaction txn,
CursorConfig cursorConfig)
Deprecated.
As of JE 4.0.13, replaced by
openCursor(com.sleepycat.je.Transaction, com.sleepycat.je.CursorConfig) . |
OperationStatus |
put(Transaction txn,
DatabaseEntry key,
DatabaseEntry data)
This operation is not allowed on a secondary database.
|
OperationResult |
put(Transaction txn,
DatabaseEntry key,
DatabaseEntry data,
Put putType,
WriteOptions options)
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.
|
compareDuplicates, compareKeys, count, count, getDatabaseName, getEnvironment, getStats, openCursor, openSequence, preload, preload, preload, removeSequence, sync, verify
public void close()
When closing the last open handle for a deferred-write database, any
cached database information is flushed to disk as if Database.sync()
were
called.
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.
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.
The database handle may not be accessed again after this method is
called, regardless of the method's success or failure, with one
exception: the close
method itself may be called any number of
times.
WARNING: To guard against memory leaks, the application should discard all references to the closed handle. While BDB makes an effort to discard references from closed objects to the allocated memory for an environment, this behavior is not guaranteed. The safe course of action for an application is to discard all references to closed BDB objects.
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class Database
DatabaseConfig.setDeferredWrite
public Database getPrimaryDatabase()
public java.util.List<SecondaryDatabase> getSecondaryDatabases()
getSecondaryDatabases
in class Database
public SecondaryConfig getSecondaryConfig()
getConfig()
.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.public SecondaryConfig getConfig()
getConfig
in class Database
EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.public SecondaryCursor openSecondaryCursor(Transaction txn, CursorConfig cursorConfig)
openCursor(com.sleepycat.je.Transaction, com.sleepycat.je.CursorConfig)
.SecondaryCursor
. Calling this method is the equivalent of
calling openCursor(com.sleepycat.je.Transaction, com.sleepycat.je.CursorConfig)
and casting the result to SecondaryCursor
.txn
- the transaction used to protect all operations performed with
the cursor, or null if the operations should not be transaction
protected. If the database is non-transactional, null must be
specified. For a transactional database, the transaction is optional
for read-only access and required for read-write access.cursorConfig
- The cursor attributes. If null, default attributes
are used.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.public SecondaryCursor openCursor(Transaction txn, CursorConfig cursorConfig)
SecondaryCursor
.openCursor
in class Database
txn
- the transaction used to protect all operations performed with
the cursor, or null if the operations should not be transaction
protected. If the database is non-transactional, null must be
specified. For a transactional database, the transaction is optional
for read-only access and required for read-write access.cursorConfig
- The cursor attributes. If null, default attributes
are used.public OperationResult delete(Transaction txn, DatabaseEntry key, WriteOptions options)
When multiple primary records are deleted, the expiration time in the returned result is that of the last record 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, com.sleepycat.je.WriteOptions)
were called. This
includes, but is not limited to, the secondary record referenced by the
given key.
delete
in class Database
key
- the key used as
input.
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.options
- the WriteOptions, or null to use default options.public OperationStatus delete(Transaction txn, DatabaseEntry key)
When the primary records are deleted, their associated secondary
records are deleted as if Database.delete(com.sleepycat.je.Transaction, com.sleepycat.je.DatabaseEntry, com.sleepycat.je.WriteOptions)
were called. This
includes, but is not limited to, the secondary record referenced by the
given key.
Calling this method is equivalent to calling delete(Transaction, DatabaseEntry, WriteOptions)
.
delete
in class Database
key
- the key used as
input.
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.OperationStatus.NOTFOUND
if
the given key is not found in the database; otherwise OperationStatus.SUCCESS
.public OperationResult get(Transaction txn, DatabaseEntry key, DatabaseEntry data, Get getType, ReadOptions options)
Get
type.
The difference between this method and the method it overrides in
Cursor
is that the key here is defined as the secondary
records's key, and the data is defined as the primary record's data.
get
in class Database
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 input parameter.data
- the data input or output parameter, depending on getType.getType
- the Get operation type. May not be null.options
- the ReadOptions, or null to use default options.public OperationResult get(Transaction txn, DatabaseEntry key, DatabaseEntry pKey, DatabaseEntry data, Get getType, ReadOptions options)
Get
type.
If the operation succeeds, the record will be locked according to the
lock mode
specified, the key, primary
key and/or data will be returned via the (non-null) DatabaseEntry
parameters, and a non-null OperationResult will be returned. If the
operation fails because the record requested is not found, null is
returned.
The following table lists each allowed operation and whether the key,
pKey and data parameters are input
or output parameters. See the individual Get
operations for
more information.
Get operation | Description | 'key' parameter | 'pKey' parameter | 'data' parameter |
---|---|---|---|---|
Get.SEARCH |
Searches using an exact match by key. | input | output | output |
Get.SEARCH_BOTH |
Searches using an exact match by key and data. | input | input | output |
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 input parameter.pKey
- the primary key input or output parameter, depending on
getType.data
- the primary data output parameter.getType
- the Get operation type. May not be null.options
- the ReadOptions, or null to use default options.OperationFailureException
- if one of the Read Operation
Failures occurs.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.java.lang.IllegalStateException
- if the database has been closed.java.lang.IllegalArgumentException
- if an invalid parameter is specified.
This includes passing a null getType, a null input key/pKey parameter,
an input key/pKey parameter with a null data array, a partial key/pKey
input parameter, and specifying a lock mode
of READ_COMMITTED.public OperationStatus get(Transaction txn, DatabaseEntry key, DatabaseEntry data, LockMode lockMode)
Database
Cursor
operations.
Calling this method is equivalent to calling Database.get(Transaction, DatabaseEntry, DatabaseEntry, Get, ReadOptions)
with
Get.SEARCH
.
get
in class Database
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.
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.lockMode
- the locking attributes; if null, default attributes are
used.OperationStatus.NOTFOUND
if no matching key/data pair is found;
otherwise, OperationStatus.SUCCESS
.public OperationStatus get(Transaction txn, DatabaseEntry key, DatabaseEntry pKey, DatabaseEntry data, LockMode lockMode)
Cursor
operations.
Calling this method is equivalent to calling get(Transaction, DatabaseEntry, DatabaseEntry, DatabaseEntry, Get,
ReadOptions)
with Get.SEARCH
.
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.pKey
- the primary key returned as
output.data
- the primary data returned as
output.lockMode
- the locking attributes; if null, default attributes are
used.OperationStatus.NOTFOUND
if no matching key/data pair is found;
otherwise, OperationStatus.SUCCESS
.OperationFailureException
- if one of the Read Operation
Failures occurs.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.java.lang.IllegalStateException
- if the database has been closed.java.lang.IllegalArgumentException
- if an invalid parameter is specified.public OperationStatus getSearchBoth(Transaction txn, DatabaseEntry key, DatabaseEntry data, LockMode lockMode)
UnsupportedOperationException
will always be thrown by this method.
The corresponding method with the pKey
parameter should be
used instead.getSearchBoth
in class Database
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.data
- the data used as
input.lockMode
- the locking attributes; if null, default attributes are
used.OperationStatus.NOTFOUND
if no matching key/data pair is found;
otherwise, OperationStatus.SUCCESS
.public OperationStatus getSearchBoth(Transaction txn, DatabaseEntry key, DatabaseEntry pKey, DatabaseEntry data, LockMode lockMode)
Calling this method is equivalent to calling get(Transaction, DatabaseEntry, DatabaseEntry, DatabaseEntry, Get,
ReadOptions)
with Get.SEARCH_BOTH
.
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.pKey
- the primary key used as
input.data
- the primary data returned as
output.lockMode
- the locking attributes; if null, default attributes are
used.OperationStatus.NOTFOUND
if no matching key/data pair is found;
otherwise, OperationStatus.SUCCESS
.OperationFailureException
- if one of the Read Operation
Failures occurs.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.java.lang.IllegalStateException
- if the database has been closed.java.lang.IllegalArgumentException
- if an invalid parameter is specified.public OperationResult put(Transaction txn, DatabaseEntry key, DatabaseEntry data, Put putType, WriteOptions options)
UnsupportedOperationException
will always be thrown by this method.
The corresponding method on the primary database should be used instead.put
in class Database
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 used as
input.data
- the data used as
input.putType
- the Put operation type. May not be null.options
- the WriteOptions, or null to use default options.public OperationStatus put(Transaction txn, DatabaseEntry key, DatabaseEntry data)
UnsupportedOperationException
will always be thrown by this method.
The corresponding method on the primary database should be used instead.put
in class Database
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 used as
input..data
- the data used as
input.OperationStatus.SUCCESS
.public OperationStatus putNoOverwrite(Transaction txn, DatabaseEntry key, DatabaseEntry data)
UnsupportedOperationException
will always be thrown by this method.
The corresponding method on the primary database should be used instead.putNoOverwrite
in class Database
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 used as
input..data
- the data used as
input.OperationStatus.KEYEXIST
if the key already appears in the database,
else OperationStatus.SUCCESS
public OperationStatus putNoDupData(Transaction txn, DatabaseEntry key, DatabaseEntry data)
UnsupportedOperationException
will always be thrown by this method.
The corresponding method on the primary database should be used instead.putNoDupData
in class Database
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 used as
input..data
- the data used as
input.OperationStatus.KEYEXIST
if the key/data pair already appears in the
database, else OperationStatus.SUCCESS
public JoinCursor join(Cursor[] cursors, JoinConfig config)
UnsupportedOperationException
will always be thrown by this method.
The corresponding method on the primary database should be used instead.join
in class Database
cursors
- an array of cursors associated with this primary
database.config
- The join attributes. If null, default attributes are
used.JoinCursor
Copyright (c) 2002, 2017 Oracle and/or its affiliates. All rights reserved.