aboutsummaryrefslogtreecommitdiffstats
path: root/src/document/view.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-30 17:57:55 +0000
committertv <tv@krebsco.de>2026-11-30 17:57:55 +0000
commit00e88cd983766abec32b5a14a8cbf20932a27f3c (patch)
treefaf9cba0a32b2cc6b6026151f37ddd13e3b06e2f /src/document/view.rs
parent93defc68f9b99da921f4746e91e5cee9e6e3f1b9 (diff)
swarm: members read of a set's entries over the document's
What a set hands the node to read was read in the document's place; it is read over it now (view::Listed): at a path, what the set's tree holds, else what the document does, and maps of both as one. A node on a set alone reads the same as before, its document being empty; one holding its store in both, to come, reads the members of both.
Diffstat (limited to 'src/document/view.rs')
-rw-r--r--src/document/view.rs161
1 files changed, 155 insertions, 6 deletions
diff --git a/src/document/view.rs b/src/document/view.rs
index ff93ae6..8df88b3 100644
--- a/src/document/view.rs
+++ b/src/document/view.rs
@@ -227,12 +227,96 @@ impl Write for Tree {
}
}
-/// Where the node reads its members: `entries`, where its document keeps them apart
-/// and hands them over as a tree (`AppState::entries`); else `doc` itself.
-pub(crate) fn members_of<'a>(entries: &'a Option<Tree>, doc: &'a dyn Read) -> &'a dyn Read {
- match entries {
- Some(tree) => tree,
- None => doc,
+/// Where the node reads its members, the policy and the swarm's ID: of what its
+/// document keeps apart and hands over as a tree (`AppState::entries`), where it does,
+/// over the document it holds. A node holding its store in a set of events alone reads
+/// the tree, the document beside being empty; one holding it in both reads the members
+/// of both.
+pub(crate) fn members_of<'a>(entries: &'a Option<Tree>, doc: &'a dyn Read) -> Listed<'a> {
+ Listed {
+ over: entries.as_ref(),
+ under: doc,
+ }
+}
+
+/// A tree over a document (`members_of`): at a path, what the tree holds there, else
+/// what the document does; where both hold maps, the two as one, the tree's values over
+/// the document's.
+pub(crate) struct Listed<'a> {
+ over: Option<&'a Tree>,
+ under: &'a dyn Read,
+}
+
+/// `over` on `under`: maps as one, key by key; anything else, `over`.
+fn laid(over: JsonValue, under: JsonValue) -> JsonValue {
+ match (over, under) {
+ (JsonValue::Object(mut over), JsonValue::Object(under)) => {
+ for (key, below) in under {
+ let merged = match over.remove(&key) {
+ Some(above) => laid(above, below),
+ None => below,
+ };
+ over.insert(key, merged);
+ }
+ JsonValue::Object(over)
+ }
+ (over, _) => over,
+ }
+}
+
+impl Listed<'_> {
+ fn read(
+ &self,
+ path: &[String],
+ under: impl FnOnce(&dyn Read) -> Result<Option<JsonValue>, StoreError>,
+ ) -> Result<Option<JsonValue>, StoreError> {
+ let Some(over) = self.over else {
+ return under(self.under);
+ };
+ // The tree first: where it holds a value that is no map, the document isn't
+ // asked, nor is what it holds on the way in the way.
+ match over.read_path(path) {
+ Ok(Some(above)) if !above.is_object() => Ok(Some(above)),
+ Ok(above) => match (above, under(self.under)) {
+ (Some(above), Ok(Some(below))) => Ok(Some(laid(above, below))),
+ (Some(above), _) => Ok(Some(above)),
+ (None, below) => below,
+ },
+ Err(_) => under(self.under),
+ }
+ }
+}
+
+impl Read for Listed<'_> {
+ fn read_path(&self, path: &[String]) -> Result<Option<JsonValue>, StoreError> {
+ self.read(path, |doc| doc.read_path(path))
+ }
+
+ fn read_plain(&self, path: &[String]) -> Result<Option<JsonValue>, StoreError> {
+ self.read(path, |doc| doc.read_plain(path))
+ }
+
+ fn read_typed(&self, path: &[String]) -> Result<Option<JsonValue>, StoreError> {
+ self.read(path, |doc| doc.read_typed(path))
+ }
+
+ fn keys_at(&self, path: &[String]) -> Result<Option<Vec<String>>, StoreError> {
+ let below = self.under.keys_at(path);
+ let Some(over) = self.over else {
+ return below;
+ };
+ match (over.keys_at(path)?, below) {
+ (Some(mut above), Ok(Some(below))) => {
+ for key in below {
+ if !above.contains(&key) {
+ above.push(key);
+ }
+ }
+ Ok(Some(above))
+ }
+ (Some(above), _) => Ok(Some(above)),
+ (None, below) => below,
+ }
}
}
@@ -309,4 +393,69 @@ mod tree_tests {
tree.delete_path(&path(&["a", "d"]), true).unwrap();
assert_eq!(tree.read_path(&[]).unwrap(), Some(json!({})));
}
+
+ #[test]
+ fn a_tree_over_a_document_reads_as_both() {
+ let mut under = Tree::new();
+ under
+ .write_path(&path(&["m", "a"]), &json!({"alias": "a", "tier": 1}))
+ .unwrap();
+ under
+ .write_path(&path(&["m", "b"]), &json!({"alias": "b"}))
+ .unwrap();
+ under
+ .write_path(&path(&["only", "under"]), &json!(1))
+ .unwrap();
+ let mut over = Tree::new();
+ over.write_path(&path(&["m", "a"]), &json!({"alias": "A"}))
+ .unwrap();
+ over.write_path(&path(&["m", "c"]), &json!({"alias": "c"}))
+ .unwrap();
+ over.write_path(&path(&["policy"]), &json!("p")).unwrap();
+ let entries = Some(over);
+ let listed = members_of(&entries, &under);
+ // Keys of both; a map of both as one, the tree's values over the document's.
+ let mut keys = listed.keys_at(&path(&["m"])).unwrap().expect("a map");
+ keys.sort();
+ assert_eq!(keys, path(&["a", "b", "c"]));
+ assert_eq!(
+ listed.read_path(&path(&["m", "a"])).unwrap(),
+ Some(json!({"alias": "A", "tier": 1}))
+ );
+ assert_eq!(
+ listed.read_path(&path(&["m", "a", "alias"])).unwrap(),
+ Some(json!("A"))
+ );
+ assert_eq!(
+ listed.read_path(&path(&["m", "b", "alias"])).unwrap(),
+ Some(json!("b"))
+ );
+ assert_eq!(
+ listed.read_plain(&path(&["m", "c", "alias"])).unwrap(),
+ Some(json!("c"))
+ );
+ assert_eq!(
+ listed.read_typed(&path(&["policy"])).unwrap(),
+ Some(json!("p"))
+ );
+ assert_eq!(
+ listed.read_path(&path(&["only", "under"])).unwrap(),
+ Some(json!(1))
+ );
+ assert_eq!(listed.read_path(&path(&["nowhere"])).unwrap(), None);
+ // No tree: the document as it is.
+ let none = None;
+ let listed = members_of(&none, &under);
+ assert_eq!(
+ listed.read_path(&path(&["m", "a", "alias"])).unwrap(),
+ Some(json!("a"))
+ );
+ assert_eq!(
+ listed
+ .keys_at(&path(&["m"]))
+ .unwrap()
+ .map(|keys| keys.len()),
+ Some(2)
+ );
+ }
}