reifydb_core/interface/transaction/
transaction.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use super::change::TransactionalDefChanges;
5use crate::interface::{
6	CdcQueryTransaction, MultiVersionCommandTransaction, MultiVersionQueryTransaction,
7	SingleVersionCommandTransaction, SingleVersionQueryTransaction,
8};
9
10pub trait CommandTransaction: MultiVersionCommandTransaction + QueryTransaction {
11	type SingleVersionCommand<'a>: SingleVersionCommandTransaction
12	where
13		Self: 'a;
14
15	fn begin_single_command(&self) -> crate::Result<Self::SingleVersionCommand<'_>>;
16
17	fn with_single_command<F, R>(&self, f: F) -> crate::Result<R>
18	where
19		F: FnOnce(&mut Self::SingleVersionCommand<'_>) -> crate::Result<R>,
20	{
21		let mut tx = self.begin_single_command()?;
22		let result = f(&mut tx)?;
23		tx.commit()?;
24		Ok(result)
25	}
26
27	/// Get reference to catalog changes for this transaction
28	fn get_changes(&self) -> &TransactionalDefChanges;
29}
30
31pub trait QueryTransaction: MultiVersionQueryTransaction {
32	type SingleVersionQuery<'a>: SingleVersionQueryTransaction
33	where
34		Self: 'a;
35
36	type CdcQuery<'a>: CdcQueryTransaction
37	where
38		Self: 'a;
39
40	fn begin_single_query(&self) -> crate::Result<Self::SingleVersionQuery<'_>>;
41
42	fn begin_cdc_query(&self) -> crate::Result<Self::CdcQuery<'_>>;
43
44	fn with_single_query<F, R>(&self, f: F) -> crate::Result<R>
45	where
46		F: FnOnce(&mut Self::SingleVersionQuery<'_>) -> crate::Result<R>,
47	{
48		let mut tx = self.begin_single_query()?;
49		let result = f(&mut tx)?;
50		Ok(result)
51	}
52
53	fn with_cdc_query<F, R>(&self, f: F) -> crate::Result<R>
54	where
55		F: FnOnce(&mut Self::CdcQuery<'_>) -> crate::Result<R>,
56	{
57		let mut tx = self.begin_cdc_query()?;
58		let result = f(&mut tx)?;
59		Ok(result)
60	}
61}