nautilus_persistence/backend/
catalog.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//! Parquet data catalog for efficient storage and retrieval of financial market data.
17//!
18//! This module provides a comprehensive data catalog implementation that uses Apache Parquet
19//! format for storing financial market data with object store backends. The catalog supports
20//! various data types including quotes, trades, bars, order book data, and other market events.
21//!
22//! # Key Features
23//!
24//! - **Object Store Integration**: Works with local filesystems, S3, and other object stores.
25//! - **Data Type Support**: Handles all major financial data types (quotes, trades, bars, etc.).
26//! - **Time-based Organization**: Organizes data by timestamp ranges for efficient querying.
27//! - **Consolidation**: Merges multiple files to optimize storage and query performance.
28//! - **Validation**: Ensures data integrity with timestamp ordering and interval validation.
29//!
30//! # Architecture
31//!
32//! The catalog organizes data in a hierarchical structure:
33//! ```text
34//! data/
35//! ├── quotes/
36//! │   └── INSTRUMENT_ID/
37//! │       └── start_ts-end_ts.parquet
38//! ├── trades/
39//! │   └── INSTRUMENT_ID/
40//! │       └── start_ts-end_ts.parquet
41//! └── bars/
42//!     └── INSTRUMENT_ID/
43//!         └── start_ts-end_ts.parquet
44//! ```
45//!
46//! # Usage
47//!
48//! ```rust,no_run
49//! use std::path::PathBuf;
50//! use nautilus_persistence::backend::catalog::ParquetDataCatalog;
51//!
52//! // Create a new catalog
53//! let catalog = ParquetDataCatalog::new(
54//!     PathBuf::from("/path/to/data"),
55//!     None,        // storage_options
56//!     Some(5000),  // batch_size
57//!     None,        // compression (defaults to SNAPPY)
58//!     None,        // max_row_group_size (defaults to 5000)
59//! );
60//!
61//! // Write data to the catalog
62//! // catalog.write_to_parquet(data, None, None)?;
63//! ```
64
65use std::{
66    fmt::Debug,
67    ops::Bound,
68    path::{Path, PathBuf},
69    sync::Arc,
70};
71
72use ahash::AHashMap;
73use datafusion::arrow::record_batch::RecordBatch;
74use futures::StreamExt;
75use heck::ToSnakeCase;
76use itertools::Itertools;
77use nautilus_common::live::runtime::get_runtime;
78use nautilus_core::{
79    UnixNanos,
80    datetime::{iso8601_to_unix_nanos, unix_nanos_to_iso8601},
81};
82use nautilus_model::data::{
83    Bar, Data, HasTsInit, IndexPriceUpdate, MarkPriceUpdate, OrderBookDelta, OrderBookDepth10,
84    QuoteTick, TradeTick, close::InstrumentClose, to_variant,
85};
86use nautilus_serialization::arrow::{DecodeDataFromRecordBatch, EncodeToRecordBatch};
87use object_store::{ObjectStore, path::Path as ObjectPath};
88use serde::Serialize;
89use unbounded_interval_tree::interval_tree::IntervalTree;
90
91use super::session::{self, DataBackendSession, QueryResult, build_query};
92use crate::parquet::write_batches_to_object_store;
93
94/// A high-performance data catalog for storing and retrieving financial market data using Apache Parquet format.
95///
96/// The `ParquetDataCatalog` provides a comprehensive solution for managing large volumes of financial
97/// market data with efficient storage, querying, and consolidation capabilities. It supports various
98/// object store backends including local filesystems, AWS S3, and other cloud storage providers.
99///
100/// # Features
101///
102/// - **Efficient Storage**: Uses Apache Parquet format with configurable compression.
103/// - **Object Store Backend**: Supports multiple storage backends through the `object_store` crate.
104/// - **Time-based Organization**: Organizes data by timestamp ranges for optimal query performance.
105/// - **Data Validation**: Ensures timestamp ordering and interval consistency.
106/// - **Consolidation**: Merges multiple files to reduce storage overhead and improve query speed.
107/// - **Type Safety**: Strongly typed data handling with compile-time guarantees.
108///
109/// # Data Organization
110///
111/// Data is organized hierarchically by data type and instrument:
112/// - `data/{data_type}/{instrument_id}/{start_ts}-{end_ts}.parquet`.
113/// - Files are named with their timestamp ranges for efficient range queries.
114/// - Intervals are validated to be disjoint to prevent data overlap.
115///
116/// # Performance Considerations
117///
118/// - **Batch Size**: Controls memory usage during data processing.
119/// - **Compression**: SNAPPY compression provides good balance of speed and size.
120/// - **Row Group Size**: Affects query performance and memory usage.
121/// - **File Consolidation**: Reduces the number of files for better query performance.
122pub struct ParquetDataCatalog {
123    /// The base path for data storage within the object store.
124    pub base_path: String,
125    /// The original URI provided when creating the catalog.
126    pub original_uri: String,
127    /// The object store backend for data persistence.
128    pub object_store: Arc<dyn ObjectStore>,
129    /// The DataFusion session for query execution.
130    pub session: DataBackendSession,
131    /// The number of records to process in each batch.
132    pub batch_size: usize,
133    /// The compression algorithm used for Parquet files.
134    pub compression: parquet::basic::Compression,
135    /// The maximum number of rows in each Parquet row group.
136    pub max_row_group_size: usize,
137}
138
139impl Debug for ParquetDataCatalog {
140    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
141        f.debug_struct(stringify!(ParquetDataCatalog))
142            .field("base_path", &self.base_path)
143            .finish()
144    }
145}
146
147impl ParquetDataCatalog {
148    /// Creates a new [`ParquetDataCatalog`] instance from a local file path.
149    ///
150    /// This is a convenience constructor that converts a local path to a URI format
151    /// and delegates to [`Self::from_uri`].
152    ///
153    /// # Parameters
154    ///
155    /// - `base_path`: The base directory path for data storage.
156    /// - `storage_options`: Optional `HashMap` containing storage-specific configuration options.
157    /// - `batch_size`: Number of records to process in each batch (default: 5000).
158    /// - `compression`: Parquet compression algorithm (default: SNAPPY).
159    /// - `max_row_group_size`: Maximum rows per Parquet row group (default: 5000).
160    ///
161    /// # Panics
162    ///
163    /// Panics if the path cannot be converted to a valid URI or if the object store
164    /// cannot be created from the path.
165    ///
166    /// # Examples
167    ///
168    /// ```rust,no_run
169    /// use std::path::PathBuf;
170    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
171    ///
172    /// let catalog = ParquetDataCatalog::new(
173    ///     PathBuf::from("/tmp/nautilus_data"),
174    ///     None,        // no storage options
175    ///     Some(1000),  // smaller batch size
176    ///     None,        // default compression
177    ///     None,        // default row group size
178    /// );
179    /// ```
180    #[must_use]
181    pub fn new(
182        base_path: PathBuf,
183        storage_options: Option<AHashMap<String, String>>,
184        batch_size: Option<usize>,
185        compression: Option<parquet::basic::Compression>,
186        max_row_group_size: Option<usize>,
187    ) -> Self {
188        let path_str = base_path.to_string_lossy().to_string();
189        Self::from_uri(
190            &path_str,
191            storage_options,
192            batch_size,
193            compression,
194            max_row_group_size,
195        )
196        .expect("Failed to create catalog from path")
197    }
198
199    /// Creates a new [`ParquetDataCatalog`] instance from a URI with optional storage options.
200    ///
201    /// Supports various URI schemes including local file paths and multiple cloud storage backends
202    /// supported by the `object_store` crate.
203    ///
204    /// # Supported URI Schemes
205    ///
206    /// - **AWS S3**: `s3://bucket/path`.
207    /// - **Google Cloud Storage**: `gs://bucket/path` or `gcs://bucket/path`.
208    /// - **Azure Blob Storage**: `az://container/path` or `abfs://container@account.dfs.core.windows.net/path`.
209    /// - **HTTP/WebDAV**: `http://` or `https://`.
210    /// - **Local files**: `file://path` or plain paths.
211    ///
212    /// # Parameters
213    ///
214    /// - `uri`: The URI for the data storage location.
215    /// - `storage_options`: Optional `HashMap` containing storage-specific configuration options:
216    ///   - For S3: `endpoint_url`, region, `access_key_id`, `secret_access_key`, `session_token`, etc.
217    ///   - For GCS: `service_account_path`, `service_account_key`, `project_id`, etc.
218    ///   - For Azure: `account_name`, `account_key`, `sas_token`, etc.
219    /// - `batch_size`: Number of records to process in each batch (default: 5000).
220    /// - `compression`: Parquet compression algorithm (default: SNAPPY).
221    /// - `max_row_group_size`: Maximum rows per Parquet row group (default: 5000).
222    ///
223    /// # Errors
224    ///
225    /// Returns an error if:
226    /// - The URI format is invalid or unsupported.
227    /// - The object store cannot be created or accessed.
228    /// - Authentication fails for cloud storage backends.
229    ///
230    /// # Examples
231    ///
232    /// ```rust,no_run
233    /// use ahash::AHashMap;
234    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
235    ///
236    /// // Local filesystem
237    /// let local_catalog = ParquetDataCatalog::from_uri(
238    ///     "/tmp/nautilus_data",
239    ///     None, None, None, None
240    /// )?;
241    ///
242    /// // S3 bucket
243    /// let s3_catalog = ParquetDataCatalog::from_uri(
244    ///     "s3://my-bucket/nautilus-data",
245    ///     None, None, None, None
246    /// )?;
247    ///
248    /// // Google Cloud Storage
249    /// let gcs_catalog = ParquetDataCatalog::from_uri(
250    ///     "gs://my-bucket/nautilus-data",
251    ///     None, None, None, None
252    /// )?;
253    ///
254    /// // Azure Blob Storage
255    /// let azure_catalog = ParquetDataCatalog::from_uri(
256    ///     "az://container/nautilus-data",
257    ///     storage_options, None, None, None
258    /// )?;
259    ///
260    /// // S3 with custom endpoint and credentials
261    /// let mut storage_options = HashMap::new();
262    /// storage_options.insert("endpoint_url".to_string(), "https://my-s3-endpoint.com".to_string());
263    /// storage_options.insert("access_key_id".to_string(), "my-key".to_string());
264    /// storage_options.insert("secret_access_key".to_string(), "my-secret".to_string());
265    ///
266    /// let s3_catalog = ParquetDataCatalog::from_uri(
267    ///     "s3://my-bucket/nautilus-data",
268    ///     Some(storage_options),
269    ///     None, None, None,
270    /// )?;
271    /// # Ok::<(), anyhow::Error>(())
272    /// ```
273    pub fn from_uri(
274        uri: &str,
275        storage_options: Option<AHashMap<String, String>>,
276        batch_size: Option<usize>,
277        compression: Option<parquet::basic::Compression>,
278        max_row_group_size: Option<usize>,
279    ) -> anyhow::Result<Self> {
280        let batch_size = batch_size.unwrap_or(5000);
281        let compression = compression.unwrap_or(parquet::basic::Compression::SNAPPY);
282        let max_row_group_size = max_row_group_size.unwrap_or(5000);
283
284        let (object_store, base_path, original_uri) =
285            crate::parquet::create_object_store_from_path(uri, storage_options)?;
286
287        Ok(Self {
288            base_path,
289            original_uri,
290            object_store,
291            session: session::DataBackendSession::new(batch_size),
292            batch_size,
293            compression,
294            max_row_group_size,
295        })
296    }
297
298    /// Returns the base path of the catalog for testing purposes.
299    #[must_use]
300    pub fn get_base_path(&self) -> String {
301        self.base_path.clone()
302    }
303
304    /// Resets the backend session to clear any cached table registrations.
305    ///
306    /// This is useful during catalog operations when files are being modified
307    /// and we need to ensure fresh data is loaded.
308    pub fn reset_session(&mut self) {
309        self.session.clear_registered_tables();
310    }
311
312    /// Writes mixed data types to the catalog by separating them into type-specific collections.
313    ///
314    /// This method takes a heterogeneous collection of market data and separates it by type,
315    /// then writes each type to its appropriate location in the catalog. This is useful when
316    /// processing mixed data streams or bulk data imports.
317    ///
318    /// # Parameters
319    ///
320    /// - `data`: A vector of mixed [`Data`] enum variants.
321    /// - `start`: Optional start timestamp to override the data's natural range.
322    /// - `end`: Optional end timestamp to override the data's natural range.
323    ///
324    /// # Notes
325    ///
326    /// - Data is automatically sorted by type before writing.
327    /// - Each data type is written to its own directory structure.
328    /// - Instrument data handling is not yet implemented (TODO).
329    ///
330    /// # Examples
331    ///
332    /// ```rust,no_run
333    /// use nautilus_model::data::Data;
334    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
335    ///
336    /// let catalog = ParquetDataCatalog::new(/* ... */);
337    /// let mixed_data: Vec<Data> = vec![/* mixed data types */];
338    ///
339    /// catalog.write_data_enum(mixed_data, None, None)?;
340    /// ```
341    pub fn write_data_enum(
342        &self,
343        data: Vec<Data>,
344        start: Option<UnixNanos>,
345        end: Option<UnixNanos>,
346    ) -> anyhow::Result<()> {
347        let mut deltas: Vec<OrderBookDelta> = Vec::new();
348        let mut depth10s: Vec<OrderBookDepth10> = Vec::new();
349        let mut quotes: Vec<QuoteTick> = Vec::new();
350        let mut trades: Vec<TradeTick> = Vec::new();
351        let mut bars: Vec<Bar> = Vec::new();
352        let mut mark_prices: Vec<MarkPriceUpdate> = Vec::new();
353        let mut index_prices: Vec<IndexPriceUpdate> = Vec::new();
354        let mut closes: Vec<InstrumentClose> = Vec::new();
355
356        for d in data.iter().cloned() {
357            match d {
358                Data::Deltas(_) => continue,
359                Data::Delta(d) => {
360                    deltas.push(d);
361                }
362                Data::Depth10(d) => {
363                    depth10s.push(*d);
364                }
365                Data::Quote(d) => {
366                    quotes.push(d);
367                }
368                Data::Trade(d) => {
369                    trades.push(d);
370                }
371                Data::Bar(d) => {
372                    bars.push(d);
373                }
374                Data::MarkPriceUpdate(p) => {
375                    mark_prices.push(p);
376                }
377                Data::IndexPriceUpdate(p) => {
378                    index_prices.push(p);
379                }
380                Data::InstrumentClose(c) => {
381                    closes.push(c);
382                }
383            }
384        }
385
386        // TODO: need to handle instruments here
387
388        self.write_to_parquet(deltas, start, end, None)?;
389        self.write_to_parquet(depth10s, start, end, None)?;
390        self.write_to_parquet(quotes, start, end, None)?;
391        self.write_to_parquet(trades, start, end, None)?;
392        self.write_to_parquet(bars, start, end, None)?;
393        self.write_to_parquet(mark_prices, start, end, None)?;
394        self.write_to_parquet(index_prices, start, end, None)?;
395        self.write_to_parquet(closes, start, end, None)?;
396
397        Ok(())
398    }
399
400    /// Writes typed data to a Parquet file in the catalog.
401    ///
402    /// This is the core method for persisting market data to the catalog. It handles data
403    /// validation, batching, compression, and ensures proper file organization with
404    /// timestamp-based naming.
405    ///
406    /// # Type Parameters
407    ///
408    /// - `T`: The data type to write, must implement required traits for serialization and cataloging.
409    ///
410    /// # Parameters
411    ///
412    /// - `data`: Vector of data records to write (must be in ascending timestamp order).
413    /// - `start`: Optional start timestamp to override the natural data range.
414    /// - `end`: Optional end timestamp to override the natural data range.
415    ///
416    /// # Returns
417    ///
418    /// Returns the [`PathBuf`] of the created file, or an empty path if no data was provided.
419    ///
420    /// # Errors
421    ///
422    /// Returns an error if:
423    /// - Data serialization to Arrow record batches fails.
424    /// - Object store write operations fail.
425    /// - File path construction fails.
426    /// - Timestamp interval validation fails after writing.
427    ///
428    /// # Panics
429    ///
430    /// Panics if:
431    /// - Data timestamps are not in ascending order.
432    /// - Record batches are empty after conversion.
433    /// - Required metadata is missing from the schema.
434    ///
435    /// # Examples
436    ///
437    /// ```rust,no_run
438    /// use nautilus_model::data::QuoteTick;
439    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
440    ///
441    /// let catalog = ParquetDataCatalog::new(/* ... */);
442    /// let quotes: Vec<QuoteTick> = vec![/* quote data */];
443    ///
444    /// let path = catalog.write_to_parquet(quotes, None, None)?;
445    /// println!("Data written to: {:?}", path);
446    /// # Ok::<(), anyhow::Error>(())
447    /// ```
448    pub fn write_to_parquet<T>(
449        &self,
450        data: Vec<T>,
451        start: Option<UnixNanos>,
452        end: Option<UnixNanos>,
453        skip_disjoint_check: Option<bool>,
454    ) -> anyhow::Result<PathBuf>
455    where
456        T: HasTsInit + EncodeToRecordBatch + CatalogPathPrefix,
457    {
458        if data.is_empty() {
459            return Ok(PathBuf::new());
460        }
461
462        let type_name = std::any::type_name::<T>().to_snake_case();
463        Self::check_ascending_timestamps(&data, &type_name)?;
464
465        let start_ts = start.unwrap_or(data.first().unwrap().ts_init());
466        let end_ts = end.unwrap_or(data.last().unwrap().ts_init());
467
468        let batches = self.data_to_record_batches(data)?;
469        let schema = batches.first().expect("Batches are empty.").schema();
470        let instrument_id = schema.metadata.get("instrument_id").cloned();
471
472        let directory = self.make_path(T::path_prefix(), instrument_id)?;
473        let filename = timestamps_to_filename(start_ts, end_ts);
474        let path = PathBuf::from(format!("{directory}/{filename}"));
475
476        // Write all batches to parquet file
477        log::info!(
478            "Writing {} batches of {type_name} data to {path:?}",
479            batches.len()
480        );
481
482        // Convert path to object store path
483        let object_path = self.to_object_path(&path.to_string_lossy());
484
485        self.execute_async(async {
486            write_batches_to_object_store(
487                &batches,
488                self.object_store.clone(),
489                &object_path,
490                Some(self.compression),
491                Some(self.max_row_group_size),
492            )
493            .await
494        })?;
495
496        if !skip_disjoint_check.unwrap_or(false) {
497            let intervals = self.get_directory_intervals(&directory)?;
498
499            if !are_intervals_disjoint(&intervals) {
500                anyhow::bail!("Intervals are not disjoint after writing a new file");
501            }
502        }
503
504        Ok(path)
505    }
506
507    /// Writes typed data to a JSON file in the catalog.
508    ///
509    /// This method provides an alternative to Parquet format for data export and debugging.
510    /// JSON files are human-readable but less efficient for large datasets.
511    ///
512    /// # Type Parameters
513    ///
514    /// - `T`: The data type to write, must implement serialization and cataloging traits.
515    ///
516    /// # Parameters
517    ///
518    /// - `data`: Vector of data records to write (must be in ascending timestamp order).
519    /// - `path`: Optional custom directory path (defaults to catalog's standard structure).
520    /// - `write_metadata`: Whether to write a separate metadata file alongside the data.
521    ///
522    /// # Returns
523    ///
524    /// Returns the [`PathBuf`] of the created JSON file.
525    ///
526    /// # Errors
527    ///
528    /// Returns an error if:
529    /// - JSON serialization fails.
530    /// - Object store write operations fail.
531    /// - File path construction fails.
532    ///
533    /// # Panics
534    ///
535    /// Panics if data timestamps are not in ascending order.
536    ///
537    /// # Examples
538    ///
539    /// ```rust,no_run
540    /// use std::path::PathBuf;
541    /// use nautilus_model::data::TradeTick;
542    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
543    ///
544    /// let catalog = ParquetDataCatalog::new(/* ... */);
545    /// let trades: Vec<TradeTick> = vec![/* trade data */];
546    ///
547    /// let path = catalog.write_to_json(
548    ///     trades,
549    ///     Some(PathBuf::from("/custom/path")),
550    ///     true  // write metadata
551    /// )?;
552    /// # Ok::<(), anyhow::Error>(())
553    /// ```
554    pub fn write_to_json<T>(
555        &self,
556        data: Vec<T>,
557        path: Option<PathBuf>,
558        write_metadata: bool,
559    ) -> anyhow::Result<PathBuf>
560    where
561        T: HasTsInit + Serialize + CatalogPathPrefix + EncodeToRecordBatch,
562    {
563        if data.is_empty() {
564            return Ok(PathBuf::new());
565        }
566
567        let type_name = std::any::type_name::<T>().to_snake_case();
568        Self::check_ascending_timestamps(&data, &type_name)?;
569
570        let start_ts = data.first().unwrap().ts_init();
571        let end_ts = data.last().unwrap().ts_init();
572
573        let directory =
574            path.unwrap_or_else(|| PathBuf::from(self.make_path(T::path_prefix(), None).unwrap()));
575        let filename = timestamps_to_filename(start_ts, end_ts).replace(".parquet", ".json");
576        let json_path = directory.join(&filename);
577
578        log::info!(
579            "Writing {} records of {type_name} data to {json_path:?}",
580            data.len()
581        );
582
583        if write_metadata {
584            let metadata = T::chunk_metadata(&data);
585            let metadata_path = json_path.with_extension("metadata.json");
586            log::info!("Writing metadata to {metadata_path:?}");
587
588            // Use object store for metadata file
589            let metadata_object_path = ObjectPath::from(metadata_path.to_string_lossy().as_ref());
590            let metadata_json = serde_json::to_vec_pretty(&metadata)?;
591            self.execute_async(async {
592                self.object_store
593                    .put(&metadata_object_path, metadata_json.into())
594                    .await
595                    .map_err(anyhow::Error::from)
596            })?;
597        }
598
599        // Use object store for main JSON file
600        let json_object_path = ObjectPath::from(json_path.to_string_lossy().as_ref());
601        let json_data = serde_json::to_vec_pretty(&serde_json::to_value(data)?)?;
602        self.execute_async(async {
603            self.object_store
604                .put(&json_object_path, json_data.into())
605                .await
606                .map_err(anyhow::Error::from)
607        })?;
608
609        Ok(json_path)
610    }
611
612    /// Validates that data timestamps are in ascending order.
613    ///
614    /// # Parameters
615    ///
616    /// - `data`: Slice of data records to validate.
617    /// - `type_name`: Name of the data type for error messages.
618    ///
619    /// # Panics
620    ///
621    /// Panics if any timestamp is less than the previous timestamp.
622    pub fn check_ascending_timestamps<T: HasTsInit>(
623        data: &[T],
624        type_name: &str,
625    ) -> anyhow::Result<()> {
626        if !data.windows(2).all(|w| w[0].ts_init() <= w[1].ts_init()) {
627            anyhow::bail!("{type_name} timestamps must be in ascending order");
628        }
629
630        Ok(())
631    }
632
633    /// Converts data into Arrow record batches for Parquet serialization.
634    ///
635    /// This method chunks the data according to the configured batch size and converts
636    /// each chunk into an Arrow record batch with appropriate metadata.
637    ///
638    /// # Type Parameters
639    ///
640    /// - `T`: The data type to convert, must implement required encoding traits.
641    ///
642    /// # Parameters
643    ///
644    /// - `data`: Vector of data records to convert.
645    ///
646    /// # Returns
647    ///
648    /// Returns a vector of Arrow [`RecordBatch`] instances ready for Parquet serialization.
649    ///
650    /// # Errors
651    ///
652    /// Returns an error if record batch encoding fails for any chunk.
653    pub fn data_to_record_batches<T>(&self, data: Vec<T>) -> anyhow::Result<Vec<RecordBatch>>
654    where
655        T: HasTsInit + EncodeToRecordBatch,
656    {
657        let mut batches = Vec::new();
658
659        for chunk in &data.into_iter().chunks(self.batch_size) {
660            let data = chunk.collect_vec();
661            let metadata = EncodeToRecordBatch::chunk_metadata(&data);
662            let record_batch = T::encode_batch(&metadata, &data)?;
663            batches.push(record_batch);
664        }
665
666        Ok(batches)
667    }
668
669    /// Extends the timestamp range of an existing Parquet file by renaming it.
670    ///
671    /// This method finds an existing file that is adjacent to the specified time range
672    /// and renames it to include the new range. This is useful when appending data
673    /// that extends the time coverage of existing files.
674    ///
675    /// # Parameters
676    ///
677    /// - `data_cls`: The data type directory name (e.g., "quotes", "trades").
678    /// - `instrument_id`: Optional instrument ID to target a specific instrument's data.
679    /// - `start`: Start timestamp of the new range to extend to.
680    /// - `end`: End timestamp of the new range to extend to.
681    ///
682    /// # Returns
683    ///
684    /// Returns `Ok(())` on success, or an error if the operation fails.
685    ///
686    /// # Errors
687    ///
688    /// Returns an error if:
689    /// - The directory path cannot be constructed.
690    /// - No adjacent file is found to extend.
691    /// - File rename operations fail.
692    /// - Interval validation fails after extension.
693    ///
694    /// # Examples
695    ///
696    /// ```rust,no_run
697    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
698    /// use nautilus_core::UnixNanos;
699    ///
700    /// let catalog = ParquetDataCatalog::new(/* ... */);
701    ///
702    /// // Extend a file's range backwards or forwards
703    /// catalog.extend_file_name(
704    ///     "quotes",
705    ///     Some("BTCUSD".to_string()),
706    ///     UnixNanos::from(1609459200000000000),
707    ///     UnixNanos::from(1609545600000000000)
708    /// )?;
709    /// # Ok::<(), anyhow::Error>(())
710    /// ```
711    pub fn extend_file_name(
712        &self,
713        data_cls: &str,
714        instrument_id: Option<String>,
715        start: UnixNanos,
716        end: UnixNanos,
717    ) -> anyhow::Result<()> {
718        let directory = self.make_path(data_cls, instrument_id)?;
719        let intervals = self.get_directory_intervals(&directory)?;
720
721        let start = start.as_u64();
722        let end = end.as_u64();
723
724        for interval in intervals {
725            if interval.0 == end + 1 {
726                // Extend backwards: new file covers [start, interval.1]
727                self.rename_parquet_file(&directory, interval.0, interval.1, start, interval.1)?;
728                break;
729            } else if interval.1 == start - 1 {
730                // Extend forwards: new file covers [interval.0, end]
731                self.rename_parquet_file(&directory, interval.0, interval.1, interval.0, end)?;
732                break;
733            }
734        }
735
736        let intervals = self.get_directory_intervals(&directory)?;
737
738        if !are_intervals_disjoint(&intervals) {
739            anyhow::bail!("Intervals are not disjoint after extending a file");
740        }
741
742        Ok(())
743    }
744
745    /// Lists all Parquet files in a specified directory.
746    ///
747    /// This method scans a directory and returns the full paths of all files with the `.parquet`
748    /// extension. It works with both local filesystems and remote object stores, making it
749    /// suitable for various storage backends.
750    ///
751    /// # Parameters
752    ///
753    /// - `directory`: The directory path to scan for Parquet files.
754    ///
755    /// # Returns
756    ///
757    /// Returns a vector of full file paths (as strings) for all Parquet files found in the directory.
758    /// The paths are relative to the object store root and suitable for use with object store operations.
759    /// Returns an empty vector if the directory doesn't exist or contains no Parquet files.
760    ///
761    /// # Errors
762    ///
763    /// Returns an error if:
764    /// - Object store listing operations fail.
765    /// - Directory access is denied.
766    /// - Network issues occur (for remote object stores).
767    ///
768    /// # Notes
769    ///
770    /// - Only files ending with `.parquet` are included.
771    /// - Subdirectories are not recursively scanned.
772    /// - File paths are returned in the order provided by the object store.
773    /// - Works with all supported object store backends (local, S3, GCS, Azure, etc.).
774    ///
775    /// # Examples
776    ///
777    /// ```rust,no_run
778    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
779    ///
780    /// let catalog = ParquetDataCatalog::new(/* ... */);
781    /// let files = catalog.list_parquet_files("data/quotes/EURUSD")?;
782    ///
783    /// for file in files {
784    ///     println!("Found Parquet file: {}", file);
785    /// }
786    /// # Ok::<(), anyhow::Error>(())
787    /// ```
788    pub fn list_parquet_files(&self, directory: &str) -> anyhow::Result<Vec<String>> {
789        self.execute_async(async {
790            let prefix = ObjectPath::from(format!("{directory}/"));
791            let mut stream = self.object_store.list(Some(&prefix));
792            let mut files = Vec::new();
793
794            while let Some(object) = stream.next().await {
795                let object = object?;
796                if object.location.as_ref().ends_with(".parquet") {
797                    files.push(object.location.to_string());
798                }
799            }
800            Ok::<Vec<String>, anyhow::Error>(files)
801        })
802    }
803
804    /// Helper method to reconstruct full URI for remote object store paths
805    #[must_use]
806    pub fn reconstruct_full_uri(&self, path_str: &str) -> String {
807        // Check if this is a remote URI scheme that needs reconstruction
808        if self.is_remote_uri() {
809            // Extract the base URL (scheme + host) from the original URI
810            if let Ok(url) = url::Url::parse(&self.original_uri)
811                && let Some(host) = url.host_str()
812            {
813                return format!("{}://{}/{}", url.scheme(), host, path_str);
814            }
815        }
816
817        // For local paths, extract the directory from the original URI
818        if self.original_uri.starts_with("file://") {
819            // Extract the path from the file:// URI
820            if let Ok(url) = url::Url::parse(&self.original_uri)
821                && let Ok(base_path) = url.to_file_path()
822            {
823                // Use platform-appropriate path separator for display
824                // but object store paths always use forward slashes
825                let base_str = base_path.to_string_lossy();
826                return self.join_paths(&base_str, path_str);
827            }
828        }
829
830        // For local paths without file:// prefix, use the original URI as base
831        if self.base_path.is_empty() {
832            // If base_path is empty and not a file URI, try using original_uri as base
833            if self.original_uri.contains("://") {
834                // Fallback: return the path as-is
835                path_str.to_string()
836            } else {
837                self.join_paths(self.original_uri.trim_end_matches('/'), path_str)
838            }
839        } else {
840            let base = self.base_path.trim_end_matches('/');
841            self.join_paths(base, path_str)
842        }
843    }
844
845    /// Helper method to join paths using forward slashes (object store convention)
846    #[must_use]
847    fn join_paths(&self, base: &str, path: &str) -> String {
848        make_object_store_path(base, &[path])
849    }
850
851    /// Helper method to check if the original URI uses a remote object store scheme
852    #[must_use]
853    pub fn is_remote_uri(&self) -> bool {
854        self.original_uri.starts_with("s3://")
855            || self.original_uri.starts_with("gs://")
856            || self.original_uri.starts_with("gcs://")
857            || self.original_uri.starts_with("az://")
858            || self.original_uri.starts_with("abfs://")
859            || self.original_uri.starts_with("http://")
860            || self.original_uri.starts_with("https://")
861    }
862
863    /// Executes a query against the catalog to retrieve market data of a specific type.
864    ///
865    /// This is the primary method for querying data from the catalog. It registers the appropriate
866    /// object store with the DataFusion session, finds all relevant Parquet files, and executes
867    /// the query across them. The method supports filtering by instrument IDs, time ranges, and
868    /// custom SQL WHERE clauses.
869    ///
870    /// # Type Parameters
871    ///
872    /// - `T`: The data type to query, must implement required traits for deserialization and cataloging.
873    ///
874    /// # Parameters
875    ///
876    /// - `instrument_ids`: Optional list of instrument IDs to filter by. If `None`, queries all instruments.
877    /// - `start`: Optional start timestamp for filtering (inclusive). If `None`, queries from the beginning.
878    /// - `end`: Optional end timestamp for filtering (inclusive). If `None`, queries to the end.
879    /// - `where_clause`: Optional SQL WHERE clause for additional filtering (e.g., "price > 100").
880    ///
881    /// # Returns
882    ///
883    /// Returns a [`QueryResult`] containing the query execution context and data.
884    /// Use [`QueryResult::collect()`] to retrieve the actual data records.
885    ///
886    /// # Errors
887    ///
888    /// Returns an error if:
889    /// - Object store registration fails for remote URIs.
890    /// - File discovery fails.
891    /// - DataFusion query execution fails.
892    /// - Data deserialization fails.
893    ///
894    /// # Performance Notes
895    ///
896    /// - Files are automatically filtered by timestamp ranges before querying.
897    /// - DataFusion optimizes queries across multiple Parquet files.
898    /// - Use specific instrument IDs and time ranges to improve performance.
899    /// - WHERE clauses are pushed down to the Parquet reader when possible.
900    ///
901    /// # Examples
902    ///
903    /// ```rust,no_run
904    /// use nautilus_model::data::QuoteTick;
905    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
906    /// use nautilus_core::UnixNanos;
907    ///
908    /// let mut catalog = ParquetDataCatalog::new(/* ... */);
909    ///
910    /// // Query all quote data
911    /// let result = catalog.query::<QuoteTick>(None, None, None, None)?;
912    /// let quotes = result.collect();
913    ///
914    /// // Query specific instruments within a time range
915    /// let result = catalog.query::<QuoteTick>(
916    ///     Some(vec!["EURUSD".to_string(), "GBPUSD".to_string()]),
917    ///     Some(UnixNanos::from(1609459200000000000)),
918    ///     Some(UnixNanos::from(1609545600000000000)),
919    ///     None
920    /// )?;
921    ///
922    /// // Query with custom WHERE clause
923    /// let result = catalog.query::<QuoteTick>(
924    ///     Some(vec!["EURUSD".to_string()]),
925    ///     None,
926    ///     None,
927    ///     Some("bid_price > 1.2000")
928    /// )?;
929    /// # Ok::<(), anyhow::Error>(())
930    /// ```
931    pub fn query<T>(
932        &mut self,
933        instrument_ids: Option<Vec<String>>,
934        start: Option<UnixNanos>,
935        end: Option<UnixNanos>,
936        where_clause: Option<&str>,
937        files: Option<Vec<String>>,
938    ) -> anyhow::Result<QueryResult>
939    where
940        T: DecodeDataFromRecordBatch + CatalogPathPrefix,
941    {
942        // Register the object store with the session for remote URIs
943        if self.is_remote_uri() {
944            let url = url::Url::parse(&self.original_uri)?;
945            let host = url
946                .host_str()
947                .ok_or_else(|| anyhow::anyhow!("Remote URI missing host/bucket name"))?;
948            let base_url = url::Url::parse(&format!("{}://{}", url.scheme(), host))?;
949            self.session
950                .register_object_store(&base_url, self.object_store.clone());
951        }
952
953        let files_list = if let Some(files) = files {
954            files
955        } else {
956            self.query_files(T::path_prefix(), instrument_ids, start, end)?
957        };
958
959        for file_uri in &files_list {
960            // Extract identifier from file path and filename to create meaningful table names
961            let identifier = extract_identifier_from_path(file_uri);
962            let safe_sql_identifier = make_sql_safe_identifier(&identifier);
963            let safe_filename = extract_sql_safe_filename(file_uri);
964
965            // Create table name from path_prefix, identifier, and filename
966            let table_name = format!(
967                "{}_{}_{}",
968                T::path_prefix(),
969                safe_sql_identifier,
970                safe_filename
971            );
972            let query = build_query(&table_name, start, end, where_clause);
973
974            // Convert object store path to filesystem path for DataFusion
975            // Only apply reconstruction if the path is not already absolute
976            let resolved_path = if file_uri.starts_with('/') {
977                // Path is already absolute, use as-is
978                file_uri.clone()
979            } else {
980                // Path is relative, reconstruct full URI
981                self.reconstruct_full_uri(file_uri)
982            };
983            self.session
984                .add_file::<T>(&table_name, &resolved_path, Some(&query))?;
985        }
986
987        Ok(self.session.get_query_result())
988    }
989
990    /// Queries typed data from the catalog and returns results as a strongly-typed vector.
991    ///
992    /// This is a convenience method that wraps the generic `query` method and automatically
993    /// collects and converts the results into a vector of the specific data type. It handles
994    /// the type conversion from the generic [`Data`] enum to the concrete type `T`.
995    ///
996    /// # Type Parameters
997    ///
998    /// - `T`: The specific data type to query and return. Must implement required traits for
999    ///   deserialization, cataloging, and conversion from the [`Data`] enum.
1000    ///
1001    /// # Parameters
1002    ///
1003    /// - `instrument_ids`: Optional list of instrument IDs to filter by. If `None`, queries all instruments.
1004    ///   For exact matches, provide the full instrument ID. For bars, partial matches are supported.
1005    /// - `start`: Optional start timestamp for filtering (inclusive). If `None`, queries from the beginning.
1006    /// - `end`: Optional end timestamp for filtering (inclusive). If `None`, queries to the end.
1007    /// - `where_clause`: Optional SQL WHERE clause for additional filtering. Use standard SQL syntax
1008    ///   with column names matching the Parquet schema (e.g., "`bid_price` > 1.2000", "volume > 1000").
1009    ///
1010    /// # Returns
1011    ///
1012    /// Returns a vector of the specific data type `T`, sorted by timestamp. The vector will be
1013    /// empty if no data matches the query criteria.
1014    ///
1015    /// # Errors
1016    ///
1017    /// Returns an error if:
1018    /// - The underlying query execution fails.
1019    /// - Data type conversion fails.
1020    /// - Object store access fails.
1021    /// - Invalid WHERE clause syntax is provided.
1022    ///
1023    /// # Performance Considerations
1024    ///
1025    /// - Use specific instrument IDs and time ranges to minimize data scanning.
1026    /// - WHERE clauses are pushed down to Parquet readers when possible.
1027    /// - Results are automatically sorted by timestamp during collection.
1028    /// - Memory usage scales with the amount of data returned.
1029    ///
1030    /// # Examples
1031    ///
1032    /// ```rust,no_run
1033    /// use nautilus_model::data::{QuoteTick, TradeTick, Bar};
1034    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
1035    /// use nautilus_core::UnixNanos;
1036    ///
1037    /// let mut catalog = ParquetDataCatalog::new(/* ... */);
1038    ///
1039    /// // Query all quotes for a specific instrument
1040    /// let quotes: Vec<QuoteTick> = catalog.query_typed_data(
1041    ///     Some(vec!["EURUSD".to_string()]),
1042    ///     None,
1043    ///     None,
1044    ///     None
1045    /// )?;
1046    ///
1047    /// // Query trades within a specific time range
1048    /// let trades: Vec<TradeTick> = catalog.query_typed_data(
1049    ///     Some(vec!["BTCUSD".to_string()]),
1050    ///     Some(UnixNanos::from(1609459200000000000)),
1051    ///     Some(UnixNanos::from(1609545600000000000)),
1052    ///     None
1053    /// )?;
1054    ///
1055    /// // Query bars with volume filter
1056    /// let bars: Vec<Bar> = catalog.query_typed_data(
1057    ///     Some(vec!["AAPL".to_string()]),
1058    ///     None,
1059    ///     None,
1060    ///     Some("volume > 1000000")
1061    /// )?;
1062    ///
1063    /// // Query multiple instruments with price filter
1064    /// let quotes: Vec<QuoteTick> = catalog.query_typed_data(
1065    ///     Some(vec!["EURUSD".to_string(), "GBPUSD".to_string()]),
1066    ///     None,
1067    ///     None,
1068    ///     Some("bid_price > 1.2000 AND ask_price < 1.3000")
1069    /// )?;
1070    /// # Ok::<(), anyhow::Error>(())
1071    /// ```
1072    pub fn query_typed_data<T>(
1073        &mut self,
1074        instrument_ids: Option<Vec<String>>,
1075        start: Option<UnixNanos>,
1076        end: Option<UnixNanos>,
1077        where_clause: Option<&str>,
1078        files: Option<Vec<String>>,
1079    ) -> anyhow::Result<Vec<T>>
1080    where
1081        T: DecodeDataFromRecordBatch + CatalogPathPrefix + TryFrom<Data>,
1082    {
1083        let query_result = self.query::<T>(instrument_ids, start, end, where_clause, files)?;
1084        let all_data = query_result.collect();
1085
1086        // Convert Data enum variants to specific type T using to_variant
1087        Ok(to_variant::<T>(all_data))
1088    }
1089
1090    /// Queries all Parquet files for a specific data type and optional instrument IDs.
1091    ///
1092    /// This method finds all Parquet files that match the specified criteria and returns
1093    /// their full URIs. The files are filtered by data type, instrument IDs (if provided),
1094    /// and timestamp range (if provided).
1095    ///
1096    /// # Parameters
1097    ///
1098    /// - `data_cls`: The data type directory name (e.g., "quotes", "trades").
1099    /// - `instrument_ids`: Optional list of instrument IDs to filter by.
1100    /// - `start`: Optional start timestamp to filter files by their time range.
1101    /// - `end`: Optional end timestamp to filter files by their time range.
1102    ///
1103    /// # Returns
1104    ///
1105    /// Returns a vector of file URI strings that match the query criteria,
1106    /// or an error if the query fails.
1107    ///
1108    /// # Errors
1109    ///
1110    /// Returns an error if:
1111    /// - The directory path cannot be constructed.
1112    /// - Object store listing operations fail.
1113    /// - URI reconstruction fails.
1114    ///
1115    /// # Examples
1116    ///
1117    /// ```rust,no_run
1118    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
1119    /// use nautilus_core::UnixNanos;
1120    ///
1121    /// let catalog = ParquetDataCatalog::new(/* ... */);
1122    ///
1123    /// // Query all quote files
1124    /// let files = catalog.query_files("quotes", None, None, None)?;
1125    ///
1126    /// // Query trade files for specific instruments within a time range
1127    /// let files = catalog.query_files(
1128    ///     "trades",
1129    ///     Some(vec!["BTCUSD".to_string(), "ETHUSD".to_string()]),
1130    ///     Some(UnixNanos::from(1609459200000000000)),
1131    ///     Some(UnixNanos::from(1609545600000000000))
1132    /// )?;
1133    /// # Ok::<(), anyhow::Error>(())
1134    /// ```
1135    pub fn query_files(
1136        &self,
1137        data_cls: &str,
1138        instrument_ids: Option<Vec<String>>,
1139        start: Option<UnixNanos>,
1140        end: Option<UnixNanos>,
1141    ) -> anyhow::Result<Vec<String>> {
1142        let mut files = Vec::new();
1143
1144        let start_u64 = start.map(|s| s.as_u64());
1145        let end_u64 = end.map(|e| e.as_u64());
1146
1147        let base_dir = self.make_path(data_cls, None)?;
1148
1149        // Use recursive listing to match Python's glob behavior
1150        let list_result = self.execute_async(async {
1151            let prefix = ObjectPath::from(format!("{base_dir}/"));
1152            let mut stream = self.object_store.list(Some(&prefix));
1153            let mut objects = Vec::new();
1154            while let Some(object) = stream.next().await {
1155                objects.push(object?);
1156            }
1157            Ok::<Vec<_>, anyhow::Error>(objects)
1158        })?;
1159
1160        let mut file_paths: Vec<String> = list_result
1161            .into_iter()
1162            .filter_map(|object| {
1163                let path_str = object.location.to_string();
1164                if path_str.ends_with(".parquet") {
1165                    Some(path_str)
1166                } else {
1167                    None
1168                }
1169            })
1170            .collect();
1171
1172        // Apply identifier filtering if provided
1173        if let Some(identifiers) = instrument_ids {
1174            let safe_identifiers: Vec<String> = identifiers
1175                .iter()
1176                .map(|id| urisafe_instrument_id(id))
1177                .collect();
1178
1179            // Exact match by default for instrument_ids or bar_types
1180            let exact_match_file_paths: Vec<String> = file_paths
1181                .iter()
1182                .filter(|file_path| {
1183                    // Extract the directory name (second to last path component)
1184                    let path_parts: Vec<&str> = file_path.split('/').collect();
1185                    if path_parts.len() >= 2 {
1186                        let dir_name = path_parts[path_parts.len() - 2];
1187                        safe_identifiers.iter().any(|safe_id| safe_id == dir_name)
1188                    } else {
1189                        false
1190                    }
1191                })
1192                .cloned()
1193                .collect();
1194
1195            if exact_match_file_paths.is_empty() && data_cls == "bars" {
1196                // Partial match of instrument_ids in bar_types for bars
1197                file_paths.retain(|file_path| {
1198                    let path_parts: Vec<&str> = file_path.split('/').collect();
1199                    if path_parts.len() >= 2 {
1200                        let dir_name = path_parts[path_parts.len() - 2];
1201                        safe_identifiers
1202                            .iter()
1203                            .any(|safe_id| dir_name.starts_with(&format!("{safe_id}-")))
1204                    } else {
1205                        false
1206                    }
1207                });
1208            } else {
1209                file_paths = exact_match_file_paths;
1210            }
1211        }
1212
1213        // Apply timestamp filtering
1214        file_paths.retain(|file_path| query_intersects_filename(file_path, start_u64, end_u64));
1215
1216        // Convert to full URIs
1217        for file_path in file_paths {
1218            let full_uri = self.reconstruct_full_uri(&file_path);
1219            files.push(full_uri);
1220        }
1221
1222        Ok(files)
1223    }
1224
1225    /// Finds the missing time intervals for a specific data type and instrument ID.
1226    ///
1227    /// This method compares a requested time range against the existing data coverage
1228    /// and returns the gaps that need to be filled. This is useful for determining
1229    /// what data needs to be fetched or backfilled.
1230    ///
1231    /// # Parameters
1232    ///
1233    /// - `start`: Start timestamp of the requested range (Unix nanoseconds).
1234    /// - `end`: End timestamp of the requested range (Unix nanoseconds).
1235    /// - `data_cls`: The data type directory name (e.g., "quotes", "trades").
1236    /// - `instrument_id`: Optional instrument ID to target a specific instrument's data.
1237    ///
1238    /// # Returns
1239    ///
1240    /// Returns a vector of (start, end) tuples representing the missing intervals,
1241    /// or an error if the operation fails.
1242    ///
1243    /// # Errors
1244    ///
1245    /// Returns an error if:
1246    /// - The directory path cannot be constructed.
1247    /// - Interval retrieval fails.
1248    /// - Gap calculation fails.
1249    ///
1250    /// # Examples
1251    ///
1252    /// ```rust,no_run
1253    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
1254    ///
1255    /// let catalog = ParquetDataCatalog::new(/* ... */);
1256    ///
1257    /// // Find missing intervals for quote data
1258    /// let missing = catalog.get_missing_intervals_for_request(
1259    ///     1609459200000000000,  // start
1260    ///     1609545600000000000,  // end
1261    ///     "quotes",
1262    ///     Some("BTCUSD".to_string())
1263    /// )?;
1264    ///
1265    /// for (start, end) in missing {
1266    ///     println!("Missing data from {} to {}", start, end);
1267    /// }
1268    /// # Ok::<(), anyhow::Error>(())
1269    /// ```
1270    pub fn get_missing_intervals_for_request(
1271        &self,
1272        start: u64,
1273        end: u64,
1274        data_cls: &str,
1275        instrument_id: Option<String>,
1276    ) -> anyhow::Result<Vec<(u64, u64)>> {
1277        let intervals = self.get_intervals(data_cls, instrument_id)?;
1278
1279        Ok(query_interval_diff(start, end, &intervals))
1280    }
1281
1282    /// Gets the last (most recent) timestamp for a specific data type and instrument ID.
1283    ///
1284    /// This method finds the latest timestamp covered by existing data files for
1285    /// the specified data type and instrument. This is useful for determining
1286    /// the most recent data available or for incremental data updates.
1287    ///
1288    /// # Parameters
1289    ///
1290    /// - `data_cls`: The data type directory name (e.g., "quotes", "trades").
1291    /// - `instrument_id`: Optional instrument ID to target a specific instrument's data.
1292    ///
1293    /// # Returns
1294    ///
1295    /// Returns `Some(timestamp)` if data exists, `None` if no data is found,
1296    /// or an error if the operation fails.
1297    ///
1298    /// # Errors
1299    ///
1300    /// Returns an error if:
1301    /// - The directory path cannot be constructed.
1302    /// - Interval retrieval fails.
1303    ///
1304    /// # Examples
1305    ///
1306    /// ```rust,no_run
1307    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
1308    ///
1309    /// let catalog = ParquetDataCatalog::new(/* ... */);
1310    ///
1311    /// // Get the last timestamp for quote data
1312    /// if let Some(last_ts) = catalog.query_last_timestamp("quotes", Some("BTCUSD".to_string()))? {
1313    ///     println!("Last quote timestamp: {}", last_ts);
1314    /// } else {
1315    ///     println!("No quote data found");
1316    /// }
1317    /// # Ok::<(), anyhow::Error>(())
1318    /// ```
1319    pub fn query_last_timestamp(
1320        &self,
1321        data_cls: &str,
1322        instrument_id: Option<String>,
1323    ) -> anyhow::Result<Option<u64>> {
1324        let intervals = self.get_intervals(data_cls, instrument_id)?;
1325
1326        if intervals.is_empty() {
1327            return Ok(None);
1328        }
1329
1330        Ok(Some(intervals.last().unwrap().1))
1331    }
1332
1333    /// Gets the time intervals covered by Parquet files for a specific data type and instrument ID.
1334    ///
1335    /// This method returns all time intervals covered by existing data files for the
1336    /// specified data type and instrument. The intervals are sorted by start time and
1337    /// represent the complete data coverage available.
1338    ///
1339    /// # Parameters
1340    ///
1341    /// - `data_cls`: The data type directory name (e.g., "quotes", "trades").
1342    /// - `instrument_id`: Optional instrument ID to target a specific instrument's data.
1343    ///
1344    /// # Returns
1345    ///
1346    /// Returns a vector of (start, end) tuples representing the covered intervals,
1347    /// sorted by start time, or an error if the operation fails.
1348    ///
1349    /// # Errors
1350    ///
1351    /// Returns an error if:
1352    /// - The directory path cannot be constructed.
1353    /// - Directory listing fails.
1354    /// - Filename parsing fails.
1355    ///
1356    /// # Examples
1357    ///
1358    /// ```rust,no_run
1359    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
1360    ///
1361    /// let catalog = ParquetDataCatalog::new(/* ... */);
1362    ///
1363    /// // Get all intervals for quote data
1364    /// let intervals = catalog.get_intervals("quotes", Some("BTCUSD".to_string()))?;
1365    /// for (start, end) in intervals {
1366    ///     println!("Data available from {} to {}", start, end);
1367    /// }
1368    /// # Ok::<(), anyhow::Error>(())
1369    /// ```
1370    pub fn get_intervals(
1371        &self,
1372        data_cls: &str,
1373        instrument_id: Option<String>,
1374    ) -> anyhow::Result<Vec<(u64, u64)>> {
1375        let directory = self.make_path(data_cls, instrument_id)?;
1376
1377        self.get_directory_intervals(&directory)
1378    }
1379
1380    /// Gets the time intervals covered by Parquet files in a specific directory.
1381    ///
1382    /// This method scans a directory for Parquet files and extracts the timestamp ranges
1383    /// from their filenames. It's used internally by other methods to determine data coverage
1384    /// and is essential for interval-based operations like gap detection and consolidation.
1385    ///
1386    /// # Parameters
1387    ///
1388    /// - `directory`: The directory path to scan for Parquet files.
1389    ///
1390    /// # Returns
1391    ///
1392    /// Returns a vector of (start, end) tuples representing the time intervals covered
1393    /// by files in the directory, sorted by start timestamp. Returns an empty vector
1394    /// if the directory doesn't exist or contains no valid Parquet files.
1395    ///
1396    /// # Errors
1397    ///
1398    /// Returns an error if:
1399    /// - Object store listing operations fail.
1400    /// - Directory access is denied.
1401    ///
1402    /// # Notes
1403    ///
1404    /// - Only files with valid timestamp-based filenames are included.
1405    /// - Files with unparsable names are silently ignored.
1406    /// - The method works with both local and remote object stores.
1407    /// - Results are automatically sorted by start timestamp.
1408    ///
1409    /// # Examples
1410    ///
1411    /// ```rust,no_run
1412    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
1413    ///
1414    /// let catalog = ParquetDataCatalog::new(/* ... */);
1415    /// let intervals = catalog.get_directory_intervals("data/quotes/EURUSD")?;
1416    ///
1417    /// for (start, end) in intervals {
1418    ///     println!("File covers {} to {}", start, end);
1419    /// }
1420    /// # Ok::<(), anyhow::Error>(())
1421    /// ```
1422    pub fn get_directory_intervals(&self, directory: &str) -> anyhow::Result<Vec<(u64, u64)>> {
1423        let mut intervals = Vec::new();
1424
1425        // Use object store for all operations
1426        let list_result = self.execute_async(async {
1427            let path = object_store::path::Path::from(directory);
1428            Ok(self
1429                .object_store
1430                .list(Some(&path))
1431                .collect::<Vec<_>>()
1432                .await)
1433        })?;
1434
1435        for result in list_result {
1436            match result {
1437                Ok(object) => {
1438                    let path_str = object.location.to_string();
1439                    if path_str.ends_with(".parquet")
1440                        && let Some(interval) = parse_filename_timestamps(&path_str)
1441                    {
1442                        intervals.push(interval);
1443                    }
1444                }
1445                Err(_) => {
1446                    // Directory doesn't exist or is empty, which is fine
1447                    break;
1448                }
1449            }
1450        }
1451
1452        intervals.sort_by_key(|&(start, _)| start);
1453
1454        Ok(intervals)
1455    }
1456
1457    /// Constructs a directory path for storing data of a specific type and instrument.
1458    ///
1459    /// This method builds the hierarchical directory structure used by the catalog to organize
1460    /// data by type and instrument. The path follows the pattern: `{base_path}/data/{type_name}/{instrument_id}`.
1461    /// Instrument IDs are automatically converted to URI-safe format by removing forward slashes.
1462    ///
1463    /// # Parameters
1464    ///
1465    /// - `type_name`: The data type directory name (e.g., "quotes", "trades", "bars").
1466    /// - `instrument_id`: Optional instrument ID. If provided, creates a subdirectory for the instrument.
1467    ///   If `None`, returns the path to the data type directory.
1468    ///
1469    /// # Returns
1470    ///
1471    /// Returns the constructed directory path as a string, or an error if path construction fails.
1472    ///
1473    /// # Errors
1474    ///
1475    /// Returns an error if:
1476    /// - The instrument ID contains invalid characters that cannot be made URI-safe.
1477    /// - Path construction fails due to system limitations.
1478    ///
1479    /// # Path Structure
1480    ///
1481    /// - Without instrument ID: `{base_path}/data/{type_name}`.
1482    /// - With instrument ID: `{base_path}/data/{type_name}/{safe_instrument_id}`.
1483    /// - If `base_path` is empty: `data/{type_name}[/{safe_instrument_id}]`.
1484    ///
1485    /// # Examples
1486    ///
1487    /// ```rust,no_run
1488    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
1489    ///
1490    /// let catalog = ParquetDataCatalog::new(/* ... */);
1491    ///
1492    /// // Path for all quote data
1493    /// let quotes_path = catalog.make_path("quotes", None)?;
1494    /// // Returns: "/base/path/data/quotes"
1495    ///
1496    /// // Path for specific instrument quotes
1497    /// let eurusd_quotes = catalog.make_path("quotes", Some("EUR/USD".to_string()))?;
1498    /// // Returns: "/base/path/data/quotes/EURUSD" (slash removed)
1499    ///
1500    /// // Path for bar data with complex instrument ID
1501    /// let bars_path = catalog.make_path("bars", Some("BTC/USD-1H".to_string()))?;
1502    /// // Returns: "/base/path/data/bars/BTCUSD-1H"
1503    /// # Ok::<(), anyhow::Error>(())
1504    /// ```
1505    pub fn make_path(
1506        &self,
1507        type_name: &str,
1508        instrument_id: Option<String>,
1509    ) -> anyhow::Result<String> {
1510        let mut components = vec!["data".to_string(), type_name.to_string()];
1511
1512        if let Some(id) = instrument_id {
1513            let safe_id = urisafe_instrument_id(&id);
1514            components.push(safe_id);
1515        }
1516
1517        let path = make_object_store_path_owned(&self.base_path, components);
1518        Ok(path)
1519    }
1520
1521    /// Helper method to rename a parquet file by moving it via object store operations
1522    fn rename_parquet_file(
1523        &self,
1524        directory: &str,
1525        old_start: u64,
1526        old_end: u64,
1527        new_start: u64,
1528        new_end: u64,
1529    ) -> anyhow::Result<()> {
1530        let old_filename =
1531            timestamps_to_filename(UnixNanos::from(old_start), UnixNanos::from(old_end));
1532        let old_path = format!("{directory}/{old_filename}");
1533        let old_object_path = self.to_object_path(&old_path);
1534
1535        let new_filename =
1536            timestamps_to_filename(UnixNanos::from(new_start), UnixNanos::from(new_end));
1537        let new_path = format!("{directory}/{new_filename}");
1538        let new_object_path = self.to_object_path(&new_path);
1539
1540        self.move_file(&old_object_path, &new_object_path)
1541    }
1542
1543    /// Converts a catalog path string to an [`ObjectPath`] for object store operations.
1544    ///
1545    /// This method handles the conversion between catalog-relative paths and object store paths,
1546    /// taking into account the catalog's base path configuration. It automatically strips the
1547    /// base path prefix when present to create the correct object store path.
1548    ///
1549    /// # Parameters
1550    ///
1551    /// - `path`: The catalog path string to convert. Can be absolute or relative.
1552    ///
1553    /// # Returns
1554    ///
1555    /// Returns an [`ObjectPath`] suitable for use with object store operations.
1556    ///
1557    /// # Path Handling
1558    ///
1559    /// - If `base_path` is empty, the path is used as-is.
1560    /// - If `base_path` is set, it's stripped from the path if present.
1561    /// - Trailing slashes and backslashes are automatically handled.
1562    /// - The resulting path is relative to the object store root.
1563    /// - All paths are normalized to use forward slashes (object store convention).
1564    ///
1565    /// # Examples
1566    ///
1567    /// ```rust,no_run
1568    /// use nautilus_persistence::backend::catalog::ParquetDataCatalog;
1569    ///
1570    /// let catalog = ParquetDataCatalog::new(/* ... */);
1571    ///
1572    /// // Convert a full catalog path
1573    /// let object_path = catalog.to_object_path("/base/data/quotes/file.parquet");
1574    /// // Returns: ObjectPath("data/quotes/file.parquet") if base_path is "/base"
1575    ///
1576    /// // Convert a relative path
1577    /// let object_path = catalog.to_object_path("data/trades/file.parquet");
1578    /// // Returns: ObjectPath("data/trades/file.parquet")
1579    /// ```
1580    #[must_use]
1581    pub fn to_object_path(&self, path: &str) -> ObjectPath {
1582        // Normalize path separators to forward slashes for object store
1583        let normalized_path = path.replace('\\', "/");
1584
1585        if self.base_path.is_empty() {
1586            return ObjectPath::from(normalized_path);
1587        }
1588
1589        // Normalize base path separators as well
1590        let normalized_base = self.base_path.replace('\\', "/");
1591        let base = normalized_base.trim_end_matches('/');
1592
1593        // Remove the catalog base prefix if present
1594        let without_base = normalized_path
1595            .strip_prefix(&format!("{base}/"))
1596            .or_else(|| normalized_path.strip_prefix(base))
1597            .unwrap_or(&normalized_path);
1598
1599        ObjectPath::from(without_base)
1600    }
1601
1602    /// Helper method to move a file using object store rename operation
1603    pub fn move_file(&self, old_path: &ObjectPath, new_path: &ObjectPath) -> anyhow::Result<()> {
1604        self.execute_async(async {
1605            self.object_store
1606                .rename(old_path, new_path)
1607                .await
1608                .map_err(anyhow::Error::from)
1609        })
1610    }
1611
1612    /// Helper method to execute async operations with a runtime
1613    pub fn execute_async<F, R>(&self, future: F) -> anyhow::Result<R>
1614    where
1615        F: std::future::Future<Output = anyhow::Result<R>>,
1616    {
1617        let rt = get_runtime();
1618        rt.block_on(future)
1619    }
1620}
1621
1622/// Trait for providing catalog path prefixes for different data types.
1623///
1624/// This trait enables type-safe organization of data within the catalog by providing
1625/// a standardized way to determine the directory structure for each data type.
1626/// Each data type maps to a specific subdirectory within the catalog's data folder.
1627///
1628/// # Implementation
1629///
1630/// Types implementing this trait should return a static string that represents
1631/// the directory name where data of that type should be stored.
1632///
1633/// # Examples
1634///
1635/// ```rust
1636/// use nautilus_persistence::backend::catalog::CatalogPathPrefix;
1637/// use nautilus_model::data::QuoteTick;
1638///
1639/// assert_eq!(QuoteTick::path_prefix(), "quotes");
1640/// ```
1641pub trait CatalogPathPrefix {
1642    /// Returns the path prefix (directory name) for this data type.
1643    ///
1644    /// # Returns
1645    ///
1646    /// A static string representing the directory name where this data type is stored.
1647    fn path_prefix() -> &'static str;
1648}
1649
1650/// Macro for implementing [`CatalogPathPrefix`] for data types.
1651///
1652/// This macro provides a convenient way to implement the trait for multiple types
1653/// with their corresponding path prefixes.
1654///
1655/// # Parameters
1656///
1657/// - `$type`: The data type to implement the trait for.
1658/// - `$path`: The path prefix string for that type.
1659macro_rules! impl_catalog_path_prefix {
1660    ($type:ty, $path:expr) => {
1661        impl CatalogPathPrefix for $type {
1662            fn path_prefix() -> &'static str {
1663                $path
1664            }
1665        }
1666    };
1667}
1668
1669// Standard implementations for financial data types
1670impl_catalog_path_prefix!(QuoteTick, "quotes");
1671impl_catalog_path_prefix!(TradeTick, "trades");
1672impl_catalog_path_prefix!(OrderBookDelta, "order_book_deltas");
1673impl_catalog_path_prefix!(OrderBookDepth10, "order_book_depths");
1674impl_catalog_path_prefix!(Bar, "bars");
1675impl_catalog_path_prefix!(IndexPriceUpdate, "index_prices");
1676impl_catalog_path_prefix!(MarkPriceUpdate, "mark_prices");
1677impl_catalog_path_prefix!(InstrumentClose, "instrument_closes");
1678
1679/// Converts timestamps to a filename using ISO 8601 format.
1680///
1681/// This function converts two Unix nanosecond timestamps to a filename that uses
1682/// ISO 8601 format with filesystem-safe characters. The format matches the Python
1683/// implementation for consistency.
1684///
1685/// # Parameters
1686///
1687/// - `timestamp_1`: First timestamp in Unix nanoseconds.
1688/// - `timestamp_2`: Second timestamp in Unix nanoseconds.
1689///
1690/// # Returns
1691///
1692/// Returns a filename string in the format: "`iso_timestamp_1_iso_timestamp_2.parquet`".
1693///
1694/// # Examples
1695///
1696/// ```rust
1697/// # use nautilus_persistence::backend::catalog::timestamps_to_filename;
1698/// # use nautilus_core::UnixNanos;
1699/// let filename = timestamps_to_filename(
1700///     UnixNanos::from(1609459200000000000),
1701///     UnixNanos::from(1609545600000000000)
1702/// );
1703/// // Returns something like: "2021-01-01T00-00-00-000000000Z_2021-01-02T00-00-00-000000000Z.parquet"
1704/// ```
1705#[must_use]
1706pub fn timestamps_to_filename(timestamp_1: UnixNanos, timestamp_2: UnixNanos) -> String {
1707    let datetime_1 = iso_timestamp_to_file_timestamp(&unix_nanos_to_iso8601(timestamp_1));
1708    let datetime_2 = iso_timestamp_to_file_timestamp(&unix_nanos_to_iso8601(timestamp_2));
1709
1710    format!("{datetime_1}_{datetime_2}.parquet")
1711}
1712
1713/// Converts an ISO 8601 timestamp to a filesystem-safe format.
1714///
1715/// This function replaces colons and dots with hyphens to make the timestamp
1716/// safe for use in filenames across different filesystems.
1717///
1718/// # Parameters
1719///
1720/// - `iso_timestamp`: ISO 8601 timestamp string (e.g., "2023-10-26T07:30:50.123456789Z").
1721///
1722/// # Returns
1723///
1724/// Returns a filesystem-safe timestamp string (e.g., "2023-10-26T07-30-50-123456789Z").
1725///
1726/// # Examples
1727///
1728/// ```rust
1729/// # use nautilus_persistence::backend::catalog::iso_timestamp_to_file_timestamp;
1730/// let safe_timestamp = iso_timestamp_to_file_timestamp("2023-10-26T07:30:50.123456789Z");
1731/// assert_eq!(safe_timestamp, "2023-10-26T07-30-50-123456789Z");
1732/// ```
1733fn iso_timestamp_to_file_timestamp(iso_timestamp: &str) -> String {
1734    iso_timestamp.replace([':', '.'], "-")
1735}
1736
1737/// Converts a filesystem-safe timestamp back to ISO 8601 format.
1738///
1739/// This function reverses the transformation done by `iso_timestamp_to_file_timestamp`,
1740/// converting filesystem-safe timestamps back to standard ISO 8601 format.
1741///
1742/// # Parameters
1743///
1744/// - `file_timestamp`: Filesystem-safe timestamp string (e.g., "2023-10-26T07-30-50-123456789Z").
1745///
1746/// # Returns
1747///
1748/// Returns an ISO 8601 timestamp string (e.g., "2023-10-26T07:30:50.123456789Z").
1749///
1750/// # Examples
1751///
1752/// ```rust
1753/// # use nautilus_persistence::backend::catalog::file_timestamp_to_iso_timestamp;
1754/// let iso_timestamp = file_timestamp_to_iso_timestamp("2023-10-26T07-30-50-123456789Z");
1755/// assert_eq!(iso_timestamp, "2023-10-26T07:30:50.123456789Z");
1756/// ```
1757fn file_timestamp_to_iso_timestamp(file_timestamp: &str) -> String {
1758    let (date_part, time_part) = file_timestamp
1759        .split_once('T')
1760        .unwrap_or((file_timestamp, ""));
1761    let time_part = time_part.strip_suffix('Z').unwrap_or(time_part);
1762
1763    // Find the last hyphen to separate nanoseconds
1764    if let Some(last_hyphen_idx) = time_part.rfind('-') {
1765        let time_with_dot_for_nanos = format!(
1766            "{}.{}",
1767            &time_part[..last_hyphen_idx],
1768            &time_part[last_hyphen_idx + 1..]
1769        );
1770        let final_time_part = time_with_dot_for_nanos.replace('-', ":");
1771        format!("{date_part}T{final_time_part}Z")
1772    } else {
1773        // Fallback if no nanoseconds part found
1774        let final_time_part = time_part.replace('-', ":");
1775        format!("{date_part}T{final_time_part}Z")
1776    }
1777}
1778
1779/// Converts an ISO 8601 timestamp string to Unix nanoseconds.
1780///
1781/// This function parses an ISO 8601 timestamp and converts it to Unix nanoseconds.
1782/// It's used to convert parsed timestamps back to the internal representation.
1783///
1784/// # Parameters
1785///
1786/// - `iso_timestamp`: ISO 8601 timestamp string (e.g., "2023-10-26T07:30:50.123456789Z").
1787///
1788/// # Returns
1789///
1790/// Returns `Ok(u64)` with the Unix nanoseconds timestamp, or an error if parsing fails.
1791///
1792/// # Examples
1793///
1794/// ```rust
1795/// # use nautilus_persistence::backend::catalog::iso_to_unix_nanos;
1796/// let nanos = iso_to_unix_nanos("2021-01-01T00:00:00.000000000Z").unwrap();
1797/// assert_eq!(nanos, 1609459200000000000);
1798/// ```
1799fn iso_to_unix_nanos(iso_timestamp: &str) -> anyhow::Result<u64> {
1800    Ok(iso8601_to_unix_nanos(iso_timestamp.to_string())?.into())
1801}
1802
1803/// Converts an instrument ID to a URI-safe format by removing forward slashes.
1804///
1805/// Some instrument IDs contain forward slashes (e.g., "BTC/USD") which are not
1806/// suitable for use in file paths. This function removes these characters to
1807/// create a safe directory name.
1808///
1809/// # Parameters
1810///
1811/// - `instrument_id`: The original instrument ID string.
1812///
1813/// # Returns
1814///
1815/// A URI-safe version of the instrument ID with forward slashes removed.
1816///
1817/// # Examples
1818///
1819/// ```rust
1820/// # use nautilus_persistence::backend::catalog::urisafe_instrument_id;
1821/// assert_eq!(urisafe_instrument_id("BTC/USD"), "BTCUSD");
1822/// assert_eq!(urisafe_instrument_id("EUR-USD"), "EUR-USD");
1823/// ```
1824fn urisafe_instrument_id(instrument_id: &str) -> String {
1825    instrument_id.replace('/', "")
1826}
1827
1828/// Extracts the identifier from a file path.
1829///
1830/// The identifier is typically the second-to-last path component (directory name).
1831/// For example, from "`data/quote_tick/EURUSD/file.parquet`", extracts "EURUSD".
1832#[must_use]
1833pub fn extract_identifier_from_path(file_path: &str) -> String {
1834    let path_parts: Vec<&str> = file_path.split('/').collect();
1835    if path_parts.len() >= 2 {
1836        path_parts[path_parts.len() - 2].to_string()
1837    } else {
1838        "unknown".to_string()
1839    }
1840}
1841
1842/// Makes an identifier safe for use in SQL table names.
1843///
1844/// Removes forward slashes, replaces dots, hyphens, and spaces with underscores, and converts to lowercase.
1845#[must_use]
1846pub fn make_sql_safe_identifier(identifier: &str) -> String {
1847    urisafe_instrument_id(identifier)
1848        .replace(['.', '-', ' ', '%'], "_")
1849        .to_lowercase()
1850}
1851
1852/// Extracts the filename from a file path and makes it SQL-safe.
1853///
1854/// For example, from "data/quote_tick/EURUSD/2021-01-01T00-00-00-000000000Z_2021-01-02T00-00-00-000000000Z.parquet",
1855/// extracts "`2021_01_01t00_00_00_000000000z_2021_01_02t00_00_00_000000000z`".
1856#[must_use]
1857pub fn extract_sql_safe_filename(file_path: &str) -> String {
1858    if file_path.is_empty() {
1859        return "unknown_file".to_string();
1860    }
1861
1862    let filename = file_path.split('/').next_back().unwrap_or("unknown_file");
1863
1864    // Remove .parquet extension
1865    let name_without_ext = if let Some(dot_pos) = filename.rfind(".parquet") {
1866        &filename[..dot_pos]
1867    } else {
1868        filename
1869    };
1870
1871    // Remove characters that can pose problems: hyphens, colons, etc.
1872    name_without_ext
1873        .replace(['-', ':', '.'], "_")
1874        .to_lowercase()
1875}
1876
1877/// Creates a platform-appropriate local path using `PathBuf`.
1878///
1879/// This function constructs file system paths using the platform's native path separators.
1880/// Use this for local file operations that need to work with the actual file system.
1881///
1882/// # Arguments
1883///
1884/// * `base_path` - The base directory path
1885/// * `components` - Path components to join
1886///
1887/// # Returns
1888///
1889/// A `PathBuf` with platform-appropriate separators
1890///
1891/// # Examples
1892///
1893/// ```rust
1894/// # use nautilus_persistence::backend::catalog::make_local_path;
1895/// let path = make_local_path("/base", &["data", "quotes", "EURUSD"]);
1896/// // On Unix: "/base/data/quotes/EURUSD"
1897/// // On Windows: "\base\data\quotes\EURUSD"
1898/// ```
1899pub fn make_local_path<P: AsRef<Path>>(base_path: P, components: &[&str]) -> PathBuf {
1900    let mut path = PathBuf::from(base_path.as_ref());
1901    for component in components {
1902        path.push(component);
1903    }
1904    path
1905}
1906
1907/// Creates an object store path using forward slashes.
1908///
1909/// Object stores (S3, GCS, etc.) always expect forward slashes regardless of platform.
1910/// Use this when creating paths for object store operations.
1911///
1912/// # Arguments
1913///
1914/// * `base_path` - The base path (can be empty)
1915/// * `components` - Path components to join
1916///
1917/// # Returns
1918///
1919/// A string path with forward slash separators
1920///
1921/// # Examples
1922///
1923/// ```rust
1924/// # use nautilus_persistence::backend::catalog::make_object_store_path;
1925/// let path = make_object_store_path("base", &["data", "quotes", "EURUSD"]);
1926/// assert_eq!(path, "base/data/quotes/EURUSD");
1927/// ```
1928#[must_use]
1929pub fn make_object_store_path(base_path: &str, components: &[&str]) -> String {
1930    let mut parts = Vec::new();
1931
1932    if !base_path.is_empty() {
1933        let normalized_base = base_path
1934            .replace('\\', "/")
1935            .trim_end_matches('/')
1936            .to_string();
1937        if !normalized_base.is_empty() {
1938            parts.push(normalized_base);
1939        }
1940    }
1941
1942    for component in components {
1943        let normalized_component = component
1944            .replace('\\', "/")
1945            .trim_start_matches('/')
1946            .trim_end_matches('/')
1947            .to_string();
1948        if !normalized_component.is_empty() {
1949            parts.push(normalized_component);
1950        }
1951    }
1952
1953    parts.join("/")
1954}
1955
1956/// Creates an object store path using forward slashes with owned strings.
1957///
1958/// This variant accepts owned strings to avoid lifetime issues.
1959///
1960/// # Arguments
1961///
1962/// * `base_path` - The base path (can be empty)
1963/// * `components` - Path components to join (owned strings)
1964///
1965/// # Returns
1966///
1967/// A string path with forward slash separators
1968#[must_use]
1969pub fn make_object_store_path_owned(base_path: &str, components: Vec<String>) -> String {
1970    let mut parts = Vec::new();
1971
1972    if !base_path.is_empty() {
1973        let normalized_base = base_path
1974            .replace('\\', "/")
1975            .trim_end_matches('/')
1976            .to_string();
1977        if !normalized_base.is_empty() {
1978            parts.push(normalized_base);
1979        }
1980    }
1981
1982    for component in components {
1983        let normalized_component = component
1984            .replace('\\', "/")
1985            .trim_start_matches('/')
1986            .trim_end_matches('/')
1987            .to_string();
1988        if !normalized_component.is_empty() {
1989            parts.push(normalized_component);
1990        }
1991    }
1992
1993    parts.join("/")
1994}
1995
1996/// Converts a local `PathBuf` to an object store path string.
1997///
1998/// This function normalizes a local file system path to the forward-slash format
1999/// expected by object stores, handling platform differences.
2000///
2001/// # Arguments
2002///
2003/// * `local_path` - The local `PathBuf` to convert
2004///
2005/// # Returns
2006///
2007/// A string with forward slash separators suitable for object store operations
2008///
2009/// # Examples
2010///
2011/// ```rust
2012/// # use std::path::PathBuf;
2013/// # use nautilus_persistence::backend::catalog::local_to_object_store_path;
2014/// let local_path = PathBuf::from("data").join("quotes").join("EURUSD");
2015/// let object_path = local_to_object_store_path(&local_path);
2016/// assert_eq!(object_path, "data/quotes/EURUSD");
2017/// ```
2018#[must_use]
2019pub fn local_to_object_store_path(local_path: &Path) -> String {
2020    local_path.to_string_lossy().replace('\\', "/")
2021}
2022
2023/// Extracts path components using platform-appropriate path parsing.
2024///
2025/// This function safely parses a path into its components, handling both
2026/// local file system paths and object store paths correctly.
2027///
2028/// # Arguments
2029///
2030/// * `path_str` - The path string to parse
2031///
2032/// # Returns
2033///
2034/// A vector of path components
2035///
2036/// # Examples
2037///
2038/// ```rust
2039/// # use nautilus_persistence::backend::catalog::extract_path_components;
2040/// let components = extract_path_components("data/quotes/EURUSD");
2041/// assert_eq!(components, vec!["data", "quotes", "EURUSD"]);
2042///
2043/// // Works with both separators
2044/// let components = extract_path_components("data\\quotes\\EURUSD");
2045/// assert_eq!(components, vec!["data", "quotes", "EURUSD"]);
2046/// ```
2047#[must_use]
2048pub fn extract_path_components(path_str: &str) -> Vec<String> {
2049    // Normalize separators and split
2050    let normalized = path_str.replace('\\', "/");
2051    normalized
2052        .split('/')
2053        .filter(|s| !s.is_empty())
2054        .map(ToString::to_string)
2055        .collect()
2056}
2057
2058/// Checks if a filename's timestamp range intersects with a query interval.
2059///
2060/// This function determines whether a Parquet file (identified by its timestamp-based
2061/// filename) contains data that falls within the specified query time range.
2062///
2063/// # Parameters
2064///
2065/// - `filename`: The filename to check (format: "`iso_timestamp_1_iso_timestamp_2.parquet`").
2066/// - `start`: Optional start timestamp for the query range.
2067/// - `end`: Optional end timestamp for the query range.
2068///
2069/// # Returns
2070///
2071/// Returns `true` if the file's time range intersects with the query range,
2072/// `false` otherwise. Returns `true` if the filename cannot be parsed.
2073///
2074/// # Examples
2075///
2076/// ```rust
2077/// # use nautilus_persistence::backend::catalog::query_intersects_filename;
2078/// // Example with ISO format filenames
2079/// assert!(query_intersects_filename(
2080///     "2021-01-01T00-00-00-000000000Z_2021-01-02T00-00-00-000000000Z.parquet",
2081///     Some(1609459200000000000),
2082///     Some(1609545600000000000)
2083/// ));
2084/// ```
2085fn query_intersects_filename(filename: &str, start: Option<u64>, end: Option<u64>) -> bool {
2086    if let Some((file_start, file_end)) = parse_filename_timestamps(filename) {
2087        (start.is_none() || start.unwrap() <= file_end)
2088            && (end.is_none() || file_start <= end.unwrap())
2089    } else {
2090        true
2091    }
2092}
2093
2094/// Parses timestamps from a Parquet filename.
2095///
2096/// Extracts the start and end timestamps from filenames that follow the ISO 8601 format:
2097/// "`iso_timestamp_1_iso_timestamp_2.parquet`" (e.g., "2021-01-01T00-00-00-000000000Z_2021-01-02T00-00-00-000000000Z.parquet")
2098///
2099/// # Parameters
2100///
2101/// - `filename`: The filename to parse (can be a full path).
2102///
2103/// # Returns
2104///
2105/// Returns `Some((start_ts, end_ts))` if the filename matches the expected format,
2106/// `None` otherwise.
2107///
2108/// # Examples
2109///
2110/// ```rust
2111/// # use nautilus_persistence::backend::catalog::parse_filename_timestamps;
2112/// assert!(parse_filename_timestamps("2021-01-01T00-00-00-000000000Z_2021-01-02T00-00-00-000000000Z.parquet").is_some());
2113/// assert_eq!(parse_filename_timestamps("invalid.parquet"), None);
2114/// ```
2115#[must_use]
2116pub fn parse_filename_timestamps(filename: &str) -> Option<(u64, u64)> {
2117    let path = Path::new(filename);
2118    let base_name = path.file_name()?.to_str()?;
2119    let base_filename = base_name.strip_suffix(".parquet")?;
2120    let (first_part, second_part) = base_filename.split_once('_')?;
2121
2122    let first_iso = file_timestamp_to_iso_timestamp(first_part);
2123    let second_iso = file_timestamp_to_iso_timestamp(second_part);
2124
2125    let first_ts = iso_to_unix_nanos(&first_iso).ok()?;
2126    let second_ts = iso_to_unix_nanos(&second_iso).ok()?;
2127
2128    Some((first_ts, second_ts))
2129}
2130
2131/// Checks if a list of closed integer intervals are all mutually disjoint.
2132///
2133/// Two intervals are disjoint if they do not overlap. This function validates that
2134/// all intervals in the list are non-overlapping, which is a requirement for
2135/// maintaining data integrity in the catalog.
2136///
2137/// # Parameters
2138///
2139/// - `intervals`: A slice of timestamp intervals as (start, end) tuples.
2140///
2141/// # Returns
2142///
2143/// Returns `true` if all intervals are disjoint, `false` if any overlap is found.
2144/// Returns `true` for empty lists or lists with a single interval.
2145///
2146/// # Examples
2147///
2148/// ```rust
2149/// # use nautilus_persistence::backend::catalog::are_intervals_disjoint;
2150/// // Disjoint intervals
2151/// assert!(are_intervals_disjoint(&[(1, 5), (10, 15), (20, 25)]));
2152///
2153/// // Overlapping intervals
2154/// assert!(!are_intervals_disjoint(&[(1, 10), (5, 15)]));
2155/// ```
2156#[must_use]
2157pub fn are_intervals_disjoint(intervals: &[(u64, u64)]) -> bool {
2158    let n = intervals.len();
2159
2160    if n <= 1 {
2161        return true;
2162    }
2163
2164    let mut sorted_intervals: Vec<(u64, u64)> = intervals.to_vec();
2165    sorted_intervals.sort_by_key(|&(start, _)| start);
2166
2167    for i in 0..(n - 1) {
2168        let (_, end1) = sorted_intervals[i];
2169        let (start2, _) = sorted_intervals[i + 1];
2170
2171        if end1 >= start2 {
2172            return false;
2173        }
2174    }
2175
2176    true
2177}
2178
2179/// Checks if intervals are contiguous (adjacent with no gaps).
2180///
2181/// Intervals are contiguous if, when sorted by start time, each interval's start
2182/// timestamp is exactly one more than the previous interval's end timestamp.
2183/// This ensures complete coverage of a time range with no gaps.
2184///
2185/// # Parameters
2186///
2187/// - `intervals`: A slice of timestamp intervals as (start, end) tuples.
2188///
2189/// # Returns
2190///
2191/// Returns `true` if all intervals are contiguous, `false` if any gaps are found.
2192/// Returns `true` for empty lists or lists with a single interval.
2193///
2194/// # Examples
2195///
2196/// ```rust
2197/// # use nautilus_persistence::backend::catalog::are_intervals_contiguous;
2198/// // Contiguous intervals
2199/// assert!(are_intervals_contiguous(&[(1, 5), (6, 10), (11, 15)]));
2200///
2201/// // Non-contiguous intervals (gap between 5 and 8)
2202/// assert!(!are_intervals_contiguous(&[(1, 5), (8, 10)]));
2203/// ```
2204#[must_use]
2205pub fn are_intervals_contiguous(intervals: &[(u64, u64)]) -> bool {
2206    let n = intervals.len();
2207    if n <= 1 {
2208        return true;
2209    }
2210
2211    let mut sorted_intervals: Vec<(u64, u64)> = intervals.to_vec();
2212    sorted_intervals.sort_by_key(|&(start, _)| start);
2213
2214    for i in 0..(n - 1) {
2215        let (_, end1) = sorted_intervals[i];
2216        let (start2, _) = sorted_intervals[i + 1];
2217
2218        if end1 + 1 != start2 {
2219            return false;
2220        }
2221    }
2222
2223    true
2224}
2225
2226/// Finds the parts of a query interval that are not covered by existing data intervals.
2227///
2228/// This function calculates the "gaps" in data coverage by comparing a requested
2229/// time range against the intervals covered by existing data files. It's used to
2230/// determine what data needs to be fetched or backfilled.
2231///
2232/// # Parameters
2233///
2234/// - `start`: Start timestamp of the query interval (inclusive).
2235/// - `end`: End timestamp of the query interval (inclusive).
2236/// - `closed_intervals`: Existing data intervals as (start, end) tuples.
2237///
2238/// # Returns
2239///
2240/// Returns a vector of (start, end) tuples representing the gaps in coverage.
2241/// Returns an empty vector if the query range is invalid or fully covered.
2242///
2243/// # Examples
2244///
2245/// ```rust
2246/// # use nautilus_persistence::backend::catalog::query_interval_diff;
2247/// // Query 1-100, have data for 10-30 and 60-80
2248/// let gaps = query_interval_diff(1, 100, &[(10, 30), (60, 80)]);
2249/// assert_eq!(gaps, vec![(1, 9), (31, 59), (81, 100)]);
2250/// ```
2251fn query_interval_diff(start: u64, end: u64, closed_intervals: &[(u64, u64)]) -> Vec<(u64, u64)> {
2252    if start > end {
2253        return Vec::new();
2254    }
2255
2256    let interval_set = get_interval_set(closed_intervals);
2257    let query_range = (Bound::Included(start), Bound::Included(end));
2258    let query_diff = interval_set.get_interval_difference(&query_range);
2259    let mut result: Vec<(u64, u64)> = Vec::new();
2260
2261    for interval in query_diff {
2262        if let Some(tuple) = interval_to_tuple(interval, start, end) {
2263            result.push(tuple);
2264        }
2265    }
2266
2267    result
2268}
2269
2270/// Creates an interval tree from closed integer intervals.
2271///
2272/// This function converts closed intervals [a, b] into half-open intervals [a, b+1)
2273/// for use with the interval tree data structure, which is used for efficient
2274/// interval operations and gap detection.
2275///
2276/// # Parameters
2277///
2278/// - `intervals`: A slice of closed intervals as (start, end) tuples.
2279///
2280/// # Returns
2281///
2282/// Returns an [`IntervalTree`] containing the converted intervals.
2283///
2284/// # Notes
2285///
2286/// - Invalid intervals (where start > end) are skipped.
2287/// - Uses saturating addition to prevent overflow when converting to half-open intervals.
2288fn get_interval_set(intervals: &[(u64, u64)]) -> IntervalTree<u64> {
2289    let mut tree = IntervalTree::default();
2290
2291    if intervals.is_empty() {
2292        return tree;
2293    }
2294
2295    for &(start, end) in intervals {
2296        if start > end {
2297            continue;
2298        }
2299
2300        tree.insert((
2301            Bound::Included(start),
2302            Bound::Excluded(end.saturating_add(1)),
2303        ));
2304    }
2305
2306    tree
2307}
2308
2309/// Converts an interval tree result back to a closed interval tuple.
2310///
2311/// This helper function converts the bounded interval representation used by
2312/// the interval tree back into the (start, end) tuple format used throughout
2313/// the catalog.
2314///
2315/// # Parameters
2316///
2317/// - `interval`: The bounded interval from the interval tree.
2318/// - `query_start`: The start of the original query range.
2319/// - `query_end`: The end of the original query range.
2320///
2321/// # Returns
2322///
2323/// Returns `Some((start, end))` for valid intervals, `None` for empty intervals.
2324fn interval_to_tuple(
2325    interval: (Bound<&u64>, Bound<&u64>),
2326    query_start: u64,
2327    query_end: u64,
2328) -> Option<(u64, u64)> {
2329    let (bound_start, bound_end) = interval;
2330
2331    let start = match bound_start {
2332        Bound::Included(val) => *val,
2333        Bound::Excluded(val) => val.saturating_add(1),
2334        Bound::Unbounded => query_start,
2335    };
2336
2337    let end = match bound_end {
2338        Bound::Included(val) => *val,
2339        Bound::Excluded(val) => {
2340            if *val == 0 {
2341                return None; // Empty interval
2342            }
2343            val - 1
2344        }
2345        Bound::Unbounded => query_end,
2346    };
2347
2348    if start <= end {
2349        Some((start, end))
2350    } else {
2351        None
2352    }
2353}