@ThreadSafe public class SpannerKVTransaction extends ForwardingKVStore implements KVTransaction
SpannerKVDatabase
transaction.Modifier and Type | Field and Description |
---|---|
protected DatabaseClient |
client |
protected TimestampBound |
consistency |
protected SpannerKVDatabase |
kvdb |
protected Logger |
log |
protected String |
tableName |
Modifier | Constructor and Description |
---|---|
protected |
SpannerKVTransaction(SpannerKVDatabase kvdb,
DatabaseClient client,
String tableName,
TimestampBound consistency)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
commit()
Commit this transaction.
|
protected KVStore |
delegate()
Get the underlying
KVStore . |
byte[] |
get(byte[] key)
Get the value associated with the given key, if any.
|
KVPair |
getAtLeast(byte[] minKey,
byte[] maxKey)
Get the key/value pair having the smallest key greater than or equal to the given minimum, if any.
|
KVPair |
getAtMost(byte[] maxKey,
byte[] minKey)
Get the key/value pair having the largest key strictly less than the given maximum, if any.
|
TimestampBound |
getConsistency()
Get the consistency level configured for this transaction.
|
SpannerKVDatabase |
getKVDatabase()
Get the
KVDatabase with which this instance is associated. |
CloseableIterator<KVPair> |
getRange(byte[] minKey,
byte[] maxKey,
boolean reverse)
Iterate the key/value pairs in the specified range.
|
com.google.cloud.Timestamp |
getTimestamp()
Get the timestamp associated with this transaction.
|
boolean |
isReadOnly()
Determine whether this transaction is read-only.
|
boolean |
isStrongConsistency()
Convenience method to determine whether this transaction is using strong consistency.
|
CloseableKVStore |
mutableSnapshot()
Create a mutable snapshot of this transaction.
|
void |
rollback()
Cancel this transaction, if not already canceled.
|
void |
setReadOnly(boolean readOnly)
Enable or disable read-only mode.
|
void |
setTimeout(long timeout)
Set transaction timeout.
|
Future<Void> |
watchKey(byte[] key)
Set key watch.
|
protected RuntimeException |
wrapException(SpannerException e) |
adjustCounter, apply, decodeCounter, encodeCounter, put, remove, removeRange
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
adjustCounter, apply, decodeCounter, encodeCounter, getRange, getRange, put, remove, removeRange, removeRange
protected final Logger log
protected final SpannerKVDatabase kvdb
protected final DatabaseClient client
protected final String tableName
protected final TimestampBound consistency
protected SpannerKVTransaction(SpannerKVDatabase kvdb, DatabaseClient client, String tableName, TimestampBound consistency)
kvdb
- associated databaseclient
- client for accesstableName
- Spanner key/value database table nameconsistency
- transaction consistency levelIllegalArgumentException
- if any paramter is nullpublic TimestampBound getConsistency()
Note that read-write transactions always use strong consistency.
public boolean isStrongConsistency()
public com.google.cloud.Timestamp getTimestamp()
For read-only transactions, this returns the Spanner timestamp at which the data was accessed. It should not be invoked until at least one data query has occurred.
For read-write transactions, this returns the Spanner timestamp at which the changes were successfully applied.
IllegalStateException
- if timestamp is not available yetIllegalStateException
- if invoked on a read/write transaction that was not successfully committedpublic SpannerKVDatabase getKVDatabase()
KVTransaction
KVDatabase
with which this instance is associated.getKVDatabase
in interface KVTransaction
public void setTimeout(long timeout)
Currently not supported; this method does nothing.
setTimeout
in interface KVTransaction
timeout
- transaction timeout in milliseconds, or zero for unlimitedpublic boolean isReadOnly()
KVTransaction
Default is false.
isReadOnly
in interface KVTransaction
public void setReadOnly(boolean readOnly)
KVTransaction
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
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.
setReadOnly
in interface KVTransaction
readOnly
- read-only settingpublic void commit()
KVTransaction
Note that if this method throws a RetryTransactionException
,
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.
commit
in interface KVTransaction
public void rollback()
KVTransaction
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()
or KVTransaction.rollback()
, in which case the invocation will be ignored.
In particular, this method should not throw StaleTransactionException
.
rollback
in interface KVTransaction
public Future<Void> watchKey(byte[] key)
Key watches are not supported.
watchKey
in interface KVTransaction
key
- the key to watchFuture
that returns key
when the value associated with key
is modifiedUnsupportedOperationException
- alwaysJTransaction.getKey()
public CloseableKVStore mutableSnapshot()
This method is not supported.
With Spanner, a transaction is not needed to create mutable snapshots; instead, see
SpannerKVDatabase.snapshot()
and MutableView
.
mutableSnapshot
in interface KVTransaction
UnsupportedOperationException
- alwayspublic byte[] get(byte[] key)
KVStore
Modifications to the returned byte[]
array do not affect this instance.
get
in interface KVStore
get
in class ForwardingKVStore
key
- keypublic KVPair getAtLeast(byte[] minKey, byte[] maxKey)
KVStore
An optional (exclusive) maximum key may also be specified; if maxKey
is null, there is no upper bound;
if maxKey <= minKey
, null is always returned.
If keys starting with 0xff
are not supported by this instance, and minKey
starts with 0xff
,
then this method returns null.
Modifications to the returned byte[]
arrays do not affect this instance.
getAtLeast
in interface KVStore
getAtLeast
in class ForwardingKVStore
minKey
- minimum key (inclusive), or null for no minimum (get the smallest key)maxKey
- maximum key (exclusive), or null for no maximum (no upper bound)key >= minKey
and key < maxKey
, or null if none existspublic KVPair getAtMost(byte[] maxKey, byte[] minKey)
KVStore
An optional (inclusive) minimum key may also be specified; if minKey
is null, there is no lower bound
(equivalent to a lower bound of the empty byte array); if minKey >= maxKey
, null is always returned.
If keys starting with 0xff
are not supported by this instance, and maxKey
starts with 0xff
,
then this method behaves as if maxKey
were null.
Modifications to the returned byte[]
arrays do not affect this instance.
getAtMost
in interface KVStore
getAtMost
in class ForwardingKVStore
maxKey
- maximum key (exclusive), or null for no maximum (get the largest key)minKey
- minimum key (inclusive), or null for no minimum (no lower bound)key < maxKey
and key >= minKey
, or null if none existspublic CloseableIterator<KVPair> getRange(byte[] minKey, byte[] maxKey, boolean reverse)
KVStore
Iterator
's
remove()
method must be supported and should have the same effect as
invoking remove()
on the corresponding key.
If keys starting with 0xff
are not supported by this instance, and minKey
starts with 0xff
,
then this method returns an empty iteration.
If keys starting with 0xff
are not supported by this instance, and maxKey
starts with 0xff
,
then this method behaves as if maxKey
were null.
The returned Iterator
must not throw ConcurrentModificationException
;
however, whether or not a "live" Iterator
reflects any modifications made after its creation is
implementation dependent. Implementations that do make post-creation updates visible in the Iterator
,
even if the update occurs after some delay, must preserve the order in which the modifications actually occurred.
The returned Iterator
itself is not guaranteed to be thread safe.
Invokers of this method are encouraged to close()
the returned iterators,
though this is not required for correct behavior.
Modifications to the returned KVPair
key and value byte[]
arrays do not affect this instance.
getRange
in interface KVStore
getRange
in class ForwardingKVStore
minKey
- minimum key (inclusive), or null for no minimum (start at the smallest key)maxKey
- maximum key (exclusive), or null for no maximum (end at the largest key)reverse
- true to return key/value pairs in reverse order (i.e., keys descending)minKey
(inclusive) to maxKey
(exclusive)protected KVStore delegate()
ForwardingKVStore
KVStore
.delegate
in class ForwardingKVStore
KVStore
protected RuntimeException wrapException(SpannerException e)
Copyright © 2022. All rights reserved.