public class Versionstamp extends java.lang.Object implements java.lang.Comparable<Versionstamp>
Tuple
.
This wraps a single array which should contain twelve bytes. The first ten bytes
are the "transaction" version, and they are usually assigned by the database
in such a way that all transactions receive a different version that is consistent
with a serialization order of the transactions within the database. (One can
use the Transaction.getVersionstamp()
method to retrieve this version from a Transaction
.) This also implies that the
transaction version of newly committed transactions will be monotonically increasing
over time. The final two bytes are the "user" version and should be set by the client.
This allows the user to use this class to impose a total order of items across multiple
transactions in the database in a consistent and conflict-free way. The user can elect to
ignore this parameter by instantiating the class with the paramaterless incomplete()
and one-parameter complete
static initializers. If they do so,
then versions are written with a default (constant) user version.
All Versionstamp
s can exist in one of two states: "incomplete" and "complete".
An "incomplete" Versionstamp
is a Versionstamp
that has not been
initialized with a meaningful transaction version. For example, this might be used
with a Versionstamp
that one wants to fill in with the current transaction's
version information. A "complete" Versionstamp
, in contradistinction, is one
that has been assigned a meaningful transaction version. This is usually the
case if one is reading back a Versionstamp
from the database.
Example usage might be to do something like the following:
CompletableFuture<byte[]> trVersionFuture = db.run((Transaction tr) -> {
// The incomplete Versionstamp will be overwritten with tr's version information when committed.
Tuple t = Tuple.from("prefix", Versionstamp.incomplete());
tr.mutate(MutationType.SET_VERSIONSTAMPED_KEY, t.packWithVersionstamp(), new byte[0]);
return tr.getVersionstamp();
});
byte[] trVersion = trVersionFuture.get();
Versionstamp v = db.run((Transaction tr) -> {
Subspace subspace = new Subspace(Tuple.from("prefix"));
byte[] serialized = tr.getRange(subspace.range(), 1).iterator().next().getKey();
Tuple t = subspace.unpack(serialized);
return t.getVersionstamp(0);
});
assert v.equals(Versionstamp.complete(trVersion));
Here, an incomplete Versionstamp
is packed and written to the database with
the SET_VERSIONSTAMPED_KEY
MutationType
. After committing, we then attempt to read back the same key that
we just wrote. Then we verify the invariant that the deserialized Versionstamp
is
the same as a complete Versionstamp
instance created from the first transaction's
version information.
Modifier and Type | Field and Description |
---|---|
static int |
LENGTH
Length of a serialized
Versionstamp instance when converted into a byte array. |
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Versionstamp other)
Compares two
Versionstamp instances in a manner consistent with their
key order when serialized in the database as keys. |
static Versionstamp |
complete(byte[] trVersion)
Creates a complete
Versionstamp instance with the given
transaction and default user versions. |
static Versionstamp |
complete(byte[] trVersion,
int userVersion)
Creates a complete
Versionstamp instance with the given
transaction and user versions. |
boolean |
equals(java.lang.Object o)
Check another object for equality.
|
static Versionstamp |
fromBytes(byte[] versionBytes)
Creates a
Versionstamp instance based on the given byte array
representation. |
byte[] |
getBytes()
Retrieve a byte-array representation of this
Versionstamp . |
byte[] |
getTransactionVersion()
Retrieve the portion of this
Versionstamp that is set by
the database. |
int |
getUserVersion()
Retrieve the portion of this
Versionstamp that is set
by the user. |
int |
hashCode()
Hash code for this
Versionstamp . |
static Versionstamp |
incomplete()
Creates an incomplete
Versionstamp instance with the default user
version. |
static Versionstamp |
incomplete(int userVersion)
Creates an incomplete
Versionstamp instance with the given
user version. |
boolean |
isComplete()
Whether this
Versionstamp 's transaction version is
meaningful. |
java.lang.String |
toString()
Generate a human-readable representation of this
Versionstamp . |
static int |
unpackUserVersion(byte[] bytes,
int pos)
From a byte array, unpack the user version starting at the given position.
|
public static final int LENGTH
Versionstamp
instance when converted into a byte array.public static int unpackUserVersion(byte[] bytes, int pos)
Versionstamp
s.bytes
- byte array including user versionpos
- starting position of user versionpublic static Versionstamp fromBytes(byte[] versionBytes)
Versionstamp
instance based on the given byte array
representation. This follows the same format as that used by
the main constructor, but the completeness of the Versionstamp
is instead automatically determined by comparing its transaction version
with the value used to indicate an unset transaction version.versionBytes
- byte array representation of Versionstamp
Versionstamp
objectpublic static Versionstamp incomplete(int userVersion)
Versionstamp
instance with the given
user version. The provided user version must fit within an unsigned
short. When converted into a byte array, the bytes for the transaction
version will be filled in with dummy bytes to be later filled
in at transaction commit time.userVersion
- intra-transaction portion of version (set by user code)Versionstamp
with the given user versionpublic static Versionstamp incomplete()
Versionstamp
instance with the default user
version. When converted into a byte array, the bytes for the transaction
version will be filled in with dummy bytes to be later filled in at
transaction commit time. If multiple keys are created using the returned
Versionstamp
within the same transaction, then all of those
keys will have the same version, but it will provide an ordering between
different transactions if that is all that is required.Versionstamp
with the default user versionpublic static Versionstamp complete(byte[] trVersion, int userVersion)
Versionstamp
instance with the given
transaction and user versions. The provided transaction version must have
exactly 10 bytes, and the user version must fit within an unsigned
short.trVersion
- inter-transaction portion of version (set by the database)userVersion
- intra-transaction portion of version (set by user code)Versionstamp
assembled from the given partspublic static Versionstamp complete(byte[] trVersion)
Versionstamp
instance with the given
transaction and default user versions. The provided transaction version
must have exactly 10 bytes.trVersion
- inter-transaction portion of version (set by the database)Versionstamp
assembled from the given transaction
version and the default user versionpublic boolean isComplete()
Versionstamp
's transaction version is
meaningful. The database will assign each transaction a different transaction
version. A Versionstamp
is considered to be "complete" if its
transaction version is one of those database-assigned versions rather than
just dummy bytes. If one uses this class with our
SET_VERSIONSTAMPED_KEY
mutation, then the appropriate bytes will be filled in within the database
during a successful commit.public byte[] getBytes()
Versionstamp
.
This representation can then be serialized and added to the database.
If this Versionstamp
is not complete, the first 10 bytes (representing the
transaction version) will not be meaningful and one should probably use with the
SET_VERSIONSTAMPED_KEY
mutation.
Warning: For performance reasons, this method does not create a copy of
its underlying data array. As a result, it is dangerous to modify the
return value of this function.Versionstamp
public byte[] getTransactionVersion()
Versionstamp
that is set by
the database. These 10 bytes are what provide an ordering between different commits.Versionstamp
public int getUserVersion()
Versionstamp
that is set
by the user. This integer is what provides an ordering within
a single commit.Versionstamp
public java.lang.String toString()
Versionstamp
. It contains
information as to whether this Versionstamp
is incomplete or not, what
its transaction version is (if the Versionstamp
is complete), and what its
user version is.toString
in class java.lang.Object
Versionstamp
public int compareTo(Versionstamp other)
Versionstamp
instances in a manner consistent with their
key order when serialized in the database as keys. The rules for comparison are:
Versionstamp
s sort before incomplete Versionstamp
sVersionstamp
s will sort based on unsigned lexicographic comparison
of their byte representations.
Versionstamp
s will sort based on their user versions.compareTo
in interface java.lang.Comparable<Versionstamp>
other
- Versionstamp
instance to compare againstVersionstamp
is smaller than other
, 1 if it is bigger, and
0 if it is equalpublic boolean equals(java.lang.Object o)
true
if and only
if the o
parameter is of type Versionstamp
and has
the same completion status and byte representation.equals
in class java.lang.Object
o
- object to compare for equalityVersionstamp
public int hashCode()
Versionstamp
. It is based off of the hash
code for the underlying data array.hashCode
in class java.lang.Object