public class Database extends Object implements Closeable
Database attributes are specified in the DatabaseConfig
class. Database handles are
free-threaded and may be used concurrently by multiple threads.
To open an existing database with default attributes:
Environment env = new Environment(home, null); Database myDatabase = env.openDatabase(null, "mydatabase", null);
To create a transactional database that supports duplicates:
DatabaseConfig dbConfig = new DatabaseConfig(); dbConfig.setTransactional(true); dbConfig.setAllowCreate(true); dbConfig.setSortedDuplicates(true); Database db = env.openDatabase(txn, "mydatabase", dbConfig);
Modifier and Type | Method and Description |
---|---|
void |
close()
Discards the database handle.
|
int |
compareDuplicates(DatabaseEntry entry1,
DatabaseEntry entry2)
Compares two data elements using either the default comparator if no
duplicate comparator has been set or the duplicate comparator if one has
been set.
|
int |
compareKeys(DatabaseEntry entry1,
DatabaseEntry entry2)
Compares two keys using either the default comparator if no BTree
comparator has been set or the BTree comparator if one has been set.
|
long |
count()
Counts the key/data pairs in the database.
|
long |
count(long memoryLimit)
Counts the key/data pairs in the database.
|
OperationStatus |
delete(Transaction txn,
DatabaseEntry key)
Removes key/data pairs from the database.
|
OperationStatus |
get(Transaction txn,
DatabaseEntry key,
DatabaseEntry data,
LockMode lockMode)
Retrieves the key/data pair with the given key.
|
DatabaseConfig |
getConfig()
Returns this Database object's configuration.
|
String |
getDatabaseName()
Returns the database name.
|
Environment |
getEnvironment()
Returns the
Environment handle for
the database environment underlying the Database . |
OperationStatus |
getSearchBoth(Transaction txn,
DatabaseEntry key,
DatabaseEntry data,
LockMode lockMode)
Retrieves the key/data pair with the given key and data value, that is,
both the key and data items must match.
|
List<SecondaryDatabase> |
getSecondaryDatabases()
Returns a list of all
SecondaryDatabase objects associated with a primary database. |
DatabaseStats |
getStats(StatsConfig config)
Returns database statistics.
|
JoinCursor |
join(Cursor[] cursors,
JoinConfig config)
Creates a specialized join cursor for use in performing equality or
natural joins on secondary indices.
|
DiskOrderedCursor |
openCursor(DiskOrderedCursorConfig cursorConfig)
Create a DiskOrderedCursor to iterate over the records in 'this'
Database.
|
Cursor |
openCursor(Transaction txn,
CursorConfig cursorConfig)
Returns a cursor into the database.
|
Sequence |
openSequence(Transaction txn,
DatabaseEntry key,
SequenceConfig config)
Opens a sequence in the database.
|
boolean |
populateSecondaries(DatabaseEntry key,
int batchSize) |
void |
populateSecondaries(Transaction txn,
DatabaseEntry key,
DatabaseEntry data) |
void |
preload(long maxBytes)
Deprecated.
As of JE 2.0.83, replaced by
preload(PreloadConfig) . |
void |
preload(long maxBytes,
long maxMillisecs)
Deprecated.
As of JE 2.0.101, replaced by
preload(PreloadConfig) . |
PreloadStats |
preload(PreloadConfig config)
Preloads the cache.
|
OperationStatus |
put(Transaction txn,
DatabaseEntry key,
DatabaseEntry data)
Stores the key/data pair into the database.
|
OperationStatus |
putNoDupData(Transaction txn,
DatabaseEntry key,
DatabaseEntry data)
Stores the key/data pair into the database if it does not already appear
in the database.
|
OperationStatus |
putNoOverwrite(Transaction txn,
DatabaseEntry key,
DatabaseEntry data)
Stores the key/data pair into the database if the key does not already
appear in the database.
|
void |
removeSequence(Transaction txn,
DatabaseEntry key)
Removes the sequence from the database.
|
void |
sync()
Flushes any cached information for this database to disk; only
applicable for deferred-write databases.
|
DatabaseStats |
verify(VerifyConfig config)
Verifies the integrity of the database.
|
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 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 Closeable
close
in interface AutoCloseable
EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.IllegalStateException
- if cursors associated with this database
are still open.DatabaseException
DatabaseConfig.setDeferredWrite
public void sync() throws DatabaseException, UnsupportedOperationException
Note that deferred-write databases are automatically flushed to disk
when the close()
method is called.
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.UnsupportedOperationException
- if this is not a deferred-write
database, or this database is read-only.IllegalStateException
- if the database has been closed.DatabaseException
DatabaseConfig.setDeferredWrite
public Sequence openSequence(Transaction txn, DatabaseEntry key, SequenceConfig config) throws SequenceNotFoundException, SequenceExistsException
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
of the sequence.config
- The sequence attributes. If null, default attributes are
used.SequenceExistsException
- if the sequence record already exists
and the SequenceConfig ExclusiveCreate
parameter is true.SequenceNotFoundException
- if the sequence record does not exist
and the SequenceConfig AllowCreate
parameter is false.OperationFailureException
- if one of the Read Operation
Failures occurs. If the sequence does not exist and the AllowCreate
parameter is true, then one
of the Write
Operation Failures may also occur.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.UnsupportedOperationException
- if this database is read-only, or
this database is configured for duplicates.IllegalStateException
- if the Sequence record is deleted by
another thread during this method invocation, or the database has been
closed.IllegalArgumentException
- if an invalid parameter is specified,
for example, an invalid SequenceConfig
parameter.public void removeSequence(Transaction txn, DatabaseEntry key) throws DatabaseException
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
of the sequence.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.DatabaseException
public Cursor openCursor(Transaction txn, CursorConfig cursorConfig) throws DatabaseException, IllegalArgumentException
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.IllegalStateException
- if the database has been closed.IllegalArgumentException
- if an invalid parameter is specified,
for example, an invalid CursorConfig
parameter.DatabaseException
public DiskOrderedCursor openCursor(DiskOrderedCursorConfig cursorConfig) throws DatabaseException, IllegalArgumentException
See DiskOrderedCursor
for more details and a description of the
consistency guarantees provided by the scan.
WARNING: After calling this method, deletion of log files by
the JE log cleaner will be disabled until DiskOrderedCursor.close()
is called. To prevent unbounded growth of
disk usage, be sure to call DiskOrderedCursor.close()
to
re-enable log file deletion.
public void populateSecondaries(Transaction txn, DatabaseEntry key, DatabaseEntry data)
txn
- is the transaction to be used to write secondary records. If
null and the secondary DBs are transactional, auto-commit will be used.key
- is the key of the locked primary record.data
- is the data of the locked primary record.public boolean populateSecondaries(DatabaseEntry key, int batchSize)
key
- contains the starting key for the batch of records to be
processed when this method is called, and contains the next key to be
processed when this method returns. If key.getData() == null
when this method is called, the batch will begin with the first record
in the database.batchSize
- is the maximum number of records to be read. The
maximum number of secondary inserts and deletions that will be included
in a single transaction is the batchSize times the number of secondary
databases (associated with this primary database) that are enabled for
incremental population.public OperationStatus delete(Transaction txn, DatabaseEntry key) throws DeleteConstraintException, LockConflictException, DatabaseException, UnsupportedOperationException, IllegalArgumentException
The key/data pair associated with the specified key is discarded from the database. In the presence of duplicate key values, all records associated with the designated key will be discarded.
The key/data pair is also deleted from any associated secondary databases.
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.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.IllegalStateException
- if the database has been closed.IllegalArgumentException
- if an invalid parameter is specified.DeleteConstraintException
LockConflictException
DatabaseException
public OperationStatus get(Transaction txn, DatabaseEntry key, DatabaseEntry data, LockMode lockMode) throws LockConflictException, DatabaseException, IllegalArgumentException
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 key used as input. It must be initialized with a
non-null byte array by the caller.data
- the data returned as output. Its byte array does not need
to be initialized by the caller.
A partial data item may be
specified to optimize for key only or partial data retrieval.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.LockConflictException
DatabaseException
public OperationStatus getSearchBoth(Transaction txn, DatabaseEntry key, DatabaseEntry data, LockMode lockMode) throws LockConflictException, DatabaseException, IllegalArgumentException
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
.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.LockConflictException
DatabaseException
public OperationStatus put(Transaction txn, DatabaseEntry key, DatabaseEntry data) throws DatabaseException
If the key already appears in the database and duplicates are not configured, the data associated with the key will be replaced. If the key already appears in the database and sorted duplicates are configured, the new data value is inserted at the correct sorted location.
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.OperationFailureException
- if one of the Write
Operation Failures occurs.OperationFailureException
- if this exception occurred earlier and
caused the transaction to be invalidated.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.UnsupportedOperationException
- if this database is read-only.IllegalStateException
- if the database has been closed.DatabaseException
public OperationStatus putNoOverwrite(Transaction txn, DatabaseEntry key, DatabaseEntry data) throws DatabaseException
This method will return OpeationStatus.KEYEXIST
if
the key already exists in the database, even if the database supports
duplicates.
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
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.IllegalStateException
- if the database has been closed.DatabaseException
public OperationStatus putNoDupData(Transaction txn, DatabaseEntry key, DatabaseEntry data) throws DatabaseException
This method may only be called if the underlying database has been configured to support sorted duplicates.
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
OperationFailureException
- if one of the Write
Operation Failures occurs.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.UnsupportedOperationException
- if this database is not configured
for duplicates, or this database is read-only.IllegalStateException
- if the database has been closed.DatabaseException
public JoinCursor join(Cursor[] cursors, JoinConfig config) throws DatabaseException, IllegalArgumentException
Each cursor in the cursors
array must have been
initialized to refer to the key on which the underlying database should
be joined. Typically, this initialization is done by calling Cursor.getSearchKey
.
Once the cursors have been passed to this method, they should not be accessed or modified until the newly created join cursor has been closed, or else inconsistent results may be returned. However, the position of the cursors will not be changed by this method or by the methods of the join cursor.
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.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,
for example, an invalid JoinConfig
parameter.DatabaseException
JoinCursor
public void preload(long maxBytes) throws DatabaseException
preload(PreloadConfig)
.maxBytes
- The maximum number of bytes to load. If maxBytes is 0,
je.evictor.maxMemory is used.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.DatabaseException
public void preload(long maxBytes, long maxMillisecs) throws DatabaseException
preload(PreloadConfig)
.maxBytes
- The maximum number of bytes to load. If maxBytes is 0,
je.evictor.maxMemory is used.maxMillisecs
- The maximum time in milliseconds to use when
preloading. Preloading stops once this limit has been reached. If
maxMillisecs is 0, preloading can go on indefinitely or until maxBytes
(if non-0) is reached.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.DatabaseException
public PreloadStats preload(PreloadConfig config) throws DatabaseException
ReplicationConfig.FEEDER_TIMEOUT
.
While this method preloads a single database, Environment.preload(com.sleepycat.je.Database[], com.sleepycat.je.PreloadConfig)
lets you preload multiple databases.
config
- The PreloadConfig object that specifies the parameters
of the preload.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 PreloadConfig.getMaxBytes
is
greater than size of the JE cache.DatabaseException
public long count() throws DatabaseException
DiskOrderedCursor
. Specifically,
it will disable deletion of log files by the JE log cleaner during its
execution and will consume a certain amount of memory (but without
affecting the memory that is available for the JE cache). To avoid
excessive memory consumption (and a potential OutOfMemoryError
)
this method places an internal limit on its memory consumption. If this
limit is reached, the method will still work properly, but its
performance will degrade. To specify a different memory limit than the
one used by this method, use the
count(long memoryLimit)
method.
Currently, the internal memory limit is calculated as 10% of the
difference between the max JVM memory (the value returned by
Runtime.getRuntime().maxMemory()) and the configured JE cache size.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.DatabaseException
public long count(long memoryLimit) throws DatabaseException
DiskOrderedCursor
. Specifically,
it will disable deletion of log files by the JE log cleaner during its
execution and will consume a certain amount of memory (but without
affecting the memory that is available for the JE cache). To avoid
excessive memory consumption (and a potential OutOfMemoryError
)
this method takes as input an upper bound on the memory it may consume.
If this limit is reached, the method will still work properly, but its
performance will degrade.memoryLimit
- The maximum memory (in bytes) that may be consumed
by this method.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.DatabaseException
public DatabaseStats getStats(StatsConfig config) throws DatabaseException
If this method has not been configured to avoid expensive operations
(using the StatsConfig.setFast
method), it will access some of or all the pages in
the database, incurring a severe performance penalty as well as possibly
flushing the underlying cache.
In the presence of multiple threads or processes accessing an active database, the information returned by this method may be out-of-date.
config
- The statistics returned; if null, default statistics are
returned.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.DatabaseException
public DatabaseStats verify(VerifyConfig config) throws DatabaseException
Verification is an expensive operation that should normally only be used for troubleshooting and debugging.
config
- Configures the verify operation; if null, the default
operation is performed.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 String getDatabaseName() throws DatabaseException
This method may be called at any time during the life of the application.
DatabaseException
public DatabaseConfig getConfig()
This may differ from the configuration used to open this object if the database existed previously.
EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.public Environment getEnvironment()
Environment
handle for
the database environment underlying the Database
.
This method may be called at any time during the life of the application.
Environment
handle
for the database environment underlying the Database
.public List<SecondaryDatabase> getSecondaryDatabases()
SecondaryDatabase
objects associated with a primary database.
If no secondaries are associated with this database, an empty list is returned.
public int compareKeys(DatabaseEntry entry1, DatabaseEntry entry2)
IllegalStateException
- if the database has been closed.IllegalArgumentException
- if either entry is a partial
DatabaseEntry, or is null.public int compareDuplicates(DatabaseEntry entry1, DatabaseEntry entry2)
IllegalStateException
- if the database has been closed.IllegalArgumentException
- if either entry is a partial
DatabaseEntry, or is null.Copyright (c) 2002, 2015 Oracle and/or its affiliates. All rights reserved.