From d6467fe4f19ecf3afa7ea8b0ab7b118317445bf2 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 1 Dec 2022 14:35:14 +0100 Subject: [PATCH] patch: Use `GMap` to simplify code Signed-off-by: Alexis Sellier --- radicle-crdt/src/gmap.rs | 4 ++++ radicle/src/cob.rs | 2 -- radicle/src/cob/patch.rs | 40 +++++++++++++--------------------------- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/radicle-crdt/src/gmap.rs b/radicle-crdt/src/gmap.rs index abe4141a..3a118c03 100644 --- a/radicle-crdt/src/gmap.rs +++ b/radicle-crdt/src/gmap.rs @@ -19,6 +19,10 @@ impl GMap { } } + pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { + self.inner.get_mut(key) + } + pub fn insert(&mut self, key: K, value: V) { match self.inner.entry(key) { Entry::Occupied(mut e) => { diff --git a/radicle/src/cob.rs b/radicle/src/cob.rs index 59a1e712..64d3cd3a 100644 --- a/radicle/src/cob.rs +++ b/radicle/src/cob.rs @@ -4,8 +4,6 @@ pub mod patch; pub mod store; pub mod thread; -pub use radicle_crdt::clock::Physical as Timestamp; - pub use cob::{ identity, object::collaboration::error, CollaborativeObject, Contents, Create, Entry, History, ObjectId, TypeName, Update, diff --git a/radicle/src/cob/patch.rs b/radicle/src/cob/patch.rs index a182002d..3895ef75 100644 --- a/radicle/src/cob/patch.rs +++ b/radicle/src/cob/patch.rs @@ -1,6 +1,4 @@ #![allow(clippy::too_many_arguments)] -use std::collections::btree_map::Entry; -use std::collections::BTreeMap; use std::fmt; use std::ops::ControlFlow; use std::ops::Deref; @@ -10,7 +8,7 @@ use std::str::FromStr; use once_cell::sync::Lazy; use radicle_crdt as crdt; use radicle_crdt::clock; -use radicle_crdt::{ActorId, ChangeId, LWWReg, LWWSet, Max, Redactable, Semilattice}; +use radicle_crdt::{ActorId, ChangeId, GMap, LWWReg, LWWSet, Max, Redactable, Semilattice}; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -133,7 +131,7 @@ pub struct Patch { pub tags: LWWSet, /// List of patch revisions. The initial changeset is part of the /// first revision. - pub revisions: BTreeMap>, + pub revisions: GMap>, } impl Semilattice for Patch { @@ -155,7 +153,7 @@ impl Default for Patch { status: Max::from(Status::default()).into(), target: Max::from(MergeTarget::default()).into(), tags: LWWSet::default(), - revisions: BTreeMap::default(), + revisions: GMap::default(), } } } @@ -262,16 +260,10 @@ impl Patch { } } Action::Revision { base, oid } => { - let revision = Redactable::Present(Revision::new(author, base, oid, timestamp)); - - match self.revisions.entry(id) { - Entry::Vacant(e) => { - e.insert(revision); - } - Entry::Occupied(mut e) => { - e.get_mut().merge(revision); - } - } + self.revisions.insert( + id, + Redactable::Present(Revision::new(author, base, oid, timestamp)), + ); } Action::Redact { revision } => { if let Some(revision) = self.revisions.get_mut(&revision) { @@ -287,16 +279,10 @@ impl Patch { ref inline, } => { if let Some(Redactable::Present(revision)) = self.revisions.get_mut(&revision) { - revision - .reviews - .entry(change.author) - .or_default() - .merge(Review::new( - verdict, - comment.to_owned(), - inline.to_owned(), - timestamp, - )); + revision.reviews.insert( + change.author, + Review::new(verdict, comment.to_owned(), inline.to_owned(), timestamp), + ); } else { return Err(ApplyError::Missing(revision)); } @@ -380,7 +366,7 @@ pub struct Revision { /// Merges of this revision into other repositories. pub merges: LWWSet>, /// Reviews of this revision's changes (one per actor). - pub reviews: BTreeMap, + pub reviews: GMap, /// When this revision was created. pub timestamp: Timestamp, } @@ -393,7 +379,7 @@ impl Revision { oid, discussion: Thread::default(), merges: LWWSet::default(), - reviews: BTreeMap::default(), + reviews: GMap::default(), timestamp, } }