Class SpannerKVTransaction
- All Implemented Interfaces:
KVStore
,KVTransaction
SpannerKVDatabase
transaction.-
Field Summary
Modifier and TypeFieldDescriptionprotected final com.google.cloud.spanner.DatabaseClient
protected final com.google.cloud.spanner.TimestampBound
protected final SpannerKVDatabase
protected final Logger
protected final String
-
Constructor Summary
ModifierConstructorDescriptionprotected
SpannerKVTransaction
(SpannerKVDatabase kvdb, com.google.cloud.spanner.DatabaseClient client, String tableName, com.google.cloud.spanner.TimestampBound consistency) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
commit()
Commit this transaction.protected KVStore
delegate()
Get the underlyingKVStore
.byte[]
get
(byte[] key) Get the value associated with the given key, if any.getAtLeast
(byte[] minKey, byte[] maxKey) Get the key/value pair having the smallest key greater than or equal to the given minimum, if any.getAtMost
(byte[] maxKey, byte[] minKey) Get the key/value pair having the largest key strictly less than the given maximum, if any.com.google.cloud.spanner.TimestampBound
Get the consistency level configured for this transaction.Get theKVDatabase
with which this instance is associated.getRange
(byte[] minKey, byte[] maxKey, boolean reverse) Iterate the key/value pairs in the specified range.com.google.cloud.Timestamp
Get the timestamp associated with this transaction.boolean
Determine whether this transaction is read-only.boolean
Convenience method to determine whether this transaction is using strong consistency.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.watchKey
(byte[] key) Set key watch.protected RuntimeException
wrapException
(com.google.cloud.spanner.SpannerException e) Methods inherited from class io.permazen.kv.util.ForwardingKVStore
adjustCounter, apply, decodeCounter, encodeCounter, put, remove, removeRange
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.KVStore
adjustCounter, apply, decodeCounter, encodeCounter, getRange, getRange, put, remove, removeRange, removeRange
Methods inherited from interface io.permazen.kv.KVTransaction
withWeakConsistency
-
Field Details
-
log
-
kvdb
-
client
protected final com.google.cloud.spanner.DatabaseClient client -
tableName
-
consistency
protected final com.google.cloud.spanner.TimestampBound consistency
-
-
Constructor Details
-
SpannerKVTransaction
protected SpannerKVTransaction(SpannerKVDatabase kvdb, com.google.cloud.spanner.DatabaseClient client, String tableName, com.google.cloud.spanner.TimestampBound consistency) Constructor.- Parameters:
kvdb
- associated databaseclient
- client for accesstableName
- Spanner key/value database table nameconsistency
- transaction consistency level- Throws:
IllegalArgumentException
- if any paramter is null
-
-
Method Details
-
getConsistency
public com.google.cloud.spanner.TimestampBound getConsistency()Get the consistency level configured for this transaction.Note that read-write transactions always use strong consistency.
- Returns:
- consistency of this transaction
-
isStrongConsistency
public boolean isStrongConsistency()Convenience method to determine whether this transaction is using strong consistency.- Returns:
- true if this transaction has strong consistency
-
getTimestamp
public com.google.cloud.Timestamp getTimestamp()Get the timestamp associated with this transaction.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.
- Returns:
- this transaction's Spanner timestamp
- Throws:
IllegalStateException
- if timestamp is not available yetIllegalStateException
- if invoked on a read/write transaction that was not successfully committed
-
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) Set transaction timeout.Currently not supported; this method does nothing.
- Specified by:
setTimeout
in interfaceKVTransaction
- Parameters:
timeout
- transaction timeout in milliseconds, or zero for unlimited
-
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
-
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
-
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
-
watchKey
Set key watch.Key watches are not supported.
- Specified by:
watchKey
in interfaceKVTransaction
- Parameters:
key
- the key to watch- Returns:
- a
Future
that returnskey
when the value associated withkey
is modified - Throws:
UnsupportedOperationException
- always- See Also:
-
readOnlySnapshot
Create a mutable snapshot of this transaction.This method is not supported.
With Spanner, a transaction is not needed to create mutable snapshots; instead, see
SpannerKVDatabase.snapshot()
andMutableView
.- Specified by:
readOnlySnapshot
in interfaceKVTransaction
- Returns:
- independent, read-only copy of this transaction's entire database content
- Throws:
UnsupportedOperationException
- always
-
get
public byte[] get(byte[] key) Description copied from interface:KVStore
Get the value associated with the given key, if any.Modifications to the returned
byte[]
array do not affect this instance.- Specified by:
get
in interfaceKVStore
- Overrides:
get
in classForwardingKVStore
- Parameters:
key
- key- Returns:
- value associated with key, or null if not found
-
getAtLeast
Description copied from interface:KVStore
Get the key/value pair having the smallest key greater than or equal to the given minimum, if any.An optional (exclusive) maximum key may also be specified; if
maxKey
is null, there is no upper bound; ifmaxKey <= minKey
, null is always returned.If keys starting with
0xff
are not supported by this instance, andminKey
starts with0xff
, then this method returns null.Modifications to the returned
byte[]
arrays do not affect this instance.- Specified by:
getAtLeast
in interfaceKVStore
- Overrides:
getAtLeast
in classForwardingKVStore
- Parameters:
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)- Returns:
- smallest key/value pair with
key >= minKey
andkey < maxKey
, or null if none exists
-
getAtMost
Description copied from interface:KVStore
Get the key/value pair having the largest key strictly less than the given maximum, if any.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); ifminKey >= maxKey
, null is always returned.If keys starting with
0xff
are not supported by this instance, andmaxKey
starts with0xff
, then this method behaves as ifmaxKey
were null.Modifications to the returned
byte[]
arrays do not affect this instance.- Specified by:
getAtMost
in interfaceKVStore
- Overrides:
getAtMost
in classForwardingKVStore
- Parameters:
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)- Returns:
- largest key/value pair with
key < maxKey
andkey >= minKey
, or null if none exists
-
getRange
Description copied from interface:KVStore
Iterate the key/value pairs in the specified range. The returnedCloseableIterator
'sremove()
method must be supported and should have the same effect as invokingremove()
on the corresponding key.If keys starting with
0xff
are not supported by this instance, andminKey
starts with0xff
, then this method returns an empty iteration.If keys starting with
0xff
are not supported by this instance, andmaxKey
starts with0xff
, then this method behaves as ifmaxKey
were null.The returned
CloseableIterator
is weakly consistent (seejava.util.concurrent
). In short, the returnedCloseableIterator
must not throwConcurrentModificationException
; however, whether or not a "live"CloseableIterator
reflects any modifications made after its creation is implementation dependent. Implementations that do make post-creation updates visible in theCloseableIterator
, even if the update occurs after some delay, must preserve the order in which the modifications actually occurred.The returned
CloseableIterator
itself is not guaranteed to be thread safe; is should only be used in the thread that created it.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 valuebyte[]
arrays do not affect this instance.- Specified by:
getRange
in interfaceKVStore
- Overrides:
getRange
in classForwardingKVStore
- Parameters:
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)- Returns:
- iteration of key/value pairs in the range
minKey
(inclusive) tomaxKey
(exclusive)
-
delegate
Description copied from class:ForwardingKVStore
Get the underlyingKVStore
.- Specified by:
delegate
in classForwardingKVStore
- Returns:
- underlying
KVStore
-
wrapException
-