aboutsummaryrefslogtreecommitdiffstats
path: root/src/document
diff options
context:
space:
mode:
Diffstat (limited to 'src/document')
-rw-r--r--src/document/inspect.rs3
-rw-r--r--src/document/store.rs55
2 files changed, 52 insertions, 6 deletions
diff --git a/src/document/inspect.rs b/src/document/inspect.rs
index f0356de..ffdf536 100644
--- a/src/document/inspect.rs
+++ b/src/document/inspect.rs
@@ -110,7 +110,8 @@ pub(crate) fn placed(doc: &Document, path: &[String]) -> Path {
break;
}
place = place.child(&Key::Map(key.as_str().into()));
- match doc.get(&object, key.as_str()) {
+ // As a read shows it, of values written there at once (`store::shown`).
+ match super::store::shown(doc, &object, key.as_str()) {
Ok(Some((automerge::Value::Object(kind), id))) => {
place.in_list = matches!(kind, automerge::ObjType::List | automerge::ObjType::Text);
object = id;
diff --git a/src/document/store.rs b/src/document/store.rs
index ba1a1ec..8b8894c 100644
--- a/src/document/store.rs
+++ b/src/document/store.rs
@@ -258,12 +258,25 @@ fn signs_values(doc: &impl ReadDoc) -> bool {
})
}
+/// What `prop` of `parent` holds as a read shows it, of the values written there at
+/// once (`candidates`): the value, and its operation's ID.
+pub(crate) fn shown(
+ doc: &impl ReadDoc,
+ parent: &ObjId,
+ prop: impl Into<Prop> + Clone,
+) -> Result<Option<(Value<'static>, ObjId)>, StoreError> {
+ Ok(candidates(doc, std::slice::from_ref(parent), prop)?
+ .into_iter()
+ .next())
+}
+
/// All concurrent values of `key` across a merged set of parent maps, the one that
/// counts first. Newest first; where the policy asks for signed values
/// (`signs_values`), signed values (`document::signed`) before any other, the highest
/// `seq` first, of equal ones the lower digest, then the others, newest first. So in a
/// strict swarm which signed value a place shows is its user's to say, not the order
-/// of operations, which whoever writes a change steers. Elsewhere nothing changes.
+/// of operations, which whoever writes a change steers. Elsewhere, and for a list's
+/// items (no signed value is one), nothing changes: Automerge's own choice.
fn candidates(
doc: &impl ReadDoc,
parents: &[ObjId],
@@ -276,7 +289,7 @@ fn candidates(
}
}
all.sort_by(|a, b| op_order(&b.1).cmp(&op_order(&a.1)));
- if all.len() > 1 {
+ if all.len() > 1 && matches!(prop.into(), Prop::Map(_)) {
let orders: Vec<_> = all.iter().map(|(value, _)| signed_order(value)).collect();
if orders.iter().any(Option::is_some) && signs_values(doc) {
let mut ordered: Vec<_> = orders.into_iter().zip(all).collect();
@@ -1203,7 +1216,7 @@ pub(crate) fn increment_path(
doc.put(&maps[0], leaf.as_str(), ScalarValue::counter(by))?;
return Ok(by);
}
- // The map that holds the newest value.
+ // The map that holds the value a read shows.
let newest = &holds[0].1;
let map = maps
.iter()
@@ -1228,9 +1241,9 @@ pub(crate) fn increment_path(
counter_at(doc, &parent, prop)
}
-/// The value of the counter `prop` of `parent` holds (the newest of its values).
+/// The value of the counter `prop` of `parent` holds, as a read shows it (`shown`).
fn counter_at(doc: &impl ReadDoc, parent: &ObjId, prop: Prop) -> Result<i64, StoreError> {
- match doc.get(parent, prop)? {
+ match shown(doc, parent, prop)? {
Some((Value::Scalar(scalar), _)) => match scalar.as_ref() {
ScalarValue::Counter(counter) => Ok(i64::from(counter)),
_ => Err(StoreError::NotACounter),
@@ -1852,6 +1865,38 @@ mod tests {
);
}
+ /// A counter beside an envelope, where the policy asks for signed values: a read
+ /// shows the envelope, and an increment finds no counter to add to.
+ #[test]
+ fn a_counter_goes_by_what_a_read_shows() {
+ let doc = |actor: &[u8]| crate::document::Document::new().with_actor(ActorId::from(actor));
+ let (mut ours, mut theirs) = (doc(b"a"), doc(b"b"));
+ try_change(&mut ours, |doc| {
+ write_path(
+ doc,
+ &path("_continuum/access/policy"),
+ &json!({"version": 1, "values": "signed"}),
+ )
+ })
+ .unwrap();
+ theirs.merge(&mut ours).unwrap();
+ try_change(&mut ours, |doc| {
+ increment_path(doc, &path("n"), 1).map(drop)
+ })
+ .unwrap();
+ try_change(&mut theirs, |doc| {
+ write_path(doc, &path("n"), &json!(envelope_of(&json!(5), 0)))
+ })
+ .unwrap();
+ ours.merge(&mut theirs).unwrap();
+ assert_eq!(read_plain(&ours, &path("n")).unwrap(), Some(json!(5)));
+ let mut tx = ours.transaction();
+ assert!(matches!(
+ increment_path(&mut tx, &path("n"), 1),
+ Err(StoreError::NotACounter)
+ ));
+ }
+
/// An envelope reads as the value it holds, a signed `null` as nothing, anything
/// else (a string shaped like one but not one in every respect too) as it is.
#[test]