Struct Db

Source
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 reads
  • writer: 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

Source

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 options
  • as_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 Db

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>

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>

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§

async fn run_batch( &self, timestamp: Self::DateTime, solver: T, state: T::State, ) -> Result<Result<(), T::Error>, Self::Error>

Execute a batch auction for a specific timestamp. Read more
Source§

impl Clone for Db

Source§

fn clone(&self) -> Db

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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>

Get the bidder id associated to the demand
Source§

async fn query_demand( &self, bidder_ids: &[Self::BidderId], as_of: Self::DateTime, ) -> Result<Vec<Self::DemandId>, Self::Error>

Query all the demand curves with non-null data associated to any of bidder_ids as-of the specified time. Read more
Source§

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>

Create a new demand with an optional initial curve.
Source§

async fn update_demand( &self, demand_id: Self::DemandId, curve_data: Option<DemandCurve>, as_of: Self::DateTime, ) -> Result<bool, Self::Error>

Update the curve data for an existing demand. Read more
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>

Retrieve a demand at a specific point in time. Read more
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>

Retrieve the history of curve changes for a demand. Read more
Source§

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>

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>

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>

Get the bidder id associated to the portfolio
Source§

async fn query_portfolio( &self, bidder_ids: &[Self::BidderId], as_of: Self::DateTime, ) -> Result<Vec<Self::PortfolioId>, Self::Error>

Query all the portfolios with non-empty groups associated to bidder_id as-of the specified time. Read more
Source§

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>

Create a new portfolio with initial demand and product associations.
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>

Update a portfolio’s demand and/or product associations. Read more
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>

Retrieve a portfolio at a specific point in time. Read more
Source§

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>

Define a new root product with no parent.
Source§

async fn partition_product<T: Send + IntoIterator<Item = (Self::ProductId, ProductData, f64)>>( &self, product_id: Self::ProductId, children: T, as_of: Self::DateTime, ) -> Result<usize, Self::Error>

Partition an existing product into new weighted children. Read more
Source§

async fn get_product( &self, product_id: Self::ProductId, _as_of: Self::DateTime, ) -> Result<Option<ProductData>, Self::Error>

Get the data associated with a product at a specific time. Read more
Source§

impl Repository for Db

Source§

type Error = Error

The error type for underlying operations
Source§

type DateTime = DateTime

A type suitable for expressing a timestamp
Source§

type BidderId = BidderId

A type representing a bidder id
Source§

type ProductId = ProductId

A type representing a product id
Source§

type DemandId = DemandId

A type representing a demand id
Source§

type PortfolioId = PortfolioId

A type representing a portfolio id

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,