Skip to main content

reifydb_transaction/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4#![cfg_attr(not(debug_assertions), deny(warnings))]
5
6use std::{
7	fmt,
8	fmt::{Display, Formatter},
9	ops::Deref,
10};
11
12use reifydb_core::{
13	interface::version::{ComponentType, HasVersion, SystemVersion},
14	return_internal_error,
15};
16use reifydb_type::{error::Error, value::uuid::Uuid7};
17use uuid::Uuid;
18
19pub mod change;
20pub mod delta;
21pub mod error;
22pub mod interceptor;
23pub mod multi;
24pub mod single;
25pub mod testing;
26pub mod transaction;
27
28/// A unique identifier for a transaction using UUIDv7 for time-ordered
29/// uniqueness
30#[repr(transparent)]
31#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
32pub struct TransactionId(pub(crate) Uuid7);
33
34impl Default for TransactionId {
35	fn default() -> Self {
36		Self::generate()
37	}
38}
39
40impl Deref for TransactionId {
41	type Target = Uuid7;
42
43	fn deref(&self) -> &Self::Target {
44		&self.0
45	}
46}
47
48impl TransactionId {
49	pub fn generate() -> Self {
50		Self(Uuid7::generate())
51	}
52}
53
54impl TryFrom<&[u8]> for TransactionId {
55	type Error = Error;
56
57	fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
58		if bytes.len() != 16 {
59			return_internal_error!("Invalid transaction ID length: expected 16 bytes, got {}", bytes.len());
60		}
61		let mut uuid_bytes = [0u8; 16];
62		uuid_bytes.copy_from_slice(bytes);
63		let uuid = Uuid::from_bytes(uuid_bytes);
64		Ok(Self(Uuid7::from(uuid)))
65	}
66}
67
68impl Display for TransactionId {
69	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
70		write!(f, "{}", self.0)
71	}
72}
73
74pub struct TransactionVersion;
75
76impl HasVersion for TransactionVersion {
77	fn version(&self) -> SystemVersion {
78		SystemVersion {
79			name: env!("CARGO_PKG_NAME")
80				.strip_prefix("reifydb-")
81				.unwrap_or(env!("CARGO_PKG_NAME"))
82				.to_string(),
83			version: env!("CARGO_PKG_VERSION").to_string(),
84			description: "Transaction management and concurrency control module".to_string(),
85			r#type: ComponentType::Module,
86		}
87	}
88}