Package com.apple.foundationdb
Interface Database
-
- All Superinterfaces:
java.lang.AutoCloseable
,ReadTransactionContext
,TransactionContext
public interface Database extends java.lang.AutoCloseable, TransactionContext
A mutable, lexicographically ordered mapping from binary keys to binary values.Transaction
s are used to manipulate data within a singleDatabase
-- multiple, concurrentTransaction
s on aDatabase
enforce ACID properties.
The simplest correct programs using FoundationDB will make use of the methods defined in theTransactionContext
interface. When used on aDatabase
these methods will callTransaction#commit()
after user code has been executed. These methods will not return successfully untilcommit()
has returned successfully.
Note:Database
objects must beclosed
when no longer in use in order to free any associated resources.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description void
close()
Close theDatabase
object and release any associated resources.default Transaction
createTransaction()
Creates aTransaction
that operates on thisDatabase
.Transaction
createTransaction(java.util.concurrent.Executor e)
Creates aTransaction
that operates on thisDatabase
with the givenExecutor
for asynchronous callbacks.Transaction
createTransaction(java.util.concurrent.Executor e, EventKeeper eventKeeper)
Creates aTransaction
that operates on thisDatabase
with the givenExecutor
for asynchronous callbacks.double
getMainThreadBusyness()
Returns a value which indicates the saturation of the client
Note: By default, this value is updated every seconddefault Tenant
openTenant(byte[] tenantName)
Opens an existing tenant to be used for running transactions.
Note: opening a tenant does not check its existence in the cluster.Tenant
openTenant(byte[] tenantName, java.util.concurrent.Executor e)
Opens an existing tenant to be used for running transactions.Tenant
openTenant(byte[] tenantName, java.util.concurrent.Executor e, EventKeeper eventKeeper)
Opens an existing tenant to be used for running transactions.Tenant
openTenant(Tuple tenantName)
Opens an existing tenant to be used for running transactions.Tenant
openTenant(Tuple tenantName, java.util.concurrent.Executor e)
Opens an existing tenant to be used for running transactions.Tenant
openTenant(Tuple tenantName, java.util.concurrent.Executor e, EventKeeper eventKeeper)
Opens an existing tenant to be used for running transactions.DatabaseOptions
options()
Returns a set of options that can be set on aDatabase
default <T> T
read(java.util.function.Function<? super ReadTransaction,T> retryable)
Runs a read-only transactional function against thisDatabase
with retry logic.<T> T
read(java.util.function.Function<? super ReadTransaction,T> retryable, java.util.concurrent.Executor e)
Runs a read-only transactional function against thisDatabase
with retry logic.default <T> java.util.concurrent.CompletableFuture<T>
readAsync(java.util.function.Function<? super ReadTransaction,? extends java.util.concurrent.CompletableFuture<T>> retryable)
Runs a read-only transactional function against thisDatabase
with retry logic.<T> java.util.concurrent.CompletableFuture<T>
readAsync(java.util.function.Function<? super ReadTransaction,? extends java.util.concurrent.CompletableFuture<T>> retryable, java.util.concurrent.Executor e)
Runs a read-only transactional function against thisDatabase
with retry logic.default <T> T
run(java.util.function.Function<? super Transaction,T> retryable)
Runs a transactional function against thisDatabase
with retry logic.<T> T
run(java.util.function.Function<? super Transaction,T> retryable, java.util.concurrent.Executor e)
Runs a transactional function against thisDatabase
with retry logic.default <T> java.util.concurrent.CompletableFuture<T>
runAsync(java.util.function.Function<? super Transaction,? extends java.util.concurrent.CompletableFuture<T>> retryable)
Runs a transactional function against thisDatabase
with retry logic.<T> java.util.concurrent.CompletableFuture<T>
runAsync(java.util.function.Function<? super Transaction,? extends java.util.concurrent.CompletableFuture<T>> retryable, java.util.concurrent.Executor e)
Runs a transactional function against thisDatabase
with retry logic.-
Methods inherited from interface com.apple.foundationdb.ReadTransactionContext
getExecutor
-
-
-
-
Method Detail
-
openTenant
default Tenant openTenant(byte[] tenantName)
Opens an existing tenant to be used for running transactions.
Note: opening a tenant does not check its existence in the cluster. If the tenant does not exist, attempts to read or write data with it will fail.- Parameters:
tenantName
- The name of the tenant to open.- Returns:
- a
Tenant
that can be used to create transactions that will operate in the tenant's key-space.
-
openTenant
Tenant openTenant(Tuple tenantName)
Opens an existing tenant to be used for running transactions. This is a convenience method that generates the tenant name by packing aTuple
.
Note: opening a tenant does not check its existence in the cluster. If the tenant does not exist, attempts to read or write data with it will fail.- Parameters:
tenantName
- The name of the tenant to open, as a Tuple.- Returns:
- a
Tenant
that can be used to create transactions that will operate in the tenant's key-space.
-
openTenant
Tenant openTenant(byte[] tenantName, java.util.concurrent.Executor e)
Opens an existing tenant to be used for running transactions.- Parameters:
tenantName
- The name of the tenant to open.e
- theExecutor
to use when executing asynchronous callbacks.- Returns:
- a
Tenant
that can be used to create transactions that will operate in the tenant's key-space.
-
openTenant
Tenant openTenant(Tuple tenantName, java.util.concurrent.Executor e)
Opens an existing tenant to be used for running transactions. This is a convenience method that generates the tenant name by packing aTuple
.- Parameters:
tenantName
- The name of the tenant to open, as a Tuple.e
- theExecutor
to use when executing asynchronous callbacks.- Returns:
- a
Tenant
that can be used to create transactions that will operate in the tenant's key-space.
-
openTenant
Tenant openTenant(byte[] tenantName, java.util.concurrent.Executor e, EventKeeper eventKeeper)
Opens an existing tenant to be used for running transactions.- Parameters:
tenantName
- The name of the tenant to open.e
- theExecutor
to use when executing asynchronous callbacks.eventKeeper
- theEventKeeper
to use when tracking instrumented calls for the tenant's transactions.- Returns:
- a
Tenant
that can be used to create transactions that will operate in the tenant's key-space.
-
openTenant
Tenant openTenant(Tuple tenantName, java.util.concurrent.Executor e, EventKeeper eventKeeper)
Opens an existing tenant to be used for running transactions. This is a convenience method that generates the tenant name by packing aTuple
.- Parameters:
tenantName
- The name of the tenant to open, as a Tuple.e
- theExecutor
to use when executing asynchronous callbacks.eventKeeper
- theEventKeeper
to use when tracking instrumented calls for the tenant's transactions.- Returns:
- a
Tenant
that can be used to create transactions that will operate in the tenant's key-space.
-
createTransaction
default Transaction createTransaction()
Creates aTransaction
that operates on thisDatabase
. Creating a transaction in this way does not associate it with aTenant
, and as a result the transaction will operate on the entire key-space for the database.
Note: Java transactions automatically set theTransactionOptions.setUsedDuringCommitProtectionDisable()
option. This is because the Java bindings disallow use ofTransaction
objects afterTransaction.onError(java.lang.Throwable)
is called.
Note: Transactions created directly on aDatabase
object cannot be used in a cluster that requires tenant-based access. To run transactions in those clusters, you must first open a tenant withopenTenant(byte[])
.- Returns:
- a newly created
Transaction
that reads from and writes to thisDatabase
.
-
createTransaction
Transaction createTransaction(java.util.concurrent.Executor e)
Creates aTransaction
that operates on thisDatabase
with the givenExecutor
for asynchronous callbacks.- Parameters:
e
- theExecutor
to use when executing asynchronous callbacks for the database- Returns:
- a newly created
Transaction
that reads from and writes to thisDatabase
.
-
createTransaction
Transaction createTransaction(java.util.concurrent.Executor e, EventKeeper eventKeeper)
Creates aTransaction
that operates on thisDatabase
with the givenExecutor
for asynchronous callbacks.- Parameters:
e
- theExecutor
to use when executing asynchronous callbacks for the databaseeventKeeper
- theEventKeeper
to use when tracking instrumented calls for the transaction.- Returns:
- a newly created
Transaction
that reads from and writes to thisDatabase
.
-
options
DatabaseOptions options()
Returns a set of options that can be set on aDatabase
- Returns:
- a set of database-specific options affecting this
Database
-
getMainThreadBusyness
double getMainThreadBusyness()
Returns a value which indicates the saturation of the client
Note: By default, this value is updated every second- Returns:
- a value where 0 indicates that the client is idle and 1 (or larger) indicates that the client is saturated.
-
read
default <T> T read(java.util.function.Function<? super ReadTransaction,T> retryable)
Runs a read-only transactional function against thisDatabase
with retry logic.apply(ReadTransaction)
will be called on the suppliedFunction
until a non-retryableFDBException
(or anyThrowable
other than anFDBException
) is thrown. This call is blocking -- this method will not return until theFunction
has been called and completed without error.- Specified by:
read
in interfaceReadTransactionContext
- Type Parameters:
T
- the return type ofretryable
- Parameters:
retryable
- the block of logic to execute in aTransaction
against this database- Returns:
- the result of the last run of
retryable
-
read
<T> T read(java.util.function.Function<? super ReadTransaction,T> retryable, java.util.concurrent.Executor e)
Runs a read-only transactional function against thisDatabase
with retry logic. Use this formulation ofread(Function)
if one wants to set a customExecutor
for the transaction when run.- Type Parameters:
T
- the return type ofretryable
- Parameters:
retryable
- the block of logic to execute in aTransaction
against this databasee
- theExecutor
to use for asynchronous callbacks- Returns:
- the result of the last run of
retryable
- See Also:
read(Function)
-
readAsync
default <T> java.util.concurrent.CompletableFuture<T> readAsync(java.util.function.Function<? super ReadTransaction,? extends java.util.concurrent.CompletableFuture<T>> retryable)
Runs a read-only transactional function against thisDatabase
with retry logic.apply(ReadTransaction)
will be called on the suppliedFunction
until a non-retryableFDBException
(or anyThrowable
other than anFDBException
) is thrown. This call is non-blocking -- this method will return immediately and with aCompletableFuture
that will be set when theFunction
has been called and completed without error.
Any errors encountered executingretryable
, or received from the database, will be set on the returnedCompletableFuture
.- Specified by:
readAsync
in interfaceReadTransactionContext
- Type Parameters:
T
- the return type ofretryable
- Parameters:
retryable
- the block of logic to execute in aReadTransaction
against this database- Returns:
- a
CompletableFuture
that will be set to the value returned by the last call toretryable
-
readAsync
<T> java.util.concurrent.CompletableFuture<T> readAsync(java.util.function.Function<? super ReadTransaction,? extends java.util.concurrent.CompletableFuture<T>> retryable, java.util.concurrent.Executor e)
Runs a read-only transactional function against thisDatabase
with retry logic. Use this version ofreadAsync(Function)
if one wants to set a customExecutor
for the transaction when run.- Type Parameters:
T
- the return type ofretryable
- Parameters:
retryable
- the block of logic to execute in aReadTransaction
against this databasee
- theExecutor
to use for asynchronous callbacks- Returns:
- a
CompletableFuture
that will be set to the value returned by the last call toretryable
- See Also:
readAsync(Function)
-
run
default <T> T run(java.util.function.Function<? super Transaction,T> retryable)
Runs a transactional function against thisDatabase
with retry logic.apply(Transaction)
will be called on the suppliedFunction
until a non-retryableFDBException
(or anyThrowable
other than anFDBException
) is thrown orcommit()
, when called afterapply()
, returns success. This call is blocking -- this method will not return untilcommit()
has been called and returned success.
As with other client/server databases, in some failure scenarios a client may be unable to determine whether a transaction succeeded. In these cases, your transaction may be executed twice. For more information about how to reason about these situations see the FounationDB Developer Guide- Specified by:
run
in interfaceTransactionContext
- Type Parameters:
T
- the return type ofretryable
- Parameters:
retryable
- the block of logic to execute in aTransaction
against this database- Returns:
- the result of the last run of
retryable
-
run
<T> T run(java.util.function.Function<? super Transaction,T> retryable, java.util.concurrent.Executor e)
Runs a transactional function against thisDatabase
with retry logic. Use this formulation ofrun(Function)
if one would like to set a customExecutor
for the transaction when run.- Type Parameters:
T
- the return type ofretryable
- Parameters:
retryable
- the block of logic to execute in aTransaction
against this databasee
- theExecutor
to use for asynchronous callbacks- Returns:
- the result of the last run of
retryable
-
runAsync
default <T> java.util.concurrent.CompletableFuture<T> runAsync(java.util.function.Function<? super Transaction,? extends java.util.concurrent.CompletableFuture<T>> retryable)
Runs a transactional function against thisDatabase
with retry logic.apply(Transaction)
will be called on the suppliedFunction
until a non-retryableFDBException
(or anyThrowable
other than anFDBException
) is thrown orcommit()
, when called afterapply()
, returns success. This call is non-blocking -- this method will return immediately and with aCompletableFuture
that will be set whencommit()
has been called and returned success.
As with other client/server databases, in some failure scenarios a client may be unable to determine whether a transaction succeeded. In these cases, your transaction may be executed twice. For more information about how to reason about these situations see the FounationDB Developer Guide
Any errors encountered executingretryable
, or received from the database, will be set on the returnedCompletableFuture
.- Specified by:
runAsync
in interfaceTransactionContext
- Type Parameters:
T
- the return type ofretryable
- Parameters:
retryable
- the block of logic to execute in aTransaction
against this database- Returns:
- a
CompletableFuture
that will be set to the value returned by the last call toretryable
-
runAsync
<T> java.util.concurrent.CompletableFuture<T> runAsync(java.util.function.Function<? super Transaction,? extends java.util.concurrent.CompletableFuture<T>> retryable, java.util.concurrent.Executor e)
Runs a transactional function against thisDatabase
with retry logic. Use this formulation of the non-blockingrunAsync(Function)
if one wants to set a customExecutor
for the transaction when run.- Type Parameters:
T
- the return type ofretryable
- Parameters:
retryable
- the block of logic to execute in aTransaction
against this databasee
- theExecutor
to use for asynchronous callbacks- Returns:
- a
CompletableFuture
that will be set to the value returned by the last call toretryable
- See Also:
run(Function)
-
close
void close()
Close theDatabase
object and release any associated resources. This must be called at least once after theDatabase
object is no longer in use. This can be called multiple times, but care should be taken that it is not in use in another thread at the time of the call.- Specified by:
close
in interfacejava.lang.AutoCloseable
-
-