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::{NautilusDataType, NautilusRecordType},
21    instruments::NautilusInstrumentType,
22};
23
24use crate::catalog::types::CatalogDataType;
25
26/// Typed record-family filter shared by streaming writer backends.
27#[derive(Clone, Debug, Default, PartialEq)]
28pub struct WriterRecordFilter {
29    entries: AHashMap<CatalogDataType, Option<AHashSet<String>>>,
30    instrument_types: AHashSet<NautilusInstrumentType>,
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
48        filter
49    }
50
51    /// Adds one data or record family with optional identifier restriction.
52    pub fn insert(&mut self, family: impl Into<CatalogDataType>, identifiers: Option<Vec<String>>) {
53        let identifiers = identifiers.map(|values| values.into_iter().collect());
54        self.entries.insert(family.into(), identifiers);
55    }
56
57    /// Adds one concrete instrument family.
58    pub fn insert_instrument_type(&mut self, instrument_type: NautilusInstrumentType) {
59        self.instrument_types.insert(instrument_type);
60    }
61
62    /// Returns whether this filter carries no restrictions.
63    #[must_use]
64    pub fn is_empty(&self) -> bool {
65        self.entries.is_empty() && self.instrument_types.is_empty()
66    }
67
68    /// Returns whether this filter allows records of `family`.
69    #[must_use]
70    pub fn contains(&self, family: &CatalogDataType) -> bool {
71        self.is_empty()
72            || self.entries.contains_key(family)
73            || (!self.instrument_types.is_empty() && is_instrument_family(family))
74    }
75
76    /// Returns whether a record of `data_type` and optional identifier passes this filter.
77    ///
78    /// An instrument record passes its class as [`CatalogDataType::Instrument`].
79    #[must_use]
80    pub fn allows(&self, data_type: &CatalogDataType, identifier: Option<&str>) -> bool {
81        if self.is_empty() {
82            return true;
83        }
84
85        let instrument_type = match data_type {
86            CatalogDataType::Instrument(instrument_type) => Some(instrument_type),
87            _ => None,
88        };
89
90        let family = catalog_family(data_type);
91
92        let Some(identifiers) = self.entries.get(&family) else {
93            return is_instrument_family(&family)
94                && instrument_type.is_some_and(|value| self.instrument_types.contains(value));
95        };
96
97        if is_instrument_family(&family)
98            && !self.instrument_types.is_empty()
99            && !instrument_type.is_some_and(|value| self.instrument_types.contains(value))
100        {
101            return false;
102        }
103
104        match identifiers {
105            None => true,
106            Some(identifiers) => {
107                identifier.is_some_and(|identifier| identifiers.contains(identifier))
108            }
109        }
110    }
111}
112
113/// Returns the family a writer stages `data_type` under; every instrument class is one family.
114#[must_use]
115pub(crate) fn catalog_family(data_type: &CatalogDataType) -> CatalogDataType {
116    match data_type {
117        CatalogDataType::Instrument(_) => CatalogDataType::Data(NautilusDataType::Instrument),
118        other => other.clone(),
119    }
120}
121
122fn is_instrument_family(family: &CatalogDataType) -> bool {
123    *family == CatalogDataType::Data(NautilusDataType::Instrument)
124}