Skip to content

Commit 7709f3c

Browse files
authored
commitlog: Set up options for toml configuration (#2942)
1 parent 96a1ad4 commit 7709f3c

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

crates/commitlog/src/lib.rs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ pub mod stream;
3232
pub mod tests;
3333

3434
/// [`Commitlog`] options.
35-
#[derive(Clone, Copy, Debug)]
35+
#[derive(Clone, Copy, Debug, PartialEq)]
36+
#[cfg_attr(
37+
feature = "serde",
38+
derive(serde::Serialize, serde::Deserialize),
39+
serde(rename_all = "kebab-case")
40+
)]
3641
pub struct Options {
3742
/// Set the log format version to write, and the maximum supported version.
3843
///
@@ -42,23 +47,27 @@ pub struct Options {
4247
/// with new or very old versions.
4348
///
4449
/// Default: [`DEFAULT_LOG_FORMAT_VERSION`]
50+
#[cfg_attr(feature = "serde", serde(default = "Options::default_log_format_version"))]
4551
pub log_format_version: u8,
4652
/// The maximum size in bytes to which log segments should be allowed to
4753
/// grow.
4854
///
4955
/// Default: 1GiB
56+
#[cfg_attr(feature = "serde", serde(default = "Options::default_max_segment_size"))]
5057
pub max_segment_size: u64,
5158
/// The maximum number of records in a commit.
5259
///
5360
/// If this number is exceeded, the commit is flushed to disk even without
5461
/// explicitly calling [`Commitlog::flush`].
5562
///
5663
/// Default: 65,535
64+
#[cfg_attr(feature = "serde", serde(default = "Options::default_max_records_in_commit"))]
5765
pub max_records_in_commit: NonZeroU16,
5866
/// Whenever at least this many bytes have been written to the currently
5967
/// active segment, an entry is added to its offset index.
6068
///
6169
/// Default: 4096
70+
#[cfg_attr(feature = "serde", serde(default = "Options::default_offset_index_interval_bytes"))]
6271
pub offset_index_interval_bytes: NonZeroU64,
6372
/// If `true`, require that the segment must be synced to disk before an
6473
/// index entry is added.
@@ -74,22 +83,53 @@ pub struct Options {
7483
/// strictly every `offset_index_interval_bytes`.
7584
///
7685
/// Default: false
86+
#[cfg_attr(
87+
feature = "serde",
88+
serde(default = "Options::default_offset_index_require_segment_fsync")
89+
)]
7790
pub offset_index_require_segment_fsync: bool,
7891
}
7992

8093
impl Default for Options {
8194
fn default() -> Self {
82-
Self {
83-
log_format_version: DEFAULT_LOG_FORMAT_VERSION,
84-
max_segment_size: 1024 * 1024 * 1024,
85-
max_records_in_commit: NonZeroU16::MAX,
86-
offset_index_interval_bytes: NonZeroU64::new(4096).unwrap(),
87-
offset_index_require_segment_fsync: false,
88-
}
95+
Self::DEFAULT
8996
}
9097
}
9198

9299
impl Options {
100+
pub const DEFAULT_MAX_SEGMENT_SIZE: u64 = 1024 * 1024 * 1024;
101+
pub const DEFAULT_MAX_RECORDS_IN_COMMIT: NonZeroU16 = NonZeroU16::MAX;
102+
pub const DEFAULT_OFFSET_INDEX_INTERVAL_BYTES: NonZeroU64 = NonZeroU64::new(4096).expect("4096 > 0, qed");
103+
pub const DEFAULT_OFFSET_INDEX_REQUIRE_SEGMENT_FSYNC: bool = false;
104+
105+
pub const DEFAULT: Self = Self {
106+
log_format_version: DEFAULT_LOG_FORMAT_VERSION,
107+
max_segment_size: Self::default_max_segment_size(),
108+
max_records_in_commit: Self::default_max_records_in_commit(),
109+
offset_index_interval_bytes: Self::default_offset_index_interval_bytes(),
110+
offset_index_require_segment_fsync: Self::default_offset_index_require_segment_fsync(),
111+
};
112+
113+
pub const fn default_log_format_version() -> u8 {
114+
DEFAULT_LOG_FORMAT_VERSION
115+
}
116+
117+
pub const fn default_max_segment_size() -> u64 {
118+
Self::DEFAULT_MAX_SEGMENT_SIZE
119+
}
120+
121+
pub const fn default_max_records_in_commit() -> NonZeroU16 {
122+
Self::DEFAULT_MAX_RECORDS_IN_COMMIT
123+
}
124+
125+
pub const fn default_offset_index_interval_bytes() -> NonZeroU64 {
126+
Self::DEFAULT_OFFSET_INDEX_INTERVAL_BYTES
127+
}
128+
129+
pub const fn default_offset_index_require_segment_fsync() -> bool {
130+
Self::DEFAULT_OFFSET_INDEX_REQUIRE_SEGMENT_FSYNC
131+
}
132+
93133
/// Compute the length in bytes of an offset index based on the settings in
94134
/// `self`.
95135
pub fn offset_index_len(&self) -> u64 {

0 commit comments

Comments
 (0)