Class SQLKVTransaction
- All Implemented Interfaces:
KVStore
,KVTransaction
SQLKVDatabase
transaction.-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static enum
Used internally to build SQL statements. -
Field Summary
Modifier and TypeFieldDescriptionprotected final Connection
protected final SQLKVDatabase
protected final Logger
-
Constructor Summary
ConstructorDescriptionSQLKVTransaction
(SQLKVDatabase database, Connection connection) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Apply all the givenMutations
to this instance.protected void
Close theConnection
associated with this instance, if it's not already closed.void
commit()
Commit this transaction.protected byte[]
decodeKey
(byte[] dbkey) Decode the given database key value encoded byencodeKey()
.protected KVStore
delegate()
Get the underlyingKVStore
.protected byte[]
encodeKey
(byte[] key) Encode the given key for the underlying database key column.protected void
finalize()
Get theKVDatabase
with which this instance is associated.protected KVTransactionException
Handle an unexpected SQL exception.boolean
Determine whether this transaction is read-only.void
put
(byte[] key, byte[] value) Set the value associated with the given key.protected <T> T
query
(SQLKVTransaction.StmtType stmtType, io.permazen.kv.sql.SQLKVTransaction.ResultSetFunction<T> resultSetFunction, boolean close, byte[]... params) protected byte[]
queryBytes
(SQLKVTransaction.StmtType stmtType, byte[]... params) protected CloseableIterator<KVPair>
queryIterator
(SQLKVTransaction.StmtType stmtType, byte[]... params) protected KVPair
queryKVPair
(SQLKVTransaction.StmtType stmtType, byte[]... params) Create a read-only snapshot of the database content represented by this transaction.void
remove
(byte[] key) Remove the key/value pair with the given key, if it exists.void
removeRange
(byte[] minKey, byte[] maxKey) Remove all key/value pairs whose keys are in a given range.void
rollback()
Cancel this transaction, if not already canceled.void
setReadOnly
(boolean readOnly) Enable or disable read-only mode.void
setTimeout
(long timeout) Change the timeout for this transaction from its default value (optional operation).protected void
update
(SQLKVTransaction.StmtType stmtType, byte[]... params) protected void
updateBatch
(SQLKVTransaction.StmtType stmtType, List<byte[]> paramList) watchKey
(byte[] key) Watch a key to monitor for changes in its value.Methods inherited from class io.permazen.kv.util.ForwardingKVStore
adjustCounter, decodeCounter, encodeCounter, get, getAtLeast, getAtMost, getRange
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.permazen.kv.KVStore
adjustCounter, decodeCounter, encodeCounter, get, getAtLeast, getAtMost, getRange, getRange, getRange, removeRange
Methods inherited from interface io.permazen.kv.KVTransaction
withWeakConsistency
-
Field Details
-
log
-
database
-
connection
-
-
Constructor Details
-
SQLKVTransaction
Constructor.- Parameters:
database
- the associated databaseconnection
- theConnection
for the transaction- Throws:
SQLException
- if an SQL error occurs
-
-
Method Details
-
getKVDatabase
Description copied from interface:KVTransaction
Get theKVDatabase
with which this instance is associated.- Specified by:
getKVDatabase
in interfaceKVTransaction
- Returns:
- associated database
-
setTimeout
public void setTimeout(long timeout) Description copied from interface:KVTransaction
Change the timeout for this transaction from its default value (optional operation).- Specified by:
setTimeout
in interfaceKVTransaction
- Parameters:
timeout
- transaction timeout in milliseconds, or zero for unlimited
-
watchKey
Watch a key to monitor for changes in its value.The implementation in
SQLKVTransaction
always throwsUnsupportedOperationException
. Subclasses may add support using a database-specific notification mechanism.- Specified by:
watchKey
in interfaceKVTransaction
- Parameters:
key
- the key to watch- Returns:
- a
Future
that returnskey
when the value associated withkey
is modified - Throws:
StaleKVTransactionException
- if this transaction is no longer usableRetryKVTransactionException
- if this transaction must be retried and is no longer usableKVDatabaseException
- if an unexpected error occursUnsupportedOperationException
- if this instance does not support key watchesIllegalArgumentException
- ifkey
is null- See Also:
-
isReadOnly
public boolean isReadOnly()Description copied from interface:KVTransaction
Determine whether this transaction is read-only.Default is false.
- Specified by:
isReadOnly
in interfaceKVTransaction
- Returns:
- true if this instance is read-only
-
commit
public void commit()Description copied from interface:KVTransaction
Commit 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:
commit
in interfaceKVTransaction
-
rollback
public void rollback()Description copied from interface:KVTransaction
Cancel 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:
rollback
in interfaceKVTransaction
-
readOnlySnapshot
Description copied from interface:KVTransaction
Create a read-only snapshot of the database content represented by this transaction.The returned
CloseableKVStore
should 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 returnedCloseableKVStore
must 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
CloseableKVStore
should be promplyclose()
'd when no longer needed to release any underlying resources. In particular, the caller must ensure that theCloseableKVStore
isclose()
'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
UnsupportedOperationException
if not supported.- Specified by:
readOnlySnapshot
in interfaceKVTransaction
- Returns:
- independent, read-only copy of this transaction's entire database content
-
handleException
Handle an unexpected SQL exception.The implementation in
SQLKVTransaction
rolls back the SQL transaction, closes the associatedConnection
, and wraps the exception viaSQLKVDatabase.wrapException()
.- Parameters:
e
- original exception- Returns:
- key/value transaction exception
-
closeConnection
protected void closeConnection()Close theConnection
associated with this instance, if it's not already closed. This method is idempotent. -
finalize
-
put
public void put(byte[] key, byte[] value) Description copied from interface:KVStore
Set the value associated with the given key.- Specified by:
put
in interfaceKVStore
- Overrides:
put
in classForwardingKVStore
- Parameters:
key
- keyvalue
- value
-
remove
public void remove(byte[] key) Description copied from interface:KVStore
Remove the key/value pair with the given key, if it exists.- Specified by:
remove
in interfaceKVStore
- Overrides:
remove
in classForwardingKVStore
- Parameters:
key
- key
-
removeRange
public void removeRange(byte[] minKey, byte[] maxKey) Description copied from interface:KVStore
Remove all key/value pairs whose keys are in a given range.The
minKey
must be less than or equal tomaxKey
; if they equal (and not null) then nothing happens; if they are both null then all entries are deleted.If keys starting with
0xff
are not supported by this instance, then:- If
minKey
starts with0xff
, then no change occurs - If
maxKey
starts with0xff
, then this method behaves as ifmaxKey
were null
- Specified by:
removeRange
in interfaceKVStore
- Overrides:
removeRange
in classForwardingKVStore
- Parameters:
minKey
- minimum key (inclusive), or null for no minimummaxKey
- maximum key (exclusive), or null for no maximum
- If
-
apply
Description copied from interface:KVStore
Apply all the givenMutations
to this instance.Mutations are always to be applied in this order: removes, puts, counter adjustments.
The implementation in
KVStore
simply iterates over the individual changes and applies them viaremove()
(for removals of a single key),removeRange()
,put()
, and/oradjustCounter()
. Implementations that can process batch updates more efficiently are encouraged to override this method.Unlike
AtomicKVStore.apply()
, this method is not required to apply the mutations atomically.- Specified by:
apply
in interfaceKVStore
- Overrides:
apply
in classForwardingKVStore
- Parameters:
mutations
- mutations to apply
-
delegate
Description copied from class:ForwardingKVStore
Get the underlyingKVStore
.- Specified by:
delegate
in classForwardingKVStore
- Returns:
- underlying
KVStore
-
setReadOnly
public void setReadOnly(boolean readOnly) Description copied from interface:KVTransaction
Enable 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
IllegalStateException
is 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:
setReadOnly
in interfaceKVTransaction
- Parameters:
readOnly
- read-only setting
-
queryBytes
-
queryKVPair
-
queryIterator
protected CloseableIterator<KVPair> queryIterator(SQLKVTransaction.StmtType stmtType, byte[]... params) -
query
protected <T> T query(SQLKVTransaction.StmtType stmtType, io.permazen.kv.sql.SQLKVTransaction.ResultSetFunction<T> resultSetFunction, boolean close, byte[]... params) -
update
-
updateBatch
-
encodeKey
protected byte[] encodeKey(byte[] key) Encode the given key for the underlying database key column.The implementation in
SQLKVTransaction
just returnskey
.- Parameters:
key
- key- Returns:
- database value for key column
- See Also:
-
decodeKey
protected byte[] decodeKey(byte[] dbkey) Decode the given database key value encoded byencodeKey()
.The implementation in
SQLKVTransaction
just returnsdbkey
.- Parameters:
dbkey
- database value for key column- Returns:
- key
- See Also:
-