ood_persistence/src/syn.rs

62 lines
2.1 KiB
Rust
Raw Normal View History

2021-10-17 23:12:55 +03:00
/// The pool is one of the main features to be realized in persistence.
///
/// Any implementation (database, file storage, memory or others) must be able to get a connection
/// to manipulate it afterwards.
2021-10-12 18:14:02 +03:00
pub trait PersistencePool {
/// `ConnectionClient` implementation that persistence can can return.
2021-10-12 18:14:02 +03:00
type Conn: ConnectionClient;
2021-10-17 23:12:55 +03:00
/// Returns new connection.
///
/// # Errors
///
/// Returns `PersistenceError` if pool cannot get a connection.
fn get_connection(&self) -> crate::Result<Self::Conn>;
2021-10-12 18:14:02 +03:00
}
2021-10-17 23:12:55 +03:00
/// Connection client knows about the inner connection, and also knows how to create transactions.
2021-10-12 18:14:02 +03:00
pub trait ConnectionClient {
2021-10-17 23:12:55 +03:00
/// Inner connection
2021-10-12 18:14:02 +03:00
type InnerConn;
/// `TransactionClient` implementation in which the connection can be updated.
2021-10-17 23:12:55 +03:00
///
/// **Note:** requires nightly rust channel and enabling the `nightly` feature.
2021-10-17 15:08:46 +03:00
#[cfg(feature = "nightly")]
type Trx<'t>: TransactionClient;
2021-10-17 23:12:55 +03:00
/// Returns inner connection
2021-10-12 18:14:02 +03:00
fn inner(&mut self) -> &mut Self::InnerConn;
2021-10-17 15:08:46 +03:00
2021-10-17 23:12:55 +03:00
/// Updates connection to transaction.
///
/// **Note:** requires nightly rust channel and enabling the `nightly` feature.
///
/// # Errors
///
/// Returns `PersistenceError` if connection cannot update to transaction.
2021-10-17 15:08:46 +03:00
#[cfg(feature = "nightly")]
fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>>;
2021-10-17 15:08:46 +03:00
}
2021-10-17 23:12:55 +03:00
/// Transaction client is updated connection client that can additionally commit and rollback data
/// in transactions.
///
/// **Note:** requires nightly rust channel and enabling the `nightly` feature.
2021-10-17 15:08:46 +03:00
#[cfg(feature = "nightly")]
pub trait TransactionClient: ConnectionClient {
2021-10-17 23:12:55 +03:00
/// Consumes the transaction, committing all changes made within it.
///
/// # Errors
///
/// Returns `PersistenceError` if transaction cannot commit
fn commit(self) -> crate::Result<()>;
2021-10-17 15:08:46 +03:00
2021-10-17 23:12:55 +03:00
/// Rolls the transaction back, discarding all changes made within it.
///
/// # Errors
///
/// Returns `PersistenceError` if transaction cannot rolls back.
fn rollback(self) -> crate::Result<()>;
2021-10-12 18:14:02 +03:00
}