refactor: rename client_id in idspan to peer (#287)

* refactor: rename client_id in idspan to peer

* fix: type err
This commit is contained in:
Zixuan Chen 2024-03-02 19:10:33 +08:00 committed by GitHub
parent a5fce60883
commit 08847d6639
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 54 additions and 60 deletions

View file

@ -53,6 +53,7 @@
"FIXME", "FIXME",
"TODO", "TODO",
"FUTURE", "FUTURE",
"PERF",
"XXX", "XXX",
"[ ]", "[ ]",
"[x]" "[x]"

View file

@ -108,7 +108,7 @@ impl ID {
#[inline] #[inline]
pub fn to_span(&self, len: usize) -> IdSpan { pub fn to_span(&self, len: usize) -> IdSpan {
IdSpan { IdSpan {
client_id: self.peer, peer: self.peer,
counter: CounterSpan::new(self.counter, self.counter + len as Counter), counter: CounterSpan::new(self.counter, self.counter + len as Counter),
} }
} }

View file

@ -270,8 +270,7 @@ impl IdLpSpan {
/// We need this because it'll make merging deletions easier. /// We need this because it'll make merging deletions easier.
#[derive(Clone, Copy, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct IdSpan { pub struct IdSpan {
// TODO: rename this to peer_id pub peer: PeerID,
pub client_id: PeerID,
pub counter: CounterSpan, pub counter: CounterSpan,
} }
@ -279,7 +278,7 @@ impl IdSpan {
#[inline] #[inline]
pub fn new(peer: PeerID, from: Counter, to: Counter) -> Self { pub fn new(peer: PeerID, from: Counter, to: Counter) -> Self {
Self { Self {
client_id: peer, peer,
counter: CounterSpan { counter: CounterSpan {
start: from, start: from,
end: to, end: to,
@ -289,7 +288,7 @@ impl IdSpan {
#[inline] #[inline]
pub fn contains(&self, id: ID) -> bool { pub fn contains(&self, id: ID) -> bool {
self.client_id == id.peer && self.counter.contains(id.counter) self.peer == id.peer && self.counter.contains(id.counter)
} }
#[inline(always)] #[inline(always)]
@ -310,29 +309,29 @@ impl IdSpan {
/// This is different from id_start. id_start may be greater than id_end, but this is the min of id_start and id_end-1 /// This is different from id_start. id_start may be greater than id_end, but this is the min of id_start and id_end-1
#[inline] #[inline]
pub fn norm_id_start(&self) -> ID { pub fn norm_id_start(&self) -> ID {
ID::new(self.client_id, self.counter.min()) ID::new(self.peer, self.counter.min())
} }
/// This is different from id_end. id_start may be greater than id_end. This is the max of id_start+1 and id_end /// This is different from id_end. id_start may be greater than id_end. This is the max of id_start+1 and id_end
#[inline] #[inline]
pub fn norm_id_end(&self) -> ID { pub fn norm_id_end(&self) -> ID {
ID::new(self.client_id, self.counter.norm_end()) ID::new(self.peer, self.counter.norm_end())
} }
pub fn to_id_span_vec(self) -> IdSpanVector { pub fn to_id_span_vec(self) -> IdSpanVector {
let mut out = IdSpanVector::default(); let mut out = IdSpanVector::default();
out.insert(self.client_id, self.counter); out.insert(self.peer, self.counter);
out out
} }
pub fn get_intersection(&self, other: &Self) -> Option<Self> { pub fn get_intersection(&self, other: &Self) -> Option<Self> {
if self.client_id != other.client_id { if self.peer != other.peer {
return None; return None;
} }
let counter = self.counter.get_intersection(&other.counter)?; let counter = self.counter.get_intersection(&other.counter)?;
Some(Self { Some(Self {
client_id: self.client_id, peer: self.peer,
counter, counter,
}) })
} }
@ -349,7 +348,7 @@ impl Sliceable for IdSpan {
#[inline] #[inline]
fn slice(&self, from: usize, to: usize) -> Self { fn slice(&self, from: usize, to: usize) -> Self {
IdSpan { IdSpan {
client_id: self.client_id, peer: self.peer,
counter: self.counter.slice(from, to), counter: self.counter.slice(from, to),
} }
} }
@ -357,7 +356,7 @@ impl Sliceable for IdSpan {
impl Mergable for IdSpan { impl Mergable for IdSpan {
fn is_mergable(&self, other: &Self, _: &()) -> bool { fn is_mergable(&self, other: &Self, _: &()) -> bool {
self.client_id == other.client_id && self.counter.is_mergable(&other.counter, &()) self.peer == other.peer && self.counter.is_mergable(&other.counter, &())
} }
fn merge(&mut self, other: &Self, _: &()) { fn merge(&mut self, other: &Self, _: &()) {
@ -493,12 +492,12 @@ mod test_id_span {
use super::*; use super::*;
macro_rules! id_spans { macro_rules! id_spans {
($([$client_id:expr, $from:expr, $to:expr]),*) => { ($([$peer:expr, $from:expr, $to:expr]),*) => {
{ {
let mut id_spans = RleVecWithIndex::new(); let mut id_spans = RleVecWithIndex::new();
$( $(
id_spans.push(IdSpan { id_spans.push(IdSpan {
client_id: $client_id, peer: $peer,
counter: CounterSpan::new($from, $to), counter: CounterSpan::new($from, $to),
}); });
)* )*
@ -511,19 +510,19 @@ mod test_id_span {
fn test_id_span_rle_vec() { fn test_id_span_rle_vec() {
let mut id_span_vec = RleVecWithIndex::new(); let mut id_span_vec = RleVecWithIndex::new();
id_span_vec.push(IdSpan { id_span_vec.push(IdSpan {
client_id: 0, peer: 0,
counter: CounterSpan::new(0, 2), counter: CounterSpan::new(0, 2),
}); });
assert_eq!(id_span_vec.merged_len(), 1); assert_eq!(id_span_vec.merged_len(), 1);
assert_eq!(id_span_vec.atom_len(), 2); assert_eq!(id_span_vec.atom_len(), 2);
id_span_vec.push(IdSpan { id_span_vec.push(IdSpan {
client_id: 0, peer: 0,
counter: CounterSpan::new(2, 4), counter: CounterSpan::new(2, 4),
}); });
assert_eq!(id_span_vec.merged_len(), 1); assert_eq!(id_span_vec.merged_len(), 1);
assert_eq!(id_span_vec.atom_len(), 4); assert_eq!(id_span_vec.atom_len(), 4);
id_span_vec.push(IdSpan { id_span_vec.push(IdSpan {
client_id: 2, peer: 2,
counter: CounterSpan::new(2, 4), counter: CounterSpan::new(2, 4),
}); });
assert_eq!(id_span_vec.merged_len(), 2); assert_eq!(id_span_vec.merged_len(), 2);

View file

@ -52,8 +52,6 @@ pub struct StrAllocResult {
pub start: usize, pub start: usize,
/// unicode end /// unicode end
pub end: usize, pub end: usize,
// TODO: remove this field?
pub utf16_len: usize,
} }
impl SharedArena { impl SharedArena {
@ -394,10 +392,8 @@ fn _alloc_value(values_lock: &mut MutexGuard<'_, Vec<LoroValue>>, value: LoroVal
fn _alloc_str(text_lock: &mut MutexGuard<'_, StrArena>, str: &str) -> StrAllocResult { fn _alloc_str(text_lock: &mut MutexGuard<'_, StrArena>, str: &str) -> StrAllocResult {
let start = text_lock.len_unicode(); let start = text_lock.len_unicode();
let start_wchars = text_lock.len_utf16();
text_lock.alloc(str); text_lock.alloc(str);
StrAllocResult { StrAllocResult {
utf16_len: text_lock.len_utf16() - start_wchars,
start, start,
end: text_lock.len_unicode(), end: text_lock.len_unicode(),
} }

View file

@ -330,7 +330,7 @@ impl CrdtRope {
for (leaf, group) in &updates.into_iter().group_by(|x| x.leaf) { for (leaf, group) in &updates.into_iter().group_by(|x| x.leaf) {
let elem = self.tree.get_elem(leaf).unwrap(); let elem = self.tree.get_elem(leaf).unwrap();
for u in group { for u in group {
debug_assert_eq!(u.id_span.client_id, elem.id.peer); debug_assert_eq!(u.id_span.peer, elem.id.peer);
let start = (u.id_span.ctr_start() - elem.id.counter).max(0); let start = (u.id_span.ctr_start() - elem.id.counter).max(0);
let end = u.id_span.ctr_end() - elem.id.counter; let end = u.id_span.ctr_end() - elem.id.counter;
tree_update_info.push(( tree_update_info.push((

View file

@ -46,7 +46,7 @@ impl IdToCursor {
/// id_span should be within the same `Cursor` and should be a `Insert` /// id_span should be within the same `Cursor` and should be a `Insert`
pub fn update_insert(&mut self, id_span: IdSpan, new_leaf: LeafIndex) { pub fn update_insert(&mut self, id_span: IdSpan, new_leaf: LeafIndex) {
debug_assert!(!id_span.is_reversed()); debug_assert!(!id_span.is_reversed());
let list = self.map.get_mut(&id_span.client_id).unwrap(); let list = self.map.get_mut(&id_span.peer).unwrap();
let last = list.last().unwrap(); let last = list.last().unwrap();
debug_assert!(last.counter + last.cursor.rle_len() as Counter > id_span.counter.max()); debug_assert!(last.counter + last.cursor.rle_len() as Counter > id_span.counter.max());
let mut index = match list.binary_search_by_key(&id_span.counter.start, |x| x.counter) { let mut index = match list.binary_search_by_key(&id_span.counter.start, |x| x.counter) {
@ -104,7 +104,7 @@ impl IdToCursor {
pub fn iter(&self, mut iter_id_span: IdSpan) -> impl Iterator<Item = IterCursor> + '_ { pub fn iter(&self, mut iter_id_span: IdSpan) -> impl Iterator<Item = IterCursor> + '_ {
iter_id_span.normalize_(); iter_id_span.normalize_();
let list = self.map.get(&iter_id_span.client_id).unwrap_or(&EMPTY_VEC); let list = self.map.get(&iter_id_span.peer).unwrap_or(&EMPTY_VEC);
let mut index = 0; let mut index = 0;
let mut offset_in_insert_set = 0; let mut offset_in_insert_set = 0;
let mut counter = 0; let mut counter = 0;
@ -146,7 +146,7 @@ impl IdToCursor {
return Some(IterCursor::Insert { return Some(IterCursor::Insert {
leaf: elem.leaf, leaf: elem.leaf,
id_span: IdSpan::new( id_span: IdSpan::new(
iter_id_span.client_id, iter_id_span.peer,
start_counter start_counter
.max(iter_id_span.counter.start) .max(iter_id_span.counter.start)
.min(iter_id_span.counter.end), .min(iter_id_span.counter.end),

View file

@ -41,9 +41,9 @@ fn to_str(output: Output) -> String {
is_first = false; is_first = false;
s += format!( s += format!(
"{}-{}(\"c{}: [{}, {})\")", "{}-{}(\"c{}: [{}, {})\")",
id_span.client_id, id_span.peer,
id_span.counter.start, id_span.counter.start,
id_span.client_id, id_span.peer,
id_span.counter.start, id_span.counter.start,
id_span.counter.end id_span.counter.end
) )

View file

@ -465,7 +465,7 @@ impl OpLog {
pub(crate) fn iter_ops(&self, id_span: IdSpan) -> impl Iterator<Item = RichOp> + '_ { pub(crate) fn iter_ops(&self, id_span: IdSpan) -> impl Iterator<Item = RichOp> + '_ {
self.changes self.changes
.get(&id_span.client_id) .get(&id_span.peer)
.map(move |changes| { .map(move |changes| {
let len = changes.len(); let len = changes.len();
let start = changes let start = changes
@ -830,7 +830,7 @@ impl OpLog {
) -> impl Iterator<Item = &'a Change> + 'a { ) -> impl Iterator<Item = &'a Change> + 'a {
let spans: Vec<_> = from.diff_iter(to).1.collect(); let spans: Vec<_> = from.diff_iter(to).1.collect();
spans.into_iter().flat_map(move |span| { spans.into_iter().flat_map(move |span| {
let peer = span.client_id; let peer = span.peer;
let cnt = span.counter.start; let cnt = span.counter.start;
let end_cnt = span.counter.end; let end_cnt = span.counter.end;
let peer_changes = self.changes.get(&peer).unwrap(); let peer_changes = self.changes.get(&peer).unwrap();

View file

@ -262,22 +262,22 @@ impl VersionVectorDiff {
} }
pub fn get_id_spans_left(&self) -> impl Iterator<Item = IdSpan> + '_ { pub fn get_id_spans_left(&self) -> impl Iterator<Item = IdSpan> + '_ {
self.left.iter().map(|(client_id, span)| IdSpan { self.left.iter().map(|(peer, span)| IdSpan {
client_id: *client_id, peer: *peer,
counter: *span, counter: *span,
}) })
} }
pub fn get_id_spans_right(&self) -> impl Iterator<Item = IdSpan> + '_ { pub fn get_id_spans_right(&self) -> impl Iterator<Item = IdSpan> + '_ {
self.right.iter().map(|(client_id, span)| IdSpan { self.right.iter().map(|(peer, span)| IdSpan {
client_id: *client_id, peer: *peer,
counter: *span, counter: *span,
}) })
} }
} }
fn subtract_start(m: &mut FxHashMap<PeerID, CounterSpan>, target: IdSpan) { fn subtract_start(m: &mut FxHashMap<PeerID, CounterSpan>, target: IdSpan) {
if let Some(span) = m.get_mut(&target.client_id) { if let Some(span) = m.get_mut(&target.peer) {
if span.start < target.counter.end { if span.start < target.counter.end {
span.start = target.counter.end; span.start = target.counter.end;
} }
@ -286,11 +286,11 @@ fn subtract_start(m: &mut FxHashMap<PeerID, CounterSpan>, target: IdSpan) {
fn merge(m: &mut FxHashMap<PeerID, CounterSpan>, mut target: IdSpan) { fn merge(m: &mut FxHashMap<PeerID, CounterSpan>, mut target: IdSpan) {
target.normalize_(); target.normalize_();
if let Some(span) = m.get_mut(&target.client_id) { if let Some(span) = m.get_mut(&target.peer) {
span.start = span.start.min(target.counter.start); span.start = span.start.min(target.counter.start);
span.end = span.end.max(target.counter.end); span.end = span.end.max(target.counter.end);
} else { } else {
m.insert(target.client_id, target.counter); m.insert(target.peer, target.counter);
} }
} }
@ -451,11 +451,11 @@ impl VersionVector {
/// Returns the spans that are in `self` but not in `rhs` /// Returns the spans that are in `self` but not in `rhs`
pub fn sub_iter<'a>(&'a self, rhs: &'a Self) -> impl Iterator<Item = IdSpan> + 'a { pub fn sub_iter<'a>(&'a self, rhs: &'a Self) -> impl Iterator<Item = IdSpan> + 'a {
self.iter().filter_map(move |(client_id, &counter)| { self.iter().filter_map(move |(peer, &counter)| {
if let Some(&rhs_counter) = rhs.get(client_id) { if let Some(&rhs_counter) = rhs.get(peer) {
if counter > rhs_counter { if counter > rhs_counter {
Some(IdSpan { Some(IdSpan {
client_id: *client_id, peer: *peer,
counter: CounterSpan { counter: CounterSpan {
start: rhs_counter, start: rhs_counter,
end: counter, end: counter,
@ -466,7 +466,7 @@ impl VersionVector {
} }
} else if counter > 0 { } else if counter > 0 {
Some(IdSpan { Some(IdSpan {
client_id: *client_id, peer: *peer,
counter: CounterSpan { counter: CounterSpan {
start: 0, start: 0,
end: counter, end: counter,
@ -479,9 +479,7 @@ impl VersionVector {
} }
pub fn sub_vec(&self, rhs: &Self) -> IdSpanVector { pub fn sub_vec(&self, rhs: &Self) -> IdSpanVector {
self.sub_iter(rhs) self.sub_iter(rhs).map(|x| (x.peer, x.counter)).collect()
.map(|x| (x.client_id, x.counter))
.collect()
} }
pub fn distance_to(&self, other: &Self) -> usize { pub fn distance_to(&self, other: &Self) -> usize {
@ -618,7 +616,7 @@ impl VersionVector {
} }
pub fn intersect_span(&self, target: IdSpan) -> Option<CounterSpan> { pub fn intersect_span(&self, target: IdSpan) -> Option<CounterSpan> {
if let Some(&end) = self.get(&target.client_id) { if let Some(&end) = self.get(&target.peer) {
if end > target.ctr_start() { if end > target.ctr_start() {
let count_end = target.ctr_end(); let count_end = target.ctr_end();
return Some(CounterSpan { return Some(CounterSpan {
@ -667,22 +665,22 @@ impl VersionVector {
} }
pub fn extend_to_include(&mut self, span: IdSpan) { pub fn extend_to_include(&mut self, span: IdSpan) {
if let Some(counter) = self.get_mut(&span.client_id) { if let Some(counter) = self.get_mut(&span.peer) {
if *counter < span.counter.norm_end() { if *counter < span.counter.norm_end() {
*counter = span.counter.norm_end(); *counter = span.counter.norm_end();
} }
} else { } else {
self.insert(span.client_id, span.counter.norm_end()); self.insert(span.peer, span.counter.norm_end());
} }
} }
pub fn shrink_to_exclude(&mut self, span: IdSpan) { pub fn shrink_to_exclude(&mut self, span: IdSpan) {
if span.counter.min() == 0 { if span.counter.min() == 0 {
self.remove(&span.client_id); self.remove(&span.peer);
return; return;
} }
if let Some(counter) = self.get_mut(&span.client_id) { if let Some(counter) = self.get_mut(&span.peer) {
if *counter > span.counter.min() { if *counter > span.counter.min() {
*counter = span.counter.min(); *counter = span.counter.min();
} }
@ -692,7 +690,7 @@ impl VersionVector {
pub fn forward(&mut self, spans: &IdSpanVector) { pub fn forward(&mut self, spans: &IdSpanVector) {
for span in spans.iter() { for span in spans.iter() {
self.extend_to_include(IdSpan { self.extend_to_include(IdSpan {
client_id: *span.0, peer: *span.0,
counter: *span.1, counter: *span.1,
}); });
} }
@ -701,7 +699,7 @@ impl VersionVector {
pub fn retreat(&mut self, spans: &IdSpanVector) { pub fn retreat(&mut self, spans: &IdSpanVector) {
for span in spans.iter() { for span in spans.iter() {
self.shrink_to_exclude(IdSpan { self.shrink_to_exclude(IdSpan {
client_id: *span.0, peer: *span.0,
counter: *span.1, counter: *span.1,
}); });
} }
@ -981,35 +979,35 @@ impl PatchedVersionVector {
#[inline] #[inline]
pub fn extend_to_include(&mut self, span: IdSpan) { pub fn extend_to_include(&mut self, span: IdSpan) {
self.patch.extend_to_include(span); self.patch.extend_to_include(span);
self.omit_if_needless(span.client_id); self.omit_if_needless(span.peer);
} }
#[inline] #[inline]
pub fn shrink_to_exclude(&mut self, span: IdSpan) { pub fn shrink_to_exclude(&mut self, span: IdSpan) {
self.patch.shrink_to_exclude(span); self.patch.shrink_to_exclude(span);
self.omit_if_needless(span.client_id); self.omit_if_needless(span.peer);
} }
#[inline] #[inline]
pub fn forward(&mut self, spans: &IdSpanVector) { pub fn forward(&mut self, spans: &IdSpanVector) {
for span in spans.iter() { for span in spans.iter() {
let span = IdSpan { let span = IdSpan {
client_id: *span.0, peer: *span.0,
counter: *span.1, counter: *span.1,
}; };
if let Some(counter) = self.patch.get_mut(&span.client_id) { if let Some(counter) = self.patch.get_mut(&span.peer) {
if *counter < span.counter.norm_end() { if *counter < span.counter.norm_end() {
*counter = span.counter.norm_end(); *counter = span.counter.norm_end();
self.omit_if_needless(span.client_id); self.omit_if_needless(span.peer);
} }
} else { } else {
let target = span.counter.norm_end(); let target = span.counter.norm_end();
if self.base.get(&span.client_id) == Some(&target) { if self.base.get(&span.peer) == Some(&target) {
continue; continue;
} }
self.patch.insert(span.client_id, target); self.patch.insert(span.peer, target);
} }
} }
} }
@ -1018,14 +1016,14 @@ impl PatchedVersionVector {
pub fn retreat(&mut self, spans: &IdSpanVector) { pub fn retreat(&mut self, spans: &IdSpanVector) {
for span in spans.iter() { for span in spans.iter() {
let span = IdSpan { let span = IdSpan {
client_id: *span.0, peer: *span.0,
counter: *span.1, counter: *span.1,
}; };
if let Some(counter) = self.patch.get_mut(&span.client_id) { if let Some(counter) = self.patch.get_mut(&span.peer) {
if *counter > span.counter.min() { if *counter > span.counter.min() {
*counter = span.counter.min(); *counter = span.counter.min();
self.omit_if_needless(span.client_id); self.omit_if_needless(span.peer);
} }
} }
} }