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