reifydb_transaction/single/
mod.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4mod svl;
5
6use async_trait::async_trait;
7use reifydb_core::{
8	EncodedKey,
9	event::EventBus,
10	interface::{SingleVersionCommandTransaction, SingleVersionTransaction, WithEventBus},
11};
12use reifydb_store_transaction::TransactionStore;
13pub use svl::{SvlCommandTransaction, SvlQueryTransaction, TransactionSvl};
14
15#[repr(u8)]
16#[derive(Clone)]
17pub enum TransactionSingle {
18	SingleVersionLock(TransactionSvl) = 0,
19}
20
21impl TransactionSingle {
22	pub fn svl(store: TransactionStore, bus: EventBus) -> Self {
23		Self::SingleVersionLock(TransactionSvl::new(store, bus))
24	}
25}
26
27impl TransactionSingle {
28	pub async fn testing() -> Self {
29		Self::SingleVersionLock(TransactionSvl::new(
30			TransactionStore::testing_memory().await,
31			EventBus::default(),
32		))
33	}
34
35	/// Async helper for single-version queries.
36	pub async fn with_query<'a, I, F, R>(&self, keys: I, f: F) -> crate::Result<R>
37	where
38		I: IntoIterator<Item = &'a EncodedKey> + Send,
39		F: FnOnce(&mut <Self as SingleVersionTransaction>::Query<'_>) -> crate::Result<R> + Send,
40		R: Send,
41	{
42		let mut tx = self.begin_query(keys).await?;
43		f(&mut tx)
44	}
45
46	/// Async helper for single-version commands.
47	pub async fn with_command<'a, I, F, R>(&self, keys: I, f: F) -> crate::Result<R>
48	where
49		I: IntoIterator<Item = &'a EncodedKey> + Send,
50		F: FnOnce(&mut <Self as SingleVersionTransaction>::Command<'_>) -> crate::Result<R> + Send,
51		R: Send,
52	{
53		let mut tx = self.begin_command(keys).await?;
54		let result = f(&mut tx)?;
55		tx.commit().await?;
56		Ok(result)
57	}
58}
59
60impl WithEventBus for TransactionSingle {
61	fn event_bus(&self) -> &EventBus {
62		match self {
63			TransactionSingle::SingleVersionLock(t) => t.event_bus(),
64		}
65	}
66}
67
68#[async_trait]
69impl SingleVersionTransaction for TransactionSingle {
70	type Query<'a> = SvlQueryTransaction<'a>;
71	type Command<'a> = SvlCommandTransaction<'a>;
72
73	#[inline]
74	async fn begin_query<'a, I>(&self, keys: I) -> reifydb_core::Result<Self::Query<'_>>
75	where
76		I: IntoIterator<Item = &'a EncodedKey> + Send,
77	{
78		match self {
79			TransactionSingle::SingleVersionLock(t) => t.begin_query(keys).await,
80		}
81	}
82
83	#[inline]
84	async fn begin_command<'a, I>(&self, keys: I) -> reifydb_core::Result<Self::Command<'_>>
85	where
86		I: IntoIterator<Item = &'a EncodedKey> + Send,
87	{
88		match self {
89			TransactionSingle::SingleVersionLock(t) => t.begin_command(keys).await,
90		}
91	}
92}