Struct mz_stash::TableTransaction
source · pub struct TableTransaction<K, V> {
pub(crate) initial: BTreeMap<K, V>,
pub(crate) pending: BTreeMap<K, Option<V>>,
pub(crate) uniqueness_violation: fn(a: &V, b: &V) -> bool,
}
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
.
Fields§
§initial: BTreeMap<K, V>
§pending: BTreeMap<K, Option<V>>
§uniqueness_violation: fn(a: &V, b: &V) -> bool
Implementations§
source§impl<K, V> TableTransaction<K, V>where
K: Ord + Eq + Clone,
V: Ord + Clone,
impl<K, V> TableTransaction<K, V>where K: Ord + Eq + Clone, V: Ord + Clone,
sourcepub fn new<KP, VP>(
initial: BTreeMap<KP, VP>,
uniqueness_violation: fn(a: &V, b: &V) -> bool
) -> Result<Self, TryFromProtoError>where
K: RustType<KP>,
V: RustType<VP>,
pub fn new<KP, VP>( initial: BTreeMap<KP, VP>, uniqueness_violation: fn(a: &V, b: &V) -> bool ) -> Result<Self, TryFromProtoError>where K: RustType<KP>, V: RustType<VP>,
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)>where
K: RustType<KP>,
V: RustType<VP>,
pub fn pending<KP, VP>(self) -> Vec<(KP, VP, Diff)>where K: RustType<KP>, V: RustType<VP>,
Consumes and returns the pending changes and their diffs. Diff
is
guaranteed to be 1 or -1.
pub(crate) fn verify(&self) -> Result<(), StashError>
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.
sourcepub(crate) fn for_values_mut<F: FnMut(&mut BTreeMap<K, Option<V>>, &K, &V)>(
&mut self,
f: F
)
pub(crate) fn for_values_mut<F: FnMut(&mut BTreeMap<K, Option<V>>, &K, &V)>( &mut self, f: F )
Iterates over the items viewable in the current transaction, and provides a Vec where additional pending items can be inserted, which will be appended to current pending items. Does not verify unqiueness.
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 migrate<F: Fn(&K, &V) -> Option<(K, V)>>(
&mut self,
f: F
) -> Result<Diff, StashError>
pub fn migrate<F: Fn(&K, &V) -> Option<(K, V)>>( &mut self, f: F ) -> Result<Diff, StashError>
Migrates k, v pairs. f
is a function that can return Some((K, V))
if the key and value should
be updated, otherwise None
. Returns the number of changed entries.
Returns an error if the uniqueness check failed.
Auto Trait Implementations§
impl<K, V> RefUnwindSafe for TableTransaction<K, V>where K: RefUnwindSafe, V: RefUnwindSafe,
impl<K, V> Send for TableTransaction<K, V>where K: Send, V: Send,
impl<K, V> Sync for TableTransaction<K, V>where K: Sync, V: Sync,
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> 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<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
.