Skip to main content

reifydb_store_multi/
config.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use reifydb_core::event::EventBus;
5use reifydb_runtime::{actor::system::ActorSpawner, context::clock::Clock};
6#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
7use reifydb_sqlite::{SqliteConfig, SqliteTempPathGuard};
8use reifydb_value::value::duration::Duration;
9
10use crate::tier::{commit::buffer::MultiCommitBufferTier, persistent::MultiPersistentTier};
11
12#[derive(Clone)]
13pub struct MultiStoreConfig {
14	pub commit: Option<CommitBufferConfig>,
15	pub persistent: Option<PersistentConfig>,
16	pub retention: RetentionConfig,
17	pub merge_config: MergeConfig,
18	pub event_bus: EventBus,
19	pub spawner: ActorSpawner,
20	pub clock: Clock,
21}
22
23impl MultiStoreConfig {
24	pub fn memory(spawner: ActorSpawner, clock: Clock, event_bus: EventBus) -> Self {
25		Self {
26			commit: Some(CommitBufferConfig {
27				storage: MultiCommitBufferTier::memory(),
28			}),
29			persistent: None,
30			retention: Default::default(),
31			merge_config: Default::default(),
32			event_bus,
33			spawner,
34			clock,
35		}
36	}
37
38	#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
39	pub fn sqlite(persistent: PersistentConfig, spawner: ActorSpawner, clock: Clock, event_bus: EventBus) -> Self {
40		Self {
41			commit: Some(CommitBufferConfig {
42				storage: MultiCommitBufferTier::memory(),
43			}),
44			persistent: Some(persistent),
45			retention: Default::default(),
46			merge_config: Default::default(),
47			event_bus,
48			spawner,
49			clock,
50		}
51	}
52
53	#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
54	pub fn sqlite_unbuffered(
55		persistent: PersistentConfig,
56		spawner: ActorSpawner,
57		clock: Clock,
58		event_bus: EventBus,
59	) -> Self {
60		Self {
61			commit: None,
62			persistent: Some(persistent),
63			retention: Default::default(),
64			merge_config: Default::default(),
65			event_bus,
66			spawner,
67			clock,
68		}
69	}
70}
71
72#[derive(Clone)]
73pub struct CommitBufferConfig {
74	pub storage: MultiCommitBufferTier,
75}
76
77#[derive(Clone)]
78pub struct PersistentConfig {
79	pub storage: MultiPersistentTier,
80	pub flush_interval: Duration,
81}
82
83impl PersistentConfig {
84	#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
85	pub fn sqlite(sqlite_config: SqliteConfig) -> Self {
86		Self {
87			storage: MultiPersistentTier::sqlite(sqlite_config),
88			flush_interval: Duration::from_seconds(5).unwrap(),
89		}
90	}
91
92	#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
93	pub fn sqlite_in_memory() -> (Self, SqliteTempPathGuard) {
94		let (storage, guard) = MultiPersistentTier::sqlite_in_memory();
95		(
96			Self {
97				storage,
98				flush_interval: Duration::from_seconds(5).unwrap(),
99			},
100			guard,
101		)
102	}
103
104	pub fn flush_interval(mut self, interval: Duration) -> Self {
105		self.flush_interval = interval;
106		self
107	}
108}
109
110#[derive(Clone, Debug)]
111pub struct RetentionConfig {
112	pub buffer: Duration,
113	pub persistent: Duration,
114}
115
116#[derive(Clone, Debug)]
117pub struct MergeConfig {
118	pub merge_threshold_rows: usize,
119	pub merge_batch_size: usize,
120	pub enable_auto_eviction: bool,
121}
122
123impl Default for RetentionConfig {
124	fn default() -> Self {
125		Self {
126			buffer: Duration::from_seconds(300).unwrap(),
127			persistent: Duration::from_seconds(3600).unwrap(),
128		}
129	}
130}
131
132impl Default for MergeConfig {
133	fn default() -> Self {
134		Self {
135			merge_threshold_rows: 100_000,
136			merge_batch_size: 10_000,
137			enable_auto_eviction: true,
138		}
139	}
140}