Struct mz_stash::TableTransaction
source · pub struct TableTransaction<K, V> { /* private fields */ }
Expand description
TableTransaction emulates some features of a typical SQL transaction over
table for a StashCollection
.
It supports:
- uniqueness constraints
- transactional reads and writes (including read-your-writes before commit)
K
is the primary key type. Multiple entries with the same key are disallowed.
V
is the an arbitrary value type.
To finalize, add the results of TableTransaction::pending()
to an
AppendBatch
.
Implementations§
source§impl<K, V> TableTransaction<K, V>
impl<K, V> TableTransaction<K, V>
sourcepub fn new<KP, VP>(
initial: BTreeMap<KP, VP>,
uniqueness_violation: fn(a: &V, b: &V) -> bool
) -> Result<Self, TryFromProtoError>
pub fn new<KP, VP>( initial: BTreeMap<KP, VP>, uniqueness_violation: fn(a: &V, b: &V) -> bool ) -> Result<Self, TryFromProtoError>
Create a new TableTransaction with initial data. uniqueness_violation
is a function
whether there is a uniqueness violation among two values.
Internally the Stash
serializes data as protobuf. All fields in a proto message are
optional, which makes using them in Rust cumbersome. Generic parameters KP
and VP
are
protobuf types which deserialize to K
and V
that a TableTransaction
is generic
over.
sourcepub fn pending<KP, VP>(self) -> Vec<(KP, VP, Diff)>
pub fn pending<KP, VP>(self) -> Vec<(KP, VP, Diff)>
Consumes and returns the pending changes and their diffs. Diff
is
guaranteed to be 1 or -1.
sourcepub fn for_values<F: FnMut(&K, &V)>(&self, f: F)
pub fn for_values<F: FnMut(&K, &V)>(&self, f: F)
Iterates over the items viewable in the current transaction in arbitrary
order and applies f
on all key, value pairs.
sourcepub fn insert(&mut self, k: K, v: V) -> Result<(), StashError>
pub fn insert(&mut self, k: K, v: V) -> Result<(), StashError>
Inserts a new k,v pair.
Returns an error if the uniqueness check failed or the key already exists.
sourcepub fn update<F: Fn(&K, &V) -> Option<V>>(
&mut self,
f: F
) -> Result<Diff, StashError>
pub fn update<F: Fn(&K, &V) -> Option<V>>( &mut self, f: F ) -> Result<Diff, StashError>
Updates k, v pairs. f
is a function that can return Some(V)
if the
value should be updated, otherwise None
. Returns the number of changed
entries.
Returns an error if the uniqueness check failed.
sourcepub fn set(&mut self, k: K, v: Option<V>) -> Result<Option<V>, StashError>
pub fn set(&mut self, k: K, v: Option<V>) -> Result<Option<V>, StashError>
Set the value for a key. Returns the previous entry if the key existed, otherwise None.
Returns an error if the uniqueness check failed.
DO NOT call this function in a loop, use Self::set_many
instead.
sourcepub fn set_many(
&mut self,
kvs: BTreeMap<K, Option<V>>
) -> Result<BTreeMap<K, Option<V>>, StashError>
pub fn set_many( &mut self, kvs: BTreeMap<K, Option<V>> ) -> Result<BTreeMap<K, Option<V>>, StashError>
Set the values for many keys. Returns the previous entry for each key if the key existed, otherwise None.
Returns an error if any uniqueness check failed.
Trait Implementations§
source§impl<K: PartialEq, V: PartialEq> PartialEq for TableTransaction<K, V>
impl<K: PartialEq, V: PartialEq> PartialEq for TableTransaction<K, V>
source§fn eq(&self, other: &TableTransaction<K, V>) -> bool
fn eq(&self, other: &TableTransaction<K, V>) -> bool
self
and other
values to be equal, and is used
by ==
.impl<K: Eq, V: Eq> Eq for TableTransaction<K, V>
impl<K, V> StructuralEq for TableTransaction<K, V>
impl<K, V> StructuralPartialEq for TableTransaction<K, V>
Auto Trait Implementations§
impl<K, V> RefUnwindSafe for TableTransaction<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for TableTransaction<K, V>
impl<K, V> Sync for TableTransaction<K, V>
impl<K, V> Unpin for TableTransaction<K, V>
impl<K, V> UnwindSafe for TableTransaction<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> FutureExt for T
impl<T> FutureExt for T
source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
source§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<P, R> ProtoType<R> for Pwhere
R: RustType<P>,
impl<P, R> ProtoType<R> for Pwhere
R: RustType<P>,
source§fn into_rust(self) -> Result<R, TryFromProtoError>
fn into_rust(self) -> Result<R, TryFromProtoError>
RustType::from_proto
.source§fn from_rust(rust: &R) -> P
fn from_rust(rust: &R) -> P
RustType::into_proto
.