Skip to main content

DataHub

Struct DataHub 

Source
pub struct DataHub { /* private fields */ }
Available on crate feature tokio only.
Expand description

The struct that acts as a central hub for data input/output operations, integrating multiple Data traits (which are passed to business logic functions as their arguments) with DataAcc traits (which implement default data I/O methods for external services).

It facilitates data access by providing DataConn objects, created from both global data sources (registered via the global uses! macro) and session-local data sources (registered via DataHub::uses method).

The DataHub is capable of performing aggregated transactional operations on all DataConn objects created from its registered DataSrc instances.

Implementations§

Source§

impl DataHub

Source

pub fn new() -> Self

Creates a new DataHub instance.

This initializes the DataHub with no local data sources and an empty data connection manager. Global data sources, if any, are copied into the data_src_map.

Source

pub fn with_commit_order(names: &[&str]) -> Self

Creates a new DataHub instance with a specified commit order for data connections.

This allows defining the order in which data connections will be committed. Connections not specified in names will be committed after the specified ones, in an undefined order. Global data sources are copied into the data_src_map.

§Parameters
  • names - An array of string slices specifying the desired commit order by data connection name.
Source

pub fn uses<S, C>(&mut self, name: impl Into<Arc<str>>, ds: S)
where S: DataSrc<C> + 'static, C: DataConn + 'static,

Registers a local data source with the DataHub.

This method allows adding a custom data source which can provide data connections. Data sources can only be added before run_async or txn_async are called.

§Parameters
  • name - The name to associate with this data source.
  • ds - The data source instance, which must implement DataSrc and have a 'static lifetime. If this DataHub is moved between threads, ds must also implement Send.
§Type Parameters
  • S - The type of the data source.
  • C - The type of the data connection provided by the data source.
Source

pub fn disuses(&mut self, name: impl AsRef<str>)

Deregisters a local data source from the DataHub.

This removes a data source previously added with uses. Data sources can only be removed before run_async or txn_async are called.

§Parameters
  • name - The name of the data source to remove.
Source

pub async fn run_async<F>(&mut self, logic_fn: F) -> Result<()>
where for<'a> F: FnMut(&'a mut DataHub) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>,

Executes an asynchronous logic function with the DataHub and handles setup and cleanup.

This method sets up local data sources, runs the provided logic_fn, and then cleans up all data connections and sources. It does not automatically commit or rollback any transactions.

§Parameters
  • logic_fn - An asynchronous function that takes a mutable reference to DataHub and returns a Result. This function contains the application’s logic. The returned Future must implement Send.
§Type Parameters
  • F - The type of the asynchronous logic function.
§Returns

A Result indicating the success or failure of the logic_fn execution or the setup of data sources.

Source

pub async fn txn_async<F>(&mut self, logic_fn: F) -> Result<()>
where for<'a> F: FnMut(&'a mut DataHub) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>,

Executes a given asynchronous logic function within a managed transaction.

This method starts by asynchronously setting up local data sources, runs the provided closure, and then attempts to asynchronously commit all open data connections in the session.

If any error occurs during the execution of the closure or during the commit phase, it initiates an asynchronous rollback on all data connections and reports the transaction failure details. Finally, it cleans up session resources.

§Parameters
  • logic_fn: An asynchronous closure that encapsulates the business logic to be executed. It takes a mutable reference to DataHub as an argument and returns a pinned, boxed future.
§Type Parameters
  • F - The type of the asynchronous transactional logic function.
§Returns
  • errs::Result<()>: Ok(()) if the closure and the commit phase succeed, or an errs::Err if any phase fails.
Source

pub async fn get_data_conn_async<C>(&mut self, name: &str) -> Result<&mut C>
where C: DataConn + 'static,

Retrieves an existing data connection or creates a new one if it doesn’t exist.

This asynchronous method first checks if a data connection with the given name and type C already exists. If not, it attempts to find a suitable data source (local or global) to create a new data connection.

§Parameters
  • name - The name of the data connection to retrieve or create.
§Type Parameters
  • C - The expected type of the data connection, which must implement DataConn and have a 'static lifetime.
§Returns

A Result which is Ok containing a mutable reference to the data connection if found or successfully created, or an Err if no suitable data source is found or connection creation fails.

Trait Implementations§

Source§

impl DataAcc for DataHub

Source§

async fn get_data_conn_async<C>(&mut self, name: &str) -> Result<&mut C>
where C: DataConn + 'static,

Retrieves a data connection of a specific type from the DataHub.

This asynchronous method attempts to get a data connection identified by name. The connection type C must implement the DataConn trait and have a 'static lifetime.

§Parameters
  • name - An identifier for the data connection to retrieve.
§Type Parameters
  • C - The expected type of the data connection, which must implement DataConn.
§Returns

A Result which is Ok containing a mutable reference to the data connection if found and castable to type C, or an Err if the connection is not found or cannot be cast.

Auto Trait Implementations§

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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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>,

Source§

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.