Struct fdb::tuple::Versionstamp
source · pub struct Versionstamp { /* private fields */ }
Expand description
Used to represent values written by versionstamp operations with a
Tuple
.
Versionstamp
contains 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
get_versionstamp
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 type to
impose a total order of items across multiple transactions in the
database in a consistent and conflict-free way.
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:
let tr_version = fdb_database
.run(|tr| async move {
let t = {
let mut tup = Tuple::new();
tup.push_back::<String>(String::from("prefix"));
tup.push_back::<Versionstamp>(Versionstamp::incomplete(0));
tup
};
unsafe {
tr.mutate(
MutationType::SetVersionstampedKey,
t.pack_with_versionstamp(Bytes::new())?,
Bytes::new(),
);
}
Ok(unsafe { tr.get_versionstamp() })
})
.await?
.get()
.await?;
let vs = fdb_database
.run(|tr| async move {
let subspace = Subspace::new(Bytes::new()).subspace(&{
let mut tup = Tuple::new();
tup.push_back::<String>("prefix".to_string());
tup
});
let subspace_range = subspace.range(&Tuple::new());
let key = subspace_range
.into_stream(&tr, RangeOptions::default())
.take(1)
.next()
.await
.unwrap()?
.into_key();
Ok(subspace
.unpack(&key.into())?
.get::<&Versionstamp>(0)
.ok_or(FdbError::new(TUPLE_GET))?
.clone())
})
.await?;
assert_eq!(vs, Versionstamp::complete(tr_version, 0));
Here, an incomplete Versionstamp
is packed and written to the
database with SetVersionstampedKey
mutation type.
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
value
created from the first transaction’s version information.
Implementations§
source§impl Versionstamp
impl Versionstamp
sourcepub fn complete(tr_version: Bytes, user_version: u16) -> Versionstamp
pub fn complete(tr_version: Bytes, user_version: u16) -> Versionstamp
Creates a complete Versionstamp
instance with the given
transaction and user versions.
Panic
Panics if the length of the transaction version is incorrect.
sourcepub fn from_bytes(version_bytes: Bytes) -> Versionstamp
pub fn from_bytes(version_bytes: Bytes) -> Versionstamp
Creates a value of Versionstamp
type based on the given
byte array.
Panic
Panics if the length of the byte array is incorrect.
sourcepub fn incomplete(user_version: u16) -> Versionstamp
pub fn incomplete(user_version: u16) -> Versionstamp
Creates an incomplete Versionstamp
instance with the given
user version.
sourcepub fn get_bytes(&self) -> Bytes
pub fn get_bytes(&self) -> Bytes
Retrieve a byte representation of this Versionstamp
.
sourcepub fn get_transaction_version(&self) -> Bytes
pub fn get_transaction_version(&self) -> Bytes
Retrieve the portion of this Versionstamp
that is set by
the database.
sourcepub fn get_user_version(&self) -> u16
pub fn get_user_version(&self) -> u16
Retrieve the portion of this Versionstamp
that is set by
the user.
sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Whether this Versionstamp
’s transaction version is
meaningful.
sourcepub fn into_parts(self) -> (Bytes, u16, bool)
pub fn into_parts(self) -> (Bytes, u16, bool)
Extract transaction version, user version and completion flag
from Versionstamp
.
Trait Implementations§
source§impl Clone for Versionstamp
impl Clone for Versionstamp
source§fn clone(&self) -> Versionstamp
fn clone(&self) -> Versionstamp
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for Versionstamp
impl Debug for Versionstamp
source§impl Ord for Versionstamp
impl Ord for Versionstamp
source§fn cmp(&self, other: &Versionstamp) -> Ordering
fn cmp(&self, other: &Versionstamp) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl PartialEq<Versionstamp> for Versionstamp
impl PartialEq<Versionstamp> for Versionstamp
source§fn eq(&self, other: &Versionstamp) -> bool
fn eq(&self, other: &Versionstamp) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd<Versionstamp> for Versionstamp
impl PartialOrd<Versionstamp> for Versionstamp
source§fn partial_cmp(&self, other: &Versionstamp) -> Option<Ordering>
fn partial_cmp(&self, other: &Versionstamp) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more