reifydb_core/interface/transaction/
single.rs1use crate::{
5 EncodedKey, EncodedKeyRange,
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(&self) -> crate::Result<Self::Query<'_>>;
17
18 fn begin_command(&self) -> crate::Result<Self::Command<'_>>;
19
20 fn with_query<F, R>(&self, f: F) -> crate::Result<R>
21 where
22 F: FnOnce(&mut Self::Query<'_>) -> crate::Result<R>,
23 {
24 let mut tx = self.begin_query()?;
25 f(&mut tx)
26 }
27
28 fn with_command<F, R>(&self, f: F) -> crate::Result<R>
29 where
30 F: FnOnce(&mut Self::Command<'_>) -> crate::Result<R>,
31 {
32 let mut tx = self.begin_command()?;
33 let result = f(&mut tx)?;
34 tx.commit()?;
35 Ok(result)
36 }
37}
38
39pub trait SingleVersionQueryTransaction {
40 fn get(&mut self, key: &EncodedKey) -> crate::Result<Option<SingleVersionValues>>;
41
42 fn contains_key(&mut self, key: &EncodedKey) -> crate::Result<bool>;
43
44 fn range(&mut self, range: EncodedKeyRange) -> crate::Result<BoxedSingleVersionIter>;
45
46 fn range_rev(&mut self, range: EncodedKeyRange) -> crate::Result<BoxedSingleVersionIter>;
47
48 fn prefix(&mut self, prefix: &EncodedKey) -> crate::Result<BoxedSingleVersionIter> {
49 self.range(EncodedKeyRange::prefix(prefix))
50 }
51
52 fn prefix_rev(&mut self, prefix: &EncodedKey) -> crate::Result<BoxedSingleVersionIter> {
53 self.range_rev(EncodedKeyRange::prefix(prefix))
54 }
55}
56
57pub trait SingleVersionCommandTransaction: SingleVersionQueryTransaction {
58 fn set(&mut self, key: &EncodedKey, row: EncodedValues) -> crate::Result<()>;
59
60 fn remove(&mut self, key: &EncodedKey) -> crate::Result<()>;
61
62 fn commit(self) -> crate::Result<()>;
63
64 fn rollback(self) -> crate::Result<()>;
65}