Skip to main content

DataHub

Struct DataHub 

Source
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

Source

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.

Source

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 &str representing the names of data connections to commit in a specific order.
Source

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

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: The DataSrc instance to register.
Source

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.
Source

pub fn run<F>(&mut self, logic_fn: F) -> Result<()>
where F: FnMut(&mut DataHub) -> 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 to DataHub as an argument.
§Returns
  • errs::Result<()>: The result of the logic function’s execution, or an error if executing logic_fn fails.
Source

pub fn txn<F>(&mut self, logic_fn: F) -> Result<()>
where F: FnMut(&mut DataHub) -> 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 to DataHub as an argument.
§Returns
  • errs::Result<()>: The final result of the transaction (success or failure of logic/commit), or an error if executing logic_fn fails.
Source

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 of DataConn expected.
§Parameters
  • name: The name of the data source/connection to retrieve.
§Returns
  • errs::Result<&mut C>: A mutable reference to the DataConn instance if successful, or an errs::Err if the data source is not found, or if the retrieved/created DataConn cannot be cast to the specified type C.

Trait Implementations§

Source§

impl DataAcc for DataHub

Source§

fn get_data_conn<C: DataConn + 'static>( &mut self, name: impl AsRef<str>, ) -> Result<&mut C>

Retrieves a mutable reference to a DataConn object by name, creating it if necessary. Read more

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

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.