nautilus_common/cache/
database.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Provides a `Cache` database backing.
17
18// Under development
19#![allow(dead_code)]
20#![allow(unused_variables)]
21
22use std::collections::HashMap;
23
24use bytes::Bytes;
25use nautilus_core::UnixNanos;
26use nautilus_model::{
27    accounts::AccountAny,
28    data::{Bar, DataType, QuoteTick, TradeTick},
29    events::{OrderEventAny, OrderSnapshot, position::snapshot::PositionSnapshot},
30    identifiers::{
31        AccountId, ClientId, ClientOrderId, ComponentId, InstrumentId, PositionId, StrategyId,
32        VenueOrderId,
33    },
34    instruments::{InstrumentAny, SyntheticInstrument},
35    orderbook::OrderBook,
36    orders::OrderAny,
37    position::Position,
38    types::Currency,
39};
40use ustr::Ustr;
41
42use crate::{custom::CustomData, signal::Signal};
43
44#[derive(Default)]
45pub struct CacheMap {
46    pub currencies: HashMap<Ustr, Currency>,
47    pub instruments: HashMap<InstrumentId, InstrumentAny>,
48    pub synthetics: HashMap<InstrumentId, SyntheticInstrument>,
49    pub accounts: HashMap<AccountId, AccountAny>,
50    pub orders: HashMap<ClientOrderId, OrderAny>,
51    pub positions: HashMap<PositionId, Position>,
52}
53
54#[async_trait::async_trait]
55pub trait CacheDatabaseAdapter {
56    fn close(&mut self) -> anyhow::Result<()>;
57
58    fn flush(&mut self) -> anyhow::Result<()>;
59
60    async fn load_all(&self) -> anyhow::Result<CacheMap>;
61
62    fn load(&self) -> anyhow::Result<HashMap<String, Bytes>>;
63
64    async fn load_currencies(&self) -> anyhow::Result<HashMap<Ustr, Currency>>;
65
66    async fn load_instruments(&self) -> anyhow::Result<HashMap<InstrumentId, InstrumentAny>>;
67
68    async fn load_synthetics(&self) -> anyhow::Result<HashMap<InstrumentId, SyntheticInstrument>>;
69
70    async fn load_accounts(&self) -> anyhow::Result<HashMap<AccountId, AccountAny>>;
71
72    async fn load_orders(&self) -> anyhow::Result<HashMap<ClientOrderId, OrderAny>>;
73
74    async fn load_positions(&self) -> anyhow::Result<HashMap<PositionId, Position>>;
75
76    fn load_index_order_position(&self) -> anyhow::Result<HashMap<ClientOrderId, Position>>;
77
78    fn load_index_order_client(&self) -> anyhow::Result<HashMap<ClientOrderId, ClientId>>;
79
80    async fn load_currency(&self, code: &Ustr) -> anyhow::Result<Option<Currency>>;
81
82    async fn load_instrument(
83        &self,
84        instrument_id: &InstrumentId,
85    ) -> anyhow::Result<Option<InstrumentAny>>;
86
87    async fn load_synthetic(
88        &self,
89        instrument_id: &InstrumentId,
90    ) -> anyhow::Result<Option<SyntheticInstrument>>;
91
92    async fn load_account(&self, account_id: &AccountId) -> anyhow::Result<Option<AccountAny>>;
93
94    async fn load_order(&self, client_order_id: &ClientOrderId)
95    -> anyhow::Result<Option<OrderAny>>;
96
97    async fn load_position(&self, position_id: &PositionId) -> anyhow::Result<Option<Position>>;
98
99    fn load_actor(&self, component_id: &ComponentId) -> anyhow::Result<HashMap<String, Bytes>>;
100
101    fn load_strategy(&self, strategy_id: &StrategyId) -> anyhow::Result<HashMap<String, Bytes>>;
102
103    fn load_signals(&self, name: &str) -> anyhow::Result<Vec<Signal>>;
104
105    fn load_custom_data(&self, data_type: &DataType) -> anyhow::Result<Vec<CustomData>>;
106
107    fn load_order_snapshot(
108        &self,
109        client_order_id: &ClientOrderId,
110    ) -> anyhow::Result<Option<OrderSnapshot>>;
111
112    fn load_position_snapshot(
113        &self,
114        position_id: &PositionId,
115    ) -> anyhow::Result<Option<PositionSnapshot>>;
116
117    fn load_quotes(&self, instrument_id: &InstrumentId) -> anyhow::Result<Vec<QuoteTick>>;
118
119    fn load_trades(&self, instrument_id: &InstrumentId) -> anyhow::Result<Vec<TradeTick>>;
120
121    fn load_bars(&self, instrument_id: &InstrumentId) -> anyhow::Result<Vec<Bar>>;
122
123    fn add(&self, key: String, value: Bytes) -> anyhow::Result<()>;
124
125    fn add_currency(&self, currency: &Currency) -> anyhow::Result<()>;
126
127    fn add_instrument(&self, instrument: &InstrumentAny) -> anyhow::Result<()>;
128
129    fn add_synthetic(&self, synthetic: &SyntheticInstrument) -> anyhow::Result<()>;
130
131    fn add_account(&self, account: &AccountAny) -> anyhow::Result<()>;
132
133    fn add_order(&self, order: &OrderAny, client_id: Option<ClientId>) -> anyhow::Result<()>;
134
135    fn add_order_snapshot(&self, snapshot: &OrderSnapshot) -> anyhow::Result<()>;
136
137    fn add_position(&self, position: &Position) -> anyhow::Result<()>;
138
139    fn add_position_snapshot(&self, snapshot: &PositionSnapshot) -> anyhow::Result<()>;
140
141    fn add_order_book(&self, order_book: &OrderBook) -> anyhow::Result<()>;
142
143    fn add_signal(&self, signal: &Signal) -> anyhow::Result<()>;
144
145    fn add_custom_data(&self, data: &CustomData) -> anyhow::Result<()>;
146
147    fn add_quote(&self, quote: &QuoteTick) -> anyhow::Result<()>;
148
149    fn add_trade(&self, trade: &TradeTick) -> anyhow::Result<()>;
150
151    fn add_bar(&self, bar: &Bar) -> anyhow::Result<()>;
152
153    fn delete_actor(&self, component_id: &ComponentId) -> anyhow::Result<()>;
154
155    fn delete_strategy(&self, component_id: &StrategyId) -> anyhow::Result<()>;
156
157    fn index_venue_order_id(
158        &self,
159        client_order_id: ClientOrderId,
160        venue_order_id: VenueOrderId,
161    ) -> anyhow::Result<()>;
162
163    fn index_order_position(
164        &self,
165        client_order_id: ClientOrderId,
166        position_id: PositionId,
167    ) -> anyhow::Result<()>;
168
169    fn update_actor(&self) -> anyhow::Result<()>;
170
171    fn update_strategy(&self) -> anyhow::Result<()>;
172
173    fn update_account(&self, account: &AccountAny) -> anyhow::Result<()>;
174
175    fn update_order(&self, order_event: &OrderEventAny) -> anyhow::Result<()>;
176
177    fn update_position(&self, position: &Position) -> anyhow::Result<()>;
178
179    fn snapshot_order_state(&self, order: &OrderAny) -> anyhow::Result<()>;
180
181    fn snapshot_position_state(&self, position: &Position) -> anyhow::Result<()>;
182
183    fn heartbeat(&self, timestamp: UnixNanos) -> anyhow::Result<()>;
184}