Skip to main content

reifydb_flow/window/engine/
config.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use crate::window::span::{SlotSpan, WindowAnchor};
5
6pub const DEFAULT_EXPIRE_BATCH: usize = 256;
7
8#[derive(Clone)]
9pub struct WindowEngineConfig {
10	expire_batch: usize,
11}
12
13impl WindowEngineConfig {
14	pub fn builder() -> WindowEngineConfigBuilder {
15		WindowEngineConfigBuilder::new()
16	}
17
18	pub fn expire_batch(&self) -> usize {
19		self.expire_batch
20	}
21}
22
23pub struct WindowEngineConfigBuilder {
24	expire_batch: usize,
25}
26
27impl WindowEngineConfigBuilder {
28	fn new() -> Self {
29		Self {
30			expire_batch: DEFAULT_EXPIRE_BATCH,
31		}
32	}
33
34	pub fn expire_batch(mut self, batch: usize) -> Self {
35		self.expire_batch = batch;
36		self
37	}
38
39	pub fn build(self) -> WindowEngineConfig {
40		WindowEngineConfig {
41			expire_batch: self.expire_batch,
42		}
43	}
44}
45
46pub struct TumblingCarryConfig<C: WindowAnchor> {
47	base: WindowEngineConfig,
48	retention: Option<SlotSpan<C>>,
49}
50
51impl<C: WindowAnchor> TumblingCarryConfig<C> {
52	pub fn builder(base: WindowEngineConfig) -> TumblingCarryConfigBuilder<C> {
53		TumblingCarryConfigBuilder::new(base)
54	}
55
56	pub fn base(&self) -> WindowEngineConfig {
57		self.base.clone()
58	}
59
60	pub fn retention(&self) -> Option<SlotSpan<C>> {
61		self.retention
62	}
63}
64
65pub struct TumblingCarryConfigBuilder<C: WindowAnchor> {
66	base: WindowEngineConfig,
67	retention: Option<SlotSpan<C>>,
68}
69
70impl<C: WindowAnchor> TumblingCarryConfigBuilder<C> {
71	fn new(base: WindowEngineConfig) -> Self {
72		Self {
73			base,
74			retention: None,
75		}
76	}
77
78	pub fn retention(mut self, retention: Option<SlotSpan<C>>) -> Self {
79		self.retention = retention;
80		self
81	}
82
83	pub fn build(self) -> TumblingCarryConfig<C> {
84		TumblingCarryConfig {
85			base: self.base,
86			retention: self.retention,
87		}
88	}
89}
90
91#[cfg(test)]
92mod tests {
93	use reifydb_value::value::datetime::DateTime;
94
95	use super::*;
96
97	#[test]
98	fn the_expire_batch_defaults_and_survives_an_override() {
99		// The batch bounds one expiry pass; a builder that dropped it would sweep unbounded.
100		assert_eq!(WindowEngineConfig::builder().build().expire_batch(), DEFAULT_EXPIRE_BATCH);
101		assert_eq!(WindowEngineConfig::builder().expire_batch(9).build().expire_batch(), 9);
102	}
103
104	#[test]
105	fn a_carry_config_forwards_its_base() {
106		let config: TumblingCarryConfig<DateTime> =
107			TumblingCarryConfig::builder(WindowEngineConfig::builder().expire_batch(7).build())
108				.retention(None)
109				.build();
110
111		assert_eq!(config.base().expire_batch(), 7, "the carry config must not detach a fresh base");
112	}
113}