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