Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions src/services/transactor/comm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -81,7 +82,7 @@ impl<T: Serialize> super::Transaction for Envelope<T> {
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,
Expand Down
41 changes: 31 additions & 10 deletions src/services/transactor/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,16 +32,35 @@ use crate::{
services::{HttpClient, JsonClient},
};

static COUNT: AtomicUsize = AtomicUsize::new(0);
static RANDOM: LazyLock<String> = LazyLock::new(|| {
format!(
"{:6X}{:4X}",
rand::random::<u32>().wrapping_mul(1 << 24),
rand::random::<u32>().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<T: Serialize> {
#[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<Timestamp>,
#[builder(setter(into), default = Utc::now())]
modified_on: Timestamp,

#[builder(setter(into, strip_option), default)]
modified_by: Option<PersonId>,
Expand All @@ -66,12 +87,12 @@ impl<T: Serialize> Transaction for CreateDocument<T> {
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,
},
Expand Down Expand Up @@ -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,
},
Expand Down