public class SimpleKVDatabase extends Object implements KVDatabase, Serializable
KVDatabase interface that provides a concurrent, transactional view
of an underlying KVStore with strong ACID semantics (Durability must be provided by the KVStore).
The ACID semantics, as well as wait timeouts and hold timeouts,
are provided by the LockManager class. If the wait timeout is exceeded, a RetryTransactionException
is thrown. If the hold timeout is exceeded, a TransactionTimeoutException is thrown.
Instances wrap an underlying KVStore which provides persistence and from which committed data is read and written.
During a transaction, all mutations are recorded internally; if/when the transaction is committed, those mutations are
applied to the underlying KVStore all at once, and this operation is bracketed by calls to
preCommit() and postCommit(). If the underlying KVStore
is a AtomicKVStore, then AtomicKVStore.mutate() is used.
Key watches are supported.
Instances implement Serializable if the underlying KVStore is; this is the case when the default
constructor, which uses a NavigableMapKVStore, is used. However, key watches and open transactions are not
remembered across a (de)serialization cycle.
LockManager,
Serialized Form| Modifier and Type | Field and Description |
|---|---|
static long |
DEFAULT_HOLD_TIMEOUT
Default hold timeout in milliseconds (5000L).
|
static long |
DEFAULT_WAIT_TIMEOUT
Default wait timeout for newly created transactions in milliseconds
(500L).
|
protected KVStore |
kv
The
KVStore for the committed data. |
protected Logger |
log |
| Constructor and Description |
|---|
SimpleKVDatabase()
Constructor.
|
SimpleKVDatabase(KVStore kv)
Constructor taking caller-supplied storage.
|
SimpleKVDatabase(KVStore kv,
long waitTimeout,
long holdTimeout)
Primary constructor.
|
SimpleKVDatabase(long waitTimeout,
long holdTimeout)
Constructor taking timeout settings.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
checkState(SimpleKVTransaction tx)
Verify that the given transaction is still usable.
|
SimpleKVTransaction |
createTransaction() |
SimpleKVTransaction |
createTransaction(Map<String,?> options) |
long |
getHoldTimeout()
Get the hold timeout configured for this instance.
|
long |
getWaitTimeout()
Get the wait timeout for newly created transactions.
|
protected void |
postCommit(SimpleKVTransaction tx,
boolean successful)
Invoked during transaction commit just after writing changes to the underlying
KVStore. |
protected void |
preCommit(SimpleKVTransaction tx)
Invoked during transaction commit just prior to writing changes to the underlying
KVStore. |
void |
setHoldTimeout(long holdTimeout)
Set the hold timeout for this instance.
|
void |
setWaitTimeout(long waitTimeout)
Set the wait timeout for newly created transactions.
|
void |
start() |
void |
stop() |
public static final long DEFAULT_WAIT_TIMEOUT
public static final long DEFAULT_HOLD_TIMEOUT
protected transient Logger log
public SimpleKVDatabase()
NavigableMapKVStore and the default wait and hold timeouts.public SimpleKVDatabase(KVStore kv)
public SimpleKVDatabase(long waitTimeout,
long holdTimeout)
KVStore.waitTimeout - how long a thread will wait for a lock before throwing RetryTransactionException
in milliseconds, or zero for unlimitedholdTimeout - how long a thread may hold a contestested lock before throwing RetryTransactionException
in milliseconds, or zero for unlimitedIllegalArgumentException - if waitTimeout or holdTimeout is negativepublic SimpleKVDatabase(KVStore kv, long waitTimeout, long holdTimeout)
kv - KVStore for the committed data, or null for an in-memory NavigableMapKVStorewaitTimeout - how long a thread will wait for a lock before throwing RetryTransactionException
in milliseconds, or zero for unlimitedholdTimeout - how long a thread may hold a contestested lock before throwing RetryTransactionException
in milliseconds, or zero for unlimitedIllegalArgumentException - if waitTimeout or holdTimeout is negativepublic long getWaitTimeout()
The wait timeout limits how long a thread will wait for a contested lock before giving up and throwing
RetryTransactionException.
public void setWaitTimeout(long waitTimeout)
DEFAULT_WAIT_TIMEOUT.waitTimeout - how long a thread will wait for a lock before throwing RetryTransactionException
in milliseconds (default), or zero for unlimitedIllegalArgumentException - if waitTimeout is negativepublic long getHoldTimeout()
The hold timeout limits how long a thread may hold on to a contested lock before being forced to release
all of its locks; after that, the next attempted operation will fail with RetryTransactionException.
public void setHoldTimeout(long holdTimeout)
DEFAULT_HOLD_TIMEOUT.holdTimeout - how long a thread may hold a contestested lock before throwing RetryTransactionException
in milliseconds, or zero for unlimitedIllegalArgumentException - if holdTimeout is negativepublic void start()
start in interface KVDatabasepublic void stop()
stop in interface KVDatabasepublic SimpleKVTransaction createTransaction(Map<String,?> options)
createTransaction in interface KVDatabasepublic SimpleKVTransaction createTransaction()
createTransaction in interface KVDatabaseprotected void preCommit(SimpleKVTransaction tx)
KVStore.
SimpleKVDatabase guarantees this method and postCommit() will be invoked in matching pairs,
and that this instance will be locked when these methods are invoked.
The implementation in SimpleKVDatabase does nothing.
tx - the transaction about to be committedRetryTransactionException - if this transaction must be retried and is no longer usableprotected void postCommit(SimpleKVTransaction tx, boolean successful)
KVStore.
SimpleKVDatabase guarantees this method and preCommit() will be invoked in matching pairs,
and that this instance will be locked when these methods are invoked.
This method is invoked even if the underlying KVStore throws an exception while changes were being written to it.
In that case, successful will be false.
The implementation in SimpleKVDatabase does nothing.
tx - the transaction that was committedsuccessful - true if all changes were written back successfully,
false if the underlying KVStore threw an exception during commit updateprotected void checkState(SimpleKVTransaction tx)
This method is invoked at the start of the KVStore data access and commit()
methods of the SimpleKVTransaction associated with this instance. This allows for any checks which depend on
a consistent view of the transaction and database together. This instance's lock will be held when this method is invoked.
Note: transaction state is also protected by this instance's lock.
The implementation in SimpleKVDatabase does nothing.
tx - the transaction being accessedStaleTransactionException - if this instance is no longer usableRetryTransactionException - if this transaction should be retriedTransactionTimeoutException - if the transaction has timed outCopyright © 2016. All rights reserved.