Skip to main content

reifydb_store_multi/
config.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 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;
10
11use crate::{buffer::tier::MultiBufferTier, persistent::MultiPersistentTier};
12
13#[derive(Clone)]
14pub struct MultiStoreConfig {
15	pub buffer: Option<BufferConfig>,
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			buffer: Some(BufferConfig {
28				storage: MultiBufferTier::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			buffer: Some(BufferConfig {
48				storage: MultiBufferTier::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			buffer: 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 BufferConfig {
80	pub storage: MultiBufferTier,
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 {
100		Self {
101			storage: MultiPersistentTier::sqlite_in_memory(),
102			flush_interval: Duration::from_secs(5),
103		}
104	}
105
106	pub fn flush_interval(mut self, interval: Duration) -> Self {
107		self.flush_interval = interval;
108		self
109	}
110}
111
112#[derive(Clone, Debug)]
113pub struct RetentionConfig {
114	pub buffer: Duration,
115	pub persistent: Duration,
116}
117
118#[derive(Clone, Debug)]
119pub struct MergeConfig {
120	pub merge_threshold_rows: usize,
121	pub merge_batch_size: usize,
122	pub enable_auto_eviction: bool,
123}
124
125impl Default for RetentionConfig {
126	fn default() -> Self {
127		Self {
128			buffer: Duration::from_secs(300),
129			persistent: Duration::from_secs(3600),
130		}
131	}
132}
133
134impl Default for MergeConfig {
135	fn default() -> Self {
136		Self {
137			merge_threshold_rows: 100_000,
138			merge_batch_size: 10_000,
139			enable_auto_eviction: true,
140		}
141	}
142}