use std::sync::Arc;
use canvist_model::BlockTree;
use canvist_model::Commit;
use canvist_model::Document;
use canvist_scene::FracKey;
use canvist_scene::NodeId;
use canvist_scene::NodeKind;
use canvist_scene::NodeRecord;
use canvist_scene::Prop;
use canvist_scene::PropKey;
use canvist_scene::Props;
use canvist_scene::Scene;
use canvist_scene::SceneSchema;
use canvist_scene::Step;
use canvist_scene::Transaction;
use rustc_hash::FxHashMap;
use rustc_hash::FxHashSet;
use yrs::Map;
use yrs::MapRef;
use yrs::Out;
use yrs::ReadTxn;
use super::LentText;
use super::codec::Pos;
use super::codec::any_to_json;
use super::index::Entry;
use super::index::Index;
use super::index::Placer;
use super::keys;
use super::local::node_map;
use super::local::props_of;
use super::local::text_of;
use crate::CollabError;
use crate::mirror::Mirror;
use crate::registry::branch_id;
use crate::remote::Repair;
use crate::remote::record::Record;
#[derive(Debug, Default)]
pub(crate) struct Dirty {
pub(crate) nodes: FxHashSet<NodeId>,
pub(crate) props: FxHashMap<NodeId, FxHashSet<Arc<str>>>,
pub(crate) text: FxHashMap<NodeId, Vec<Record>>,
}
#[derive(Debug)]
pub(crate) struct TextFrame {
pub(crate) mirror: Mirror,
pub(crate) array: Option<yrs::ID>,
}
impl Dirty {
pub(crate) fn is_empty(&self) -> bool {
self.nodes.is_empty() && self.props.is_empty() && self.text.is_empty()
}
}
type Placement = (NodeId, FracKey);
pub(crate) type TextRepairs = Vec<(NodeId, Vec<Repair>)>;
pub(crate) struct Reconciler<'a, T: ReadTxn> {
pub(crate) txn: &'a T,
pub(crate) nodes: &'a MapRef,
pub(crate) schema: &'a Arc<SceneSchema>,
pub(crate) index: &'a mut Index,
pub(crate) texts: &'a mut FxHashMap<NodeId, TextFrame>,
pub(crate) clock: &'a mut u64,
pub(crate) repairs: TextRepairs,
}
#[derive(Debug, Default)]
struct Plan {
inserts: Vec<NodeId>,
moves: Vec<(NodeId, NodeId, FracKey)>,
removes: Vec<NodeId>,
rebuild: Vec<NodeId>,
placed: FxHashMap<NodeId, (NodeId, FracKey)>,
}
impl<T: ReadTxn> Reconciler<'_, T> {
pub(crate) fn run<'l>(
&mut self,
transaction: &mut Transaction<'_>,
dirty: Dirty,
mut lent: Option<&mut (dyn LentText + 'l)>,
) -> Result<(), CollabError> {
for &id in &dirty.nodes {
let entry = self.read_entry(id);
self.index.set(id, entry);
}
let plan = self.plan(transaction.scene(), &dirty.nodes);
if !plan.rebuild.is_empty() {
transaction.remove(&plan.rebuild)?;
}
self.insert(transaction, &plan)?;
move_all(transaction, plan.moves.clone())?;
if !plan.removes.is_empty() {
transaction.remove(&plan.removes)?;
}
for id in &plan.removes {
self.texts.remove(id);
}
let inserted: FxHashSet<NodeId> = plan.inserts.iter().copied().collect();
self.props(transaction, &dirty, &inserted)?;
for (id, records) in dirty.text {
if let Some(frame) = self.texts.get(&id).filter(|_| !inserted.contains(&id)) {
frame.mirror.receive(records);
}
}
self.text(transaction, &mut lent)
}
pub(crate) fn read_entry(&mut self, id: NodeId) -> Option<Entry> {
let map = node_map(self.nodes, self.txn, id)?;
let kind = match map.get(self.txn, keys::TYPE) {
Some(Out::Any(yrs::Any::String(name))) => self.schema.node_type(&name),
_ => None,
};
let pos = match map.get(self.txn, keys::POS) {
Some(Out::Any(any)) => Pos::from_any(&any),
_ => None,
};
if let Some(pos) = &pos {
*self.clock = (*self.clock).max(pos.clock);
}
Some(Entry { kind, pos })
}
fn plan(&self, scene: &Scene, dirty: &FxHashSet<NodeId>) -> Plan {
let in_scene = dirty
.iter()
.filter(|id| scene.contains(**id))
.flat_map(|&id| scene.subtree(id));
let affected = self.index.closure(dirty.iter().copied().chain(in_scene));
let mut placer = Placer::new(self.index, self.schema);
let mut plan = Plan::default();
let mut sorted: Vec<NodeId> = affected.into_iter().collect();
sorted.sort_unstable();
for id in sorted {
let desired = placer.placement(id).filter(|_| self.has_text_if_needed(id));
let current = scene
.node(id)
.map(|node| (node.parent(), node.key().clone()));
self.classify(&mut plan, id, (desired, current), dirty);
}
plan
}
fn is_text(&self, id: NodeId) -> bool {
self.index
.get(id)
.and_then(|entry| entry.kind)
.is_some_and(|kind| self.schema.spec(kind).kind() == NodeKind::Text)
}
fn classify(
&self,
plan: &mut Plan,
id: NodeId,
(desired, current): (Option<Placement>, Option<Placement>),
dirty: &FxHashSet<NodeId>,
) {
match (desired, current) {
(Some(desired), None) => {
plan.inserts.push(id);
plan.placed.insert(id, desired);
}
(Some(desired), Some(_))
if dirty.contains(&id) && self.is_text(id) && self.text_replaced(id) =>
{
plan.rebuild.push(id);
plan.inserts.push(id);
plan.placed.insert(id, desired);
}
(Some(desired), Some(current)) if desired != current => {
plan.moves.push((id, desired.0, desired.1));
}
(None, Some(_)) => plan.removes.push(id),
_ => {}
}
}
fn has_text_if_needed(&self, id: NodeId) -> bool {
!self.is_text(id)
|| node_map(self.nodes, self.txn, id)
.is_some_and(|map| text_of(&map, self.txn).is_some())
}
fn insert(
&mut self,
transaction: &mut Transaction<'_>,
plan: &Plan,
) -> Result<(), CollabError> {
if plan.inserts.is_empty() {
return Ok(());
}
let depth = |mut id: NodeId| {
let mut depth = 0usize;
while let Some((parent, _)) = plan.placed.get(&id) {
depth += 1;
id = *parent;
}
depth
};
let mut order = plan.inserts.clone();
order.sort_by_key(|&id| (depth(id), id));
let client = transaction.scene().client();
let records = order
.into_iter()
.map(|id| self.record(id, &plan.placed[&id], client))
.collect::<Result<Vec<_>, _>>()?;
transaction.step(Step::InsertNodes { nodes: records })?;
Ok(())
}
fn record(
&mut self,
id: NodeId,
placement: &(NodeId, FracKey),
client: u32,
) -> Result<NodeRecord, CollabError> {
let kind = self
.index
.get(id)
.and_then(|entry| entry.kind)
.ok_or(CollabError::SceneNode(id))?;
let behaviour = self.schema.spec(kind).kind();
let map = node_map(self.nodes, self.txn, id).ok_or(CollabError::SceneNode(id))?;
let props = props_of(&map, self.txn).map_or_else(
|| Props::for_kind(behaviour),
|props| read_props(&props, self.txn, behaviour),
);
let text = match behaviour {
NodeKind::Text => Some(self.text_record(id, &map, client)?),
_ => None,
};
Ok(NodeRecord {
id,
kind,
parent: placement.0,
key: placement.1.clone(),
props,
text,
})
}
fn text_record(
&mut self,
id: NodeId,
map: &MapRef,
client: u32,
) -> Result<Vec<BlockTree>, CollabError> {
let array = text_of(map, self.txn).ok_or(CollabError::SceneNode(id))?;
let schema = self.schema.text().ok_or(CollabError::SceneNode(id))?;
let mut mirror = Mirror::new(schema.clone());
let (doc, repairs) = mirror.document(self.txn, &array, client)?;
self.texts.insert(
id,
TextFrame {
mirror,
array: branch_id(&array),
},
);
self.repairs.push((id, repairs));
Ok(doc
.children(None)
.iter()
.filter_map(|&block| doc.tree(block))
.collect())
}
fn props(
&mut self,
transaction: &mut Transaction<'_>,
dirty: &Dirty,
inserted: &FxHashSet<NodeId>,
) -> Result<(), CollabError> {
let mut targets: Vec<NodeId> = dirty
.nodes
.iter()
.chain(dirty.props.keys())
.copied()
.filter(|id| !inserted.contains(id) && transaction.scene().contains(*id))
.collect();
targets.sort_unstable();
targets.dedup();
for id in targets {
for prop in self.prop_changes(transaction.scene(), id, dirty) {
transaction.set(id, prop)?;
}
}
Ok(())
}
fn prop_changes(&self, scene: &Scene, id: NodeId, dirty: &Dirty) -> Vec<Prop> {
let (Some(node), Some(kind)) = (scene.node(id), scene.kind(id)) else {
return Vec::new();
};
let shared = node_map(self.nodes, self.txn, id)
.and_then(|map| props_of(&map, self.txn))
.map_or_else(
|| Props::for_kind(kind),
|props| read_props(&props, self.txn, kind),
);
let keys: Vec<PropKey> = if dirty.nodes.contains(&id) {
all_keys(node.props(), &shared)
} else {
dirty
.props
.get(&id)
.into_iter()
.flatten()
.filter_map(|name| PropKey::parse(name))
.collect()
};
keys.into_iter()
.map(|key| shared.get(&key))
.filter(|prop| *prop != node.props().get(&prop.key()))
.collect()
}
fn text<'l>(
&mut self,
transaction: &mut Transaction<'_>,
lent: &mut Option<&mut (dyn LentText + 'l)>,
) -> Result<(), CollabError> {
let mut pending: Vec<NodeId> = self
.texts
.iter()
.filter(|(_, frame)| frame.mirror.has_changes())
.map(|(&id, _)| id)
.collect();
pending.sort_unstable();
for id in pending {
let txn = self.txn;
let Some(array) = node_map(self.nodes, txn, id).and_then(|map| text_of(&map, txn))
else {
continue;
};
let Some(frame) = self.texts.get_mut(&id) else {
continue;
};
let mut repairs = Vec::new();
let mut change = |doc: &mut Document| -> Result<Commit, CollabError> {
let (commit, found) = frame.mirror.mirror(txn, &array, doc)?;
repairs = found;
Ok(commit)
};
match lent.as_deref_mut() {
Some(holder) if holder.node() == id => {
holder.apply(&mut change)?;
}
_ => transaction.text_external(id, change)?,
}
self.repairs.push((id, repairs));
}
Ok(())
}
pub(crate) fn text_replaced(&self, id: NodeId) -> bool {
let current = node_map(self.nodes, self.txn, id)
.and_then(|map| text_of(&map, self.txn))
.and_then(|array| branch_id(&array));
let mirrored = self.texts.get(&id).and_then(|frame| frame.array);
current != mirrored
}
}
pub(crate) fn read_props<T: ReadTxn>(map: &MapRef, txn: &T, kind: NodeKind) -> Props {
let mut props = Props::for_kind(kind);
for (name, value) in map.iter(txn) {
let Out::Any(any) = value else {
continue;
};
let Some(key) = PropKey::parse(name) else {
continue;
};
if let Ok(prop) = Prop::from_json(&key, &any_to_json(&any))
&& prop.fits(kind)
{
props.set(prop);
}
}
props
}
fn all_keys(a: &Props, b: &Props) -> Vec<PropKey> {
let mut keys: Vec<PropKey> = PropKey::CORE.to_vec();
keys.extend(
a.custom
.keys()
.chain(b.custom.keys())
.map(|name| PropKey::Custom(name.clone())),
);
keys.extend(
a.overrides
.keys()
.chain(b.overrides.keys())
.map(|(target, key)| PropKey::Override(*target, Box::new(key.clone()))),
);
keys.sort();
keys.dedup();
keys
}
fn move_all(
transaction: &mut Transaction<'_>,
mut moves: Vec<(NodeId, NodeId, FracKey)>,
) -> Result<(), CollabError> {
while !moves.is_empty() {
let scene = transaction.scene();
let ready = moves
.iter()
.position(|&(node, parent, _)| parent != node && !scene.is_ancestor(node, parent))
.ok_or(CollabError::Diverged)?;
let (node, parent, key) = moves.swap_remove(ready);
transaction.step(Step::Move { node, parent, key })?;
}
Ok(())
}