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",
"TODO",
"FUTURE",
"PERF",
"XXX",
"[ ]",
"[x]"

View file

@ -108,7 +108,7 @@ impl ID {
#[inline]
pub fn to_span(&self, len: usize) -> IdSpan {
IdSpan {
client_id: self.peer,
peer: self.peer,
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.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct IdSpan {
// TODO: rename this to peer_id
pub client_id: PeerID,
pub peer: PeerID,
pub counter: CounterSpan,
}
@ -279,7 +278,7 @@ impl IdSpan {
#[inline]
pub fn new(peer: PeerID, from: Counter, to: Counter) -> Self {
Self {
client_id: peer,
peer,
counter: CounterSpan {
start: from,
end: to,
@ -289,7 +288,7 @@ impl IdSpan {
#[inline]
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)]
@ -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
#[inline]
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
#[inline]
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 {
let mut out = IdSpanVector::default();
out.insert(self.client_id, self.counter);
out.insert(self.peer, self.counter);
out
}
pub fn get_intersection(&self, other: &Self) -> Option<Self> {
if self.client_id != other.client_id {
if self.peer != other.peer {
return None;
}
let counter = self.counter.get_intersection(&other.counter)?;
Some(Self {
client_id: self.client_id,
peer: self.peer,
counter,
})
}
@ -349,7 +348,7 @@ impl Sliceable for IdSpan {
#[inline]
fn slice(&self, from: usize, to: usize) -> Self {
IdSpan {
client_id: self.client_id,
peer: self.peer,
counter: self.counter.slice(from, to),
}
}
@ -357,7 +356,7 @@ impl Sliceable for IdSpan {
impl Mergable for IdSpan {
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, _: &()) {
@ -493,12 +492,12 @@ mod test_id_span {
use super::*;
macro_rules! id_spans {
($([$client_id:expr, $from:expr, $to:expr]),*) => {
($([$peer:expr, $from:expr, $to:expr]),*) => {
{
let mut id_spans = RleVecWithIndex::new();
$(
id_spans.push(IdSpan {
client_id: $client_id,
peer: $peer,
counter: CounterSpan::new($from, $to),
});
)*
@ -511,19 +510,19 @@ mod test_id_span {
fn test_id_span_rle_vec() {
let mut id_span_vec = RleVecWithIndex::new();
id_span_vec.push(IdSpan {
client_id: 0,
peer: 0,
counter: CounterSpan::new(0, 2),
});
assert_eq!(id_span_vec.merged_len(), 1);
assert_eq!(id_span_vec.atom_len(), 2);
id_span_vec.push(IdSpan {
client_id: 0,
peer: 0,
counter: CounterSpan::new(2, 4),
});
assert_eq!(id_span_vec.merged_len(), 1);
assert_eq!(id_span_vec.atom_len(), 4);
id_span_vec.push(IdSpan {
client_id: 2,
peer: 2,
counter: CounterSpan::new(2, 4),
});
assert_eq!(id_span_vec.merged_len(), 2);

View file

@ -52,8 +52,6 @@ pub struct StrAllocResult {
pub start: usize,
/// unicode end
pub end: usize,
// TODO: remove this field?
pub utf16_len: usize,
}
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 {
let start = text_lock.len_unicode();
let start_wchars = text_lock.len_utf16();
text_lock.alloc(str);
StrAllocResult {
utf16_len: text_lock.len_utf16() - start_wchars,
start,
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) {
let elem = self.tree.get_elem(leaf).unwrap();
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 end = u.id_span.ctr_end() - elem.id.counter;
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`
pub fn update_insert(&mut self, id_span: IdSpan, new_leaf: LeafIndex) {
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();
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) {
@ -104,7 +104,7 @@ impl IdToCursor {
pub fn iter(&self, mut iter_id_span: IdSpan) -> impl Iterator<Item = IterCursor> + '_ {
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 offset_in_insert_set = 0;
let mut counter = 0;
@ -146,7 +146,7 @@ impl IdToCursor {
return Some(IterCursor::Insert {
leaf: elem.leaf,
id_span: IdSpan::new(
iter_id_span.client_id,
iter_id_span.peer,
start_counter
.max(iter_id_span.counter.start)
.min(iter_id_span.counter.end),

View file

@ -41,9 +41,9 @@ fn to_str(output: Output) -> String {
is_first = false;
s += format!(
"{}-{}(\"c{}: [{}, {})\")",
id_span.client_id,
id_span.peer,
id_span.counter.start,
id_span.client_id,
id_span.peer,
id_span.counter.start,
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> + '_ {
self.changes
.get(&id_span.client_id)
.get(&id_span.peer)
.map(move |changes| {
let len = changes.len();
let start = changes
@ -830,7 +830,7 @@ impl OpLog {
) -> impl Iterator<Item = &'a Change> + 'a {
let spans: Vec<_> = from.diff_iter(to).1.collect();
spans.into_iter().flat_map(move |span| {
let peer = span.client_id;
let peer = span.peer;
let cnt = span.counter.start;
let end_cnt = span.counter.end;
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> + '_ {
self.left.iter().map(|(client_id, span)| IdSpan {
client_id: *client_id,
self.left.iter().map(|(peer, span)| IdSpan {
peer: *peer,
counter: *span,
})
}
pub fn get_id_spans_right(&self) -> impl Iterator<Item = IdSpan> + '_ {
self.right.iter().map(|(client_id, span)| IdSpan {
client_id: *client_id,
self.right.iter().map(|(peer, span)| IdSpan {
peer: *peer,
counter: *span,
})
}
}
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 {
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) {
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.end = span.end.max(target.counter.end);
} 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`
pub fn sub_iter<'a>(&'a self, rhs: &'a Self) -> impl Iterator<Item = IdSpan> + 'a {
self.iter().filter_map(move |(client_id, &counter)| {
if let Some(&rhs_counter) = rhs.get(client_id) {
self.iter().filter_map(move |(peer, &counter)| {
if let Some(&rhs_counter) = rhs.get(peer) {
if counter > rhs_counter {
Some(IdSpan {
client_id: *client_id,
peer: *peer,
counter: CounterSpan {
start: rhs_counter,
end: counter,
@ -466,7 +466,7 @@ impl VersionVector {
}
} else if counter > 0 {
Some(IdSpan {
client_id: *client_id,
peer: *peer,
counter: CounterSpan {
start: 0,
end: counter,
@ -479,9 +479,7 @@ impl VersionVector {
}
pub fn sub_vec(&self, rhs: &Self) -> IdSpanVector {
self.sub_iter(rhs)
.map(|x| (x.client_id, x.counter))
.collect()
self.sub_iter(rhs).map(|x| (x.peer, x.counter)).collect()
}
pub fn distance_to(&self, other: &Self) -> usize {
@ -618,7 +616,7 @@ impl VersionVector {
}
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() {
let count_end = target.ctr_end();
return Some(CounterSpan {
@ -667,22 +665,22 @@ impl VersionVector {
}
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() {
*counter = span.counter.norm_end();
}
} 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) {
if span.counter.min() == 0 {
self.remove(&span.client_id);
self.remove(&span.peer);
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() {
*counter = span.counter.min();
}
@ -692,7 +690,7 @@ impl VersionVector {
pub fn forward(&mut self, spans: &IdSpanVector) {
for span in spans.iter() {
self.extend_to_include(IdSpan {
client_id: *span.0,
peer: *span.0,
counter: *span.1,
});
}
@ -701,7 +699,7 @@ impl VersionVector {
pub fn retreat(&mut self, spans: &IdSpanVector) {
for span in spans.iter() {
self.shrink_to_exclude(IdSpan {
client_id: *span.0,
peer: *span.0,
counter: *span.1,
});
}
@ -981,35 +979,35 @@ impl PatchedVersionVector {
#[inline]
pub fn extend_to_include(&mut self, span: IdSpan) {
self.patch.extend_to_include(span);
self.omit_if_needless(span.client_id);
self.omit_if_needless(span.peer);
}
#[inline]
pub fn shrink_to_exclude(&mut self, span: IdSpan) {
self.patch.shrink_to_exclude(span);
self.omit_if_needless(span.client_id);
self.omit_if_needless(span.peer);
}
#[inline]
pub fn forward(&mut self, spans: &IdSpanVector) {
for span in spans.iter() {
let span = IdSpan {
client_id: *span.0,
peer: *span.0,
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() {
*counter = span.counter.norm_end();
self.omit_if_needless(span.client_id);
self.omit_if_needless(span.peer);
}
} else {
let target = span.counter.norm_end();
if self.base.get(&span.client_id) == Some(&target) {
if self.base.get(&span.peer) == Some(&target) {
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) {
for span in spans.iter() {
let span = IdSpan {
client_id: *span.0,
peer: *span.0,
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() {
*counter = span.counter.min();
self.omit_if_needless(span.client_id);
self.omit_if_needless(span.peer);
}
}
}