Struct ultra_batch::BatchExecutor

source ·
pub struct BatchExecutor<E>
where E: Executor,
{ /* private fields */ }
Expand description

Batches calls to an Executor, such as for bulk inserting, updating, or deleting records in a datastore. BatchExecutors are asynchronous and designed to be passed and shared between threads or tasks. Cloning a BatchExecutor is shallow and will use the same underlying Executor.

BatchExecutor is designed primarily for bulk database operations– for example, inserting lots of records, where a single query to insert 50 new records is much faster than 50 separate queries. However, it can be used for fetching data or any other bulk operation as well.

Unlike BatchFetcher, BatchExecutor has no concepts of keys, values, deduplication, or caching; each executed value is passed directly to the underlying Executor. As such, it could also be suitable for writing a custom caching layer in situations where BatchFetcher is not suitable.

BatchExecutors introduce a small amount of latency for executions. Each time a new value or set of values is sent for execution, it will first wait for more values to buid a batch. The execution will only trigger after a timeout is reached or once enough values have been queued in the batch. See BatchExecutorBuilder for options to tweak latency and batch sizes.

§Execution semantics

If the underlying Executor returns an error during the batch execution, then all pending execute and execute_many requests will fail. The same values can be resubmitted to retry.

If the underlying Executor succeeds but does not return a Vec that contains results for all values, then calls to execute may return None. Calls to execute_many may return a Vec containing less output values than input values.

Implementations§

source§

impl<E> BatchExecutor<E>
where E: Executor + Send + Sync + 'static,

source

pub fn build(executor: E) -> BatchExecutorBuilder<E>

Create a new BatchExecutor athat uses the given Executor to execute values. Returns a BatchExecutorBuilder, which can be used to customize the BatchExecutor. Call .finish() to create the BatchExecutor.

§Examples

Creating a BatchExecutor with default options:

let user_inserter = UserInserter::new(db_conn);
let batch_inserter = BatchExecutor::build(user_inserter).finish();

Creating a BatchExecutor with custom options:

let user_inserter = UserInserter::new(db_conn);
let batch_inserter = BatchExecutor::build(user_inserter)
    .eager_batch_size(Some(50))
    .delay_duration(tokio::time::Duration::from_millis(5))
    .finish();
source

pub async fn execute( &self, key: E::Value ) -> Result<Option<E::Result>, ExecuteError>

Submit a value to be executed by the Executor. Returns the result value returned by the Executor for this given item. See the type-level docs for BatchExecutor for detailed execution semantics.

source

pub async fn execute_many( &self, values: Vec<E::Value> ) -> Result<Vec<E::Result>, ExecuteError>

Submit multiple values to be executed by the Executor. Returns a Vec containg values for each result returned by the Executor for each given input value (but note that the returned Vec may not have values for all inputs if the Executor did not return enough results). See the type-level docs for BatchExecutor for detailed execution semantics.

Trait Implementations§

source§

impl<E> Clone for BatchExecutor<E>
where E: Executor,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<E> Freeze for BatchExecutor<E>

§

impl<E> !RefUnwindSafe for BatchExecutor<E>

§

impl<E> Send for BatchExecutor<E>

§

impl<E> Sync for BatchExecutor<E>

§

impl<E> Unpin for BatchExecutor<E>

§

impl<E> !UnwindSafe for BatchExecutor<E>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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 T
where U: From<T>,

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
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
source§

impl<T> Erased for T