Class Versionstamp
- java.lang.Object
-
- com.apple.foundationdb.tuple.Versionstamp
-
- All Implemented Interfaces:
java.lang.Comparable<Versionstamp>
public class Versionstamp extends java.lang.Object implements java.lang.Comparable<Versionstamp>
Used to represent values written by versionstamp operations with aTuple. 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 theTransaction.getVersionstamp()method to retrieve this version from aTransaction.) 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 parameterlessincomplete()and one-parametercompletestatic initializers. If they do so, then versions are written with a default (constant) user version.All
Versionstamps can exist in one of two states: "incomplete" and "complete". An "incomplete"Versionstampis aVersionstampthat has not been initialized with a meaningful transaction version. For example, this might be used with aVersionstampthat 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 aVersionstampfrom 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
Versionstampis packed and written to the database with theSET_VERSIONSTAMPED_KEYMutationType. After committing, we then attempt to read back the same key that we just wrote. Then we verify the invariant that the deserializedVersionstampis the same as a completeVersionstampinstance created from the first transaction's version information.
-
-
Field Summary
Fields Modifier and Type Field Description static intLENGTHLength of a serializedVersionstampinstance when converted into a byte array.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(Versionstamp other)Compares twoVersionstampinstances in a manner consistent with their key order when serialized in the database as keys.static Versionstampcomplete(byte[] trVersion)Creates a completeVersionstampinstance with the given transaction and default user versions.static Versionstampcomplete(byte[] trVersion, int userVersion)Creates a completeVersionstampinstance with the given transaction and user versions.booleanequals(java.lang.Object o)Check another object for equality.static VersionstampfromBytes(byte[] versionBytes)Creates aVersionstampinstance based on the given byte array representation.byte[]getBytes()Retrieve a byte-array representation of thisVersionstamp.byte[]getTransactionVersion()Retrieve the portion of thisVersionstampthat is set by the database.intgetUserVersion()Retrieve the portion of thisVersionstampthat is set by the user.inthashCode()Hash code for thisVersionstamp.static Versionstampincomplete()Creates an incompleteVersionstampinstance with the default user version.static Versionstampincomplete(int userVersion)Creates an incompleteVersionstampinstance with the given user version.booleanisComplete()Whether thisVersionstamp's transaction version is meaningful.java.lang.StringtoString()Generate a human-readable representation of thisVersionstamp.static intunpackUserVersion(byte[] bytes, int pos)From a byte array, unpack the user version starting at the given position.
-
-
-
Field Detail
-
LENGTH
public static final int LENGTH
Length of a serializedVersionstampinstance when converted into a byte array.- See Also:
- Constant Field Values
-
-
Method Detail
-
unpackUserVersion
public static int unpackUserVersion(byte[] bytes, int pos)From a byte array, unpack the user version starting at the given position. This assumes that the bytes are stored in big-endian order as an unsigned short, which is the way the user version is serialized in packedVersionstamps.- Parameters:
bytes- byte array including user versionpos- starting position of user version- Returns:
- the unpacked user version included in the array
-
fromBytes
public static Versionstamp fromBytes(byte[] versionBytes)
Creates aVersionstampinstance based on the given byte array representation. This follows the same format as that used by the main constructor, but the completeness of theVersionstampis instead automatically determined by comparing its transaction version with the value used to indicate an unset transaction version.- Parameters:
versionBytes- byte array representation ofVersionstamp- Returns:
- equivalent instantiated
Versionstampobject
-
incomplete
public static Versionstamp incomplete(int userVersion)
Creates an incompleteVersionstampinstance 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.- Parameters:
userVersion- intra-transaction portion of version (set by user code)- Returns:
- an incomplete
Versionstampwith the given user version
-
incomplete
public static Versionstamp incomplete()
Creates an incompleteVersionstampinstance 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 returnedVersionstampwithin 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.- Returns:
- an incomplete
Versionstampwith the default user version
-
complete
public static Versionstamp complete(byte[] trVersion, int userVersion)
Creates a completeVersionstampinstance 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.- Parameters:
trVersion- inter-transaction portion of version (set by the database)userVersion- intra-transaction portion of version (set by user code)- Returns:
- a complete
Versionstampassembled from the given parts
-
complete
public static Versionstamp complete(byte[] trVersion)
Creates a completeVersionstampinstance with the given transaction and default user versions. The provided transaction version must have exactly 10 bytes.- Parameters:
trVersion- inter-transaction portion of version (set by the database)- Returns:
- a complete
Versionstampassembled from the given transaction version and the default user version
-
isComplete
public boolean isComplete()
Whether thisVersionstamp's transaction version is meaningful. The database will assign each transaction a different transaction version. AVersionstampis 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 ourSET_VERSIONSTAMPED_KEYmutation, then the appropriate bytes will be filled in within the database during a successful commit.- Returns:
- whether the transaction version has been set
-
getBytes
public byte[] getBytes()
Retrieve a byte-array representation of thisVersionstamp. This representation can then be serialized and added to the database. If thisVersionstampis not complete, the first 10 bytes (representing the transaction version) will not be meaningful and one should probably use with theSET_VERSIONSTAMPED_KEYmutation. 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.- Returns:
- byte representation of this
Versionstamp
-
getTransactionVersion
public byte[] getTransactionVersion()
Retrieve the portion of thisVersionstampthat is set by the database. These 10 bytes are what provide an ordering between different commits.- Returns:
- transaction version of this
Versionstamp
-
getUserVersion
public int getUserVersion()
Retrieve the portion of thisVersionstampthat is set by the user. This integer is what provides an ordering within a single commit.- Returns:
- user version of this
Versionstamp
-
toString
public java.lang.String toString()
Generate a human-readable representation of thisVersionstamp. It contains information as to whether thisVersionstampis incomplete or not, what its transaction version is (if theVersionstampis complete), and what its user version is.- Overrides:
toStringin classjava.lang.Object- Returns:
- a human-readable representation of this
Versionstamp
-
compareTo
public int compareTo(Versionstamp other)
Compares twoVersionstampinstances in a manner consistent with their key order when serialized in the database as keys. The rules for comparison are:- All complete
Versionstamps sort before incompleteVersionstamps -
Two complete
Versionstamps will sort based on unsigned lexicographic comparison of their byte representations. - Two incomplete
Versionstamps will sort based on their user versions.
- Specified by:
compareToin interfacejava.lang.Comparable<Versionstamp>- Parameters:
other-Versionstampinstance to compare against- Returns:
- -1 if this
Versionstampis smaller thanother, 1 if it is bigger, and 0 if it is equal
- All complete
-
equals
public boolean equals(java.lang.Object o)
Check another object for equality. This will returntrueif and only if theoparameter is of typeVersionstampand has the same completion status and byte representation.- Overrides:
equalsin classjava.lang.Object- Parameters:
o- object to compare for equality- Returns:
- whether the passed object is an equivalent
Versionstamp
-
hashCode
public int hashCode()
Hash code for thisVersionstamp. It is based off of the hash code for the underlying data array.- Overrides:
hashCodein classjava.lang.Object- Returns:
- hash-table compliant hash code for this instance
-
-