reifydb_core/interface/transaction/
single.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 crate::{
5	EncodedKey,
6	interface::{SingleVersionValues, WithEventBus},
7	value::encoded::EncodedValues,
8};
9
10pub type BoxedSingleVersionIter<'a> = Box<dyn Iterator<Item = SingleVersionValues> + Send + 'a>;
11
12pub trait SingleVersionTransaction: WithEventBus + Send + Sync + Clone + 'static {
13	type Query<'a>: SingleVersionQueryTransaction;
14	type Command<'a>: SingleVersionCommandTransaction;
15
16	fn begin_query<'a, I>(&self, keys: I) -> crate::Result<Self::Query<'_>>
17	where
18		I: IntoIterator<Item = &'a EncodedKey>;
19
20	fn begin_command<'a, I>(&self, keys: I) -> crate::Result<Self::Command<'_>>
21	where
22		I: IntoIterator<Item = &'a EncodedKey>;
23
24	fn with_query<'a, I, F, R>(&self, keys: I, f: F) -> crate::Result<R>
25	where
26		I: IntoIterator<Item = &'a EncodedKey>,
27		F: FnOnce(&mut Self::Query<'_>) -> crate::Result<R>,
28	{
29		let mut tx = self.begin_query(keys)?;
30		f(&mut tx)
31	}
32
33	fn with_command<'a, I, F, R>(&self, keys: I, f: F) -> crate::Result<R>
34	where
35		I: IntoIterator<Item = &'a EncodedKey>,
36		F: FnOnce(&mut Self::Command<'_>) -> crate::Result<R>,
37	{
38		let mut tx = self.begin_command(keys)?;
39		let result = f(&mut tx)?;
40		tx.commit()?;
41		Ok(result)
42	}
43}
44
45pub trait SingleVersionQueryTransaction {
46	fn get(&mut self, key: &EncodedKey) -> crate::Result<Option<SingleVersionValues>>;
47
48	fn contains_key(&mut self, key: &EncodedKey) -> crate::Result<bool>;
49}
50
51pub trait SingleVersionCommandTransaction: SingleVersionQueryTransaction {
52	fn set(&mut self, key: &EncodedKey, row: EncodedValues) -> crate::Result<()>;
53
54	fn remove(&mut self, key: &EncodedKey) -> crate::Result<()>;
55
56	fn commit(self) -> crate::Result<()>;
57
58	fn rollback(self) -> crate::Result<()>;
59}