Skip to content

Commit

Permalink
Add Cache methods in Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Apr 15, 2024
1 parent 39fdf2d commit 98021a3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 64 deletions.
39 changes: 18 additions & 21 deletions nautilus_core/common/src/cache/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ use nautilus_model::{
component_id::ComponentId, instrument_id::InstrumentId, position_id::PositionId,
strategy_id::StrategyId, trader_id::TraderId, venue_order_id::VenueOrderId,
},
instruments::{synthetic::SyntheticInstrument, Instrument},
instruments::{synthetic::SyntheticInstrument, InstrumentAny},
orders::base::{Order, OrderAny},
position::Position,
types::currency::Currency,
};
use ustr::Ustr;

use crate::enums::SerializationEncoding;
use crate::{enums::SerializationEncoding, interface::account::Account};

/// A type of database operation.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -128,17 +128,17 @@ impl CacheDatabaseAdapter {
Ok(HashMap::new()) // TODO
}

pub fn load_instruments(&self) -> anyhow::Result<HashMap<InstrumentId, Box<dyn Instrument>>> {
pub fn load_instruments(&self) -> anyhow::Result<HashMap<InstrumentId, InstrumentAny>> {
Ok(HashMap::new()) // TODO
}

pub fn load_synthetics(&self) -> anyhow::Result<HashMap<InstrumentId, SyntheticInstrument>> {
Ok(HashMap::new()) // TODO
}

// pub fn load_accounts() -> anyhow::Result<HashMap<AccountId, Box<dyn Account>>> {
// Ok(HashMap::new()) // TODO
// }
pub fn load_accounts(&self) -> anyhow::Result<HashMap<AccountId, Box<dyn Account>>> {
Ok(HashMap::new()) // TODO
}

pub fn load_orders(&self) -> anyhow::Result<HashMap<ClientOrderId, OrderAny>> {
Ok(HashMap::new()) // TODO
Expand All @@ -160,10 +160,7 @@ impl CacheDatabaseAdapter {
todo!() // TODO
}

pub fn load_instrument(
&self,
instrument_id: &InstrumentId,
) -> anyhow::Result<Box<dyn Instrument>> {
pub fn load_instrument(&self, instrument_id: &InstrumentId) -> anyhow::Result<InstrumentAny> {
todo!() // TODO
}

Expand Down Expand Up @@ -212,27 +209,27 @@ impl CacheDatabaseAdapter {
todo!() // TODO
}

pub fn add_currency(&self, currency: Currency) -> anyhow::Result<()> {
pub fn add_currency(&self, currency: &Currency) -> anyhow::Result<()> {
todo!() // TODO
}

pub fn add_instrument(&self, instrument: Box<dyn Instrument>) -> anyhow::Result<()> {
pub fn add_instrument(&self, instrument: &InstrumentAny) -> anyhow::Result<()> {
todo!() // TODO
}

pub fn add_synthetic(&self, synthetic: SyntheticInstrument) -> anyhow::Result<()> {
pub fn add_synthetic(&self, synthetic: &SyntheticInstrument) -> anyhow::Result<()> {
todo!() // TODO
}

// pub fn add_account(&self) -> anyhow::Result<Box<dyn Account>> {
// todo!() // TODO
// }
pub fn add_account(&self, account: &dyn Account) -> anyhow::Result<Box<dyn Account>> {
todo!() // TODO
}

pub fn add_order(&self, order: &OrderAny) -> anyhow::Result<()> {
todo!() // TODO
}

pub fn add_position(&self, position: Position) -> anyhow::Result<()> {
pub fn add_position(&self, position: &Position) -> anyhow::Result<()> {
todo!() // TODO
}

Expand Down Expand Up @@ -264,19 +261,19 @@ impl CacheDatabaseAdapter {
todo!() // TODO
}

pub fn update_order(&self, order: Box<dyn Order>) -> anyhow::Result<()> {
pub fn update_order(&self, order: &OrderAny) -> anyhow::Result<()> {
todo!() // TODO
}

pub fn update_position(&self, position: Position) -> anyhow::Result<()> {
pub fn update_position(&self, position: &Position) -> anyhow::Result<()> {
todo!() // TODO
}

pub fn snapshot_order_state(&self, order: OrderAny) -> anyhow::Result<()> {
pub fn snapshot_order_state(&self, order: &OrderAny) -> anyhow::Result<()> {
todo!() // TODO
}

pub fn snapshot_position_state(&self, position: Position) -> anyhow::Result<()> {
pub fn snapshot_position_state(&self, position: &Position) -> anyhow::Result<()> {
todo!() // TODO
}

Expand Down
82 changes: 39 additions & 43 deletions nautilus_core/common/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use nautilus_model::{
position_id::PositionId, strategy_id::StrategyId, venue::Venue,
venue_order_id::VenueOrderId,
},
instruments::{synthetic::SyntheticInstrument, Instrument},
instruments::{synthetic::SyntheticInstrument, InstrumentAny},
orderbook::book::OrderBook,
orders::base::OrderAny,
polymorphism::{
Expand All @@ -49,7 +49,7 @@ use nautilus_model::{
use ustr::Ustr;

use self::database::CacheDatabaseAdapter;
use crate::enums::SerializationEncoding;
use crate::{enums::SerializationEncoding, interface::account::Account};

pub struct CacheConfig {
pub encoding: SerializationEncoding,
Expand Down Expand Up @@ -175,10 +175,10 @@ pub struct Cache {
books: HashMap<InstrumentId, OrderBook>,
bars: HashMap<BarType, VecDeque<Bar>>,
currencies: HashMap<Ustr, Currency>,
instruments: HashMap<InstrumentId, Box<dyn Instrument>>,
instruments: HashMap<InstrumentId, InstrumentAny>,
synthetics: HashMap<InstrumentId, SyntheticInstrument>,
// accounts: HashMap<AccountId, Box<dyn Account>>, // TODO: Account not object safe
orders: HashMap<ClientOrderId, OrderAny>, // TODO: Efficency (use enum)
accounts: HashMap<AccountId, Box<dyn Account>>,
orders: HashMap<ClientOrderId, OrderAny>,
// order_lists: HashMap<OrderListId, VecDeque<OrderList>>, TODO: Need `OrderList`
positions: HashMap<PositionId, Position>,
position_snapshots: HashMap<PositionId, Vec<u8>>,
Expand Down Expand Up @@ -234,8 +234,8 @@ impl Cache {
currencies: HashMap::new(),
instruments: HashMap::new(),
synthetics: HashMap::new(),
// accounts: HashMap<AccountId, Box<dyn Account>>, TODO: Decide where trait should go
orders: HashMap::new(), // TODO: Efficency (use enum)
accounts: HashMap::new(),
orders: HashMap::new(),
// order_lists: HashMap<OrderListId, VecDeque<OrderList>>, TODO: Need `OrderList`
positions: HashMap::new(),
position_snapshots: HashMap::new(),
Expand Down Expand Up @@ -290,18 +290,18 @@ impl Cache {
Ok(())
}

// pub fn cache_accounts(&mut self) -> anyhow::Result<()> {
// self.accounts = match &self.database {
// Some(db) => db.load_accounts()?,
// None => HashMap::new(),
// };
//
// info!(
// "Cached {} synthetic instruments from database",
// self.general.len()
// );
// Ok(())
// }
pub fn cache_accounts(&mut self) -> anyhow::Result<()> {
self.accounts = match &self.database {
Some(db) => db.load_accounts()?,
None => HashMap::new(),
};

info!(
"Cached {} synthetic instruments from database",
self.general.len()
);
Ok(())
}

pub fn cache_orders(&mut self) -> anyhow::Result<()> {
self.orders = match &self.database {
Expand Down Expand Up @@ -486,51 +486,47 @@ impl Cache {

pub fn add_currency(&mut self, currency: Currency) -> anyhow::Result<()> {
debug!("Add `Currency` {}", currency.code);
self.currencies.insert(currency.code, currency);

if let Some(database) = &self.database {
database.add_currency(currency)?;
database.add_currency(&currency)?;
}

self.currencies.insert(currency.code, currency);
Ok(())
}

pub fn add_instrument<T>(&mut self, instrument: T) -> anyhow::Result<()>
where
T: Instrument + Clone,
{
pub fn add_instrument(&mut self, instrument: InstrumentAny) -> anyhow::Result<()> {
debug!("Add `Instrument` {}", instrument.id());
self.instruments
.insert(instrument.id(), Box::new(instrument.clone()));

// TODO: Revisit boxing
if let Some(database) = &self.database {
database.add_instrument(Box::new(instrument))?;
database.add_instrument(&instrument)?;
}

self.instruments.insert(instrument.id(), instrument);
Ok(())
}

pub fn add_synthetic(&mut self, synthetic: SyntheticInstrument) -> anyhow::Result<()> {
debug!("Add `SyntheticInstrument` {}", synthetic.id);
self.synthetics.insert(synthetic.id, synthetic.clone());

if let Some(database) = &self.database {
database.add_synthetic(synthetic)?;
database.add_synthetic(&synthetic)?;
}

self.synthetics.insert(synthetic.id, synthetic.clone());
Ok(())
}

// pub fn add_account<T>(&mut self, account: T) -> anyhow::Result<()>
// where
// T: Account,
// {
// debug!("Add `Account` {}", account.id());
// self.accounts.insert(account.id(), account);
//
// if let Some(database) = &self.database {
// database.add_synthetic(synthetic)?;
// }
// Ok(())
// }
pub fn add_account(&mut self, account: Box<dyn Account>) -> anyhow::Result<()> {
debug!("Add `Account` {}", account.id());

if let Some(database) = &self.database {
database.add_account(account.as_ref())?;
}

self.accounts.insert(account.id(), account);
Ok(())
}

/// Add the order to the cache indexed with any given identifiers.
///
Expand Down

0 comments on commit 98021a3

Please sign in to comment.