Skip to main content

selene_core/
origin.rs

1//! Mutation origin metadata per spec 02 section 8.
2
3use serde::{Deserialize, Serialize};
4
5use crate::NodeId;
6
7/// Origin of a graph mutation.
8#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize)]
9pub enum Origin {
10    /// Mutation originated in this selene-db instance.
11    #[default]
12    Local,
13    /// Reserved for future replication (no production producer today).
14    ///
15    /// The WAL entry-header slot and the recovery dedup branch carry this
16    /// variant, but no engine code path emits it yet; it is retained as the
17    /// stable on-disk surface for a future replication producer.
18    ///
19    /// `source_node_id` names a replication source node, not a property-graph
20    /// node. Sequence values are caller-defined; `0` is allowed.
21    Replicated {
22        /// Federation source node.
23        source_node_id: NodeId,
24        /// Source sequence number.
25        source_seq: u64,
26    },
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn local_and_replicated_distinct() {
35        assert_ne!(
36            Origin::Local,
37            Origin::Replicated {
38                source_node_id: NodeId::new(1),
39                source_seq: 1,
40            }
41        );
42    }
43
44    #[test]
45    fn replicated_carries_source_metadata() {
46        let origin = Origin::Replicated {
47            source_node_id: NodeId::new(7),
48            source_seq: 42,
49        };
50        match origin {
51            Origin::Replicated {
52                source_node_id,
53                source_seq,
54            } => {
55                assert_eq!(source_node_id, NodeId::new(7));
56                assert_eq!(source_seq, 42);
57            }
58            Origin::Local => panic!("expected replicated origin"),
59        }
60    }
61
62    #[test]
63    fn default_is_local() {
64        assert_eq!(Origin::default(), Origin::Local);
65    }
66
67    #[test]
68    fn replicated_sequence_zero_is_allowed() {
69        let origin = Origin::Replicated {
70            source_node_id: NodeId::new(1),
71            source_seq: 0,
72        };
73        assert!(matches!(origin, Origin::Replicated { source_seq: 0, .. }));
74    }
75}