public class SDatabase extends java.lang.Object implements GetHelper, PutHelper, TxnHelper, java.lang.AutoCloseable
For most database activities, you must open the handle using the open method. When you are done with them, handles must be closed using the close method.
Databases are organized within a database environment. Environments provide
transactions, recovery, replication and other advanced features. Also, if
you are using multiple databases, then environments allow your databases to
share a common in-memory cache, which makes for more efficient usage of your
hardware's resources. See SEnvironment
for information on using
database environments.
Database attributes are specified in the SDatabaseConfig
class.
To open an existing database with default attributes:
BdbServerConnection conn = BdbServerConnection.connect(host, port); SEnvironment env = conn.openEnvironment(home, null); SDatabase myDatabase = env.openDatabase(null, "mydatabase", null);To create a database that supports duplicates:
SDatabaseConfig dbConfig = new SDatabaseConfig(); dbConfig.setAllowCreate(true); dbConfig.setSortedDuplicates(true); SDatabase myDatabase = env.openDatabase(null, "mydatabase", dbConfig);
GetHelper.RemoteGetFunction, GetHelper.RemotePGetFunction
PutHelper.RemotePutFunction
TxnHelper.TransactionOperations<V>
Modifier and Type | Field and Description |
---|---|
protected BdbService.Client |
client
The remote service client.
|
protected TDatabase |
tDb
The remote database handle.
|
Modifier | Constructor and Description |
---|---|
protected |
SDatabase(TDatabase tDb,
java.lang.String fileName,
java.lang.String databaseName,
BdbService.Client client,
SEnvironment env)
Constructor for subclasses.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Flush any cached database information to disk, close any open cursors,
free allocated resources, close underlying files, and discard the
database handle.
|
SCompactStats |
compact(STransaction txn,
SDatabaseEntry start,
SDatabaseEntry stop,
SDatabaseEntry end,
SCompactConfig config)
Compact a Btree or Recno database or returns unused Btree, Hash or Recno
database pages to the underlying filesystem.
|
protected TDbGetConfig |
createConfig(SDatabaseEntryBase data,
SLockMode lockMode) |
SOperationStatus |
delete(STransaction txn,
SDatabaseEntry key)
Remove key/data pairs from the database.
|
SOperationStatus |
deleteMultiple(STransaction txn,
SMultipleDataEntry keys)
Remove key/data pairs from the database.
|
SOperationStatus |
deleteMultipleKey(STransaction txn,
SMultiplePairs pairs)
Remove key/data pairs from the database.
|
SOperationStatus |
exists(STransaction txn,
SDatabaseEntry key)
Checks if the specified key appears in the database.
|
SOperationStatus |
get(STransaction txn,
SDatabaseEntry key,
SDatabaseEntryBase data,
SLockMode lockMode)
Retrieves the key/data pair with the given key from the database.
|
SDatabaseConfig |
getConfig()
Return this SDatabase object's configuration.
|
java.lang.String |
getDatabaseFile()
Return the database's underlying file name.
|
java.lang.String |
getDatabaseName()
Return the database name.
|
SEnvironment |
getEnvironment()
Return the
SEnvironment handle for the database environment
underlying this database. |
SKeyRange |
getKeyRange(STransaction txn,
SDatabaseEntry key)
Return an estimate of the proportion of keys in the database less than,
equal to, and greater than the specified key.
|
SOperationStatus |
getSearchBoth(STransaction txn,
SDatabaseEntry key,
SDatabaseEntryBase data,
SLockMode lockMode)
Retrieves the key/data pair with the given key and data value, that is,
both the key and data items must match.
|
SOperationStatus |
getSearchRecordNumber(STransaction txn,
SDatabaseEntry key,
SDatabaseEntryBase data,
SLockMode lockMode)
Retrieves the key/data pair associated with the specific numbered record
of the database.
|
java.util.Set<SSecondaryDatabase> |
getSecondaryDatabases()
Return the set of secondary databases associated with this primary
database.
|
java.nio.ByteOrder |
getServerByteOrder()
Return the native byte order of the connected server.
|
SDatabaseStats |
getStats(STransaction txn,
SStatsConfig config)
Return database statistics.
|
default <V> V |
handleRuntimeExceptions(com.sleepycat.client.RemoteServiceCallable<V> remote) |
SJoinCursor |
join(SCursor[] cursors,
SJoinConfig config)
Creates a specialized join cursor for use in performing equality or
natural joins on secondary indices.
|
SCursor |
openCursor(STransaction txn,
SCursorConfig config)
Return a cursor into the database.
|
SSequence |
openSequence(STransaction txn,
SDatabaseEntry key,
SSequenceConfig config)
Open a sequence represented by the key in the database.
|
SOperationStatus |
put(STransaction txn,
SDatabaseEntry key,
SDatabaseEntry data)
Store the key/data pair into the database.
|
SOperationStatus |
putMultipleKey(STransaction txn,
SMultiplePairs pairs,
boolean overwrite)
Store a set of key/data pairs into the database.
|
SOperationStatus |
putNoDupData(STransaction txn,
SDatabaseEntry key,
SDatabaseEntry data)
Store the key/data pair into the database if it does not already appear
in the database.
|
SOperationStatus |
putNoOverwrite(STransaction txn,
SDatabaseEntry key,
SDatabaseEntry data)
Store the key/data pair into the database if the key does not already
appear in the database.
|
default <V> V |
remoteCall(com.sleepycat.client.RemoteServiceCallable<V> remote) |
default <V> V |
remoteCallWithIOException(com.sleepycat.client.RemoteServiceCallable<V> remote) |
void |
removeSequence(STransaction txn,
SDatabaseEntry key,
boolean autoCommitNoSync,
boolean force)
Remove the sequence from the database.
|
void |
setConfig(SDatabaseConfig config)
Change the settings in an existing database handle.
|
int |
truncate(STransaction txn,
boolean countRecords)
Empty the database, discarding all records it contains.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
createGetSearchTerm, createPGetSearchTerm, remoteGet, remotePGet, updateKeyData, updateTuple
calculateSKey, remotePut
runInSingleTxn, runInSingleTxnWithIOException
protected final TDatabase tDb
protected final BdbService.Client client
protected SDatabase(TDatabase tDb, java.lang.String fileName, java.lang.String databaseName, BdbService.Client client, SEnvironment env)
tDb
- the Thrift objectfileName
- the file namedatabaseName
- the database nameclient
- the Thrift client objectenv
- the enclosing environmentpublic void close() throws SDatabaseException
Closing a database handle will close any open cursors that refer to it. However, you should make sure to close all your transaction handles before closing your database handle.
Because key/data pairs are cached in memory, failing to sync the file with the this methods may result in inconsistent or lost information. So, to ensure that any data cached in main memory are reflected in the underlying file system, applications should make a point to always close database handles.
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 interface java.lang.AutoCloseable
SDatabaseException
- if any error occurspublic SDatabaseConfig getConfig() throws SDatabaseException
This may differ from the configuration used to open this object if the database existed previously.
SDatabaseException
- if any error occurspublic void setConfig(SDatabaseConfig config) throws SDatabaseException
getConfig()
to get the current settings because unset attributes are
not changed.config
- the database attributes; if null, no attribute is changedSDatabaseException
- if any error occurspublic java.lang.String getDatabaseFile()
public java.lang.String getDatabaseName()
public SEnvironment getEnvironment()
SEnvironment
handle for the database environment
underlying this database.getEnvironment
in interface TxnHelper
SEnvironment
handle for the database environment
underlying this databasepublic java.util.Set<SSecondaryDatabase> getSecondaryDatabases()
PutHelper
getSecondaryDatabases
in interface PutHelper
public java.nio.ByteOrder getServerByteOrder()
public SOperationStatus get(STransaction txn, SDatabaseEntry key, SDatabaseEntryBase data, SLockMode lockMode) throws SDatabaseException
Duplicates are sorted by:
SCursor
operations.
When called on a database that has been made into a secondary index,
this method returns the key from the secondary index and the data item
from the primary database.txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitkey
- the key used as input. It must be initialized by the caller.data
- the data returned as output. Use SMultipleDataEntry
to return all duplicates of the key.lockMode
- the locking attributes; if null, default attributes are
usedSOperationStatus.NOTFOUND
if no matching key/data pair is
found; SOperationStatus.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, SOperationStatus.SUCCESS
.SDatabaseException
- if any error occurspublic SOperationStatus getSearchBoth(STransaction txn, SDatabaseEntry key, SDatabaseEntryBase data, SLockMode lockMode) throws SDatabaseException
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitkey
- the key used as input. It must be initialized by the caller.data
- the data used as input; only SDatabaseEntry
is
supportedlockMode
- the locking attributes; if null, default attributes are
usedSOperationStatus.NOTFOUND
if no matching key/data pair is
found; SOperationStatus.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, SOperationStatus.SUCCESS
.SDatabaseException
- if any error occurspublic SOperationStatus getSearchRecordNumber(STransaction txn, SDatabaseEntry key, SDatabaseEntryBase data, SLockMode lockMode) throws SDatabaseException
The specified key must be a record number as described in
SDatabaseEntry
. 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.
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitkey
- the key returned as outputdata
- the data returned as output. Use SMultipleDataEntry
to return all duplicates of the key.lockMode
- the locking attributes; if null, default attributes are
usedSOperationStatus.NOTFOUND
if no matching key/data pair is
found; SOperationStatus.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, SOperationStatus.SUCCESS
.SDatabaseException
- if any error occursprotected TDbGetConfig createConfig(SDatabaseEntryBase data, SLockMode lockMode)
public SOperationStatus exists(STransaction txn, SDatabaseEntry key) throws SDatabaseException
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitkey
- the key used as inputSOperationStatus.NOTFOUND
if no record is available;
SOperationStatus.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, SOperationStatus.SUCCESS
.SDatabaseException
- if any error occurspublic SKeyRange getKeyRange(STransaction txn, SDatabaseEntry key) throws SDatabaseException
The underlying database must be of type Btree.
This method does not retain the locks it acquires for the life of the transaction, so estimates are not repeatable.
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitkey
- the key being comparedSDatabaseException
- if any error occurspublic SOperationStatus put(STransaction txn, SDatabaseEntry key, SDatabaseEntry data) throws SDatabaseException
If the key already appears in the database and duplicates are not configured, the existing key/data pair 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. If the key already appears in the database and unsorted duplicates are configured, the new data value is appended at the end of the duplicate set.
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitkey
- the key SDatabaseEntry operated ondata
- the data SDatabaseEntry storedSDatabaseException
- if any error occurspublic SOperationStatus putNoDupData(STransaction txn, SDatabaseEntry key, SDatabaseEntry data) throws SDatabaseException
This method may only be called if the underlying database has been configured to support sorted duplicates. (This method may not be specified to the Queue or Recno access methods.)
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitkey
- the key SDatabaseEntry operated ondata
- the data SDatabaseEntry storedSDatabaseException
- if any error occurspublic SOperationStatus putNoOverwrite(STransaction txn, SDatabaseEntry key, SDatabaseEntry data) throws SDatabaseException
This method will fail if the key already exists in the database, even if the database supports duplicates.
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitkey
- the key SDatabaseEntry operated ondata
- the data SDatabaseEntry storedSDatabaseException
- if any error occurspublic SOperationStatus putMultipleKey(STransaction txn, SMultiplePairs pairs, boolean overwrite) throws SDatabaseException
This method may not be called on databases configured with unsorted duplicates.
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitpairs
- the key and data pairs operated onoverwrite
- if this flag is true and any of the keys already exist
in the database, they will be replaced. Otherwise a SOperationStatus.KEYEXIST
error will be returnedSOperationStatus.KEYEXIST
SDatabaseException
- if any error occurspublic SOperationStatus delete(STransaction txn, SDatabaseEntry key) throws SDatabaseException
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. When called on a database that has been made into a secondary index, this method deletes the key/data pair from the primary database and all secondary indices.
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitkey
- the key operated onSOperationStatus.NOTFOUND
if the
specified key is not found in the database; the method will return
SOperationStatus.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 the method will
return SOperationStatus.SUCCESS
.SDatabaseException
- if any error occurspublic SOperationStatus deleteMultiple(STransaction txn, SMultipleDataEntry keys) throws SDatabaseException
The key/data pairs associated with the specified keys are discarded from the database. In the presence of duplicate key values, all records associated with the designated keys will be discarded.
The key/data pairs are also deleted from any associated secondary databases. When called on a database that has been made into a secondary index, this method deletes the key/data pairs from the primary database and all secondary indices.
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitkeys
- the set of keys operated onSOperationStatus.NOTFOUND
if the
specified key is not found in the database; the method will return
SOperationStatus.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 the method will
return SOperationStatus.SUCCESS
.SDatabaseException
- if any error occurspublic SOperationStatus deleteMultipleKey(STransaction txn, SMultiplePairs pairs) throws SDatabaseException
The specified key/data pairs are discarded from the database.
The key/data pairs are also deleted from any associated secondary databases. When called on a database that has been made into a secondary index, this method deletes the key/data pairs from the primary database and all secondary indices.
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitpairs
- the set of key/data pairs operated onSOperationStatus.NOTFOUND
if the
specified key is not found in the database; the method will return
SOperationStatus.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 the method will
return SOperationStatus.SUCCESS
.SDatabaseException
- if any error occurspublic SCursor openCursor(STransaction txn, SCursorConfig config) throws SDatabaseException
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.
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
usedSDatabaseException
- if any error occurspublic SJoinCursor join(SCursor[] cursors, SJoinConfig config) throws SDatabaseException
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 SCursor.getSearchKey(com.sleepycat.client.SDatabaseEntry, com.sleepycat.client.SDatabaseEntryBase, com.sleepycat.client.SLockMode)
.
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 databaseconfig
- the join attributes; if null, default attributes are usedSDatabaseException
- if any error occurspublic SSequence openSequence(STransaction txn, SDatabaseEntry key, SSequenceConfig config) throws SDatabaseException
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitkey
- The key SDatabaseEntry
of the sequence. It specifies
which record in the database stores the persistent sequence data.config
- The sequence attributes; If null, default attributes are
usedSDatabaseException
- if any error occurspublic void removeSequence(STransaction txn, SDatabaseEntry key, boolean autoCommitNoSync, boolean force) throws SDatabaseException
If force
is set to true, all open handles for the sequence are
closed so that the remove can proceed. If force
is
false, and there is at least one open handle for the sequence, SResourceInUseException
is thrown.
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitkey
- The key SDatabaseEntry
of the sequence. It specifies
which record in the database stores the persistent sequence data.autoCommitNoSync
- if true, configure auto-commit operations on the
sequence to not flush the transaction logforce
- if true all open handles on the sequence are closed and the
sequence is removed; if false the sequence is removed only if there is
no open handle on itSDatabaseException
- if any error occurspublic SCompactStats compact(STransaction txn, SDatabaseEntry start, SDatabaseEntry stop, SDatabaseEntry end, SCompactConfig config) throws SDatabaseException
txn
- if the operation is part of an application-specified
transaction, the txn parameter is a transaction handle returned from
SEnvironment.beginTransaction(com.sleepycat.client.STransaction, com.sleepycat.client.STransactionConfig)
, otherwise NULL
.
If a transaction handle is supplied to this method, then the operation
is performed using that transaction. In this event, large sections of
the tree may be locked during the course of the transaction.
If no transaction handle is specified, the operation will be implicitly
transaction protected using multiple transactions. These transactions
will be periodically committed to avoid locking large sections of the
tree. Any deadlocks encountered cause the compaction operation to be
retried from the point of the last transaction commit.start
- if not null, the start parameter is the starting point for
compaction in a Btree or Recno database. Compaction will start at the
smallest key greater than or equal to the specified key. If null,
compaction will start at the beginning of the database.stop
- if not null, the stop parameter is the stopping point for
compaction in a Btree or Recno database. Compaction will stop at the
page with the smallest key greater than the specified key. If null,
compaction will stop at the end of the database.end
- if not null, the end parameter will be filled in with the key
marking the end of the compaction operation in a Btree or Recno
database. It is generally the first key of the page where processing
stopped.config
- the compaction operation attributes; if null, default
attributes are usedSDatabaseException
- if any error occurspublic int truncate(STransaction txn, boolean countRecords) throws SDatabaseException
When called on a database configured with secondary indices, this method truncates the primary database and all secondary indices. If configured to return a count of the records discarded, the returned count is the count of records discarded from the primary database.
It is an error to call this method on a database with open cursors.
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitcountRecords
- if true, count and return the number of records
discardedcountRecords
is
falseSDatabaseException
- if any error occurspublic SDatabaseStats getStats(STransaction txn, SStatsConfig config) throws SDatabaseException
If this method has not been configured to avoid expensive operations
(using the SStatsConfig.setFast(boolean)
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 buffer pool.
In the presence of multiple threads or processes accessing an active database, the information returned by this method may be out-of-date.
If the database was not opened read-only and this method was not
configured using the SStatsConfig.setFast(boolean)
method, cached key and
record numbers will be updated after the statistical information has
been gathered.
txn
- an explicit transaction may be specified, or null may be
specified to use auto-commitconfig
- the statistics returned; if null, default statistics are
returnedSDatabaseException
- if any error occurspublic <V> V remoteCallWithIOException(com.sleepycat.client.RemoteServiceCallable<V> remote) throws java.io.IOException
java.io.IOException
public <V> V remoteCall(com.sleepycat.client.RemoteServiceCallable<V> remote)
public <V> V handleRuntimeExceptions(com.sleepycat.client.RemoteServiceCallable<V> remote) throws org.apache.thrift.TException
org.apache.thrift.TException
Copyright (c) 2016, 2019 Oracle and/or its affiliates. All rights reserved.