pub struct DataHub { /* private fields */ }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.
Upon creation, it collects references to globally set-up data sources into its internal map for quick access.
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 constructor allows defining a specific order for pre-commit, commit, and post-commit
operations for named data connections. Data connections not specified in names will
be processed after the named ones, in their order of acquisition.
Upon creation, it collects references to globally set-up data sources into its internal map for quick access.
§Parameters
names: A slice of&strrepresenting the names of data connections to commit in a specific order.
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 session-local data source with this DataHub instance.
This method is similar to the global [uses!] macro but registers a data source
that is local to this specific DataHub session. Once the DataHub’s state is
“fixed” (while DataHub::run or DataHub::txn method is executing),
further calls to uses are ignored. However, after the method completes,
the DataHub’s “fixed” state is reset, allowing for new data sources to be
registered or removed via DataHub::disuses method in subsequent operations.
§Parameters
name: The unique name for the local data source.ds: TheDataSrcinstance to register.
Sourcepub fn disuses(&mut self, name: impl AsRef<str>)
pub fn disuses(&mut self, name: impl AsRef<str>)
Unregisters and drops a session-local data source by its name.
This method removes a data source that was previously registered via DataHub::uses.
This operation is ignored if the DataHub’s state is already “fixed”.
§Parameters
name: The name of the local data source to unregister.
Sourcepub fn run<F>(&mut self, logic_fn: F) -> Result<()>
pub fn run<F>(&mut self, logic_fn: F) -> Result<()>
Executes a given logic function without transaction control.
This method sets up local data sources, runs the provided closure,
and then cleans up the DataHub’s session resources. It does not
perform commit or rollback operations.
§Parameters
logic_fn: A closure that encapsulates the business logic to be executed. It takes a mutable reference toDataHubas an argument.
§Returns
errs::Result<()>: The result of the logic function’s execution, or an error if executinglogic_fnfails.
Sourcepub fn txn<F>(&mut self, logic_fn: F) -> Result<()>
pub fn txn<F>(&mut self, logic_fn: F) -> Result<()>
Executes a given logic function within a transaction.
This method first sets up local data sources, then runs the provided closure.
If the closure returns Ok, it attempts to commit all changes. If the commit fails,
or if the logic function itself returns an errs::Err, a rollback operation
is performed. After succeeding pre_commit and commit methods of all DataConns,
post_commit methods of all DataConns are executed.
Finally, it cleans up the DataHub’s session resources.
§Parameters
logic_fn: A closure that encapsulates the business logic to be executed. It takes a mutable reference toDataHubas an argument.
§Returns
errs::Result<()>: The final result of the transaction (success or failure of logic/commit), or an error if executinglogic_fnfails.
Sourcepub fn get_data_conn<C>(&mut self, name: impl AsRef<str>) -> Result<&mut C>where
C: DataConn + 'static,
pub fn get_data_conn<C>(&mut self, name: impl AsRef<str>) -> Result<&mut C>where
C: DataConn + 'static,
Retrieves a mutable reference to a DataConn object by name, creating it if necessary.
This is the core method used by DataAcc implementations to obtain connections
to external data services. It first checks if a DataConn with the given name
already exists in the DataHub’s session. If not, it attempts to find a
corresponding DataSrc and create a new DataConn from it.
§Type Parameters
C: The concrete type ofDataConnexpected.
§Parameters
name: The name of the data source/connection to retrieve.