Skip to main content

kernel/graph/
residency.rs

1// Copyright 2026 Mark Alan Boykin
2// This Source Code Form is subject to the terms of the Mozilla Public
3// License, v. 2.0. If a copy of the MPL was not distributed with this
4// file, You can obtain one at https://mozilla.org/MPL/2.0/.
5// SPDX-License-Identifier: MPL-2.0
6
7//! Replacing an available-data view is a load, not a journaled edit.
8use super::{CoverageNote, Graph, NodeFacetStore};
9use crate::persistence::GraphSnapshot;
10impl Graph {
11    /// Validate the complete candidate before publishing a resident replacement.
12    pub fn replace_loaded_recorded_snapshot(
13        &mut self,
14        snapshot: &GraphSnapshot,
15        facets: &NodeFacetStore,
16        coverage: CoverageNote,
17    ) -> Result<(), super::snapshot::ResourceSnapshotError> {
18        let mut candidate = Self::try_from_recorded_snapshot(snapshot)?;
19        candidate.install_facets(facets.clone())?;
20        candidate.set_known_coverage(coverage);
21        candidate.revision = self
22            .revision
23            .checked_add(1)
24            .expect("graph revision exhausted");
25        candidate.content_revision = self
26            .content_revision
27            .checked_add(1)
28            .expect("content revision exhausted");
29        candidate.url_grouping_revision = self
30            .url_grouping_revision
31            .checked_add(1)
32            .expect("URL revision exhausted");
33        candidate.visit_revision = self
34            .visit_revision
35            .checked_add(1)
36            .expect("visit revision exhausted");
37        candidate.semantic_observation_revision = self
38            .semantic_observation_revision
39            .checked_add(1)
40            .expect("observation revision exhausted");
41        candidate.appearance_admission_revision = self
42            .appearance_admission_revision
43            .checked_add(1)
44            .expect("appearance revision exhausted");
45        *self = candidate;
46        Ok(())
47    }
48}
49#[cfg(test)]
50mod tests {
51    use super::*;
52    #[test]
53    fn replacement_advances_cache_revisions_and_refuses_atomically() {
54        let mut graph = Graph::new();
55        let key = super::super::apply::add_node(
56            &mut graph,
57            None,
58            "https://old.test".into(),
59            Default::default(),
60        );
61        let old = graph.revision();
62        let mut other = Graph::new();
63        super::super::apply::add_node(
64            &mut other,
65            None,
66            "https://new.test".into(),
67            Default::default(),
68        );
69        graph
70            .replace_loaded_recorded_snapshot(
71                &other.to_snapshot_at(0),
72                other.facets(),
73                Default::default(),
74            )
75            .unwrap();
76        assert!(graph.revision() > old);
77        let before = serde_json::to_value(graph.to_snapshot_at(0)).unwrap();
78        let mut broken = other.to_snapshot_at(0);
79        broken.shown_resources[0].resource_id = uuid::Uuid::nil().to_string();
80        assert!(
81            graph
82                .replace_loaded_recorded_snapshot(&broken, other.facets(), Default::default())
83                .is_err()
84        );
85        assert_eq!(
86            serde_json::to_value(graph.to_snapshot_at(0)).unwrap(),
87            before
88        );
89        let _ = key;
90    }
91}