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 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.Database.DbStateCLOSED, configuration, envHandle, handleLocker, INVALID, OPEN| Constructor and Description |
|---|
SecondaryDatabase(Environment env,
SecondaryConfig secConfig,
Database primaryDatabase)
Creates a secondary database but does not open or fully initialize it.
|
| Modifier and Type | Method and Description |
|---|---|
(package private) void |
clearForeignKeyTrigger()
Should be called by the foreignKeyTrigger while holding a write lock on
the trigger list.
|
(package private) void |
clearPrimary()
Should be called by the secondaryTrigger while holding a write lock on
the trigger list.
|
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.
|
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()
Returns a copy of the secondary configuration of this database.
|
(package private) void |
initExisting(Environment env,
Locker locker,
DatabaseImpl database,
DatabaseConfig dbConfig)
Open a database, called by Environment
|
(package private) void |
initNew(Environment env,
Locker locker,
java.lang.String databaseName,
DatabaseConfig dbConfig)
Create a database, called by Environment
|
JoinCursor |
join(Cursor[] cursors,
JoinConfig config)
This operation is not allowed on a secondary database.
|
(package private) Cursor |
newDbcInstance(Transaction txn,
CursorConfig cursorConfig)
Overrides Database method.
|
(package private) static java.lang.UnsupportedOperationException |
notAllowedException() |
(package private) void |
onForeignKeyDelete(Locker locker,
DatabaseEntry secKey)
Called by the ForeignKeyTrigger when a record in the foreign database is
deleted.
|
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.
|
(package private) DatabaseException |
secondaryCorruptException() |
(package private) void |
trace(java.util.logging.Level level,
java.lang.String methodName)
Send trace messages to the java.util.logger.
|
(package private) void |
updateSecondary(Locker locker,
Cursor cursor,
DatabaseEntry priKey,
DatabaseEntry oldData,
DatabaseEntry newData)
Updates a single secondary when a put() or delete() is performed on the
primary.
|
addCursor, addTrigger, checkEnv, checkProhibitedDbState, checkRequiredDbState, count, deleteInternal, getConfig, getDatabaseImpl, getDatabaseName, getDebugName, getEnvironment, getSecondaryDatabases, getStats, hasTriggers, invalidate, isTransactional, isWritable, notifyTriggers, openCursor, openSequence, preload, preload, preload, putInternal, removeCursor, removeSequence, removeTrigger, setHandleLocker, sync, trace, trace, verifySecondaryDatabase(Environment env, SecondaryConfig secConfig, Database primaryDatabase) throws DatabaseException
DatabaseExceptionvoid initNew(Environment env, Locker locker, java.lang.String databaseName, DatabaseConfig dbConfig) throws DatabaseException
initNew in class DatabaseDatabaseExceptionvoid initExisting(Environment env, Locker locker, DatabaseImpl database, DatabaseConfig dbConfig) throws DatabaseException
initExisting in class DatabaseDatabaseExceptionpublic 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.
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.
close in class DatabaseDatabaseException - if a failure occurs.DatabaseConfig.setDeferredWritevoid clearPrimary()
void clearForeignKeyTrigger()
public Database getPrimaryDatabase() throws DatabaseException
DatabaseExceptionpublic SecondaryConfig getSecondaryConfig() throws DatabaseException
DatabaseException - if a failure occurs.public SecondaryConfig getPrivateSecondaryConfig()
public SecondaryCursor openSecondaryCursor(Transaction txn, CursorConfig cursorConfig) throws DatabaseException
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.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.DatabaseException - if a failure occurs.Cursor newDbcInstance(Transaction txn, CursorConfig cursorConfig) throws DatabaseException
newDbcInstance in class DatabaseDatabaseExceptionpublic OperationStatus delete(Transaction txn, DatabaseEntry key) throws DatabaseException
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 Databasetxn - 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.OperationStatus.NOTFOUND if
the specified key is not found in the database; otherwise the method
will return OperationStatus.SUCCESS.DeadlockException - if the operation was selected to resolve a
deadlock.DatabaseException - if a failure occurs.public OperationStatus get(Transaction txn, DatabaseEntry key, DatabaseEntry data, LockMode lockMode) throws DatabaseException
Cursor
operations.get in class Databasetxn - 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.OperationStatus.NOTFOUND if no matching key/data pair is found;
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 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.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 data, LockMode lockMode) throws DatabaseException
UnsupportedOperationException will always be thrown by this method.
The corresponding method with the pKey parameter should be
used instead.getSearchBoth in class Databasetxn - 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 keyused as input. It must be initialized with a non-null
byte array by the caller.data - the dataused 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.DeadlockException - if the operation was selected to resolve a
deadlock.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 keyused as input. It must be initialized with
a non-null byte array by the caller.pKey - the primary keyused 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.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 put(Transaction txn, DatabaseEntry key, DatabaseEntry data) throws DatabaseException
UnsupportedOperationException will always be thrown by this method.
The corresponding method on the primary database should be used instead.put in class Databasetxn - 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.DeadlockException - if the operation was selected to resolve a
deadlock.DatabaseException - if a failure occurs.public OperationStatus putNoOverwrite(Transaction txn, DatabaseEntry key, DatabaseEntry data) throws DatabaseException
UnsupportedOperationException will always be thrown by this method.
The corresponding method on the primary database should be used instead.putNoOverwrite in class Databasetxn - 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.SUCCESSDeadlockException - if the operation was selected to resolve a
deadlock.DatabaseException - if any other failure occurs.public OperationStatus putNoDupData(Transaction txn, DatabaseEntry key, DatabaseEntry data) throws DatabaseException
UnsupportedOperationException will always be thrown by this method.
The corresponding method on the primary database should be used instead.putNoDupData in class Databasetxn - 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.DeadlockException - if the operation was selected to resolve a
deadlock.DatabaseException - if a failure occurs.public JoinCursor join(Cursor[] cursors, JoinConfig config) throws DatabaseException
UnsupportedOperationException will always be thrown by this method.
The corresponding method on the primary database should be used instead.join in class Databasecursors - an array of cursors associated with this primary
database.config - The join attributes. If null, default attributes are
used.DatabaseException - if a failure occurs. @see JoinCursorvoid updateSecondary(Locker locker, Cursor cursor, DatabaseEntry priKey, DatabaseEntry oldData, DatabaseEntry newData) throws DatabaseException
locker - the internal locker.cursor - secondary cursor to use, or null if this method should
open and close a cursor if one is needed.priKey - the primary key.oldData - the primary data before the change, or null if the record
did not previously exist.newData - the primary data after the change, or null if the record
has been deleted.DatabaseExceptionvoid onForeignKeyDelete(Locker locker, DatabaseEntry secKey) throws DatabaseException
secKey - is the primary key of the foreign database, which is the
secondary key (ordinary key) of this secondary database.DatabaseExceptionDatabaseException secondaryCorruptException() throws DatabaseException
DatabaseExceptionstatic java.lang.UnsupportedOperationException notAllowedException()
void trace(java.util.logging.Level level,
java.lang.String methodName)
throws DatabaseException
DatabaseExceptionCopyright (c) 2004,2008 Oracle. All rights reserved.