project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | use std::{collections::VecDeque, sync::Arc}; |
| 2 | |
| 3 | use fractional_index::FractionalIndex; |
| 4 | use loro_common::{ |
| 5 | ContainerID, ContainerType, Counter, IdFull, IdLp, LoroError, LoroResult, LoroTreeError, |
| 6 | LoroValue, PeerID, TreeID, ID, |
| 7 | }; |
| 8 | use rustc_hash::FxHashMap; |
| 9 | use smallvec::smallvec; |
| 10 | |
| 11 | use crate::{ |
| 12 | container::tree::tree_op::TreeOp, |
| 13 | delta::{TreeDiffItem, TreeExternalDiff}, |
| 14 | state::{ |
| 15 | FiIfNotConfigured, FractionalIndexGenResult, NodePosition, TreeNode, TreeNodeWithChildren, |
| 16 | TreeParentId, |
| 17 | }, |
| 18 | txn::{EventHint, Transaction}, |
| 19 | BasicHandler, HandlerTrait, MapHandler, |
| 20 | }; |
| 21 | |
| 22 | use super::{create_handler, Handler, MaybeDetached}; |
| 23 | |
| 24 | #[derive(Clone)] |
| 25 | pub struct TreeHandler { |
| 26 | pub(super) inner: MaybeDetached<TreeInner>, |
| 27 | } |
| 28 | |
| 29 | #[derive(Clone)] |
| 30 | pub(super) struct TreeInner { |
| 31 | next_counter: Counter, |
| 32 | map: FxHashMap<TreeID, MapHandler>, |
| 33 | parent_links: FxHashMap<TreeID, Option<TreeID>>, |
| 34 | children_links: FxHashMap<Option<TreeID>, Vec<TreeID>>, |
| 35 | } |
| 36 | |
| 37 | impl TreeInner { |
| 38 | fn new() -> Self { |
| 39 | TreeInner { |
| 40 | next_counter: 0, |
| 41 | map: FxHashMap::default(), |
| 42 | parent_links: FxHashMap::default(), |
| 43 | children_links: FxHashMap::default(), |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | fn create(&mut self, parent: Option<TreeID>, index: usize) -> TreeID { |
| 48 | let id = TreeID::new(PeerID::MAX, self.next_counter); |
| 49 | self.next_counter += 1; |
| 50 | self.map.insert(id, MapHandler::new_detached()); |
| 51 | self.parent_links.insert(id, parent); |
| 52 | let children = self.children_links.entry(parent).or_default(); |
| 53 | children.insert(index, id); |
| 54 | id |
| 55 | } |
| 56 | |
| 57 | fn mov(&mut self, target: TreeID, new_parent: Option<TreeID>, index: usize) -> LoroResult<()> { |
| 58 | let old_parent = self |
| 59 | .parent_links |
| 60 | .get_mut(&target) |
| 61 | .ok_or(LoroTreeError::TreeNodeNotExist(target))?; |
| 62 | let children = self.children_links.get_mut(old_parent).unwrap(); |
| 63 | children.retain(|x| x != &target); |
| 64 | self.parent_links.insert(target, new_parent); |
| 65 | let children = self.children_links.entry(new_parent).or_default(); |
| 66 | children.insert(index, target); |
| 67 | Ok(()) |
| 68 | } |
| 69 | |
| 70 | fn delete(&mut self, id: TreeID) -> LoroResult<()> { |
| 71 | self.map.remove(&id); |
| 72 | let parent = self |
| 73 | .parent_links |
| 74 | .remove(&id) |
| 75 | .ok_or(LoroTreeError::TreeNodeNotExist(id))?; |
| 76 | let children = self.children_links.get_mut(&parent).unwrap(); |
| 77 | children.retain(|x| x != &id); |
| 78 | self.children_links.remove(&Some(id)); |
| 79 | Ok(()) |
| 80 | } |
| 81 | |
| 82 | fn get_id_by_index(&self, parent: &Option<TreeID>, index: usize) -> Option<TreeID> { |
| 83 | self.children_links |
| 84 | .get(parent) |
| 85 | .and_then(|x| x.get(index).cloned()) |
| 86 | } |
| 87 | |
| 88 | fn get_parent(&self, id: &TreeID) -> Option<Option<TreeID>> { |
| 89 | self.parent_links.get(id).cloned() |
| 90 | } |
| 91 | |
| 92 | fn get_children(&self, parent: Option<TreeID>) -> Option<Vec<TreeID>> { |
| 93 | self.children_links.get(&parent).cloned() |
| 94 | } |
| 95 | |
| 96 | fn children_num(&self, parent: Option<TreeID>) -> Option<usize> { |
| 97 | self.children_links.get(&parent).map(|x| x.len()) |
| 98 | } |
| 99 | |
| 100 | fn is_parent(&self, target: &TreeID, parent: &Option<TreeID>) -> bool { |
| 101 | self.parent_links.get(target) == Some(parent) |
| 102 | } |
| 103 | |
| 104 | fn get_index_by_tree_id(&self, target: &TreeID) -> Option<usize> { |
| 105 | self.parent_links |
| 106 | .get(target) |
| 107 | .and_then(|parent| self.children_links.get(parent)) |
| 108 | .and_then(|children| children.iter().position(|x| x == target)) |
| 109 | } |
| 110 | |
| 111 | fn get_value(&self, deep: bool) -> LoroValue { |
| 112 | let mut ans = vec![]; |
| 113 | |
| 114 | let empty_vec = vec![]; |
| 115 | let mut q = VecDeque::from_iter( |
| 116 | self.children_links |
| 117 | .get(&None) |
| 118 | .unwrap_or(&empty_vec) |
| 119 | .iter() |
| 120 | .enumerate() |
| 121 | .zip(std::iter::repeat(None::<TreeID>)), |
| 122 | ); |
| 123 | |
| 124 | while let Some(((idx, target), parent)) = q.pop_front() { |
| 125 | let map = self.map.get(target).unwrap(); |
| 126 | let mut loro_map_value = FxHashMap::default(); |
| 127 | loro_map_value.insert("id".to_string(), target.to_string().into()); |
| 128 | let parent = parent |
| 129 | .map(|p| p.to_string().into()) |
| 130 | .unwrap_or(LoroValue::Null); |
| 131 | loro_map_value.insert("parent".to_string(), parent); |
| 132 | loro_map_value.insert( |
| 133 | "meta".to_string(), |
| 134 | if deep { |
| 135 | map.get_deep_value() |
| 136 | } else { |
| 137 | String::from("UnResolved").into() |
| 138 | }, |
| 139 | ); |
| 140 | loro_map_value.insert("index".to_string(), (idx as i64).into()); |
| 141 | ans.push(loro_map_value); |
| 142 | if let Some(children) = self.children_links.get(&Some(*target)) { |
| 143 | for (idx, child) in children.iter().enumerate() { |
| 144 | q.push_back(((idx, child), Some(*target))); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | ans.into() |
| 149 | } |
| 150 | |
| 151 | fn get_nodes_under(&self, root: TreeParentId) -> Vec<TreeNode> { |
| 152 | let root_id = root.tree_id(); |
| 153 | let mut ans = vec![]; |
| 154 | let empty_vec = vec![]; |
| 155 | let mut q = VecDeque::from_iter( |
| 156 | self.children_links |
| 157 | .get(&root_id) |
| 158 | .unwrap_or(&empty_vec) |
| 159 | .iter() |
| 160 | .enumerate() |
| 161 | .zip(std::iter::repeat(root)), |
| 162 | ); |
| 163 | |
| 164 | while let Some(((index, target), parent)) = q.pop_front() { |
| 165 | ans.push(TreeNode { |
| 166 | id: *target, |
| 167 | parent, |
| 168 | fractional_index: FractionalIndex::default(), |
| 169 | index, |
| 170 | last_move_op: IdFull { |
| 171 | peer: target.peer, |
| 172 | lamport: 0, |
| 173 | counter: target.counter, |
| 174 | }, |
| 175 | }); |
| 176 | if let Some(children) = self.children_links.get(&Some(*target)) { |
| 177 | let parent = TreeParentId::Node(*target); |
| 178 | q.extend( |
| 179 | children |
| 180 | .iter() |
| 181 | .enumerate() |
| 182 | .map(|(index, target)| ((index, target), parent)), |
| 183 | ); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | ans |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | impl HandlerTrait for TreeHandler { |
| 192 | fn to_handler(&self) -> Handler { |
| 193 | Handler::Tree(self.clone()) |
| 194 | } |
| 195 | |
| 196 | fn attach( |
| 197 | &self, |
| 198 | txn: &mut Transaction, |
| 199 | parent: &BasicHandler, |
| 200 | self_id: ContainerID, |
| 201 | ) -> LoroResult<Self> { |
| 202 | match &self.inner { |
| 203 | MaybeDetached::Detached(t) => { |
| 204 | let mut t = t.lock(); |
| 205 | let inner = create_handler(parent, self_id); |
| 206 | let tree = inner.into_tree().unwrap(); |
| 207 | |
| 208 | let children = t.value.children_links.get(&None); |
| 209 | let mut q = children |
| 210 | .map(|c| { |
| 211 | VecDeque::from_iter( |
| 212 | c.iter() |
| 213 | .enumerate() |
| 214 | .zip(std::iter::repeat(TreeParentId::Root)), |
| 215 | ) |
| 216 | }) |
| 217 | .unwrap_or_default(); |
| 218 | while let Some(((idx, target), parent)) = q.pop_front() { |
| 219 | let real_id = |
| 220 | tree.create_with_txn(txn, parent, idx, FiIfNotConfigured::UseJitterZero)?; |
| 221 | let map = t.value.map.get(target).unwrap(); |
| 222 | map.attach( |
| 223 | txn, |
| 224 | tree.inner.try_attached_state()?, |
| 225 | real_id.associated_meta_container(), |
| 226 | )?; |
| 227 | |
| 228 | if let Some(children) = t.value.children_links.get(&Some(*target)) { |
| 229 | for (idx, child) in children.iter().enumerate() { |
| 230 | q.push_back(((idx, child), TreeParentId::Node(real_id))); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | t.attached = tree.attached_handler().cloned(); |
| 235 | Ok(tree) |
| 236 | } |
| 237 | MaybeDetached::Attached(a) => { |
| 238 | let new_inner = create_handler(a, self_id); |
| 239 | let ans = new_inner.into_tree().unwrap(); |
| 240 | |
| 241 | let mut pending = self |
| 242 | .get_all_hierarchy_nodes_under(TreeParentId::Root) |
| 243 | .into_iter() |
| 244 | .rev() |
| 245 | .map(|node| (TreeParentId::Root, node)) |
| 246 | .collect::<Vec<_>>(); |
| 247 | while let Some((parent, mut node)) = pending.pop() { |
| 248 | let real_id = ans.create_with_txn( |
| 249 | txn, |
| 250 | parent, |
| 251 | node.index, |
| 252 | FiIfNotConfigured::UseJitterZero, |
| 253 | )?; |
| 254 | self.get_meta(node.id)?.attach( |
| 255 | txn, |
| 256 | ans.inner.try_attached_state()?, |
| 257 | real_id.associated_meta_container(), |
| 258 | )?; |
| 259 | pending.extend( |
| 260 | std::mem::take(&mut node.children) |
| 261 | .into_iter() |
| 262 | .rev() |
| 263 | .map(|child| (TreeParentId::Node(real_id), child)), |
| 264 | ); |
| 265 | } |
| 266 | Ok(ans) |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | fn is_attached(&self) -> bool { |
| 272 | self.inner.is_attached() |
| 273 | } |
| 274 | |
| 275 | fn attached_handler(&self) -> Option<&BasicHandler> { |
| 276 | self.inner.attached_handler() |
| 277 | } |
| 278 | |
| 279 | fn get_value(&self) -> LoroValue { |
| 280 | match &self.inner { |
| 281 | MaybeDetached::Detached(t) => { |
| 282 | let t = t.lock(); |
| 283 | t.value.get_value(false) |
| 284 | } |
| 285 | MaybeDetached::Attached(a) => a.get_value(), |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | fn get_deep_value(&self) -> LoroValue { |
| 290 | match &self.inner { |
| 291 | MaybeDetached::Detached(t) => { |
| 292 | let t = t.lock(); |
| 293 | t.value.get_value(true) |
| 294 | } |
| 295 | MaybeDetached::Attached(a) => a.get_deep_value(), |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | fn kind(&self) -> ContainerType { |
| 300 | ContainerType::Tree |
| 301 | } |
| 302 | |
| 303 | fn get_attached(&self) -> Option<Self> { |
| 304 | match &self.inner { |
| 305 | MaybeDetached::Detached(d) => d.lock().attached.clone().map(|x| Self { |
| 306 | inner: MaybeDetached::Attached(x), |
| 307 | }), |
| 308 | MaybeDetached::Attached(_a) => Some(self.clone()), |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | fn from_handler(h: Handler) -> Option<Self> { |
| 313 | match h { |
| 314 | Handler::Tree(x) => Some(x), |
| 315 | _ => None, |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | fn doc(&self) -> Option<crate::LoroDoc> { |
| 320 | match &self.inner { |
| 321 | MaybeDetached::Detached(_) => None, |
| 322 | MaybeDetached::Attached(a) => Some(a.doc()), |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | impl std::fmt::Debug for TreeHandler { |
| 328 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 329 | match &self.inner { |
| 330 | MaybeDetached::Detached(_) => write!(f, "TreeHandler Detached"), |
| 331 | MaybeDetached::Attached(a) => write!(f, "TreeHandler {}", a.id), |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | impl TreeHandler { |
| 337 | /// Create a new container that is detached from the document. |
| 338 | /// |
| 339 | /// The edits on a detached container will not be persisted/synced. |
| 340 | /// To attach the container to the document, please insert it into an attached |
| 341 | /// container. |
| 342 | pub fn new_detached() -> Self { |
| 343 | Self { |
| 344 | inner: MaybeDetached::new_detached(TreeInner::new()), |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /// Meta maps of a detached tree's nodes; empty when the tree is attached. |
| 349 | pub(super) fn detached_meta_maps(&self) -> Vec<MapHandler> { |
| 350 | match &self.inner { |
| 351 | MaybeDetached::Detached(t) => t.lock().value.map.values().cloned().collect(), |
| 352 | MaybeDetached::Attached(_) => Vec::new(), |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | /// Get the deep value of the tree with its container id, as a |
| 357 | /// `{ cid, value }` map. Each node in `value` carries the deep value of its |
| 358 | /// associated meta map under the `meta` field. |
| 359 | pub fn get_deep_value_with_id(&self) -> LoroResult<LoroValue> { |
| 360 | let inner = self.inner.try_attached_state()?; |
| 361 | Ok(inner.with_doc_state(|state| { |
| 362 | state.get_container_deep_value_with_id(inner.container_idx, None) |
| 363 | })) |
| 364 | } |
| 365 | |
| 366 | pub fn delete(&self, target: TreeID) -> LoroResult<()> { |
| 367 | match &self.inner { |
| 368 | MaybeDetached::Detached(t) => { |
| 369 | let mut t = t.lock(); |
| 370 | t.value.delete(target)?; |
| 371 | Ok(()) |
| 372 | } |
| 373 | MaybeDetached::Attached(a) => a.with_txn(|txn| self.delete_with_txn(txn, target)), |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | pub(crate) fn delete_with_txn(&self, txn: &mut Transaction, target: TreeID) -> LoroResult<()> { |
| 378 | let inner = self.inner.try_attached_state()?; |
| 379 | let index = match self.get_index_by_tree_id(&target) { |
| 380 | Some(i) => i, |
| 381 | None => { |
| 382 | return Err(LoroTreeError::TreeNodeDeletedOrNotExist(target).into()); |
| 383 | } |
| 384 | }; |
| 385 | txn.apply_local_op( |
| 386 | inner.container_idx, |
| 387 | crate::op::RawOpContent::Tree(Arc::new(TreeOp::Delete { target })), |
| 388 | EventHint::Tree(smallvec![TreeDiffItem { |
| 389 | target, |
| 390 | action: TreeExternalDiff::Delete { |
| 391 | old_parent: self |
| 392 | .get_node_parent(&target) |
| 393 | .ok_or(LoroTreeError::TreeNodeDeletedOrNotExist(target))?, |
| 394 | old_index: index |
| 395 | }, |
| 396 | }]), |
| 397 | &inner.doc, |
| 398 | ) |
| 399 | } |
| 400 | |
| 401 | pub fn create(&self, parent: TreeParentId) -> LoroResult<TreeID> { |
| 402 | match parent { |
| 403 | TreeParentId::Deleted | TreeParentId::Unexist => { |
| 404 | return Err(LoroTreeError::InvalidParent.into()); |
| 405 | } |
| 406 | _ => {} |
| 407 | } |
| 408 | let index: usize = self.children_num(&parent).unwrap_or(0); |
| 409 | match &self.inner { |
| 410 | MaybeDetached::Detached(t) => { |
| 411 | let t = &mut t.lock().value; |
| 412 | Ok(t.create(parent.tree_id(), index)) |
| 413 | } |
| 414 | MaybeDetached::Attached(a) => { |
| 415 | a.with_txn(|txn| self.create_with_txn(txn, parent, index, FiIfNotConfigured::Zero)) |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | pub fn create_at(&self, parent: TreeParentId, index: usize) -> LoroResult<TreeID> { |
| 421 | match parent { |
| 422 | TreeParentId::Deleted | TreeParentId::Unexist => { |
| 423 | return Err(LoroTreeError::InvalidParent.into()); |
| 424 | } |
| 425 | _ => {} |
| 426 | } |
| 427 | let children_len = self.children_num(&parent).unwrap_or(0); |
| 428 | if index > children_len { |
| 429 | return Err(LoroTreeError::IndexOutOfBound { |
| 430 | len: children_len, |
| 431 | index, |
| 432 | } |
| 433 | .into()); |
| 434 | } |
| 435 | match &self.inner { |
| 436 | MaybeDetached::Detached(t) => { |
| 437 | let t = &mut t.lock().value; |
| 438 | Ok(t.create(parent.tree_id(), index)) |
| 439 | } |
| 440 | MaybeDetached::Attached(a) => { |
| 441 | a.with_txn(|txn| self.create_with_txn(txn, parent, index, FiIfNotConfigured::Throw)) |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /// For undo/redo, Specify the TreeID of the created node |
| 447 | pub(crate) fn create_at_with_target_for_apply_diff( |
| 448 | &self, |
| 449 | parent: TreeParentId, |
| 450 | position: FractionalIndex, |
| 451 | target: TreeID, |
| 452 | ) -> LoroResult<bool> { |
| 453 | let MaybeDetached::Attached(a) = &self.inner else { |
| 454 | unreachable!(); |
| 455 | }; |
| 456 | |
| 457 | if let Some(p) = self.get_node_parent(&target) { |
| 458 | if p == parent { |
| 459 | return Ok(false); |
| 460 | // If parent is deleted, we need to create the node, so this op from move_apply_diff |
| 461 | } |
| 462 | match p { |
| 463 | TreeParentId::Node(p) => { |
| 464 | if !self.is_node_unexist(&target) && !self.is_node_deleted(&p)? { |
| 465 | return self.move_at_with_target_for_apply_diff(parent, position, target); |
| 466 | } |
| 467 | } |
| 468 | TreeParentId::Root => { |
| 469 | return self.move_at_with_target_for_apply_diff(parent, position, target); |
| 470 | } |
| 471 | TreeParentId::Deleted | TreeParentId::Unexist => {} |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | // A diff made for another version of the doc can name a parent this doc |
| 476 | // doesn't have: an error, which `apply_diff` rolls back. |
| 477 | let with_event = match parent.tree_id() { |
| 478 | Some(p) => !self.is_node_deleted(&p)?, |
| 479 | None => true, |
| 480 | }; |
| 481 | if !with_event { |
| 482 | return Ok(false); |
| 483 | } |
| 484 | |
| 485 | // println!( |
| 486 | // "create_at_with_target_for_apply_diff: {:?} {:?}", |
| 487 | // target, parent |
| 488 | // ); |
| 489 | |
| 490 | let index = self |
| 491 | .get_index_by_fractional_index( |
| 492 | &parent, |
| 493 | &NodePosition { |
| 494 | position: position.clone(), |
| 495 | idlp: self.next_idlp(), |
| 496 | }, |
| 497 | ) |
| 498 | // TODO: parent has deleted? |
| 499 | .unwrap_or(0); |
| 500 | |
| 501 | let children = a.with_txn(|txn| { |
| 502 | let inner = self.inner.try_attached_state()?; |
| 503 | |
| 504 | txn.apply_local_op( |
| 505 | inner.container_idx, |
| 506 | crate::op::RawOpContent::Tree(Arc::new(TreeOp::Create { |
| 507 | target, |
| 508 | parent: parent.tree_id(), |
| 509 | position: position.clone(), |
| 510 | })), |
| 511 | EventHint::Tree(smallvec![TreeDiffItem { |
| 512 | target, |
| 513 | action: TreeExternalDiff::Create { |
| 514 | parent, |
| 515 | index, |
| 516 | position: position.clone(), |
| 517 | }, |
| 518 | }]), |
| 519 | &inner.doc, |
| 520 | )?; |
| 521 | |
| 522 | Ok(self |
| 523 | .children(&TreeParentId::Node(target)) |
| 524 | .unwrap_or_default()) |
| 525 | })?; |
| 526 | for child in children { |
| 527 | let position = self.get_position_by_tree_id(&child).unwrap(); |
| 528 | self.create_at_with_target_for_apply_diff(TreeParentId::Node(target), position, child)?; |
| 529 | } |
| 530 | Ok(true) |
| 531 | } |
| 532 | |
| 533 | /// For undo/redo, Specify the TreeID of the created node |
| 534 | pub(crate) fn move_at_with_target_for_apply_diff( |
| 535 | &self, |
| 536 | parent: TreeParentId, |
| 537 | position: FractionalIndex, |
| 538 | target: TreeID, |
| 539 | ) -> LoroResult<bool> { |
| 540 | let MaybeDetached::Attached(a) = &self.inner else { |
| 541 | unreachable!(); |
| 542 | }; |
| 543 | |
| 544 | // // the move node does not exist, create it |
| 545 | // if self.is_node_unexist(&target) || self.is_node_deleted(&target).unwrap() { |
| 546 | // return self.create_at_with_target_for_apply_diff(parent, position, target); |
| 547 | // } |
| 548 | |
| 549 | if let (Some(p), Some(fi)) = ( |
| 550 | self.get_node_parent(&target), |
| 551 | self.get_position_by_tree_id(&target), |
| 552 | ) { |
| 553 | if p == parent && position == fi { |
| 554 | return Ok(false); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | let old_parent = self.get_node_parent(&target).unwrap(); |
| 559 | let old_index = self.get_index_by_tree_id(&target).unwrap(); |
| 560 | let mut index = self |
| 561 | .get_index_by_fractional_index( |
| 562 | &parent, |
| 563 | &NodePosition { |
| 564 | position: position.clone(), |
| 565 | idlp: self.next_idlp(), |
| 566 | }, |
| 567 | ) |
| 568 | .unwrap_or(0); |
| 569 | if old_parent == parent && old_index < index { |
| 570 | index -= 1; |
| 571 | } |
| 572 | let with_event = match parent.tree_id() { |
| 573 | Some(p) => !self.is_node_deleted(&p)?, |
| 574 | None => true, |
| 575 | }; |
| 576 | |
| 577 | if !with_event { |
| 578 | return Ok(false); |
| 579 | } |
| 580 | |
| 581 | // println!( |
| 582 | // "move_at_with_target_for_apply_diff: {:?} {:?}", |
| 583 | // target, parent |
| 584 | // ); |
| 585 | |
| 586 | a.with_txn(|txn| { |
| 587 | let inner = self.inner.try_attached_state()?; |
| 588 | txn.apply_local_op( |
| 589 | inner.container_idx, |
| 590 | crate::op::RawOpContent::Tree(Arc::new(TreeOp::Move { |
| 591 | target, |
| 592 | parent: parent.tree_id(), |
| 593 | position: position.clone(), |
| 594 | })), |
| 595 | EventHint::Tree(smallvec![TreeDiffItem { |
| 596 | target, |
| 597 | action: TreeExternalDiff::Move { |
| 598 | parent, |
| 599 | index, |
| 600 | position: position.clone(), |
| 601 | // the old parent should be exist, so we can unwrap |
| 602 | old_parent, |
| 603 | old_index, |
| 604 | }, |
| 605 | }]), |
| 606 | &inner.doc, |
| 607 | ) |
| 608 | })?; |
| 609 | Ok(true) |
| 610 | } |
| 611 | |
| 612 | pub(crate) fn create_with_txn( |
| 613 | &self, |
| 614 | txn: &mut Transaction, |
| 615 | parent: TreeParentId, |
| 616 | index: usize, |
| 617 | cfg: FiIfNotConfigured, |
| 618 | ) -> LoroResult<TreeID> { |
| 619 | let inner = self.inner.try_attached_state()?; |
| 620 | let target = TreeID::from_id(txn.next_id()); |
| 621 | |
| 622 | match self.generate_position_at(&target, &parent, index, cfg) { |
| 623 | FractionalIndexGenResult::Ok(position) => { |
| 624 | self.create_with_position(inner, txn, target, parent, index, position) |
| 625 | } |
| 626 | FractionalIndexGenResult::Rearrange(ids) => { |
| 627 | for (i, (id, position)) in ids.into_iter().enumerate() { |
| 628 | if i == 0 { |
| 629 | self.create_with_position(inner, txn, id, parent, index, position)?; |
| 630 | continue; |
| 631 | } |
| 632 | self.mov_with_position(inner, txn, id, parent, index + i, position, index + i)?; |
| 633 | } |
| 634 | Ok(target) |
| 635 | } |
| 636 | FractionalIndexGenResult::NotConfigured => { |
| 637 | Err(LoroTreeError::FractionalIndexNotEnabled.into()) |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | pub fn mov(&self, target: TreeID, parent: TreeParentId) -> LoroResult<()> { |
| 643 | match &self.inner { |
| 644 | MaybeDetached::Detached(_) => { |
| 645 | let mut index: usize = self.children_num(&parent).unwrap_or(0); |
| 646 | if self.is_parent(&target, &parent) { |
| 647 | index -= 1; |
| 648 | } |
| 649 | self.move_to(target, parent, index) |
| 650 | } |
| 651 | MaybeDetached::Attached(a) => { |
| 652 | let mut index: usize = self.children_num(&parent).unwrap_or(0); |
| 653 | if self.is_parent(&target, &parent) { |
| 654 | index -= 1; |
| 655 | } |
| 656 | a.with_txn(|txn| { |
| 657 | self.mov_with_txn(txn, target, parent, index, FiIfNotConfigured::Zero) |
| 658 | }) |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | pub fn mov_after(&self, target: TreeID, other: TreeID) -> LoroResult<()> { |
| 664 | let parent = self |
| 665 | .get_node_parent(&other) |
| 666 | .ok_or(LoroTreeError::TreeNodeNotExist(other))?; |
| 667 | let mut index = self |
| 668 | .get_index_by_tree_id(&other) |
| 669 | .ok_or(LoroTreeError::TreeNodeDeletedOrNotExist(other))? |
| 670 | + 1; |
| 671 | if self.is_parent(&target, &parent) { |
| 672 | if let Some(target_index) = self.get_index_by_tree_id(&target) { |
| 673 | if target_index < index { |
| 674 | index -= 1; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | self.move_to(target, parent, index) |
| 679 | } |
| 680 | |
| 681 | pub fn mov_before(&self, target: TreeID, other: TreeID) -> LoroResult<()> { |
| 682 | let parent = self |
| 683 | .get_node_parent(&other) |
| 684 | .ok_or(LoroTreeError::TreeNodeNotExist(other))?; |
| 685 | let mut index = self |
| 686 | .get_index_by_tree_id(&other) |
| 687 | .ok_or(LoroTreeError::TreeNodeDeletedOrNotExist(other))?; |
| 688 | if self.is_parent(&target, &parent) && index >= 1 { |
| 689 | if let Some(target_index) = self.get_index_by_tree_id(&target) { |
| 690 | if target_index < index { |
| 691 | index -= 1; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | self.move_to(target, parent, index) |
| 696 | } |
| 697 | |
| 698 | pub fn move_to(&self, target: TreeID, parent: TreeParentId, index: usize) -> LoroResult<()> { |
| 699 | match &self.inner { |
| 700 | MaybeDetached::Detached(t) => { |
| 701 | let mut t = t.lock(); |
| 702 | t.value.mov(target, parent.tree_id(), index) |
| 703 | } |
| 704 | MaybeDetached::Attached(a) => a.with_txn(|txn| { |
| 705 | self.mov_with_txn(txn, target, parent, index, FiIfNotConfigured::Throw) |
| 706 | }), |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | pub(crate) fn mov_with_txn( |
| 711 | &self, |
| 712 | txn: &mut Transaction, |
| 713 | target: TreeID, |
| 714 | parent: TreeParentId, |
| 715 | index: usize, |
| 716 | cfg: FiIfNotConfigured, |
| 717 | ) -> LoroResult<()> { |
| 718 | let inner = self.inner.try_attached_state()?; |
| 719 | let mut children_len = self.children_num(&parent).unwrap_or(0); |
| 720 | let mut already_in_parent = false; |
| 721 | // check the input is valid |
| 722 | if self.is_parent(&target, &parent) { |
| 723 | // If the position after moving is same as the current position , do nothing |
| 724 | if let Some(current_index) = self.get_index_by_tree_id(&target) { |
| 725 | if current_index == index { |
| 726 | return Ok(()); |
| 727 | } |
| 728 | // move out first, we cannot delete the position here |
| 729 | // If throw error, the tree will be in a inconsistent state |
| 730 | children_len -= 1; |
| 731 | already_in_parent = true; |
| 732 | } |
| 733 | }; |
| 734 | if index > children_len { |
| 735 | return Err(LoroTreeError::IndexOutOfBound { |
| 736 | len: children_len, |
| 737 | index, |
| 738 | } |
| 739 | .into()); |
| 740 | } |
| 741 | let Some(old_index) = self.get_index_by_tree_id(&target) else { |
| 742 | return Err(LoroError::TreeError( |
| 743 | LoroTreeError::TreeNodeDeletedOrNotExist(target), |
| 744 | )); |
| 745 | }; |
| 746 | |
| 747 | if already_in_parent { |
| 748 | self.delete_position(&parent, &target); |
| 749 | } |
| 750 | |
| 751 | match self.generate_position_at(&target, &parent, index, cfg) { |
| 752 | FractionalIndexGenResult::Ok(position) => { |
| 753 | self.mov_with_position(inner, txn, target, parent, index, position, old_index) |
| 754 | } |
| 755 | FractionalIndexGenResult::Rearrange(ids) => { |
| 756 | for (i, (id, position)) in ids.into_iter().enumerate() { |
| 757 | self.mov_with_position(inner, txn, id, parent, index + i, position, old_index)?; |
| 758 | } |
| 759 | Ok(()) |
| 760 | } |
| 761 | FractionalIndexGenResult::NotConfigured => { |
| 762 | Err(LoroTreeError::FractionalIndexNotEnabled.into()) |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | #[allow(clippy::too_many_arguments)] |
| 768 | fn create_with_position( |
| 769 | &self, |
| 770 | inner: &BasicHandler, |
| 771 | txn: &mut Transaction, |
| 772 | tree_id: TreeID, |
| 773 | parent: TreeParentId, |
| 774 | index: usize, |
| 775 | position: FractionalIndex, |
| 776 | ) -> LoroResult<TreeID> { |
| 777 | txn.apply_local_op( |
| 778 | inner.container_idx, |
| 779 | crate::op::RawOpContent::Tree(Arc::new(TreeOp::Create { |
| 780 | target: tree_id, |
| 781 | parent: parent.tree_id(), |
| 782 | position: position.clone(), |
| 783 | })), |
| 784 | EventHint::Tree(smallvec![TreeDiffItem { |
| 785 | target: tree_id, |
| 786 | action: TreeExternalDiff::Create { |
| 787 | parent, |
| 788 | index, |
| 789 | position, |
| 790 | }, |
| 791 | }]), |
| 792 | &inner.doc, |
| 793 | )?; |
| 794 | Ok(tree_id) |
| 795 | } |
| 796 | |
| 797 | #[allow(clippy::too_many_arguments)] |
| 798 | fn mov_with_position( |
| 799 | &self, |
| 800 | inner: &BasicHandler, |
| 801 | txn: &mut Transaction, |
| 802 | target: TreeID, |
| 803 | parent: TreeParentId, |
| 804 | index: usize, |
| 805 | position: FractionalIndex, |
| 806 | old_index: usize, |
| 807 | ) -> LoroResult<()> { |
| 808 | txn.apply_local_op( |
| 809 | inner.container_idx, |
| 810 | crate::op::RawOpContent::Tree(Arc::new(TreeOp::Move { |
| 811 | target, |
| 812 | parent: parent.tree_id(), |
| 813 | position: position.clone(), |
| 814 | })), |
| 815 | EventHint::Tree(smallvec![TreeDiffItem { |
| 816 | target, |
| 817 | action: TreeExternalDiff::Move { |
| 818 | parent, |
| 819 | index, |
| 820 | position, |
| 821 | old_parent: self |
| 822 | .get_node_parent(&target) |
| 823 | .ok_or(LoroTreeError::TreeNodeDeletedOrNotExist(target))?, |
| 824 | old_index, |
| 825 | }, |
| 826 | }]), |
| 827 | &inner.doc, |
| 828 | ) |
| 829 | } |
| 830 | |
| 831 | pub fn get_meta(&self, target: TreeID) -> LoroResult<MapHandler> { |
| 832 | match &self.inner { |
| 833 | MaybeDetached::Detached(d) => { |
| 834 | let d = d.lock(); |
| 835 | d.value |
| 836 | .map |
| 837 | .get(&target) |
| 838 | .cloned() |
| 839 | .ok_or(LoroTreeError::TreeNodeNotExist(target).into()) |
| 840 | } |
| 841 | MaybeDetached::Attached(a) => { |
| 842 | if self.is_node_unexist(&target) { |
| 843 | return Err(LoroTreeError::TreeNodeNotExist(target).into()); |
| 844 | } |
| 845 | let map_container_id = target.associated_meta_container(); |
| 846 | let handler = create_handler(a, map_container_id); |
| 847 | handler.into_map().map_err(|_| { |
| 848 | LoroError::DecodeError( |
| 849 | "Tree node's associated meta container is not a map".into(), |
| 850 | ) |
| 851 | }) |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | pub fn is_node_unexist(&self, target: &TreeID) -> bool { |
| 857 | match &self.inner { |
| 858 | MaybeDetached::Detached(d) => { |
| 859 | let d = d.lock(); |
| 860 | !d.value.map.contains_key(target) |
| 861 | } |
| 862 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 863 | let a = state.as_tree_state().unwrap(); |
| 864 | a.is_node_unexist(target) |
| 865 | }), |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | pub fn is_node_deleted(&self, target: &TreeID) -> LoroResult<bool> { |
| 870 | match &self.inner { |
| 871 | MaybeDetached::Detached(t) => { |
| 872 | let t = t.lock(); |
| 873 | t.value |
| 874 | .map |
| 875 | .get(target) |
| 876 | .and(Some(true)) |
| 877 | .ok_or(LoroTreeError::TreeNodeNotExist(*target).into()) |
| 878 | } |
| 879 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 880 | let a = state.as_tree_state().unwrap(); |
| 881 | a.is_node_deleted(target) |
| 882 | .ok_or(LoroTreeError::TreeNodeNotExist(*target).into()) |
| 883 | }), |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | /// Get the parent of the node, if the node does not exist, return None |
| 888 | pub fn get_node_parent(&self, target: &TreeID) -> Option<TreeParentId> { |
| 889 | match &self.inner { |
| 890 | MaybeDetached::Detached(t) => { |
| 891 | let t = t.lock(); |
| 892 | t.value.get_parent(target).map(TreeParentId::from) |
| 893 | } |
| 894 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 895 | let a = state.as_tree_state().unwrap(); |
| 896 | a.parent(target) |
| 897 | }), |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | // TODO: iterator |
| 902 | pub fn children(&self, parent: &TreeParentId) -> Option<Vec<TreeID>> { |
| 903 | match &self.inner { |
| 904 | MaybeDetached::Detached(t) => { |
| 905 | let t = t.lock(); |
| 906 | t.value.get_children(parent.tree_id()) |
| 907 | } |
| 908 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 909 | let a = state.as_tree_state().unwrap(); |
| 910 | a.get_children(parent).map(|x| x.collect()) |
| 911 | }), |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | pub fn children_num(&self, parent: &TreeParentId) -> Option<usize> { |
| 916 | match &self.inner { |
| 917 | MaybeDetached::Detached(t) => { |
| 918 | let t = t.lock(); |
| 919 | t.value.children_num(parent.tree_id()) |
| 920 | } |
| 921 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 922 | let a = state.as_tree_state().unwrap(); |
| 923 | a.children_num(parent) |
| 924 | }), |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | /// Check if the node is exist. include deleted node. |
| 929 | pub fn contains(&self, target: TreeID) -> bool { |
| 930 | match &self.inner { |
| 931 | MaybeDetached::Detached(t) => { |
| 932 | let t = t.lock(); |
| 933 | t.value.map.contains_key(&target) |
| 934 | } |
| 935 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 936 | let a = state.as_tree_state().unwrap(); |
| 937 | !a.is_node_unexist(&target) |
| 938 | }), |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | pub fn get_child_at(&self, parent: &TreeParentId, index: usize) -> Option<TreeID> { |
| 943 | match &self.inner { |
| 944 | MaybeDetached::Detached(t) => { |
| 945 | let t = t.lock(); |
| 946 | t.value.get_id_by_index(&parent.tree_id(), index) |
| 947 | } |
| 948 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 949 | let a = state.as_tree_state().unwrap(); |
| 950 | a.get_id_by_index(parent, index) |
| 951 | }), |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | pub fn is_parent(&self, target: &TreeID, parent: &TreeParentId) -> bool { |
| 956 | match &self.inner { |
| 957 | MaybeDetached::Detached(t) => { |
| 958 | let t = t.lock(); |
| 959 | t.value.is_parent(target, &parent.tree_id()) |
| 960 | } |
| 961 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 962 | let a = state.as_tree_state().unwrap(); |
| 963 | a.is_parent(target, parent) |
| 964 | }), |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | /// Get all nodes in the tree, including deleted nodes |
| 969 | pub fn nodes(&self) -> Vec<TreeID> { |
| 970 | match &self.inner { |
| 971 | MaybeDetached::Detached(t) => { |
| 972 | let t = t.lock(); |
| 973 | t.value.map.keys().cloned().collect() |
| 974 | } |
| 975 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 976 | let a = state.as_tree_state().unwrap(); |
| 977 | a.nodes() |
| 978 | }), |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | pub fn get_nodes_under(&self, parent: TreeParentId) -> Vec<TreeNode> { |
| 983 | match &self.inner { |
| 984 | MaybeDetached::Detached(t) => t.lock().value.get_nodes_under(parent), |
| 985 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 986 | let a = state.as_tree_state().unwrap(); |
| 987 | a.get_all_tree_nodes_under(parent) |
| 988 | }), |
| 989 | } |
| 990 | } |
| 991 | pub fn roots(&self) -> Vec<TreeID> { |
| 992 | self.children(&TreeParentId::Root).unwrap_or_default() |
| 993 | } |
| 994 | |
| 995 | pub fn get_all_hierarchy_nodes_under(&self, parent: TreeParentId) -> Vec<TreeNodeWithChildren> { |
| 996 | match &self.inner { |
| 997 | MaybeDetached::Detached(_t) => { |
| 998 | unreachable!() |
| 999 | } |
| 1000 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 1001 | let a = state.as_tree_state().unwrap(); |
| 1002 | a.get_all_hierarchy_nodes_under(parent) |
| 1003 | }), |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | #[allow(non_snake_case)] |
| 1008 | pub fn __internal__next_tree_id(&self) -> TreeID { |
| 1009 | match &self.inner { |
| 1010 | MaybeDetached::Detached(d) => { |
| 1011 | let d = d.lock(); |
| 1012 | TreeID::new(PeerID::MAX, d.value.next_counter) |
| 1013 | } |
| 1014 | MaybeDetached::Attached(a) => a |
| 1015 | .with_txn(|txn| Ok(TreeID::from_id(txn.next_id()))) |
| 1016 | .unwrap(), |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | fn generate_position_at( |
| 1021 | &self, |
| 1022 | target: &TreeID, |
| 1023 | parent: &TreeParentId, |
| 1024 | index: usize, |
| 1025 | cfg: FiIfNotConfigured, |
| 1026 | ) -> FractionalIndexGenResult { |
| 1027 | let MaybeDetached::Attached(a) = &self.inner else { |
| 1028 | unreachable!() |
| 1029 | }; |
| 1030 | a.with_state(|state| { |
| 1031 | let a = state.as_tree_state_mut().unwrap(); |
| 1032 | a.generate_position_at(target, parent, index, cfg) |
| 1033 | }) |
| 1034 | } |
| 1035 | |
| 1036 | /// Get the index of the target node in the parent node |
| 1037 | /// |
| 1038 | /// O(logN) |
| 1039 | pub fn get_index_by_tree_id(&self, target: &TreeID) -> Option<usize> { |
| 1040 | match &self.inner { |
| 1041 | MaybeDetached::Detached(t) => { |
| 1042 | let t = t.lock(); |
| 1043 | t.value.get_index_by_tree_id(target) |
| 1044 | } |
| 1045 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 1046 | let a = state.as_tree_state().unwrap(); |
| 1047 | a.get_index_by_tree_id(target) |
| 1048 | }), |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | pub fn get_position_by_tree_id(&self, target: &TreeID) -> Option<FractionalIndex> { |
| 1053 | match &self.inner { |
| 1054 | MaybeDetached::Detached(t) => t |
| 1055 | .lock() |
| 1056 | .value |
| 1057 | .parent_links |
| 1058 | .contains_key(target) |
| 1059 | .then(FractionalIndex::default), |
| 1060 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 1061 | let a = state.as_tree_state().unwrap(); |
| 1062 | a.get_position(target) |
| 1063 | }), |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | fn delete_position(&self, parent: &TreeParentId, target: &TreeID) { |
| 1068 | let MaybeDetached::Attached(a) = &self.inner else { |
| 1069 | unreachable!() |
| 1070 | }; |
| 1071 | a.with_state(|state| { |
| 1072 | let a = state.as_tree_state_mut().unwrap(); |
| 1073 | a.try_delete_position_cache(parent, target) |
| 1074 | }) |
| 1075 | } |
| 1076 | |
| 1077 | // use for apply diff |
| 1078 | pub(crate) fn get_index_by_fractional_index( |
| 1079 | &self, |
| 1080 | parent: &TreeParentId, |
| 1081 | node_position: &NodePosition, |
| 1082 | ) -> Option<usize> { |
| 1083 | match &self.inner { |
| 1084 | MaybeDetached::Detached(_) => { |
| 1085 | unreachable!(); |
| 1086 | } |
| 1087 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 1088 | let a = state.as_tree_state().unwrap(); |
| 1089 | a.get_index_by_position(parent, node_position) |
| 1090 | }), |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | pub(crate) fn next_idlp(&self) -> IdLp { |
| 1095 | match &self.inner { |
| 1096 | MaybeDetached::Detached(_) => { |
| 1097 | unreachable!() |
| 1098 | } |
| 1099 | MaybeDetached::Attached(a) => a.with_txn(|txn| Ok(txn.next_idlp())).unwrap(), |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | pub fn is_fractional_index_enabled(&self) -> bool { |
| 1104 | match &self.inner { |
| 1105 | MaybeDetached::Detached(_) => true, |
| 1106 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 1107 | let a = state.as_tree_state().unwrap(); |
| 1108 | a.is_fractional_index_enabled() |
| 1109 | }), |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | /// Set whether to generate fractional index for Tree Position. The LoroDoc is set to use jitter 0 by default. |
| 1114 | /// |
| 1115 | /// The jitter is used to avoid conflicts when multiple users are creating the node at the same position. |
| 1116 | /// value 0 is default, which means no jitter, any value larger than 0 will enable jitter. |
| 1117 | /// |
| 1118 | /// Generally speaking, jitter will affect the growth rate of document size. |
| 1119 | /// [Read more about it](https://www.loro.dev/blog/movable-tree#implementation-and-encoding-size) |
| 1120 | pub fn enable_fractional_index(&self, jitter: u8) { |
| 1121 | match &self.inner { |
| 1122 | MaybeDetached::Detached(_) => { |
| 1123 | // No-op on detached trees |
| 1124 | let _ = jitter; |
| 1125 | } |
| 1126 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 1127 | let a = state.as_tree_state_mut().unwrap(); |
| 1128 | a.enable_generate_fractional_index(jitter); |
| 1129 | }), |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | /// Disable the fractional index generation for Tree Position when |
| 1134 | /// you don't need the Tree's siblings to be sorted. The fractional index will be always default. |
| 1135 | /// |
| 1136 | /// The LoroDoc is set to disable fractional index by default. |
| 1137 | pub fn disable_fractional_index(&self) { |
| 1138 | match &self.inner { |
| 1139 | MaybeDetached::Detached(_) => { |
| 1140 | // No-op on detached trees |
| 1141 | } |
| 1142 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 1143 | let a = state.as_tree_state_mut().unwrap(); |
| 1144 | a.disable_generate_fractional_index(); |
| 1145 | }), |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | pub fn is_deleted(&self) -> bool { |
| 1150 | match &self.inner { |
| 1151 | MaybeDetached::Detached(_) => false, |
| 1152 | MaybeDetached::Attached(a) => a.is_deleted(), |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | pub fn is_empty(&self) -> bool { |
| 1157 | match &self.inner { |
| 1158 | MaybeDetached::Detached(t) => { |
| 1159 | let t = t.lock(); |
| 1160 | t.value.map.is_empty() |
| 1161 | } |
| 1162 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 1163 | let a = state.as_tree_state().unwrap(); |
| 1164 | a.is_empty() |
| 1165 | }), |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | pub fn get_last_move_id(&self, target: &TreeID) -> Option<ID> { |
| 1170 | match &self.inner { |
| 1171 | MaybeDetached::Detached(_) => None, |
| 1172 | MaybeDetached::Attached(a) => a.with_state(|state| { |
| 1173 | let a = state.as_tree_state().unwrap(); |
| 1174 | a.get_last_move_id(target) |
| 1175 | }), |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | pub fn clear(&self) -> LoroResult<()> { |
| 1180 | match &self.inner { |
| 1181 | MaybeDetached::Detached(t) => { |
| 1182 | let mut t = t.lock(); |
| 1183 | t.value.map.clear(); |
| 1184 | t.value.children_links.clear(); |
| 1185 | t.value.parent_links.clear(); |
| 1186 | Ok(()) |
| 1187 | } |
| 1188 | MaybeDetached::Attached(_) => { |
| 1189 | let nodes = self.get_nodes_under(TreeParentId::Root); |
| 1190 | for node in nodes { |
| 1191 | self.delete(node.id)?; |
| 1192 | } |
| 1193 | Ok(()) |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | } |