log/config.rs
1//! Configuration options for OpenData Log operations.
2//!
3//! This module defines the configuration and options structs that control
4//! the behavior of the log, including storage setup and operation parameters.
5
6use std::time::Duration;
7
8use common::StorageConfig;
9use serde::{Deserialize, Serialize};
10use serde_with::{DurationMilliSeconds, serde_as};
11
12/// Configuration for opening a [`LogDb`](crate::LogDb).
13///
14/// This struct holds all the settings needed to initialize a log instance,
15/// including storage backend configuration.
16///
17/// # Example
18///
19/// ```no_run
20/// use log::{Config, SegmentConfig};
21/// use common::StorageConfig;
22///
23/// # #[tokio::main]
24/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
25/// let config = Config {
26/// storage: StorageConfig::default(),
27/// segmentation: SegmentConfig::default(),
28/// };
29/// let log = log::LogDb::open(config).await?;
30/// # Ok(())
31/// # }
32/// ```
33#[derive(Debug, Clone, Default, Serialize, Deserialize)]
34pub struct Config {
35 /// Storage backend configuration.
36 ///
37 /// Determines where and how log data is persisted. See [`StorageConfig`]
38 /// for available options including in-memory and SlateDB backends.
39 pub storage: StorageConfig,
40
41 /// Segmentation configuration.
42 ///
43 /// Controls how the log is partitioned into segments for efficient
44 /// time-based queries and retention management.
45 #[serde(default)]
46 pub segmentation: SegmentConfig,
47}
48
49/// Configuration for log segmentation.
50///
51/// Segments partition the log into time-based chunks, enabling efficient
52/// range queries and retention management. See RFC 0002 for details.
53#[serde_as]
54#[derive(Debug, Clone, Default, Serialize, Deserialize)]
55pub struct SegmentConfig {
56 /// Interval for automatic segment sealing based on wall-clock time.
57 ///
58 /// When set, a new segment is created after the specified duration has
59 /// elapsed since the current segment was created. This enables time-based
60 /// partitioning for efficient queries and retention.
61 ///
62 /// When `None` (the default), automatic sealing is disabled and all
63 /// entries are written to segment 0 indefinitely.
64 ///
65 /// # Example
66 ///
67 /// ```
68 /// use std::time::Duration;
69 /// use log::SegmentConfig;
70 ///
71 /// // Create a new segment every hour
72 /// let config = SegmentConfig {
73 /// seal_interval: Some(Duration::from_secs(3600)),
74 /// };
75 /// ```
76 #[serde_as(as = "Option<DurationMilliSeconds<u64>>")]
77 #[serde(default)]
78 pub seal_interval: Option<Duration>,
79}
80
81/// Options for scan operations.
82///
83/// Controls the behavior of [`LogRead::scan`](crate::LogRead::scan) and
84/// [`LogRead::scan_with_options`](crate::LogRead::scan_with_options).
85/// Additional options may be added in future versions.
86#[derive(Debug, Clone, Default)]
87pub struct ScanOptions {
88 // Reserved for future options such as:
89 // - read_level: control consistency vs performance tradeoff
90 // - cache_policy: control block cache behavior
91}
92
93/// Options for count operations.
94///
95/// Controls the behavior of [`LogRead::count`](crate::LogRead::count) and
96/// [`LogRead::count_with_options`](crate::LogRead::count_with_options).
97#[derive(Debug, Clone, Default)]
98pub struct CountOptions {
99 /// Whether to return an approximate count.
100 ///
101 /// When `true`, the count may be computed from index metadata without
102 /// reading individual entries, providing faster results at the cost
103 /// of accuracy. Useful for progress indicators and lag estimation.
104 ///
105 /// When `false` (the default), an exact count is computed by scanning
106 /// the relevant index entries.
107 pub approximate: bool,
108}
109
110/// Configuration for opening a [`LogDbReader`](crate::LogDbReader).
111///
112/// This struct holds settings for read-only log access, including storage
113/// backend configuration and automatic refresh settings.
114///
115/// # Example
116///
117/// ```no_run
118/// use log::ReaderConfig;
119/// use common::StorageConfig;
120/// use std::time::Duration;
121///
122/// # #[tokio::main]
123/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
124/// let config = ReaderConfig {
125/// storage: StorageConfig::default(),
126/// refresh_interval: Duration::from_secs(1),
127/// };
128/// let reader = log::LogDbReader::open(config).await?;
129/// # Ok(())
130/// # }
131/// ```
132#[serde_as]
133#[derive(Debug, Clone, Serialize, Deserialize)]
134pub struct ReaderConfig {
135 /// Storage backend configuration.
136 ///
137 /// Determines where and how log data is read. See [`StorageConfig`]
138 /// for available options including in-memory and SlateDB backends.
139 pub storage: StorageConfig,
140
141 /// Interval for discovering new log data.
142 ///
143 /// The reader periodically checks for new data written by other processes
144 /// at this interval. This enables readers to see new log entries without
145 /// manual refresh calls.
146 ///
147 /// Defaults to 1 second.
148 #[serde_as(as = "DurationMilliSeconds<u64>")]
149 #[serde(default = "default_refresh_interval")]
150 pub refresh_interval: Duration,
151}
152
153fn default_refresh_interval() -> Duration {
154 Duration::from_secs(1)
155}
156
157impl Default for ReaderConfig {
158 fn default() -> Self {
159 Self {
160 storage: StorageConfig::default(),
161 refresh_interval: default_refresh_interval(),
162 }
163 }
164}