Skip to main content

raft_log/
config.rs

1/// Configuration for Raft-log.
2///
3/// This struct holds various configuration parameters for the Raft-log,
4/// including WAL storage and cache settings.
5///
6/// Optional parameters are `Option<T>` in this struct, and default values is
7/// evaluated when a getter method is called.
8#[derive(Clone, Debug, Default)]
9pub struct Config {
10    /// WAL storage configuration.
11    pub wal: chunked_wal::Config,
12
13    /// Maximum number of items to keep in the log cache
14    pub log_cache_max_items: Option<usize>,
15
16    /// Maximum capacity of the log cache in bytes
17    pub log_cache_capacity: Option<usize>,
18}
19
20impl Config {
21    /// Creates a new Config with the specified directory and default values for
22    /// other fields
23    pub fn new(dir: impl ToString) -> Self {
24        Self {
25            wal: chunked_wal::Config::new(dir),
26            ..Default::default()
27        }
28    }
29
30    /// Returns the maximum number of items in log cache (defaults to 100,000)
31    pub fn log_cache_max_items(&self) -> usize {
32        self.log_cache_max_items.unwrap_or(100_000)
33    }
34
35    /// Returns the maximum capacity of log cache in bytes (defaults to 1GB)
36    pub fn log_cache_capacity(&self) -> usize {
37        self.log_cache_capacity.unwrap_or(1024 * 1024 * 1024)
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::Config;
44
45    #[test]
46    fn test_explicit_config_embeds_wal_config() {
47        let config = Config {
48            wal: crate::chunked_wal::Config {
49                dir: "raft-log-dir".to_string(),
50                read_buffer_size: Some(13),
51                chunk_max_records: Some(17),
52                chunk_max_size: Some(19),
53                truncate_incomplete_record: None,
54                flush_batch_wait: None,
55                flush_batch_max_items: None,
56            },
57            log_cache_max_items: Some(7),
58            log_cache_capacity: Some(11),
59        };
60
61        assert_eq!("raft-log-dir", config.wal.dir);
62        assert_eq!(Some(13), config.wal.read_buffer_size);
63        assert_eq!(Some(17), config.wal.chunk_max_records);
64        assert_eq!(Some(19), config.wal.chunk_max_size);
65        assert_eq!(None, config.wal.truncate_incomplete_record);
66        assert_eq!(None, config.wal.flush_batch_wait);
67        assert_eq!(None, config.wal.flush_batch_max_items);
68        assert_eq!(7, config.log_cache_max_items());
69        assert_eq!(11, config.log_cache_capacity());
70    }
71}