Class LMDBKVTransaction<T>
- Type Parameters:
T- buffer type
- All Implemented Interfaces:
KVStore,KVTransaction
- Direct Known Subclasses:
ByteArrayLMDBKVTransaction
KVTransaction.-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedLMDBKVTransaction(LMDBKVDatabase<T> kvdb, org.lmdbjava.Env<T> env, org.lmdbjava.Dbi<T> db) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidcommit()Commit this transaction.protected abstract LMDBKVStore<T>createKVStore(org.lmdbjava.Dbi<T> db, org.lmdbjava.Txn<T> tx) protected KVStoredelegate()Get the underlyingKVStore.Get theKVDatabasewith which this instance is associated.booleanDetermine whether this transaction is read-only.Create a read-only snapshot of the database content represented by this transaction.voidrollback()Cancel this transaction, if not already canceled.voidsetReadOnly(boolean readOnly) Enable or disable read-only mode.voidsetTimeout(long timeout) Change the timeout for this transaction from its default value (optional operation).Watch a key to monitor for changes in its value.Methods inherited from class io.permazen.kv.util.ForwardingKVStore
adjustCounter, apply, decodeCounter, encodeCounter, get, getAtLeast, getAtMost, getRange, put, remove, removeRangeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.permazen.kv.KVStore
adjustCounter, apply, decodeCounter, encodeCounter, get, getAtLeast, getAtMost, getRange, getRange, getRange, put, remove, removeRange, removeRangeMethods inherited from interface io.permazen.kv.KVTransaction
withWeakConsistency
-
Constructor Details
-
LMDBKVTransaction
protected LMDBKVTransaction(LMDBKVDatabase<T> kvdb, org.lmdbjava.Env<T> env, org.lmdbjava.Dbi<T> db) Constructor.- Parameters:
kvdb- associated databaseenv- environmentdb- database handle
-
-
Method Details
-
getKVDatabase
Description copied from interface:KVTransactionGet theKVDatabasewith which this instance is associated.- Specified by:
getKVDatabasein interfaceKVTransaction- Returns:
- associated database
-
commit
public void commit()Description copied from interface:KVTransactionCommit this transaction.Note that if this method throws a
RetryKVTransactionException, the transaction was either successfully committed or rolled back. In either case, this instance is no longer usable.Note also for some implementations, even read-only transactions must be
KVTransaction.commit()'ed in order for the data accessed during the transaction to be guaranteed to be up to date.- Specified by:
commitin interfaceKVTransaction
-
rollback
public void rollback()Description copied from interface:KVTransactionCancel this transaction, if not already canceled.After this method returns, this instance is no longer usable.
Note: for some implementations, rolling back a transaction invalidates guarantees about the the data read during the transaction being up to date, even if the transaction was
setReadOnly().This method may be invoked at any time, even after a previous invocation of
KVTransaction.commit()orKVTransaction.rollback(), in which case the invocation will be ignored. In particular, this method must not throwStaleKVTransactionException.- Specified by:
rollbackin interfaceKVTransaction
-
isReadOnly
public boolean isReadOnly()Description copied from interface:KVTransactionDetermine whether this transaction is read-only.Default is false.
- Specified by:
isReadOnlyin interfaceKVTransaction- Returns:
- true if this instance is read-only
-
setReadOnly
public void setReadOnly(boolean readOnly) Description copied from interface:KVTransactionEnable or disable read-only mode.Read-only transactions allow mutations, but all changes are discarded on
KVTransaction.commit().Some implementations may impose one or more of the following restrictions on this method:
setReadOnly()may only be invoked prior to accessing data;setReadOnly()may only be invoked prior to mutating data; and/or- Once set to read-only, a transaction may not be set back to read-write
IllegalStateExceptionis thrown.Note: for some implementations, the data read from a transaction that is never
KVTransaction.commit()'ed is not guaranteed to be up to date, even if that transaction is read-only.Default is false.
- Specified by:
setReadOnlyin interfaceKVTransaction- Parameters:
readOnly- read-only setting
-
readOnlySnapshot
Description copied from interface:KVTransactionCreate a read-only snapshot of the database content represented by this transaction.The returned
CloseableKVStoreshould be treated as read-only. It may not actually be read-only, but if it's not, then any changes should have no effect on this instance. The returnedCloseableKVStoremust be completely independent from this transaction (subsequent changes to either one do not affect the other).Note: as with any other information extracted from a
KVTransaction, the returned content should not be considered valid until this transaction has been successfully committed.The returned
CloseableKVStoreshould be promplyclose()'d when no longer needed to release any underlying resources. In particular, the caller must ensure that theCloseableKVStoreisclose()'d even if this transaction's commit fails. This may require adding a transaction synchronization callback, etc.This is an optional method; only some underlying key/value store technologies can efficiently support it. Implementations should throw
UnsupportedOperationExceptionif not supported.- Specified by:
readOnlySnapshotin interfaceKVTransaction- Returns:
- independent, read-only copy of this transaction's entire database content
-
setTimeout
public void setTimeout(long timeout) Description copied from interface:KVTransactionChange the timeout for this transaction from its default value (optional operation).- Specified by:
setTimeoutin interfaceKVTransaction- Parameters:
timeout- transaction timeout in milliseconds, or zero for unlimited
-
watchKey
Description copied from interface:KVTransactionWatch a key to monitor for changes in its value.When this method is invoked,
key's current value (if any) as read by this transaction is remembered. The returnedFuturecompletes if and when a different value forkeyis subsequently committed by some transaction, including possibly this one. This includes creation or deletion of the key.Key watches outlive the transaction in which they are created, persisting until they complete or are
cancel()'ed. When aKVDatabaseisKVDatabase.stop()'ed, all outstanding key watches are implicitlycancel()'ed.Caveats
Key watches are not without overhead; applications should avoid overuse. For example, consider creating a single key that is used to consolidate modifications to some set of keys; at the Permazen layer, modification to multiple objects and/or fields can detected and consolidated using an
@OnChangemethod that increments a singleCounterfield, whose key is then watched (to determine the key corresponding to a Java model object field, usePermazenField.getKey()).Conceptually, detection of changes behaves as if by a background thread that periodically creates a new transaction and reads the key's value (the actual implementation will likely be more efficient). This means a change that is quickly reverted could be missed, and that multiple changes could occur before notification. In addition, spurious notifications may occur, where the key's value has not changed.
A key watch is only guaranteed to be valid if the transaction in which it was created successfully commits. In particular, nothing is specified about how or whether
Futures associated with failed transactions complete, so theFutures returned by this method should not be relied on until after a successful commit (perhaps with the help of a transaction callback).Key watch support is optional; instances that don't support key watches throw
UnsupportedOperationException. Some implementations may only support watching a key that already exists.Note: many
KVDatabaseimplementations actually return aListenableFuture. However, listeners must not perform any long running or blocking operations. Also, because the semantics ofRetryKVTransactionExceptionallow for the possibility that the transaction actually did commit, "duplicate" listener notifications could occur.Key watch
Futures that have not completed yet, but are no longer needed, must becancel()'ed to avoid memory leaks.Key watch support is indepdendent of whether the transaction is read-only.
- Specified by:
watchKeyin interfaceKVTransaction- Parameters:
key- the key to watch- Returns:
- a
Futurethat returnskeywhen the value associated withkeyis modified - See Also:
-
delegate
Description copied from class:ForwardingKVStoreGet the underlyingKVStore.- Specified by:
delegatein classForwardingKVStore- Returns:
- underlying
KVStore
-
createKVStore
-