pub struct DataHub { /* private fields */ }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
impl DataHub
Sourcepub fn new() -> Self
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.
Sourcepub fn with_commit_order(names: &[&str]) -> Self
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.
Sourcepub fn uses<S, C>(&mut self, name: impl Into<Arc<str>>, ds: S)
pub fn uses<S, C>(&mut self, name: impl Into<Arc<str>>, ds: S)
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 implementDataSrcand have a'staticlifetime. If thisDataHubis moved between threads,dsmust also implementSend.
§Type Parameters
S- The type of the data source.C- The type of the data connection provided by the data source.
Sourcepub fn disuses(&mut self, name: impl AsRef<str>)
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.
Sourcepub async fn run_async<F>(&mut self, logic_fn: F) -> Result<()>
pub async fn run_async<F>(&mut self, logic_fn: F) -> Result<()>
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 toDataHuband returns aResult. This function contains the application’s logic. The returnedFuturemust implementSend.
§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.
Sourcepub async fn txn_async<F>(&mut self, logic_fn: F) -> Result<()>
pub async fn txn_async<F>(&mut self, logic_fn: F) -> Result<()>
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 toDataHubas 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 anerrs::Errif any phase fails.
Sourcepub async fn get_data_conn_async<C>(&mut self, name: &str) -> Result<&mut C>where
C: DataConn + 'static,
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 implementDataConnand have a'staticlifetime.
§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
impl DataAcc for DataHub
Source§async fn get_data_conn_async<C>(&mut self, name: &str) -> Result<&mut C>where
C: DataConn + 'static,
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 implementDataConn.
§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.