Skip to content

Commit 08bdb09

Browse files
committed
Continue RedisCacheDatabaseAdapter
1 parent ac94818 commit 08bdb09

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

nautilus_core/common/src/cache/database.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub trait CacheDatabaseAdapter {
6868
instrument_id: &InstrumentId,
6969
) -> anyhow::Result<SyntheticInstrument>;
7070

71-
fn load_account(&mut self, account_id: &AccountId) -> anyhow::Result<()>;
71+
fn load_account(&mut self, account_id: &AccountId) -> anyhow::Result<Box<dyn Account>>;
7272

7373
fn load_order(&mut self, client_order_id: &ClientOrderId) -> anyhow::Result<OrderAny>;
7474

nautilus_core/infrastructure/src/redis/cache.rs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use std::{
1717
collections::{HashMap, VecDeque},
18+
str::FromStr,
1819
sync::mpsc::{channel, Receiver, Sender, TryRecvError},
1920
thread::{self, JoinHandle},
2021
time::{Duration, Instant},
@@ -710,23 +711,68 @@ impl CacheDatabaseAdapter for RedisCacheDatabaseAdapter {
710711
}
711712

712713
fn load_instruments(&mut self) -> anyhow::Result<HashMap<InstrumentId, InstrumentAny>> {
713-
todo!()
714+
let mut instruments = HashMap::new();
715+
716+
for key in self.database.keys(&format!("{INSTRUMENTS}*"))? {
717+
let parts: Vec<&str> = key.as_str().rsplitn(2, ':').collect();
718+
let instrument_id = InstrumentId::from_str(parts.first().unwrap())?;
719+
let instrument = self.load_instrument(&instrument_id)?;
720+
instruments.insert(instrument_id, instrument);
721+
}
722+
723+
Ok(instruments)
714724
}
715725

716726
fn load_synthetics(&mut self) -> anyhow::Result<HashMap<InstrumentId, SyntheticInstrument>> {
717-
todo!()
727+
let mut synthetics = HashMap::new();
728+
729+
for key in self.database.keys(&format!("{SYNTHETICS}*"))? {
730+
let parts: Vec<&str> = key.as_str().rsplitn(2, ':').collect();
731+
let instrument_id = InstrumentId::from_str(parts.first().unwrap())?;
732+
let synthetic = self.load_synthetic(&instrument_id)?;
733+
synthetics.insert(instrument_id, synthetic);
734+
}
735+
736+
Ok(synthetics)
718737
}
719738

720739
fn load_accounts(&mut self) -> anyhow::Result<HashMap<AccountId, Box<dyn Account>>> {
721-
todo!()
740+
let mut accounts = HashMap::new();
741+
742+
for key in self.database.keys(&format!("{ACCOUNTS}*"))? {
743+
let parts: Vec<&str> = key.as_str().rsplitn(2, ':').collect();
744+
let account_id = AccountId::from(*parts.first().unwrap());
745+
let account = self.load_account(&account_id)?;
746+
accounts.insert(account_id, account);
747+
}
748+
749+
Ok(accounts)
722750
}
723751

724752
fn load_orders(&mut self) -> anyhow::Result<HashMap<ClientOrderId, OrderAny>> {
725-
todo!()
753+
let mut orders = HashMap::new();
754+
755+
for key in self.database.keys(&format!("{ORDERS}*"))? {
756+
let parts: Vec<&str> = key.as_str().rsplitn(2, ':').collect();
757+
let client_order_id = ClientOrderId::from(*parts.first().unwrap());
758+
let order = self.load_order(&client_order_id)?;
759+
orders.insert(client_order_id, order);
760+
}
761+
762+
Ok(orders)
726763
}
727764

728765
fn load_positions(&mut self) -> anyhow::Result<HashMap<PositionId, Position>> {
729-
todo!()
766+
let mut positions = HashMap::new();
767+
768+
for key in self.database.keys(&format!("{POSITIONS}*"))? {
769+
let parts: Vec<&str> = key.as_str().rsplitn(2, ':').collect();
770+
let position_id = PositionId::from(*parts.first().unwrap());
771+
let position = self.load_position(&position_id)?;
772+
positions.insert(position_id, position);
773+
}
774+
775+
Ok(positions)
730776
}
731777

732778
fn load_index_order_position(&mut self) -> anyhow::Result<HashMap<ClientOrderId, Position>> {
@@ -752,7 +798,7 @@ impl CacheDatabaseAdapter for RedisCacheDatabaseAdapter {
752798
todo!()
753799
}
754800

755-
fn load_account(&mut self, account_id: &AccountId) -> anyhow::Result<()> {
801+
fn load_account(&mut self, account_id: &AccountId) -> anyhow::Result<Box<dyn Account>> {
756802
todo!()
757803
}
758804

0 commit comments

Comments
 (0)