Skip to content

Commit

Permalink
fix(codec): reconnect items and update markers after merging (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
forehalo authored and darkskygit committed Aug 31, 2023
1 parent f99bd56 commit 4312e2d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
20 changes: 20 additions & 0 deletions y-octo/src/doc/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,7 @@ impl DocStore {
(Node::Item(lref), Node::Item(rref)) => {
let mut litem = unsafe { lref.get_mut_unchecked() };
let mut ritem = unsafe { rref.get_mut_unchecked() };
let llen = litem.len();

if litem.id.client != ritem.id.client
// not same delete status
Expand Down Expand Up @@ -929,6 +930,25 @@ impl DocStore {
break;
}
}

if let Some(Parent::Type(p)) = &litem.parent {
if let Some(parent) = p.ty_mut() {
if let Some(markers) = &parent.markers {
markers.replace_marker(rref.clone(), lref.clone(), -(llen as i64));
}
}
}

if ritem.keep() {
litem.flags.set_keep()
}

litem.right = ritem.right.clone();

if let Some(Node::Item(right)) = &litem.right {
let mut right = unsafe { right.get_mut_unchecked() };
right.left = Some(Node::Item(lref.clone()))
}
}
_ => {
break;
Expand Down
2 changes: 1 addition & 1 deletion y-octo/src/doc/types/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod iterator;
mod search_marker;

pub(crate) use iterator::ListIterator;
pub use search_marker::MarkerList;
pub(crate) use search_marker::MarkerList;

use super::*;

Expand Down
27 changes: 19 additions & 8 deletions y-octo/src/doc/types/list/search_marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use super::*;
const MAX_SEARCH_MARKER: usize = 80;

#[derive(Clone, Debug)]
pub struct SearchMarker {
pub(super) ptr: Somr<Item>,
pub(super) index: u64,
pub(crate) struct SearchMarker {
pub ptr: Somr<Item>,
pub index: u64,
}

impl SearchMarker {
Expand All @@ -39,7 +39,7 @@ impl SearchMarker {
/// instance behind [RwLock] guard already, so it's safe to make the list
/// internal mutable.
#[derive(Debug)]
pub struct MarkerList(RefCell<VecDeque<SearchMarker>>);
pub(crate) struct MarkerList(RefCell<VecDeque<SearchMarker>>);

impl Deref for MarkerList {
type Target = RefCell<VecDeque<SearchMarker>>;
Expand All @@ -56,7 +56,7 @@ impl DerefMut for MarkerList {
}

impl MarkerList {
pub(crate) fn new() -> Self {
pub fn new() -> Self {
MarkerList(RefCell::new(VecDeque::new()))
}

Expand All @@ -74,7 +74,7 @@ impl MarkerList {
}

// update mark position if the index is within the range of the marker
pub(super) fn update_marker_changes(&self, index: u64, len: i64) {
pub fn update_marker_changes(&self, index: u64, len: i64) {
let mut list = self.borrow_mut();

for marker in list.iter_mut() {
Expand Down Expand Up @@ -107,7 +107,7 @@ impl MarkerList {
}

// find and return the marker that is closest to the index
pub(super) fn find_marker(&self, parent: &YType, index: u64) -> Option<SearchMarker> {
pub fn find_marker(&self, parent: &YType, index: u64) -> Option<SearchMarker> {
if parent.start.is_none() || index == 0 {
return None;
}
Expand Down Expand Up @@ -201,9 +201,20 @@ impl MarkerList {
}

#[allow(dead_code)]
pub(super) fn get_last_marker(&self) -> Option<SearchMarker> {
pub fn get_last_marker(&self) -> Option<SearchMarker> {
self.borrow().back().cloned()
}

pub fn replace_marker(&self, raw: Somr<Item>, new: Somr<Item>, len_shift: i64) {
let mut list = self.borrow_mut();

for marker in list.iter_mut() {
if marker.ptr == raw {
marker.ptr = new.clone();
marker.index = ((marker.index as i64) + len_shift) as u64;
}
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit 4312e2d

Please sign in to comment.