pub struct Db {
pub reader: Pool<Sqlite>,
pub writer: Pool<Sqlite>,
}
Expand description
SQLite database implementation for flow trading repositories.
This struct provides separate reader and writer connection pools to a SQLite database,
implementing all the repository traits defined in fts-core
. The separation of read
and write connections allows for better concurrency control and follows SQLite best
practices for Write-Ahead Logging (WAL) mode.
§Connection Management
reader
: A connection pool for read operations, allowing concurrent readswriter
: A single-connection pool for write operations, ensuring serialized writes
§Example
let config = SqliteConfig::default();
let now = DateTime::from(time::OffsetDateTime::now_utc());
let db = Db::open(&config, now).await?;
Fields§
§reader: Pool<Sqlite>
Connection pool for read operations
writer: Pool<Sqlite>
Connection pool for write operations (limited to 1 connection)
Implementations§
Source§impl Db
impl Db
Sourcepub async fn open(config: &SqliteConfig, as_of: DateTime) -> Result<Self, Error>
pub async fn open(config: &SqliteConfig, as_of: DateTime) -> Result<Self, Error>
Open a connection to the specified SQLite database.
Creates a new database if one doesn’t exist (when create_if_missing
is true),
applies all pending migrations, and ensures the batch table is initialized.
§Arguments
config
- Configuration specifying database path and creation optionsas_of
- Initial timestamp for the batch table if creating a new database
§Database Configuration
The database is configured with the following settings for optimal performance:
- WAL mode for better concurrency
- Foreign keys enabled for referential integrity
- Optimized cache and memory settings for flow trading workloads
§Errors
Returns sqlx::Error
if:
- Database connection fails
- Migrations fail to apply
- Initial batch row creation fails
Trait Implementations§
Source§impl<T> BatchRepository<T> for Dbwhere
T: Send + Solver<DemandId, PortfolioId, ProductId>,
T::Error: Send,
T::State: Send,
T::PortfolioOutcome: Unpin + Send + Serialize + DeserializeOwned,
T::ProductOutcome: Unpin + Send + Serialize + DeserializeOwned,
impl<T> BatchRepository<T> for Dbwhere
T: Send + Solver<DemandId, PortfolioId, ProductId>,
T::Error: Send,
T::State: Send,
T::PortfolioOutcome: Unpin + Send + Serialize + DeserializeOwned,
T::ProductOutcome: Unpin + Send + Serialize + DeserializeOwned,
Source§async fn get_portfolio_outcomes(
&self,
portfolio_id: Self::PortfolioId,
query: DateTimeRangeQuery<Self::DateTime>,
limit: usize,
) -> Result<DateTimeRangeResponse<OutcomeRecord<Self::DateTime, T::PortfolioOutcome>, Self::DateTime>, Self::Error>
async fn get_portfolio_outcomes( &self, portfolio_id: Self::PortfolioId, query: DateTimeRangeQuery<Self::DateTime>, limit: usize, ) -> Result<DateTimeRangeResponse<OutcomeRecord<Self::DateTime, T::PortfolioOutcome>, Self::DateTime>, Self::Error>
Get the portfolio’s outcomes
This returns a list of outcomes, each corresponding to a specific point in time.
The records are ordered by valid_from
in descending order
and are grouped by valid_from
.
Source§async fn get_product_outcomes(
&self,
product_id: Self::ProductId,
query: DateTimeRangeQuery<Self::DateTime>,
limit: usize,
) -> Result<DateTimeRangeResponse<OutcomeRecord<Self::DateTime, T::ProductOutcome>, Self::DateTime>, Self::Error>
async fn get_product_outcomes( &self, product_id: Self::ProductId, query: DateTimeRangeQuery<Self::DateTime>, limit: usize, ) -> Result<DateTimeRangeResponse<OutcomeRecord<Self::DateTime, T::ProductOutcome>, Self::DateTime>, Self::Error>
Get the product’s outcomes
This returns a list of outcomes, each corresponding to a specific point in time.
The records are ordered by valid_from
in descending order
and are grouped by valid_from
.
Source§impl<DemandData: Send + Unpin + Serialize + DeserializeOwned> DemandRepository<DemandData> for Db
impl<DemandData: Send + Unpin + Serialize + DeserializeOwned> DemandRepository<DemandData> for Db
Source§async fn get_demand_bidder_id(
&self,
demand_id: Self::DemandId,
) -> Result<Option<Self::BidderId>, Self::Error>
async fn get_demand_bidder_id( &self, demand_id: Self::DemandId, ) -> Result<Option<Self::BidderId>, Self::Error>
Source§async fn query_demand(
&self,
bidder_ids: &[Self::BidderId],
as_of: Self::DateTime,
) -> Result<Vec<Self::DemandId>, Self::Error>
async fn query_demand( &self, bidder_ids: &[Self::BidderId], as_of: Self::DateTime, ) -> Result<Vec<Self::DemandId>, Self::Error>
bidder_ids
as-of the specified time. Read moreSource§async fn create_demand(
&self,
demand_id: Self::DemandId,
bidder_id: Self::BidderId,
app_data: DemandData,
curve_data: Option<DemandCurve>,
as_of: Self::DateTime,
) -> Result<(), Self::Error>
async fn create_demand( &self, demand_id: Self::DemandId, bidder_id: Self::BidderId, app_data: DemandData, curve_data: Option<DemandCurve>, as_of: Self::DateTime, ) -> Result<(), Self::Error>
Source§async fn update_demand(
&self,
demand_id: Self::DemandId,
curve_data: Option<DemandCurve>,
as_of: Self::DateTime,
) -> Result<bool, Self::Error>
async fn update_demand( &self, demand_id: Self::DemandId, curve_data: Option<DemandCurve>, as_of: Self::DateTime, ) -> Result<bool, Self::Error>
Source§async fn get_demand(
&self,
demand_id: Self::DemandId,
as_of: Self::DateTime,
) -> Result<Option<DemandRecord<Self::DateTime, Self::BidderId, Self::DemandId, Self::PortfolioId, DemandData>>, Self::Error>
async fn get_demand( &self, demand_id: Self::DemandId, as_of: Self::DateTime, ) -> Result<Option<DemandRecord<Self::DateTime, Self::BidderId, Self::DemandId, Self::PortfolioId, DemandData>>, Self::Error>
Source§async fn get_demand_history(
&self,
demand_id: Self::DemandId,
query: DateTimeRangeQuery<Self::DateTime>,
limit: usize,
) -> Result<DateTimeRangeResponse<ValueRecord<Self::DateTime, DemandCurve>, Self::DateTime>, Self::Error>
async fn get_demand_history( &self, demand_id: Self::DemandId, query: DateTimeRangeQuery<Self::DateTime>, limit: usize, ) -> Result<DateTimeRangeResponse<ValueRecord<Self::DateTime, DemandCurve>, Self::DateTime>, Self::Error>
Source§impl<PortfolioData: Send + Unpin + Serialize + DeserializeOwned> PortfolioRepository<PortfolioData> for Db
impl<PortfolioData: Send + Unpin + Serialize + DeserializeOwned> PortfolioRepository<PortfolioData> for Db
Source§async fn get_portfolio_demand_history(
&self,
portfolio_id: Self::PortfolioId,
query: DateTimeRangeQuery<Self::DateTime>,
limit: usize,
) -> Result<DateTimeRangeResponse<ValueRecord<Self::DateTime, DemandGroup<Self::DemandId>>, Self::DateTime>, Self::Error>
async fn get_portfolio_demand_history( &self, portfolio_id: Self::PortfolioId, query: DateTimeRangeQuery<Self::DateTime>, limit: usize, ) -> Result<DateTimeRangeResponse<ValueRecord<Self::DateTime, DemandGroup<Self::DemandId>>, Self::DateTime>, Self::Error>
Get the history of this portfolio’s demands
This returns a list of records, each containing the state of the portfolio’s demand group
at a specific point in time. The records are ordered by valid_from
in descending order
and are grouped by valid_from
. This is important for a more
pointer to work correctly,
so the demand_group is actually a map of demand_id
to weight
at that point in time.
Source§async fn get_portfolio_product_history(
&self,
portfolio_id: Self::PortfolioId,
query: DateTimeRangeQuery<Self::DateTime>,
limit: usize,
) -> Result<DateTimeRangeResponse<ValueRecord<Self::DateTime, ProductGroup<Self::ProductId>>, Self::DateTime>, Self::Error>
async fn get_portfolio_product_history( &self, portfolio_id: Self::PortfolioId, query: DateTimeRangeQuery<Self::DateTime>, limit: usize, ) -> Result<DateTimeRangeResponse<ValueRecord<Self::DateTime, ProductGroup<Self::ProductId>>, Self::DateTime>, Self::Error>
Get the history of this portfolio’s products
This returns a list of records, each containing the state of the portfolio’s product group
at a specific point in time. The records are ordered by valid_from
in descending order
and are grouped by valid_from
. This is important for a more
pointer to work correctly,
so the product_group is actually a map of product_id
to weight
at that point in time.
Source§async fn get_portfolio_bidder_id(
&self,
portfolio_id: Self::PortfolioId,
) -> Result<Option<Self::BidderId>, Self::Error>
async fn get_portfolio_bidder_id( &self, portfolio_id: Self::PortfolioId, ) -> Result<Option<Self::BidderId>, Self::Error>
Source§async fn query_portfolio(
&self,
bidder_ids: &[Self::BidderId],
as_of: Self::DateTime,
) -> Result<Vec<Self::PortfolioId>, Self::Error>
async fn query_portfolio( &self, bidder_ids: &[Self::BidderId], as_of: Self::DateTime, ) -> Result<Vec<Self::PortfolioId>, Self::Error>
bidder_id
as-of the specified time. Read moreSource§async fn create_portfolio(
&self,
portfolio_id: Self::PortfolioId,
bidder_id: Self::BidderId,
app_data: PortfolioData,
demand_group: DemandGroup<Self::DemandId>,
product_group: ProductGroup<Self::ProductId>,
as_of: Self::DateTime,
) -> Result<(), Self::Error>
async fn create_portfolio( &self, portfolio_id: Self::PortfolioId, bidder_id: Self::BidderId, app_data: PortfolioData, demand_group: DemandGroup<Self::DemandId>, product_group: ProductGroup<Self::ProductId>, as_of: Self::DateTime, ) -> Result<(), Self::Error>
Source§async fn update_portfolio(
&self,
portfolio_id: Self::PortfolioId,
demand_group: Option<DemandGroup<Self::DemandId>>,
product_group: Option<ProductGroup<Self::ProductId>>,
as_of: Self::DateTime,
) -> Result<bool, Self::Error>
async fn update_portfolio( &self, portfolio_id: Self::PortfolioId, demand_group: Option<DemandGroup<Self::DemandId>>, product_group: Option<ProductGroup<Self::ProductId>>, as_of: Self::DateTime, ) -> Result<bool, Self::Error>
Source§async fn get_portfolio(
&self,
portfolio_id: Self::PortfolioId,
as_of: Self::DateTime,
) -> Result<Option<PortfolioRecord<Self::DateTime, Self::BidderId, Self::PortfolioId, Self::DemandId, Self::ProductId, PortfolioData>>, Self::Error>
async fn get_portfolio( &self, portfolio_id: Self::PortfolioId, as_of: Self::DateTime, ) -> Result<Option<PortfolioRecord<Self::DateTime, Self::BidderId, Self::PortfolioId, Self::DemandId, Self::ProductId, PortfolioData>>, Self::Error>
Source§impl<ProductData: Send + Unpin + 'static + Serialize + DeserializeOwned> ProductRepository<ProductData> for Db
impl<ProductData: Send + Unpin + 'static + Serialize + DeserializeOwned> ProductRepository<ProductData> for Db
Source§async fn create_product(
&self,
product_id: Self::ProductId,
app_data: ProductData,
as_of: Self::DateTime,
) -> Result<(), Self::Error>
async fn create_product( &self, product_id: Self::ProductId, app_data: ProductData, as_of: Self::DateTime, ) -> Result<(), Self::Error>
Source§impl Repository for Db
impl Repository for Db
Source§type PortfolioId = PortfolioId
type PortfolioId = PortfolioId
Auto Trait Implementations§
impl Freeze for Db
impl !RefUnwindSafe for Db
impl Send for Db
impl Sync for Db
impl Unpin for Db
impl !UnwindSafe for Db
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more