reifydb_transaction/multi/transaction/serializable/
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
4// This file includes and modifies code from the skipdb project (https://github.com/al8n/skipdb),
5// originally licensed under the Apache License, Version 2.0.
6// Original copyright:
7//   Copyright (c) 2024 Al Liu
8//
9// The original Apache License can be found at:
10//   http://www.apache.org/licenses/LICENSE-2.0
11
12use std::{ops::Deref, sync::Arc};
13
14use TransactionSingleVersion::SingleVersionLock;
15pub use command::*;
16pub use query::*;
17use reifydb_core::{CommitVersion, EncodedKey, EncodedKeyRange, event::EventBus};
18use reifydb_store_transaction::{
19	MultiVersionContains, MultiVersionGet, MultiVersionRange, MultiVersionRangeRev, TransactionStore,
20};
21
22use crate::multi::transaction::version::StandardVersionProvider;
23
24#[allow(clippy::module_inception)]
25mod command;
26pub(crate) mod query;
27
28use crate::{
29	multi::transaction::{Committed, TransactionManager},
30	single::{TransactionSingleVersion, TransactionSvl},
31};
32
33pub struct TransactionSerializable(Arc<Inner>);
34
35pub struct Inner {
36	pub(crate) tm: TransactionManager<StandardVersionProvider>,
37	pub(crate) store: TransactionStore,
38	pub(crate) event_bus: EventBus,
39}
40
41impl Deref for TransactionSerializable {
42	type Target = Inner;
43
44	fn deref(&self) -> &Self::Target {
45		&self.0
46	}
47}
48
49impl Clone for TransactionSerializable {
50	fn clone(&self) -> Self {
51		Self(self.0.clone())
52	}
53}
54
55impl Inner {
56	fn new(store: TransactionStore, single: TransactionSingleVersion, event_bus: EventBus) -> Self {
57		let tm = TransactionManager::new(StandardVersionProvider::new(single).unwrap()).unwrap();
58
59		Self {
60			tm,
61			store,
62			event_bus,
63		}
64	}
65
66	fn version(&self) -> crate::Result<CommitVersion> {
67		self.tm.version()
68	}
69}
70
71impl TransactionSerializable {
72	pub fn testing() -> Self {
73		let store = TransactionStore::testing_memory();
74		let event_bus = EventBus::new();
75		Self::new(store.clone(), SingleVersionLock(TransactionSvl::new(store, event_bus.clone())), event_bus)
76	}
77}
78
79impl TransactionSerializable {
80	pub fn new(store: TransactionStore, single: TransactionSingleVersion, event_bus: EventBus) -> Self {
81		Self(Arc::new(Inner::new(store, single, event_bus)))
82	}
83}
84
85impl TransactionSerializable {
86	pub fn version(&self) -> crate::Result<CommitVersion> {
87		self.0.version()
88	}
89	pub fn begin_query(&self) -> crate::Result<QueryTransaction> {
90		QueryTransaction::new(self.clone(), None)
91	}
92}
93
94impl TransactionSerializable {
95	pub fn begin_command(&self) -> crate::Result<CommandTransaction> {
96		CommandTransaction::new(self.clone())
97	}
98}
99
100pub enum Transaction {
101	Query(QueryTransaction),
102	Command(CommandTransaction),
103}
104
105impl TransactionSerializable {
106	pub fn get(&self, key: &EncodedKey, version: CommitVersion) -> Result<Option<Committed>, reifydb_type::Error> {
107		Ok(self.store.get(key, version)?.map(|sv| sv.into()))
108	}
109
110	pub fn contains_key(&self, key: &EncodedKey, version: CommitVersion) -> Result<bool, reifydb_type::Error> {
111		self.store.contains(key, version)
112	}
113
114	pub fn range_batched(
115		&self,
116		range: EncodedKeyRange,
117		version: CommitVersion,
118		batch_size: u64,
119	) -> reifydb_type::Result<<TransactionStore as MultiVersionRange>::RangeIter<'_>> {
120		self.store.range_batched(range, version, batch_size)
121	}
122
123	pub fn range(
124		&self,
125		range: EncodedKeyRange,
126		version: CommitVersion,
127	) -> reifydb_type::Result<<TransactionStore as MultiVersionRange>::RangeIter<'_>> {
128		self.range_batched(range, version, 1024)
129	}
130
131	pub fn range_rev_batched(
132		&self,
133		range: EncodedKeyRange,
134		version: CommitVersion,
135		batch_size: u64,
136	) -> reifydb_type::Result<<TransactionStore as MultiVersionRangeRev>::RangeIterRev<'_>> {
137		self.store.range_rev_batched(range, version, batch_size)
138	}
139
140	pub fn range_rev(
141		&self,
142		range: EncodedKeyRange,
143		version: CommitVersion,
144	) -> reifydb_type::Result<<TransactionStore as MultiVersionRangeRev>::RangeIterRev<'_>> {
145		self.range_rev_batched(range, version, 1024)
146	}
147}