Skip to main content

reifydb_store_multi/
config.rs

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