Skip to main content

nautilus_persistence/writer/
filter.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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//! Record-family filters for streaming writer backends.
17
18use ahash::{AHashMap, AHashSet};
19use nautilus_model::{
20    data::NautilusRecordType,
21    instruments::{InstrumentAny, NautilusInstrumentType},
22};
23
24use crate::{catalog::traits::NautilusRecordTypePrefix, common::paths::CatalogPathPrefix};
25
26/// Typed record-family filter shared by streaming writer backends.
27#[derive(Clone, Debug, Default, PartialEq)]
28pub struct WriterRecordFilter {
29    entries: AHashMap<String, Option<AHashSet<String>>>,
30    instrument_types: AHashSet<String>,
31}
32
33impl WriterRecordFilter {
34    /// Creates an empty filter which allows all records.
35    #[must_use]
36    pub fn new() -> Self {
37        Self::default()
38    }
39
40    /// Creates a filter allowing complete record families.
41    #[must_use]
42    pub fn from_record_types(record_types: impl IntoIterator<Item = NautilusRecordType>) -> Self {
43        let mut filter = Self::new();
44        for record_type in record_types {
45            filter.insert(&record_type, None);
46        }
47        filter
48    }
49
50    /// Adds one record family with optional identifier restriction.
51    pub fn insert(&mut self, record_type: &NautilusRecordType, identifiers: Option<Vec<String>>) {
52        let prefix = record_type.path_prefix().into_owned();
53        self.insert_prefix(prefix, identifiers);
54    }
55
56    /// Adds one catalog path prefix with optional identifier restriction.
57    pub fn insert_prefix(&mut self, prefix: impl Into<String>, identifiers: Option<Vec<String>>) {
58        let identifiers = identifiers.map(|values| values.into_iter().collect());
59        self.entries.insert(prefix.into(), identifiers);
60    }
61
62    /// Adds one concrete instrument family.
63    pub fn insert_instrument_type(&mut self, instrument_type: &NautilusInstrumentType) {
64        self.instrument_types.insert(instrument_type.to_string());
65    }
66
67    /// Returns whether this filter carries no restrictions.
68    #[must_use]
69    pub fn is_empty(&self) -> bool {
70        self.entries.is_empty() && self.instrument_types.is_empty()
71    }
72
73    /// Returns whether this filter mentions a record prefix.
74    #[must_use]
75    pub fn contains_prefix(&self, record_prefix: &str) -> bool {
76        self.is_empty()
77            || self.entries.contains_key(record_prefix)
78            || (!self.instrument_types.is_empty() && record_prefix == InstrumentAny::path_prefix())
79    }
80
81    /// Returns whether record prefix and optional identifier pass this filter.
82    #[must_use]
83    pub fn allows(
84        &self,
85        record_prefix: &str,
86        identifier: Option<&str>,
87        instrument_type: Option<&str>,
88    ) -> bool {
89        if self.is_empty() {
90            return true;
91        }
92
93        let Some(identifiers) = self.entries.get(record_prefix) else {
94            return record_prefix == InstrumentAny::path_prefix()
95                && !self.instrument_types.is_empty()
96                && instrument_type.is_some_and(|value| self.instrument_types.contains(value));
97        };
98
99        if record_prefix == InstrumentAny::path_prefix()
100            && !self.instrument_types.is_empty()
101            && !instrument_type.is_some_and(|value| self.instrument_types.contains(value))
102        {
103            return false;
104        }
105
106        match identifiers {
107            None => true,
108            Some(identifiers) => {
109                identifier.is_some_and(|identifier| identifiers.contains(identifier))
110            }
111        }
112    }
113}