patch: Use `GMap` to simplify code

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-12-01 14:35:14 +01:00
parent ff79347eb0
commit d6467fe4f1
No known key found for this signature in database
3 changed files with 17 additions and 29 deletions

View File

@ -19,6 +19,10 @@ impl<K: Ord, V: Semilattice> GMap<K, V> {
} }
} }
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) { pub fn insert(&mut self, key: K, value: V) {
match self.inner.entry(key) { match self.inner.entry(key) {
Entry::Occupied(mut e) => { Entry::Occupied(mut e) => {

View File

@ -4,8 +4,6 @@ pub mod patch;
pub mod store; pub mod store;
pub mod thread; pub mod thread;
pub use radicle_crdt::clock::Physical as Timestamp;
pub use cob::{ pub use cob::{
identity, object::collaboration::error, CollaborativeObject, Contents, Create, Entry, History, identity, object::collaboration::error, CollaborativeObject, Contents, Create, Entry, History,
ObjectId, TypeName, Update, ObjectId, TypeName, Update,

View File

@ -1,6 +1,4 @@
#![allow(clippy::too_many_arguments)] #![allow(clippy::too_many_arguments)]
use std::collections::btree_map::Entry;
use std::collections::BTreeMap;
use std::fmt; use std::fmt;
use std::ops::ControlFlow; use std::ops::ControlFlow;
use std::ops::Deref; use std::ops::Deref;
@ -10,7 +8,7 @@ use std::str::FromStr;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use radicle_crdt as crdt; use radicle_crdt as crdt;
use radicle_crdt::clock; 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 serde::{Deserialize, Serialize};
use thiserror::Error; use thiserror::Error;
@ -133,7 +131,7 @@ pub struct Patch {
pub tags: LWWSet<Tag>, pub tags: LWWSet<Tag>,
/// List of patch revisions. The initial changeset is part of the /// List of patch revisions. The initial changeset is part of the
/// first revision. /// first revision.
pub revisions: BTreeMap<RevisionId, Redactable<Revision>>, pub revisions: GMap<RevisionId, Redactable<Revision>>,
} }
impl Semilattice for Patch { impl Semilattice for Patch {
@ -155,7 +153,7 @@ impl Default for Patch {
status: Max::from(Status::default()).into(), status: Max::from(Status::default()).into(),
target: Max::from(MergeTarget::default()).into(), target: Max::from(MergeTarget::default()).into(),
tags: LWWSet::default(), tags: LWWSet::default(),
revisions: BTreeMap::default(), revisions: GMap::default(),
} }
} }
} }
@ -262,16 +260,10 @@ impl Patch {
} }
} }
Action::Revision { base, oid } => { Action::Revision { base, oid } => {
let revision = Redactable::Present(Revision::new(author, base, oid, timestamp)); self.revisions.insert(
id,
match self.revisions.entry(id) { Redactable::Present(Revision::new(author, base, oid, timestamp)),
Entry::Vacant(e) => { );
e.insert(revision);
}
Entry::Occupied(mut e) => {
e.get_mut().merge(revision);
}
}
} }
Action::Redact { revision } => { Action::Redact { revision } => {
if let Some(revision) = self.revisions.get_mut(&revision) { if let Some(revision) = self.revisions.get_mut(&revision) {
@ -287,16 +279,10 @@ impl Patch {
ref inline, ref inline,
} => { } => {
if let Some(Redactable::Present(revision)) = self.revisions.get_mut(&revision) { if let Some(Redactable::Present(revision)) = self.revisions.get_mut(&revision) {
revision revision.reviews.insert(
.reviews change.author,
.entry(change.author) Review::new(verdict, comment.to_owned(), inline.to_owned(), timestamp),
.or_default() );
.merge(Review::new(
verdict,
comment.to_owned(),
inline.to_owned(),
timestamp,
));
} else { } else {
return Err(ApplyError::Missing(revision)); return Err(ApplyError::Missing(revision));
} }
@ -380,7 +366,7 @@ pub struct Revision {
/// Merges of this revision into other repositories. /// Merges of this revision into other repositories.
pub merges: LWWSet<Max<Merge>>, pub merges: LWWSet<Max<Merge>>,
/// Reviews of this revision's changes (one per actor). /// Reviews of this revision's changes (one per actor).
pub reviews: BTreeMap<ActorId, Review>, pub reviews: GMap<ActorId, Review>,
/// When this revision was created. /// When this revision was created.
pub timestamp: Timestamp, pub timestamp: Timestamp,
} }
@ -393,7 +379,7 @@ impl Revision {
oid, oid,
discussion: Thread::default(), discussion: Thread::default(),
merges: LWWSet::default(), merges: LWWSet::default(),
reviews: BTreeMap::default(), reviews: GMap::default(),
timestamp, timestamp,
} }
} }