surrealcs_kernel/logging/messages/transactions/
id.rs

1//! Defines basic functions for creating IDs for transactions. These should be called once per transaction in the transaction
2//! interface and then passed to all actors in a message.
3
4/// Creates a transaction ID based on the connection index, transaction index, and the current timestamp.
5///
6/// # Arguments
7/// * `connection_index`: The index of the connection in the allocator
8/// * `transaction_index`: The index of the transaction in the connection
9///
10/// # Returns
11/// A string representing the transaction ID
12pub fn create_id(connection_index: usize, transaction_index: usize) -> String {
13	let timestamp = chrono::Utc::now().timestamp().to_string();
14	format!("{}-{}-{}", connection_index, transaction_index, timestamp)
15}
16
17/// Gets the connection index from the transaction ID.
18///
19/// # Arguments
20/// * `id`: The transaction ID
21///
22/// # Returns
23/// The connection index
24pub fn get_connection_index(id: &str) -> usize {
25	let parts: Vec<&str> = id.split("-").collect();
26	parts[0].parse::<usize>().unwrap()
27}
28
29/// Gets the transaction index from the transaction ID.
30///
31/// # Arguments
32/// * `id`: The transaction ID
33///
34/// # Returns
35/// The transaction index
36pub fn get_transaction_index(id: &str) -> usize {
37	let parts: Vec<&str> = id.split("-").collect();
38	parts[1].parse::<usize>().unwrap()
39}
40
41#[cfg(test)]
42mod tests {
43	use super::*;
44
45	#[test]
46	fn test_create_id() {
47		let id = create_id(1, 1);
48		assert_eq!(id.len(), 14);
49		assert!(id.contains("1-1-"));
50	}
51
52	#[test]
53	fn test_get_connection_index() {
54		let id = create_id(1, 2);
55		let index = get_connection_index(&id);
56		assert_eq!(index, 1);
57	}
58
59	#[test]
60	fn test_get_transaction_index() {
61		let id = create_id(1, 2);
62		let index = get_transaction_index(&id);
63		assert_eq!(index, 2);
64	}
65}