forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

mergeable.rs93 lines, 3.6 KiBHistoryRawDownload
1//! Mergeable-container edge resolution on `DocState`.
2//!
3//! Mergeable child containers (created via `MapHandler::ensure_mergeable_*`) use deterministic
4//! `ContainerID::Root` ids in a reserved namespace. The cid encodes `(parent, key, kind)`, while
5//! the parent map's compact binary child ref is the single source of truth for whether that child
6//! is currently active.
7//!
8//! The ref is intentionally stored as a specially constructed binary marker value. This keeps
9//! user-editable strings from being reinterpreted as internal container topology, and lets the
10//! resolver fail closed unless the ref's digest matches the exact `(parent, key, kind)` being
11//! resolved.
12
13use loro_common::{ContainerID, ContainerType, InternalString, LoroMapValue};
14
15use crate::{container::idx::ContainerIdx, event::Index};
16
17use super::{ContainerState, DocState};
18
19impl DocState {
20 /// Resolve a child edge using the document-level logical edge model.
21 ///
22 /// Ordinary child containers use the materialized child index stored in their parent state.
23 /// Mergeable children are resolved lazily from their deterministic cid plus the parent map's
24 /// current binary child ref, so snapshot/update import does not need any eager mergeable-edge
25 /// rebuild.
26 pub(super) fn get_logical_child_index(
27 &mut self,
28 parent_idx: ContainerIdx,
29 child_id: &ContainerID,
30 ) -> Option<Index> {
31 if let Some((parent_id, key, kind)) = child_id.parse_mergeable() {
32 return self.resolve_mergeable_child_index(parent_idx, &parent_id, &key, kind);
33 }
34
35 self.store
36 .get_container_mut(parent_idx)
37 .and_then(|parent_state| parent_state.get_child_index(child_id))
38 }
39
40 pub(super) fn contains_logical_child(
41 &mut self,
42 parent_idx: ContainerIdx,
43 child_id: &ContainerID,
44 ) -> bool {
45 self.get_logical_child_index(parent_idx, child_id).is_some()
46 }
47
48 fn resolve_mergeable_child_index(
49 &mut self,
50 parent_idx: ContainerIdx,
51 encoded_parent_id: &ContainerID,
52 key: &str,
53 kind: ContainerType,
54 ) -> Option<Index> {
55 if parent_idx.get_type() != ContainerType::Map {
56 return None;
57 }
58
59 let actual_parent_id = self.arena.idx_to_id(parent_idx)?;
60 if &actual_parent_id != encoded_parent_id {
61 return None;
62 }
63
64 let value = self.store.map_get(parent_idx, key)?;
65 if loro_common::parse_mergeable_marker(&actual_parent_id, key, &value) == Some(kind) {
66 Some(Index::Key(key.into()))
67 } else {
68 None
69 }
70 }
71
72 /// Derive `(key, cid)` pairs for the active mergeable children of a map directly from its
73 /// current value table.
74 ///
75 /// This is used by deep-value and alive-container walks. It mirrors regular map semantics:
76 /// the current value at a key determines which child is visible. For mergeable children, that
77 /// value is a compact binary child ref rather than `LoroValue::Container`.
78 pub(super) fn mergeable_children_from_value(
79 &self,
80 parent_id: &ContainerID,
81 map_value: &LoroMapValue,
82 ) -> Vec<(InternalString, ContainerID)> {
83 let mut ans = Vec::new();
84 for (key, value) in map_value.iter() {
85 if let Some(kind) = loro_common::parse_mergeable_marker(parent_id, key, value) {
86 let key_istr: InternalString = key.as_str().into();
87 let cid = ContainerID::new_mergeable(parent_id, key, kind);
88 ans.push((key_istr, cid));
89 }
90 }
91 ans
92 }
93}