nautilus_common/cache/
index.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
16use std::collections::{HashMap, HashSet};
17
18use nautilus_model::identifiers::{
19    AccountId, ClientId, ClientOrderId, ComponentId, ExecAlgorithmId, InstrumentId, PositionId,
20    StrategyId, Venue, VenueOrderId,
21};
22
23/// A key-value lookup index for a `Cache`.
24#[derive(Debug)]
25pub struct CacheIndex {
26    pub(crate) venue_account: HashMap<Venue, AccountId>,
27    pub(crate) venue_orders: HashMap<Venue, HashSet<ClientOrderId>>,
28    pub(crate) venue_positions: HashMap<Venue, HashSet<PositionId>>,
29    pub(crate) venue_order_ids: HashMap<VenueOrderId, ClientOrderId>,
30    pub(crate) client_order_ids: HashMap<ClientOrderId, VenueOrderId>,
31    pub(crate) order_position: HashMap<ClientOrderId, PositionId>,
32    pub(crate) order_strategy: HashMap<ClientOrderId, StrategyId>,
33    pub(crate) order_client: HashMap<ClientOrderId, ClientId>,
34    pub(crate) position_strategy: HashMap<PositionId, StrategyId>,
35    pub(crate) position_orders: HashMap<PositionId, HashSet<ClientOrderId>>,
36    pub(crate) instrument_orders: HashMap<InstrumentId, HashSet<ClientOrderId>>,
37    pub(crate) instrument_positions: HashMap<InstrumentId, HashSet<PositionId>>,
38    pub(crate) strategy_orders: HashMap<StrategyId, HashSet<ClientOrderId>>,
39    pub(crate) strategy_positions: HashMap<StrategyId, HashSet<PositionId>>,
40    pub(crate) exec_algorithm_orders: HashMap<ExecAlgorithmId, HashSet<ClientOrderId>>,
41    pub(crate) exec_spawn_orders: HashMap<ClientOrderId, HashSet<ClientOrderId>>,
42    pub(crate) orders: HashSet<ClientOrderId>,
43    pub(crate) orders_open: HashSet<ClientOrderId>,
44    pub(crate) orders_closed: HashSet<ClientOrderId>,
45    pub(crate) orders_emulated: HashSet<ClientOrderId>,
46    pub(crate) orders_inflight: HashSet<ClientOrderId>,
47    pub(crate) orders_pending_cancel: HashSet<ClientOrderId>,
48    pub(crate) positions: HashSet<PositionId>,
49    pub(crate) positions_open: HashSet<PositionId>,
50    pub(crate) positions_closed: HashSet<PositionId>,
51    pub(crate) actors: HashSet<ComponentId>,
52    pub(crate) strategies: HashSet<StrategyId>,
53    pub(crate) exec_algorithms: HashSet<ExecAlgorithmId>,
54}
55
56impl Default for CacheIndex {
57    /// Creates a new default [`CacheIndex`] instance.
58    fn default() -> Self {
59        Self {
60            venue_account: HashMap::new(),
61            venue_orders: HashMap::new(),
62            venue_positions: HashMap::new(),
63            venue_order_ids: HashMap::new(),
64            client_order_ids: HashMap::new(),
65            order_position: HashMap::new(),
66            order_strategy: HashMap::new(),
67            order_client: HashMap::new(),
68            position_strategy: HashMap::new(),
69            position_orders: HashMap::new(),
70            instrument_orders: HashMap::new(),
71            instrument_positions: HashMap::new(),
72            strategy_orders: HashMap::new(),
73            strategy_positions: HashMap::new(),
74            exec_algorithm_orders: HashMap::new(),
75            exec_spawn_orders: HashMap::new(),
76            orders: HashSet::new(),
77            orders_open: HashSet::new(),
78            orders_closed: HashSet::new(),
79            orders_emulated: HashSet::new(),
80            orders_inflight: HashSet::new(),
81            orders_pending_cancel: HashSet::new(),
82            positions: HashSet::new(),
83            positions_open: HashSet::new(),
84            positions_closed: HashSet::new(),
85            actors: HashSet::new(),
86            strategies: HashSet::new(),
87            exec_algorithms: HashSet::new(),
88        }
89    }
90}
91
92impl CacheIndex {
93    /// Clears the index which will clear/reset all internal state.
94    pub fn clear(&mut self) {
95        self.venue_account.clear();
96        self.venue_orders.clear();
97        self.venue_positions.clear();
98        self.venue_order_ids.clear();
99        self.client_order_ids.clear();
100        self.order_position.clear();
101        self.order_strategy.clear();
102        self.order_client.clear();
103        self.position_strategy.clear();
104        self.position_orders.clear();
105        self.instrument_orders.clear();
106        self.instrument_positions.clear();
107        self.strategy_orders.clear();
108        self.strategy_positions.clear();
109        self.exec_algorithm_orders.clear();
110        self.exec_spawn_orders.clear();
111        self.orders.clear();
112        self.orders_open.clear();
113        self.orders_closed.clear();
114        self.orders_emulated.clear();
115        self.orders_inflight.clear();
116        self.orders_pending_cancel.clear();
117        self.positions.clear();
118        self.positions_open.clear();
119        self.positions_closed.clear();
120        self.actors.clear();
121        self.strategies.clear();
122        self.exec_algorithms.clear();
123    }
124}