|
Berkeley DB Java Edition version 5.0.34 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.sleepycat.je.Database
com.sleepycat.je.SecondaryDatabase
public class SecondaryDatabase
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:
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
.
Field Summary |
---|
Fields inherited from class com.sleepycat.je.Database |
---|
logger |
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. |
SecondaryConfig |
getConfig()
Returns a copy of the secondary configuration of this database. |
Database |
getPrimaryDatabase()
Returns the primary database associated with this secondary database. |
SecondaryConfig |
getPrivateSecondaryConfig()
Returns the secondary config without cloning, for internal use. |
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() . |
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. |
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. |
Methods inherited from class com.sleepycat.je.Database |
---|
compareDuplicates, compareKeys, count, 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 |
---|
public void close() throws DatabaseException
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.
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 Closeable
close
in class Database
DatabaseException
DatabaseConfig.setDeferredWrite
public Database getPrimaryDatabase()
public SecondaryConfig getSecondaryConfig() throws DatabaseException
getConfig()
.
EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.
DatabaseException
public SecondaryConfig getConfig() throws DatabaseException
getConfig
in class Database
EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.
DatabaseException
public SecondaryConfig getPrivateSecondaryConfig()
public SecondaryCursor openSecondaryCursor(Transaction txn, CursorConfig cursorConfig) throws DatabaseException
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.
DatabaseException
public SecondaryCursor openCursor(Transaction txn, CursorConfig cursorConfig) throws DatabaseException
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.
DatabasePreemptedException
- in a replicated
environment if the master has truncated, removed or renamed the
database.
OperationFailureException
- if this exception occurred earlier and
caused the transaction to be invalidated.
EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.
DatabaseException
public OperationStatus delete(Transaction txn, DatabaseEntry key) throws DeleteConstraintException, LockConflictException, DatabaseException, UnsupportedOperationException, IllegalArgumentException
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.
delete
in class Database
key
- the secondary key used as input. It must be initialized with
a non-null byte array by the caller.
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 specified key is not found in the database; otherwise the method
will return OperationStatus.SUCCESS
.
OperationFailureException
- if one of the Write
Operation Failures occurs.
EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.
UnsupportedOperationException
- if this database is read-only.
IllegalArgumentException
- if an invalid parameter is specified.
DeleteConstraintException
LockConflictException
DatabaseException
public OperationStatus get(Transaction txn, DatabaseEntry key, DatabaseEntry data, LockMode lockMode) throws DatabaseException
Database
Cursor
operations.
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
.
OperationFailureException
- if one of the Read Operation
Failures occurs.
EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.
DatabaseException
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;
otherwise, OperationStatus.SUCCESS
.
OperationFailureException
- if one of the Read Operation
Failures occurs.
EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.
IllegalStateException
- if the database has been closed.
IllegalArgumentException
- if an invalid parameter is specified.
DatabaseException
public OperationStatus getSearchBoth(Transaction txn, DatabaseEntry key, DatabaseEntry data, LockMode lockMode) throws UnsupportedOperationException
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. 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.
OperationStatus.NOTFOUND
if no matching key/data pair is found;
otherwise, OperationStatus.SUCCESS
.
UnsupportedOperationException
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;
otherwise, OperationStatus.SUCCESS
.
OperationFailureException
- if one of the Read Operation
Failures occurs.
EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.
IllegalStateException
- if the database has been closed.
IllegalArgumentException
- if an invalid parameter is specified.
DatabaseException
public OperationStatus put(Transaction txn, DatabaseEntry key, DatabaseEntry data) throws UnsupportedOperationException
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 DatabaseEntry
operated on.data
- the data DatabaseEntry
stored.
OperationStatus.SUCCESS
if the operation succeeds.
UnsupportedOperationException
- if this database is read-only.public OperationStatus putNoOverwrite(Transaction txn, DatabaseEntry key, DatabaseEntry data) throws UnsupportedOperationException
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 DatabaseEntry
operated on.data
- the data DatabaseEntry
stored.
OperationStatus.KEYEXIST
if the key already appears in the database,
else OperationStatus.SUCCESS
UnsupportedOperationException
- if this database is read-only.public OperationStatus putNoDupData(Transaction txn, DatabaseEntry key, DatabaseEntry data) throws UnsupportedOperationException
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 DatabaseEntry
operated on.data
- the data DatabaseEntry
stored.
OperationStatus.KEYEXIST
if the key/data pair already appears in the
database, else OperationStatus.SUCCESS
UnsupportedOperationException
- if this database is not configured
for duplicates, or this database is read-only.public JoinCursor join(Cursor[] cursors, JoinConfig config) throws UnsupportedOperationException
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. In a replicated environment, an explicit transaction must be
specified when opening each cursor, unless read-uncommitted isolation is
isolation is specified via the CursorConfig
or LockMode
parameter.config
- The join attributes. If null, default attributes are
used.
UnsupportedOperationException
JoinCursor
|
Berkeley DB Java Edition version 5.0.34 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |