kernel/graph/
input_key.rs1use super::Graph;
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
9pub struct GraphInputKey {
10 source: u64,
11 version: u64,
12}
13
14fn fresh() -> u64 {
15 use std::sync::atomic::{AtomicU64, Ordering};
16 static NEXT: AtomicU64 = AtomicU64::new(1);
17 NEXT.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |n| n.checked_add(1))
18 .expect("graph input key space exhausted")
19}
20
21impl GraphInputKey {
22 pub(super) fn new_source() -> Self {
23 let source = fresh();
24 Self {
25 source,
26 version: source,
27 }
28 }
29 pub(super) fn advance(&mut self) {
30 self.version = fresh();
31 }
32 pub fn same_source(self, other: Self) -> bool {
35 self.source == other.source
36 }
37}
38
39impl Graph {
40 pub fn structural_input_key(&self) -> GraphInputKey {
41 self.runtime_input_keys[0]
42 }
43 pub fn content_input_key(&self) -> GraphInputKey {
44 self.runtime_input_keys[1]
45 }
46 pub fn url_grouping_input_key(&self) -> GraphInputKey {
47 self.runtime_input_keys[2]
48 }
49 pub fn visit_input_key(&self) -> GraphInputKey {
50 self.runtime_input_keys[3]
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57 #[test]
58 fn input_keys_preserve_clones_separate_forks_and_do_not_cross_snapshots() {
59 let mut graph = Graph::new();
60 let node = graph.add_node("https://one.example".into(), Default::default());
61 let structural = graph.structural_input_key();
62 let content = graph.content_input_key();
63 let mut clone = graph.clone();
64 assert_eq!(clone.structural_input_key(), structural);
65 clone.set_node_title(node, "Edited".into());
66 assert_eq!(clone.structural_input_key(), structural);
67 assert_ne!(clone.content_input_key(), content);
68 assert!(clone.content_input_key().same_source(content));
69 let mut fork = graph.clone();
70 fork.set_node_title(node, "Different".into());
71 assert_eq!(clone.content_revision(), fork.content_revision());
72 assert_ne!(clone.content_input_key(), fork.content_input_key());
73 let restored = Graph::from_snapshot(&graph.to_snapshot());
74 assert!(!restored.structural_input_key().same_source(structural));
75 assert_ne!(
76 Graph::new().structural_input_key(),
77 Graph::new().structural_input_key()
78 );
79 }
80}