Skip to main content

reifydb_store_multi/store/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::{ops::Deref, sync::Arc};
5
6use reifydb_core::event::EventBus;
7use reifydb_runtime::{
8	SharedRuntimeConfig,
9	actor::{mailbox::ActorRef, system::ActorSystem},
10	clock::Clock,
11};
12use tracing::instrument;
13
14use crate::{HotConfig, cold::ColdStorage, config::MultiStoreConfig, hot::storage::HotStorage, warm::WarmStorage};
15
16pub mod drop;
17pub mod multi;
18pub mod router;
19pub mod version;
20pub mod worker;
21
22use worker::{DropActor, DropMessage, DropWorkerConfig};
23
24use crate::Result;
25
26#[derive(Clone)]
27pub struct StandardMultiStore(Arc<StandardMultiStoreInner>);
28
29pub struct StandardMultiStoreInner {
30	pub(crate) hot: Option<HotStorage>,
31	pub(crate) warm: Option<WarmStorage>,
32	pub(crate) cold: Option<ColdStorage>,
33	/// Reference to the drop actor for sending drop requests.
34	pub(crate) drop_actor: ActorRef<DropMessage>,
35	/// Actor system that owns the drop actor.
36	_actor_system: ActorSystem,
37	/// Event bus for emitting storage statistics events.
38	pub(crate) event_bus: EventBus,
39}
40
41impl StandardMultiStore {
42	#[instrument(name = "store::multi::new", level = "debug", skip(config), fields(
43		has_hot = config.hot.is_some(),
44		has_warm = config.warm.is_some(),
45		has_cold = config.cold.is_some(),
46	))]
47	pub fn new(config: MultiStoreConfig) -> Result<Self> {
48		let hot = config.hot.map(|c| c.storage);
49		// TODO: warm and cold are placeholders for now
50		let warm = None;
51		let cold = None;
52		let _ = config.warm;
53		let _ = config.cold;
54
55		// Use the provided actor system for the drop actor
56		let actor_system = config.actor_system.clone();
57
58		// Spawn drop actor
59		let storage = hot.as_ref().expect("hot tier is required");
60		let drop_config = DropWorkerConfig::default();
61		let drop_actor = DropActor::spawn(
62			&actor_system,
63			drop_config,
64			storage.clone(),
65			config.event_bus.clone(),
66			Clock::default(),
67		);
68
69		Ok(Self(Arc::new(StandardMultiStoreInner {
70			hot,
71			warm,
72			cold,
73			drop_actor,
74			_actor_system: actor_system,
75			event_bus: config.event_bus,
76		})))
77	}
78
79	/// Get access to the hot storage tier.
80	///
81	/// Returns `None` if the hot tier is not configured.
82	pub fn hot(&self) -> Option<&HotStorage> {
83		self.hot.as_ref()
84	}
85}
86
87impl Deref for StandardMultiStore {
88	type Target = StandardMultiStoreInner;
89
90	fn deref(&self) -> &Self::Target {
91		&self.0
92	}
93}
94
95impl StandardMultiStore {
96	pub fn testing_memory() -> Self {
97		let actor_system = ActorSystem::new(SharedRuntimeConfig::default().actor_system_config());
98		Self::testing_memory_with_eventbus(EventBus::new(&actor_system))
99	}
100
101	pub fn testing_memory_with_eventbus(event_bus: EventBus) -> Self {
102		let actor_system = ActorSystem::new(SharedRuntimeConfig::default().actor_system_config());
103		Self::new(MultiStoreConfig {
104			hot: Some(HotConfig {
105				storage: HotStorage::memory(),
106			}),
107			warm: None,
108			cold: None,
109			retention: Default::default(),
110			merge_config: Default::default(),
111			event_bus,
112			actor_system,
113		})
114		.unwrap()
115	}
116}