Class CachingKVDatabase

java.lang.Object
io.permazen.kv.caching.AbstractCachingConfig
io.permazen.kv.caching.CachingKVDatabase
All Implemented Interfaces:
CachingConfig, KVDatabase

public class CachingKVDatabase extends AbstractCachingConfig implements KVDatabase
A wrapper around an inner KVDatabase that adds a caching layer to transactions by wrapping them in a CachingKVStore.

See CachingKVStore for details on how caching is performed.

Consistency Assumptions

Warning: this class assumes that the underlying KVDatabase provides transactions guaranteeing fully consistent reads: the data returned by any two read operations, no matter when they occur, must be consistent within any given transaction. A corollary is that transactions must be fully isolated from each other. Enabling assertions on this package may detect some violations of this assumption.

See Also:
  • Field Details

  • Constructor Details

    • CachingKVDatabase

      public CachingKVDatabase()
      Default constructor.

      Instances must be configured with an inner KVDatabase before starting.

    • CachingKVDatabase

      public CachingKVDatabase(KVDatabase inner)
      Primary constructor.
      Parameters:
      inner - inner KVDatabase
  • Method Details

    • getInitialRttEstimate

      public long getInitialRttEstimate()
      Get the initial round trip time estimate.

      Default is 50 ms.

      Returns:
      initial round trip time estimate in nanoseconds
    • setInitialRttEstimate

      public void setInitialRttEstimate(long initialRttEstimate)
      Set the initial round trip time estimate.

      This is just an initial estimate. The actual value used adjusts dynamically as transactions occur and actual RTT measurements are made.

      Default is 50 ms.

      Parameters:
      initialRttEstimate - initial round trip time estimate in nanoseconds
      Throws:
      IllegalStateException - if this instance is already started
      IllegalArgumentException - if initialRttEstimate < 0
    • getKVDatabase

      public KVDatabase getKVDatabase()
      Get the inner KVDatabase.
      Returns:
      the underlying KVDatabase
    • setKVDatabase

      public void setKVDatabase(KVDatabase inner)
      Configure the underlying KVDatabase.
      Parameters:
      inner - the underlying KVDatabase
      Throws:
      IllegalStateException - if this instance is already started
    • getExecutorService

      public ExecutorService getExecutorService()
      Get the configured ExecutorService, if any.
      Returns:
      caller-supplied executor to use for background tasks, or null for none
    • setExecutorService

      public void setExecutorService(ExecutorService executor)
      Configure a ExecutorService to use for asynchronous queries to the underlying database.

      This property is optional; if none is configured, a private thread pool will be automatically created and setup internally by start() and torn down by stop(). If an outside executor is configured here, then it will not be shutdown by stop().

      Parameters:
      executor - caller-supplied executor to use for background tasks, or null for none
      Throws:
      IllegalStateException - if this instance is already started
    • getThreadPoolSize

      public int getThreadPoolSize()
      Get the number of threads in the internally-created thread pool.

      This property is ignored if a custom ExecutorService is configured via setExecutorService().

      Default value is 10.

      Returns:
      number of threads in thread pool
    • setThreadPoolSize

      public void setThreadPoolSize(int threadPoolSize)
      Set the number of threads in the internally-created thread pool.

      This property is ignored if a custom ExecutorService is configured via setExecutorService().

      Default value is 10.

      Parameters:
      threadPoolSize - number of threads in thread pool
      Throws:
      IllegalStateException - if this instance is already started
      IllegalArgumentException - if threadPoolSize <= 0
    • start

      @PostConstruct public void start()
      Description copied from interface: KVDatabase
      Start this instance. This method must be called prior to creating any transactions.

      This method is idempotent: if this instance is already started, nothing happens.

      Whether an instance that has been started and stopped can be restarted is implementation-dependent.

      Specified by:
      start in interface KVDatabase
    • stop

      @PreDestroy public void stop()
      Description copied from interface: KVDatabase
      Stop this instance.

      This method is idempotent: if this instance has not been started, or is already stopped, nothing happens.

      Specified by:
      stop in interface KVDatabase
    • createTransaction

      public CachingKVTransaction createTransaction()
      Description copied from interface: KVDatabase
      Create a new transaction.
      Specified by:
      createTransaction in interface KVDatabase
      Returns:
      newly created transaction
    • createTransaction

      public CachingKVTransaction createTransaction(Map<String,?> options)
      Description copied from interface: KVDatabase
      Create a new transaction with the specified options.
      Specified by:
      createTransaction in interface KVDatabase
      Parameters:
      options - optional transaction options; may be null
      Returns:
      newly created transaction
    • createTransaction

      protected CachingKVTransaction createTransaction(Supplier<? extends KVTransaction> innerTxCreator)
    • getRttEstimate

      public double getRttEstimate()
      Get the current round trip time estimate.
      Returns:
      current RTT estimate in nanoseconds
      Throws:
      IllegalStateException - if this instance has never start()ed