Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
crdt: Small improvements
Alexis Sellier committed 3 years ago
commit 252b6cd7bc4d3667f336c2c478c319485bd8715e
parent f140c6d28eb41454173c8a5d8685381afc3639fa
4 files changed +23 -3
modified radicle-crdt/src/change.rs
@@ -100,13 +100,14 @@ impl<G: Signer, A: Clone + Serialize> Actor<G, A> {
    /// Create a new change.
    pub fn change(&mut self, action: A) -> Change<A> {
        let author = *self.signer.public_key();
-
        let clock = self.clock.tick();
+
        let clock = self.clock;
        let change = Change {
            action,
            author,
            clock,
        };
        self.changes.insert((self.clock, author), change.clone());
+
        self.clock.tick();

        change
    }
modified radicle-crdt/src/clock.rs
@@ -29,6 +29,11 @@ impl Lamport {
        self.counter.merge(other.counter);
        self.tick()
    }
+

+
    /// Reset clock to default state.
+
    pub fn reset(&mut self) {
+
        self.counter = Max::default();
+
    }
}

impl From<u64> for Lamport {
modified radicle-crdt/src/lwwmap.rs
@@ -2,11 +2,14 @@ use std::collections::btree_map::Entry;
use std::collections::BTreeMap;

use crate::lwwreg::LWWReg;
-
use crate::Semilattice;
+
use crate::{clock, Semilattice};

/// Last-Write-Wins Map.
+
///
+
/// In case a value is added and removed under a key at the same time,
+
/// the "add" takes precedence over the "remove".
#[derive(Debug, Clone, PartialEq, Eq)]
-
pub struct LWWMap<K, V, C> {
+
pub struct LWWMap<K, V, C = clock::Lamport> {
    inner: BTreeMap<K, LWWReg<Option<V>, C>>,
}

modified radicle-crdt/src/lwwset.rs
@@ -2,6 +2,9 @@ use crate::clock;
use crate::{lwwmap::LWWMap, Semilattice};

/// Last-Write-Wins Set.
+
///
+
/// In case the same value is added and removed at the same time,
+
/// the "add" takes precedence over the "remove".
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LWWSet<T, C = clock::Lamport> {
    inner: LWWMap<T, (), C>,
@@ -129,6 +132,14 @@ mod tests {
        set.remove('a', 2);
        assert!(!set.contains(&'a'));
        assert!(!set.iter().any(|c| *c == 'a'));
+

+
        set.insert('b', 3);
+
        set.remove('b', 3);
+
        assert!(set.contains(&'b')); // Insert precedence.
+

+
        set.remove('c', 3);
+
        set.insert('c', 3);
+
        assert!(set.contains(&'c')); // Insert precedence.
    }

    #[test]