diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/access/rules.rs | 26 | ||||
| -rw-r--r-- | src/document/store.rs | 52 |
2 files changed, 58 insertions, 20 deletions
diff --git a/src/access/rules.rs b/src/access/rules.rs index c84adc9..1df1d58 100644 --- a/src/access/rules.rs +++ b/src/access/rules.rs @@ -98,6 +98,9 @@ pub(crate) struct Reads { /// Whether the document's own policy, at its heads then, is strict /// (`before_strict`): kept across `seen`, read again once the document takes more. strict_now: Option<(Vec<ChangeHash>, bool)>, + /// The changes that turned the document strict, at its heads then (`turned_strict`): + /// kept as `strict_now` is. + turned_now: Option<(Vec<ChangeHash>, Vec<ChangeHash>)>, } impl Reads { @@ -107,6 +110,7 @@ impl Reads { *self = Self { seen: seen.to_vec(), strict_now: self.strict_now.take(), + turned_now: self.turned_now.take(), ..Self::default() }; } @@ -125,6 +129,15 @@ impl Reads { } } } + + /// The changes that turned `doc` strict (`turned_strict`): found once for its heads. + fn turned_now(&mut self, doc: &Document) -> &[ChangeHash] { + let heads = doc.get_heads(); + if self.turned_now.as_ref().is_none_or(|(at, _)| *at != heads) { + self.turned_now = Some((heads, turned_strict(doc))); + } + &self.turned_now.as_ref().expect("just found").1 + } } impl Reads { @@ -179,7 +192,7 @@ pub(crate) fn judge_reading( .clone(); if !policy.strict() && reads.strict_now(doc) - && let Some(why) = before_strict(doc, change) + && let Some(why) = before_strict(doc, change, reads.turned_now(doc)) { return Err(Refusal::Rule(why)); } @@ -378,10 +391,10 @@ pub(crate) fn judge_reading( /// not, is refused like any other, so a change made later to look as old can't take its /// place. A swarm strict from its genesis change (`--bootstrap-policy`) has no such /// time. Asked only where `doc`'s policy is strict: elsewhere, nothing changes. -fn before_strict(doc: &Document, change: &Change<'_>) -> Option<String> { +fn before_strict(doc: &Document, change: &Change<'_>, turned: &[ChangeHash]) -> Option<String> { let held = crate::document::changes::holds(doc, &change.hash); - let beside = turned_strict(doc).into_iter().find(|turned| { - *turned != change.hash + let beside = turned.iter().find(|turned| { + **turned != change.hash && !in_history(doc, turned, change.deps) && !(held && in_history(doc, &change.hash, std::slice::from_ref(turned))) })?; @@ -399,9 +412,10 @@ fn turned_strict(doc: &Document) -> Vec<ChangeHash> { let strict_at = |heads: &[ChangeHash]| { At::new(doc, heads).is_some_and(|at| super::policy::of(&at).strict()) }; + let made = store::made(doc); let mut turned = Vec::new(); let mut looked: HashSet<ChangeHash> = HashSet::new(); - let mut writes = store::writers(doc, doc, &path); + let mut writes = store::writers(&made, doc, &path); while let Some((hash, deps)) = writes.pop() { if !looked.insert(hash) { continue; @@ -410,7 +424,7 @@ fn turned_strict(doc: &Document) -> Vec<ChangeHash> { turned.push(hash); } if let Some(before) = At::new(doc, &deps) { - writes.extend(store::writers(doc, &before, &path)); + writes.extend(store::writers(&made, &before, &path)); } } turned diff --git a/src/document/store.rs b/src/document/store.rs index 1f26053..27edf49 100644 --- a/src/document/store.rs +++ b/src/document/store.rs @@ -281,11 +281,40 @@ fn signs_values(doc: &impl ReadDoc) -> bool { .is_some_and(|json| crate::access::policy::read(&json).strict()) } -/// 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. +/// Every change a document holds, by its writer and the numbers of its operations, with +/// the changes it builds on: read once for any number of [`writers`]. +pub(crate) struct Made( + Vec<( + ActorId, + std::ops::Range<u64>, + automerge::ChangeHash, + Vec<automerge::ChangeHash>, + )>, +); + +/// What `doc` holds, for [`writers`]. +pub(crate) fn made(doc: &Automerge) -> Made { + Made( + doc.get_changes(&[]) + .into_iter() + .map(|change| { + let start = change.start_op().get(); + ( + change.actor_id().clone(), + start..start + change.len() as u64, + change.hash(), + change.deps().to_vec(), + ) + }) + .collect(), + ) +} + +/// The changes, of those `made` has, that wrote what `path` holds as `view` 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, + made: &Made, view: &impl ReadDoc, path: &[String], ) -> Vec<(automerge::ChangeHash, Vec<automerge::ChangeHash>)> { @@ -295,18 +324,13 @@ pub(crate) fn writers( { operations_within(view, &maps, leaf.as_str().into(), &mut ids); } - if ids.is_empty() { - return Vec::new(); - } - doc.get_changes(&[]) - .into_iter() - .filter(|change| { - let start = change.start_op().get(); - let made = start..start + change.len() as u64; + made.0 + .iter() + .filter(|(actor, ops, _, _)| { ids.iter() - .any(|(counter, actor)| change.actor_id() == actor && made.contains(counter)) + .any(|(counter, by)| by == actor && ops.contains(counter)) }) - .map(|change| (change.hash(), change.deps().to_vec())) + .map(|(_, _, hash, deps)| (*hash, deps.clone())) .collect() } |
