Class CachingKVDatabase
- All Implemented Interfaces:
CachingConfig
,KVDatabase
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 Summary
Modifier and TypeFieldDescriptionstatic final int
Default for the initial round-trip time estimate in milliseconds.static final int
Default thread pool size when noExecutorService
is explicitly configured.Fields inherited from interface io.permazen.kv.caching.CachingConfig
DEFAULT_MAX_RANGE_BYTES, DEFAULT_MAX_RANGES, DEFAULT_MAX_TOTAL_BYTES, DEFAULT_READ_AHEAD, DEFAULT_WAIT_FACTOR
-
Constructor Summary
ConstructorDescriptionDefault constructor.CachingKVDatabase
(KVDatabase inner) Primary constructor. -
Method Summary
Modifier and TypeMethodDescriptionCreate a new transaction.protected CachingKVTransaction
createTransaction
(Supplier<? extends KVTransaction> innerTxCreator) createTransaction
(Map<String, ?> options) Create a new transaction with the specified options.Get the configuredExecutorService
, if any.long
Get the initial round trip time estimate.Get the innerKVDatabase
.double
Get the current round trip time estimate.int
Get the number of threads in the internally-created thread pool.void
setExecutorService
(ExecutorService executor) Configure aExecutorService
to use for asynchronous queries to the underlying database.void
setInitialRttEstimate
(long initialRttEstimate) Set the initial round trip time estimate.void
setKVDatabase
(KVDatabase inner) Configure the underlyingKVDatabase
.void
setThreadPoolSize
(int threadPoolSize) Set the number of threads in the internally-created thread pool.void
start()
Start this instance.void
stop()
Stop this instance.Methods inherited from class io.permazen.kv.caching.AbstractCachingConfig
getMaxRangeBytes, getMaxRanges, getMaxTotalBytes, getWaitFactor, isReadAhead, setMaxRangeBytes, setMaxRanges, setMaxTotalBytes, setReadAhead, setWaitFactor
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.permazen.kv.caching.CachingConfig
copyCachingConfigTo
-
Field Details
-
DEFAULT_INITIAL_RTT_ESTIMATE_MILLIS
public static final int DEFAULT_INITIAL_RTT_ESTIMATE_MILLISDefault for the initial round-trip time estimate in milliseconds. -
DEFAULT_THREAD_POOL_SIZE
public static final int DEFAULT_THREAD_POOL_SIZEDefault thread pool size when noExecutorService
is explicitly configured.
-
-
Constructor Details
-
CachingKVDatabase
public CachingKVDatabase()Default constructor.Instances must be configured with an inner
KVDatabase
before starting. -
CachingKVDatabase
Primary constructor.- Parameters:
inner
- innerKVDatabase
-
-
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 startedIllegalArgumentException
- ifinitialRttEstimate < 0
-
getKVDatabase
Get the innerKVDatabase
.- Returns:
- the underlying
KVDatabase
-
setKVDatabase
Configure the underlyingKVDatabase
.- Parameters:
inner
- the underlyingKVDatabase
- Throws:
IllegalStateException
- if this instance is already started
-
getExecutorService
Get the configuredExecutorService
, if any.- Returns:
- caller-supplied executor to use for background tasks, or null for none
-
setExecutorService
Configure aExecutorService
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 bystop()
. If an outside executor is configured here, then it will not be shutdown bystop()
.- 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 viasetExecutorService()
.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 viasetExecutorService()
.Default value is 10.
- Parameters:
threadPoolSize
- number of threads in thread pool- Throws:
IllegalStateException
- if this instance is already startedIllegalArgumentException
- ifthreadPoolSize <= 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 interfaceKVDatabase
-
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 interfaceKVDatabase
-
createTransaction
Description copied from interface:KVDatabase
Create a new transaction.- Specified by:
createTransaction
in interfaceKVDatabase
- Returns:
- newly created transaction
-
createTransaction
Description copied from interface:KVDatabase
Create a new transaction with the specified options.- Specified by:
createTransaction
in interfaceKVDatabase
- Parameters:
options
- optional transaction options; may be null- Returns:
- newly created transaction
-
createTransaction
-
getRttEstimate
public double getRttEstimate()Get the current round trip time estimate.- Returns:
- current RTT estimate in nanoseconds
- Throws:
IllegalStateException
- if this instance has neverstart()
ed
-