use crate::hlc::Hlc;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
pub const TOMBSTONE_GC_MS: u64 = 90 * 24 * 60 * 60 * 1000;
pub const CONFLICT_KEEP_MS: u64 = 30 * 24 * 60 * 60 * 1000;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Lww {
KeepLocal,
TakeRemote,
Equal,
}
pub fn lww(local: &Hlc, remote: &Hlc) -> Lww {
match local.cmp_total(remote) {
std::cmp::Ordering::Greater => Lww::KeepLocal,
std::cmp::Ordering::Less => Lww::TakeRemote,
std::cmp::Ordering::Equal => Lww::Equal,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EditOrDelete {
KeepEdit,
TakeDelete,
}
pub fn edit_vs_delete(edit: &Hlc, delete: &Hlc) -> EditOrDelete {
if delete.strictly_later_than(edit) { EditOrDelete::TakeDelete } else { EditOrDelete::KeepEdit }
}
pub fn can_gc_tombstone(deleted: &Hlc, now_ms: u64, every_peer_cursor_past_delete: bool) -> bool {
every_peer_cursor_past_delete && now_ms.saturating_sub(deleted.physical_ms) >= TOMBSTONE_GC_MS
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OrSet<T: Eq + Hash> {
adds: HashMap<T, Vec<Hlc>>,
tombs: HashMap<T, Hlc>,
}
impl<T: Eq + Hash + Clone> Default for OrSet<T> {
fn default() -> Self {
Self { adds: HashMap::new(), tombs: HashMap::new() }
}
}
impl<T: Eq + Hash + Clone> OrSet<T> {
pub fn new() -> Self {
Self::default()
}
pub fn add(&mut self, elem: T, hlc: Hlc) {
self.adds.entry(elem).or_default().push(hlc);
}
pub fn remove(&mut self, elem: T, hlc: Hlc) {
match self.tombs.get(&elem) {
Some(prev) if lww(prev, &hlc) != Lww::TakeRemote => {}
_ => {
self.tombs.insert(elem, hlc);
}
}
}
fn present(&self, elem: &T) -> bool {
let Some(adds) = self.adds.get(elem) else { return false };
let tomb = self.tombs.get(elem);
adds.iter().any(|a| match tomb {
None => true,
Some(d) => edit_vs_delete(a, d) == EditOrDelete::KeepEdit,
})
}
pub fn contains(&self, elem: &T) -> bool {
self.present(elem)
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
self.adds.keys().filter(move |e| self.present(e))
}
pub fn values(&self) -> HashSet<T> {
self.iter().cloned().collect()
}
pub fn merge(&mut self, other: &Self) {
for (e, clocks) in &other.adds {
let slot = self.adds.entry(e.clone()).or_default();
for c in clocks {
if !slot.contains(c) {
slot.push(c.clone());
}
}
}
for (e, h) in &other.tombs {
self.remove(e.clone(), h.clone());
}
}
}
pub fn folder_cycle<I, F>(child: &I, new_parent: Option<&I>, mut parent_of: F) -> bool
where
I: Eq + Hash + Clone,
F: FnMut(&I) -> Option<I>,
{
let Some(start) = new_parent else { return false };
let mut seen = HashSet::new();
let mut cur = start.clone();
loop {
if &cur == child {
return true;
}
if !seen.insert(cur.clone()) {
return true;
}
match parent_of(&cur) {
Some(p) => cur = p,
None => return false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::hlc::NodeId;
fn n(s: &str) -> NodeId {
NodeId::new(s)
}
fn h(ms: u64, c: u32, node: &str) -> Hlc {
Hlc::new(ms, c, n(node))
}
#[test]
fn lww_takes_the_later_clock() {
let a = h(10, 0, "a");
let b = h(20, 0, "b");
assert_eq!(lww(&a, &b), Lww::TakeRemote);
assert_eq!(lww(&b, &a), Lww::KeepLocal);
assert_eq!(lww(&a, &a), Lww::Equal);
let title = lww(&h(10, 0, "a"), &h(11, 0, "b"));
let parent = lww(&h(12, 0, "a"), &h(11, 0, "b"));
assert_eq!(title, Lww::TakeRemote);
assert_eq!(parent, Lww::KeepLocal);
}
#[test]
fn concurrent_edit_beats_delete_later_delete_wins() {
let edit = h(100, 1, "a");
let concurrent = h(100, 1, "b");
let later = h(100, 2, "b");
assert_eq!(edit_vs_delete(&edit, &concurrent), EditOrDelete::KeepEdit);
assert_eq!(edit_vs_delete(&edit, &later), EditOrDelete::TakeDelete);
assert_eq!(edit_vs_delete(&later, &edit), EditOrDelete::KeepEdit);
}
#[test]
fn or_set_keeps_concurrent_adds_and_lww_a_tag_remove() {
let mut a = OrSet::new();
let mut b = OrSet::new();
a.add("work", h(10, 0, "a"));
b.add("home", h(10, 0, "b"));
a.merge(&b);
assert_eq!(a.values(), HashSet::from(["work", "home"]));
a.remove("work", h(10, 0, "c")); assert!(a.contains(&"work"));
a.remove("work", h(11, 0, "c"));
assert!(!a.contains(&"work"));
assert!(a.contains(&"home"));
}
#[test]
fn tombstone_gc_waits_for_age_and_cursors() {
let t = h(1000, 0, "a");
assert!(!can_gc_tombstone(&t, 1000 + TOMBSTONE_GC_MS - 1, true));
assert!(!can_gc_tombstone(&t, 1000 + TOMBSTONE_GC_MS, false));
assert!(can_gc_tombstone(&t, 1000 + TOMBSTONE_GC_MS, true));
assert_eq!(CONFLICT_KEEP_MS, 30 * 24 * 60 * 60 * 1000);
}
#[test]
fn folder_cycle_is_detected_and_a_root_move_is_not() {
let parents = HashMap::from([("b", "a"), ("c", "b")]);
let parent_of = |id: &&str| parents.get(*id).copied();
assert!(folder_cycle(&"a", Some(&"c"), parent_of));
assert!(!folder_cycle(&"a", Some(&"d"), parent_of));
assert!(!folder_cycle(&"a", None, parent_of));
}
}