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    /// Creates a new Config with all configurable parameters
31    pub fn new_full(
32        dir: impl ToString,
33        log_cache_max_items: Option<usize>,
34        log_cache_capacity: Option<usize>,
35        read_buffer_size: Option<usize>,
36        chunk_max_records: Option<usize>,
37        chunk_max_size: Option<usize>,
38    ) -> Self {
39        Self {
40            wal: chunked_wal::Config {
41                dir: dir.to_string(),
42                read_buffer_size,
43                chunk_max_records,
44                chunk_max_size,
45                truncate_incomplete_record: None,
46                flush_batch_wait: None,
47                flush_batch_max_items: None,
48            },
49            log_cache_max_items,
50            log_cache_capacity,
51        }
52    }
53
54    /// Returns the maximum number of items in log cache (defaults to 100,000)
55    pub fn log_cache_max_items(&self) -> usize {
56        self.log_cache_max_items.unwrap_or(100_000)
57    }
58
59    /// Returns the maximum capacity of log cache in bytes (defaults to 1GB)
60    pub fn log_cache_capacity(&self) -> usize {
61        self.log_cache_capacity.unwrap_or(1024 * 1024 * 1024)
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::Config;
68
69    #[test]
70    fn test_new_full_embeds_wal_config() {
71        let config = Config::new_full(
72            "raft-log-dir",
73            Some(7),
74            Some(11),
75            Some(13),
76            Some(17),
77            Some(19),
78        );
79
80        assert_eq!("raft-log-dir", config.wal.dir);
81        assert_eq!(Some(13), config.wal.read_buffer_size);
82        assert_eq!(Some(17), config.wal.chunk_max_records);
83        assert_eq!(Some(19), config.wal.chunk_max_size);
84        assert_eq!(None, config.wal.truncate_incomplete_record);
85        assert_eq!(None, config.wal.flush_batch_wait);
86        assert_eq!(None, config.wal.flush_batch_max_items);
87        assert_eq!(7, config.log_cache_max_items());
88        assert_eq!(11, config.log_cache_capacity());
89    }
90}