diff options
Diffstat (limited to 'src/document/store.rs')
| -rw-r--r-- | src/document/store.rs | 69 |
1 files changed, 51 insertions, 18 deletions
diff --git a/src/document/store.rs b/src/document/store.rs index bc50535..759d188 100644 --- a/src/document/store.rs +++ b/src/document/store.rs @@ -258,25 +258,58 @@ fn signs_values(doc: &impl ReadDoc) -> bool { }) } -/// The change that wrote the value a read of `path` shows (`candidates`), if `doc` holds -/// one there. -pub(crate) fn written_by(doc: &Automerge, path: &[String]) -> Option<automerge::ChangeHash> { - let (leaf, parents) = path.split_last()?; - let Some(Resolved::Maps(maps)) = resolve_through(doc, parents).ok()? else { - return None; - }; - let (_, id) = candidates(doc, &maps, leaf.as_str()) - .ok()? +/// The changes that wrote what `path` holds as `view` (of `doc`) shows it: each value +/// written there at once and, of a map, every key within it, all the way down; each +/// with the changes it builds on. +pub(crate) fn writers( + doc: &Automerge, + view: &impl ReadDoc, + path: &[String], +) -> Vec<(automerge::ChangeHash, Vec<automerge::ChangeHash>)> { + let mut ids = Vec::new(); + if let Some((leaf, parents)) = path.split_last() + && let Ok(Some(Resolved::Maps(maps))) = resolve_through(view, parents) + { + operations_within(view, &maps, leaf.as_str().into(), &mut ids); + } + if ids.is_empty() { + return Vec::new(); + } + doc.get_changes(&[]) .into_iter() - .next()?; - let ObjId::Id(counter, actor, _) = id else { - return None; - }; - doc.get_changes(&[]).into_iter().find_map(|change| { - let start = change.start_op().get(); - (change.actor_id() == &actor && (start..start + change.len() as u64).contains(&counter)) - .then(|| change.hash()) - }) + .filter(|change| { + let start = change.start_op().get(); + let made = start..start + change.len() as u64; + ids.iter() + .any(|(counter, actor)| change.actor_id() == actor && made.contains(counter)) + }) + .map(|change| (change.hash(), change.deps().to_vec())) + .collect() +} + +/// The operations that wrote `prop` of `parents` as `view` shows it, and, of a map, +/// every key within it. +fn operations_within( + view: &impl ReadDoc, + parents: &[ObjId], + prop: Prop, + ids: &mut Vec<(u64, ActorId)>, +) { + for parent in parents { + let Ok(all) = view.get_all(parent, prop.clone()) else { + continue; + }; + for (value, id) in all { + if let ObjId::Id(counter, actor, _) = &id { + ids.push((*counter, actor.clone())); + } + if matches!(value, Value::Object(ObjType::Map)) { + for key in view.keys(&id) { + operations_within(view, std::slice::from_ref(&id), key.into(), ids); + } + } + } + } } /// What `prop` of `parent` holds as a read shows it, of the values written there at |
