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,

source

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.

source

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.

source

pub(crate) fn verify(&self) -> Result<(), StashError>

source

pub fn for_values<F: FnMut(&K, &V)>(&self, f: F)

Iterates over the items viewable in the current transaction in arbitrary order.

source

pub fn get(&self, k: &K) -> Option<&V>

Returns the current value of k.

source

pub fn items(&self) -> BTreeMap<K, V>

Returns the items viewable in the current transaction.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn delete<F: Fn(&K, &V) -> bool>(&mut self, f: F) -> Vec<(K, V)>

Deletes items for which f returns true. Returns the keys and values of the deleted entries.

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FutureExt for T

source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoRequest<T> for T

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
source§

impl<P, R> ProtoType<R> for Pwhere R: RustType<P>,

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more