reifydb_transaction/
lib.rs1#![cfg_attr(not(debug_assertions), deny(clippy::disallowed_methods))]
5#![cfg_attr(debug_assertions, warn(clippy::disallowed_methods))]
6#![cfg_attr(not(debug_assertions), deny(warnings))]
7#![allow(clippy::tabs_in_doc_comments)]
8
9use std::{
10 fmt,
11 fmt::{Display, Formatter},
12 ops::Deref,
13};
14
15use reifydb_core::{
16 interface::version::{ComponentType, HasVersion, SystemVersion},
17 return_internal_error,
18};
19use reifydb_runtime::context::{clock::Clock, rng::Rng};
20use reifydb_value::{error::Error, value::uuid::Uuid7};
21use uuid::{Builder, Uuid};
22
23pub mod accumulator;
24pub mod change;
25pub mod commit;
26pub mod delta;
27pub mod dictionary;
28pub mod error;
29pub mod interceptor;
30pub mod multi;
31pub mod queue;
32pub mod single;
33pub mod transaction;
34
35#[repr(transparent)]
36#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Default)]
37pub struct TransactionId(pub(crate) Uuid7);
38
39impl Deref for TransactionId {
40 type Target = Uuid7;
41
42 fn deref(&self) -> &Self::Target {
43 &self.0
44 }
45}
46
47impl TransactionId {
48 pub fn generate(clock: &Clock, rng: &Rng) -> Self {
49 let millis = clock.now().to_millis();
50 let random_bytes = rng.infra_bytes_10();
51 Self(Uuid7(Builder::from_unix_timestamp_millis(millis, &random_bytes).into_uuid()))
52 }
53}
54
55impl TryFrom<&[u8]> for TransactionId {
56 type Error = Error;
57
58 fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
59 if bytes.len() != 16 {
60 return_internal_error!("Invalid transaction ID length: expected 16 bytes, got {}", bytes.len());
61 }
62 let mut uuid_bytes = [0u8; 16];
63 uuid_bytes.copy_from_slice(bytes);
64 let uuid = Uuid::from_bytes(uuid_bytes);
65 Ok(Self(Uuid7::from(uuid)))
66 }
67}
68
69impl Display for TransactionId {
70 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
71 write!(f, "{}", self.0)
72 }
73}
74
75pub struct TransactionVersion;
76
77impl HasVersion for TransactionVersion {
78 fn version(&self) -> SystemVersion {
79 SystemVersion {
80 name: env!("CARGO_PKG_NAME")
81 .strip_prefix("reifydb-")
82 .unwrap_or(env!("CARGO_PKG_NAME"))
83 .to_string(),
84 version: env!("CARGO_PKG_VERSION").to_string(),
85 description: "Transaction management and concurrency control module".to_string(),
86 r#type: ComponentType::Module,
87 }
88 }
89}