use crate::{u64_to_i64, unix_timestamp_millis};
pub const DEFAULT_QUEUE_LEASE_MS: u64 = 30_000;
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct ObjectKey {
pub collection: String,
pub id: String,
}
impl ObjectKey {
pub fn new(collection: impl Into<String>, id: impl Into<String>) -> Self {
Self {
collection: collection.into(),
id: id.into(),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MemoryObject {
pub key: ObjectKey,
pub body: String,
pub version: u64,
pub created_at: String,
pub updated_at: String,
}
impl MemoryObject {
pub fn new(
collection: impl Into<String>,
id: impl Into<String>,
body: impl Into<String>,
) -> Self {
Self {
key: ObjectKey::new(collection, id),
body: body.into(),
version: 0,
created_at: String::new(),
updated_at: String::new(),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MemoryEvent {
pub stream: String,
pub event_type: String,
pub body: String,
pub sequence: u64,
pub created_at: String,
}
impl MemoryEvent {
pub fn new(
stream: impl Into<String>,
event_type: impl Into<String>,
body: impl Into<String>,
) -> Self {
Self {
stream: stream.into(),
event_type: event_type.into(),
body: body.into(),
sequence: 0,
created_at: String::new(),
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum QueueJobStatus {
Ready,
Leased,
Completed,
Dead,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct QueueJob {
pub queue: String,
pub id: String,
pub body: String,
pub attempts: u32,
pub max_attempts: u32,
pub status: QueueJobStatus,
pub available_at_ms: i64,
pub leased_at_ms: Option<i64>,
pub lease_expires_at_ms: Option<i64>,
pub completed_at_ms: Option<i64>,
pub dead_at_ms: Option<i64>,
pub created_at: String,
pub last_error: String,
}
impl QueueJob {
pub fn new(
queue: impl Into<String>,
id: impl Into<String>,
body: impl Into<String>,
max_attempts: u32,
) -> Self {
Self {
queue: queue.into(),
id: id.into(),
body: body.into(),
attempts: 0,
max_attempts,
status: QueueJobStatus::Ready,
available_at_ms: 0,
leased_at_ms: None,
lease_expires_at_ms: None,
completed_at_ms: None,
dead_at_ms: None,
created_at: String::new(),
last_error: String::new(),
}
}
#[must_use]
pub fn delay_by_ms(mut self, delay_ms: u64) -> Self {
self.available_at_ms = unix_timestamp_millis().saturating_add(u64_to_i64(delay_ms));
self
}
#[must_use]
pub const fn available_at_ms(mut self, available_at_ms: i64) -> Self {
self.available_at_ms = available_at_ms;
self
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ListEventsOptions {
pub from_sequence: Option<u64>,
pub limit: Option<u64>,
}
#[derive(Clone, Debug, Default)]
pub struct ListObjectsOptions {
pub filter: Vec<(String, serde_json::Value)>,
pub limit: Option<u64>,
pub offset: Option<u64>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QueueClaimOptions {
pub lease_ms: u64,
}
impl Default for QueueClaimOptions {
fn default() -> Self {
Self {
lease_ms: DEFAULT_QUEUE_LEASE_MS,
}
}
}
impl QueueClaimOptions {
#[must_use]
pub const fn new(lease_ms: u64) -> Self {
Self { lease_ms }
}
}
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct QueueNackOptions {
pub delay_ms: u64,
pub error: String,
}
impl QueueNackOptions {
#[must_use]
pub const fn new(delay_ms: u64) -> Self {
Self {
delay_ms,
error: String::new(),
}
}
#[must_use]
pub fn with_error(delay_ms: u64, error: impl Into<String>) -> Self {
Self {
delay_ms,
error: error.into(),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct SearchOptions {
pub collections: Option<Vec<String>>,
pub limit: Option<usize>,
pub filter: Option<serde_json::Value>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct SearchHit {
pub kind: String,
pub collection: String,
pub id: String,
pub text: String,
pub score: f64,
pub body: String,
pub version: Option<u64>,
pub created_at: String,
pub updated_at: Option<String>,
pub event_type: Option<String>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Link {
pub id: String,
pub from_ref: String,
pub link_type: String,
pub to_ref: String,
pub weight: Option<f64>,
pub metadata_json: String,
pub created_at: String,
}
impl Link {
pub fn new(
from_ref: impl Into<String>,
link_type: impl Into<String>,
to_ref: impl Into<String>,
) -> Self {
Self {
id: String::new(),
from_ref: from_ref.into(),
link_type: link_type.into(),
to_ref: to_ref.into(),
weight: None,
metadata_json: "{}".to_string(),
created_at: String::new(),
}
}
#[must_use]
pub const fn with_weight(mut self, weight: f64) -> Self {
self.weight = Some(weight);
self
}
#[must_use]
pub fn with_metadata(mut self, metadata: impl Into<String>) -> Self {
self.metadata_json = metadata.into();
self
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct LinkQueryOptions {
pub link_type: Option<String>,
pub limit: Option<usize>,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum LinkDirection {
Outgoing,
Incoming,
#[default]
Both,
}