diff --git a/Cargo.toml b/Cargo.toml index 2a6aaf2..db50f63 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,12 +21,13 @@ tracing = "0.1.41" url = { version = "2.5.4", features = ["serde"] } chrono = { version = "0.4.40", features = ["serde"] } derive_builder = "0.20.2" -ksuid = "0.2.0" bytes = "1.10.1" uuid = { version = "1.16.0", features = ["serde"] } config = "0.15.11" secrecy = { version = "0.10.3", features = ["serde"] } serde_with = "3.12.0" +rand = "0.9.1" + actix-web = { version = "4.10.2", optional = true, features = ["rustls"] } rdkafka = { version = "0.38.0", optional = true, features = [ "cmake-build", diff --git a/src/services/transactor/comm/mod.rs b/src/services/transactor/comm/mod.rs index 0ed0f4d..66f94a9 100644 --- a/src/services/transactor/comm/mod.rs +++ b/src/services/transactor/comm/mod.rs @@ -16,14 +16,15 @@ use serde::{Deserialize, Serialize, de::DeserializeOwned}; use serde_json::{self as json, Value}; +use super::tx::{Doc, Obj, Tx, TxDomainEvent}; +use crate::services::core::Ref; +use crate::services::transactor::document::generate_object_id; use crate::{ Result, services::{HttpClient, JsonClient}, }; mod message; -use super::tx::{Doc, Obj, Tx, TxDomainEvent}; -use crate::services::core::Ref; pub use message::*; #[derive(Serialize, Deserialize, Debug, Clone)] @@ -81,7 +82,7 @@ impl super::Transaction for Envelope { class: Ref::from(crate::services::core::class::TxDomainEvent), }, - id: ksuid::Ksuid::generate().to_hex(), + id: generate_object_id(), space: "core:space:Tx".to_string(), modified_on: None, diff --git a/src/services/transactor/document.rs b/src/services/transactor/document.rs index c1079e6..11589f9 100644 --- a/src/services/transactor/document.rs +++ b/src/services/transactor/document.rs @@ -12,11 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. // -use std::collections::HashMap; - +use chrono::Utc; use derive_builder::Builder; use serde::{Deserialize, Serialize, de::DeserializeOwned}; use serde_json::{self as json, Value}; +use std::collections::HashMap; +use std::sync::LazyLock; +use std::sync::atomic::AtomicUsize; use super::{ Transaction, @@ -30,16 +32,35 @@ use crate::{ services::{HttpClient, JsonClient}, }; +static COUNT: AtomicUsize = AtomicUsize::new(0); +static RANDOM: LazyLock = LazyLock::new(|| { + format!( + "{:6X}{:4X}", + rand::random::().wrapping_mul(1 << 24), + rand::random::().wrapping_mul(1 << 16) + ) +}); + +pub(crate) fn generate_object_id() -> Ref { + let count = COUNT.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let mut timestamp = Utc::now().timestamp() / 1000; + if timestamp < 0 { + timestamp = 0; + } + + format!("{timestamp:8X}{}{count}", &*RANDOM) +} + #[derive(Default, Debug, derive_builder::Builder, Clone)] pub struct CreateDocument { - #[builder(setter(into))] + #[builder(setter(into), default = generate_object_id())] object_id: Ref, #[builder(setter(into))] object_class: String, - #[builder(setter(into, strip_option), default)] - modified_on: Option, + #[builder(setter(into), default = Utc::now())] + modified_on: Timestamp, #[builder(setter(into, strip_option), default)] modified_by: Option, @@ -66,12 +87,12 @@ impl Transaction for CreateDocument { class: Ref::from(crate::services::core::class::TxCreateDoc), }, - id: ksuid::Ksuid::generate().to_hex(), - modified_on: self.modified_on, + id: generate_object_id(), + modified_on: Some(self.modified_on), modified_by: self.modified_by, created_on: self.created_on, created_by: self.created_by, - space: "core:space:Tx".to_string(), + space: Ref::from(crate::services::core::space::Tx), }, object_space: self.object_space, }, @@ -123,12 +144,12 @@ impl Transaction for RemoveDocument { class: Ref::from(crate::services::core::class::TxRemoveDoc), }, - id: ksuid::Ksuid::generate().to_hex(), + id: generate_object_id(), modified_on: self.modified_on, modified_by: self.modified_by, created_on: self.created_on, created_by: self.created_by, - space: "core:space:Tx".to_string(), + space: Ref::from(crate::services::core::space::Tx), }, object_space: self.object_space, },