pub mod counter;
pub mod delta_set;
pub mod flag;
pub mod hlc;
pub mod itc;
pub mod register;
pub mod serial;
pub mod set;
pub mod hll;
pub mod map;
pub mod keyfun;
#[cfg(feature = "wasm")]
pub mod keyfun_wasm;
use std::cmp::Ordering;
pub use crate::datatypes::counter::PnCounter;
pub use crate::datatypes::delta_set::{BufferedDelta, DeltaBuffer, DeltaOrSet, OrSetDelta};
pub use crate::datatypes::flag::EwFlag;
pub use crate::datatypes::hlc::{hlc_cmp, Hlc, HlcError};
pub use crate::datatypes::itc::{Event as ItcEvent, Id as ItcId, Itc};
pub use crate::datatypes::register::LwwRegister;
pub use crate::datatypes::serial::{
counter_from_bytes, counter_to_bytes, flag_from_bytes, flag_to_bytes, hll_from_bytes,
hll_to_bytes, map_from_bytes, map_to_bytes, peek_tag, register_from_bytes, register_to_bytes,
set_from_bytes, set_to_bytes, CrdtSerialError, TAG_COUNTER, TAG_FLAG, TAG_HLL, TAG_MAP,
TAG_REGISTER, TAG_SET,
};
pub use crate::datatypes::set::OrSet;
pub use crate::datatypes::hll::HyperLogLog;
pub use crate::datatypes::map::{FieldKey, FieldType, FieldValue, Map, MapOp, NestedOp};
pub use crate::datatypes::keyfun::{KeyFun, KeyFunError};
#[cfg(feature = "wasm")]
pub use crate::datatypes::keyfun_wasm::{WasmKeyfunStore, KEYFUN_ALLOC, KEYFUN_ROUTE};
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ActorId {
pub dc: String,
pub peer: String,
}
impl ActorId {
pub fn new(dc: impl Into<String>, peer: impl Into<String>) -> Self {
Self {
dc: dc.into(),
peer: peer.into(),
}
}
}
#[must_use]
pub fn lww_order(a_ts: u64, a_actor: &ActorId, b_ts: u64, b_actor: &ActorId) -> Ordering {
a_ts.cmp(&b_ts).then_with(|| a_actor.cmp(b_actor))
}
pub trait Crdt {
type Value;
fn merge(&mut self, other: &Self);
fn value(&self) -> Self::Value;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn actor_id_is_lex_ordered() {
let a = ActorId::new("dc1", "alpha");
let b = ActorId::new("dc1", "beta");
let c = ActorId::new("dc2", "alpha");
assert!(a < b);
assert!(b < c);
assert_eq!(a, ActorId::new("dc1", "alpha"));
}
#[test]
fn lww_order_breaks_ties_by_actor() {
let a = ActorId::new("dc1", "alpha");
let b = ActorId::new("dc1", "beta");
assert_eq!(lww_order(5, &a, 5, &b), Ordering::Less);
assert_eq!(lww_order(6, &a, 5, &b), Ordering::Greater);
assert_eq!(lww_order(5, &a, 5, &a), Ordering::Equal);
}
}