Interface Database

All Superinterfaces:
AutoCloseable, ReadTransactionContext, TransactionContext

public interface Database extends AutoCloseable, TransactionContext
A mutable, lexicographically ordered mapping from binary keys to binary values. Transactions are used to manipulate data within a single Database -- multiple, concurrent Transactions on a Database enforce ACID properties.

The simplest correct programs using FoundationDB will make use of the methods defined in the TransactionContext interface. When used on a Database these methods will call Transaction#commit() after user code has been executed. These methods will not return successfully until commit() has returned successfully.

Note: Database objects must be closed when no longer in use in order to free any associated resources.
  • Method Details

    • 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 a Tuple.

      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, Executor e)
      Opens an existing tenant to be used for running transactions.
      Parameters:
      tenantName - The name of the tenant to open.
      e - the Executor 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, Executor e)
      Opens an existing tenant to be used for running transactions. This is a convenience method that generates the tenant name by packing a Tuple.
      Parameters:
      tenantName - The name of the tenant to open, as a Tuple.
      e - the Executor 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, Executor e, EventKeeper eventKeeper)
      Opens an existing tenant to be used for running transactions.
      Parameters:
      tenantName - The name of the tenant to open.
      e - the Executor to use when executing asynchronous callbacks.
      eventKeeper - the EventKeeper 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, 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 a Tuple.
      Parameters:
      tenantName - The name of the tenant to open, as a Tuple.
      e - the Executor to use when executing asynchronous callbacks.
      eventKeeper - the EventKeeper 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 a Transaction that operates on this Database. Creating a transaction in this way does not associate it with a Tenant, and as a result the transaction will operate on the entire key-space for the database.

      Note: Java transactions automatically set the TransactionOptions.setUsedDuringCommitProtectionDisable() option. This is because the Java bindings disallow use of Transaction objects after Transaction.onError(java.lang.Throwable) is called.

      Note: Transactions created directly on a Database object cannot be used in a cluster that requires tenant-based access. To run transactions in those clusters, you must first open a tenant with openTenant(byte[]).
      Returns:
      a newly created Transaction that reads from and writes to this Database.
    • createTransaction

      Transaction createTransaction(Executor e)
      Creates a Transaction that operates on this Database with the given Executor for asynchronous callbacks.
      Parameters:
      e - the Executor to use when executing asynchronous callbacks for the database
      Returns:
      a newly created Transaction that reads from and writes to this Database.
    • createTransaction

      Transaction createTransaction(Executor e, EventKeeper eventKeeper)
      Creates a Transaction that operates on this Database with the given Executor for asynchronous callbacks.
      Parameters:
      e - the Executor to use when executing asynchronous callbacks for the database
      eventKeeper - the EventKeeper to use when tracking instrumented calls for the transaction.
      Returns:
      a newly created Transaction that reads from and writes to this Database.
    • options

      DatabaseOptions options()
      Returns a set of options that can be set on a Database
      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.
    • purgeBlobGranules

      default CompletableFuture<byte[]> purgeBlobGranules(byte[] beginKey, byte[] endKey, boolean force)
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      force - if true delete all data, if not keep data >= purgeVersion
      Returns:
      the key to watch for purge complete
    • purgeBlobGranules

      default CompletableFuture<byte[]> purgeBlobGranules(byte[] beginKey, byte[] endKey, long purgeVersion, boolean force)
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      purgeVersion - version to purge at
      force - if true delete all data, if not keep data >= purgeVersion
      Returns:
      the key to watch for purge complete
    • purgeBlobGranules

      CompletableFuture<byte[]> purgeBlobGranules(byte[] beginKey, byte[] endKey, long purgeVersion, boolean force, Executor e)
      Queues a purge of blob granules for the specified key range, at the specified version.
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      purgeVersion - version to purge at
      force - if true delete all data, if not keep data >= purgeVersion
      e - the Executor to use for asynchronous callbacks
      Returns:
      the key to watch for purge complete
    • waitPurgeGranulesComplete

      default CompletableFuture<Void> waitPurgeGranulesComplete(byte[] purgeKey)
      Runs waitPurgeGranulesComplete(byte[] purgeKey) on the default executor.
      Parameters:
      purgeKey - key to watch
      Returns:
      void
    • waitPurgeGranulesComplete

      CompletableFuture<Void> waitPurgeGranulesComplete(byte[] purgeKey, Executor e)
      Wait for a previous call to purgeBlobGranules to complete.
      Parameters:
      purgeKey - key to watch
      e - the Executor to use for asynchronous callbacks
      Returns:
      void
    • blobbifyRange

      default CompletableFuture<Boolean> blobbifyRange(byte[] beginKey, byte[] endKey)
      Runs blobbifyRange(byte[] beginKey, byte[] endKey) on the default executor.
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      Returns:
      if the recording of the range was successful
    • blobbifyRange

      CompletableFuture<Boolean> blobbifyRange(byte[] beginKey, byte[] endKey, Executor e)
      Sets a range to be blobbified in the database. Must be a completely unblobbified range.
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      e - the Executor to use for asynchronous callbacks
      Returns:
      if the recording of the range was successful
    • blobbifyRangeBlocking

      default CompletableFuture<Boolean> blobbifyRangeBlocking(byte[] beginKey, byte[] endKey)
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      Returns:
      if the recording of the range was successful
    • blobbifyRangeBlocking

      CompletableFuture<Boolean> blobbifyRangeBlocking(byte[] beginKey, byte[] endKey, Executor e)
      Sets a range to be blobbified in the database. Must be a completely unblobbified range.
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      e - the Executor to use for asynchronous callbacks
      Returns:
      if the recording of the range was successful
    • unblobbifyRange

      default CompletableFuture<Boolean> unblobbifyRange(byte[] beginKey, byte[] endKey)
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      Returns:
      if the recording of the range was successful
    • unblobbifyRange

      CompletableFuture<Boolean> unblobbifyRange(byte[] beginKey, byte[] endKey, Executor e)
      Unsets a blobbified range in the database. The range must be aligned to known blob ranges.
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      e - the Executor to use for asynchronous callbacks
      Returns:
      if the recording of the range was successful
    • listBlobbifiedRanges

      default CompletableFuture<KeyRangeArrayResult> listBlobbifiedRanges(byte[] beginKey, byte[] endKey, int rangeLimit)
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      rangeLimit - batch size
      Returns:
      a future with the list of blobbified ranges: [lastLessThan(beginKey), firstGreaterThanOrEqual(endKey)]
    • listBlobbifiedRanges

      CompletableFuture<KeyRangeArrayResult> listBlobbifiedRanges(byte[] beginKey, byte[] endKey, int rangeLimit, Executor e)
      Lists blobbified ranges in the database. There may be more if result.size() == rangeLimit.
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      rangeLimit - batch size
      e - the Executor to use for asynchronous callbacks
      Returns:
      a future with the list of blobbified ranges: [lastLessThan(beginKey), firstGreaterThanOrEqual(endKey)]
    • verifyBlobRange

      default CompletableFuture<Long> verifyBlobRange(byte[] beginKey, byte[] endKey)
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      Returns:
      a future with the version of the last blob granule.
    • verifyBlobRange

      default CompletableFuture<Long> verifyBlobRange(byte[] beginKey, byte[] endKey, long version)
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      version - version to read at
      Returns:
      a future with the version of the last blob granule.
    • verifyBlobRange

      CompletableFuture<Long> verifyBlobRange(byte[] beginKey, byte[] endKey, long version, Executor e)
      Checks if a blob range is blobbified.
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      version - version to read at
      e - the Executor to use for asynchronous callbacks
      Returns:
      a future with the version of the last blob granule.
    • flushBlobRange

      default CompletableFuture<Boolean> flushBlobRange(byte[] beginKey, byte[] endKey, boolean compact)
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      compact - force compact or just flush
      Returns:
      a future with a boolean for success or failure
    • flushBlobRange

      default CompletableFuture<Boolean> flushBlobRange(byte[] beginKey, byte[] endKey, boolean compact, long version)
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      compact - force compact or just flush
      version - version to flush at
      Returns:
      a future with a boolean for success or failure
    • flushBlobRange

      CompletableFuture<Boolean> flushBlobRange(byte[] beginKey, byte[] endKey, boolean compact, long version, Executor e)
      Checks if a blob range is blobbified.
      Parameters:
      beginKey - start of the key range
      endKey - end of the key range
      compact - force compact or just flush
      version - version to flush at
      e - the Executor to use for asynchronous callbacks
      Returns:
      a future with a boolean for success or failure
    • read

      default <T> T read(Function<? super ReadTransaction,T> retryable)
      Runs a read-only transactional function against this Database with retry logic. apply(ReadTransaction) will be called on the supplied Function until a non-retryable FDBException (or any Throwable other than an FDBException) is thrown. This call is blocking -- this method will not return until the Function has been called and completed without error.
      Specified by:
      read in interface ReadTransactionContext
      Type Parameters:
      T - the return type of retryable
      Parameters:
      retryable - the block of logic to execute in a Transaction against this database
      Returns:
      the result of the last run of retryable
    • read

      <T> T read(Function<? super ReadTransaction,T> retryable, Executor e)
      Runs a read-only transactional function against this Database with retry logic. Use this formulation of read(Function) if one wants to set a custom Executor for the transaction when run.
      Type Parameters:
      T - the return type of retryable
      Parameters:
      retryable - the block of logic to execute in a Transaction against this database
      e - the Executor to use for asynchronous callbacks
      Returns:
      the result of the last run of retryable
      See Also:
    • readAsync

      default <T> CompletableFuture<T> readAsync(Function<? super ReadTransaction,? extends CompletableFuture<T>> retryable)
      Runs a read-only transactional function against this Database with retry logic. apply(ReadTransaction) will be called on the supplied Function until a non-retryable FDBException (or any Throwable other than an FDBException) is thrown. This call is non-blocking -- this method will return immediately and with a CompletableFuture that will be set when the Function has been called and completed without error.

      Any errors encountered executing retryable, or received from the database, will be set on the returned CompletableFuture.
      Specified by:
      readAsync in interface ReadTransactionContext
      Type Parameters:
      T - the return type of retryable
      Parameters:
      retryable - the block of logic to execute in a ReadTransaction against this database
      Returns:
      a CompletableFuture that will be set to the value returned by the last call to retryable
    • readAsync

      <T> CompletableFuture<T> readAsync(Function<? super ReadTransaction,? extends CompletableFuture<T>> retryable, Executor e)
      Runs a read-only transactional function against this Database with retry logic. Use this version of readAsync(Function) if one wants to set a custom Executor for the transaction when run.
      Type Parameters:
      T - the return type of retryable
      Parameters:
      retryable - the block of logic to execute in a ReadTransaction against this database
      e - the Executor to use for asynchronous callbacks
      Returns:
      a CompletableFuture that will be set to the value returned by the last call to retryable
      See Also:
    • run

      default <T> T run(Function<? super Transaction,T> retryable)
      Runs a transactional function against this Database with retry logic. apply(Transaction) will be called on the supplied Function until a non-retryable FDBException (or any Throwable other than an FDBException) is thrown or commit(), when called after apply(), returns success. This call is blocking -- this method will not return until commit() 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 interface TransactionContext
      Type Parameters:
      T - the return type of retryable
      Parameters:
      retryable - the block of logic to execute in a Transaction against this database
      Returns:
      the result of the last run of retryable
    • run

      <T> T run(Function<? super Transaction,T> retryable, Executor e)
      Runs a transactional function against this Database with retry logic. Use this formulation of run(Function) if one would like to set a custom Executor for the transaction when run.
      Type Parameters:
      T - the return type of retryable
      Parameters:
      retryable - the block of logic to execute in a Transaction against this database
      e - the Executor to use for asynchronous callbacks
      Returns:
      the result of the last run of retryable
    • runAsync

      default <T> CompletableFuture<T> runAsync(Function<? super Transaction,? extends CompletableFuture<T>> retryable)
      Runs a transactional function against this Database with retry logic. apply(Transaction) will be called on the supplied Function until a non-retryable FDBException (or any Throwable other than an FDBException) is thrown or commit(), when called after apply(), returns success. This call is non-blocking -- this method will return immediately and with a CompletableFuture that will be set when commit() 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 executing retryable, or received from the database, will be set on the returned CompletableFuture.
      Specified by:
      runAsync in interface TransactionContext
      Type Parameters:
      T - the return type of retryable
      Parameters:
      retryable - the block of logic to execute in a Transaction against this database
      Returns:
      a CompletableFuture that will be set to the value returned by the last call to retryable
    • runAsync

      <T> CompletableFuture<T> runAsync(Function<? super Transaction,? extends CompletableFuture<T>> retryable, Executor e)
      Runs a transactional function against this Database with retry logic. Use this formulation of the non-blocking runAsync(Function) if one wants to set a custom Executor for the transaction when run.
      Type Parameters:
      T - the return type of retryable
      Parameters:
      retryable - the block of logic to execute in a Transaction against this database
      e - the Executor to use for asynchronous callbacks
      Returns:
      a CompletableFuture that will be set to the value returned by the last call to retryable
      See Also:
    • close

      void close()
      Close the Database object and release any associated resources. This must be called at least once after the Database 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 interface AutoCloseable
    • getClientStatus

      default CompletableFuture<byte[]> getClientStatus()
      Returns client-side status information
      Returns:
      a CompletableFuture containing a JSON string with client status health information
    • getClientStatus

      CompletableFuture<byte[]> getClientStatus(Executor e)
      Returns client-side status information
      Parameters:
      e - the Executor to use for asynchronous callbacks
      Returns:
      a CompletableFuture containing a JSON string with client status health information