manifold_graph/
edge.rs

1//! Edge types for graph storage.
2
3use uuid::Uuid;
4
5/// An edge in the graph with properties and temporal tracking.
6#[derive(Debug, Clone, PartialEq)]
7pub struct Edge {
8    /// Source vertex ID
9    pub source: Uuid,
10    /// Edge type (e.g., "follows", "knows", "contains")
11    pub edge_type: String,
12    /// Target vertex ID
13    pub target: Uuid,
14    /// Whether this edge is active (vs passive/hidden/deleted)
15    pub is_active: bool,
16    /// Edge weight or score
17    pub weight: f32,
18    /// Creation timestamp in nanoseconds since Unix epoch
19    pub created_at: u64,
20    /// Deletion timestamp in nanoseconds since Unix epoch (0 if not deleted)
21    pub deleted_at: u64,
22}
23
24impl Edge {
25    /// Creates a new edge with current timestamp
26    pub fn new(
27        source: Uuid,
28        edge_type: impl Into<String>,
29        target: Uuid,
30        is_active: bool,
31        weight: f32,
32    ) -> Self {
33        Self {
34            source,
35            edge_type: edge_type.into(),
36            target,
37            is_active,
38            weight,
39            created_at: current_timestamp_nanos(),
40            deleted_at: 0,
41        }
42    }
43
44    /// Creates a new edge with explicit timestamps
45    pub fn with_timestamps(
46        source: Uuid,
47        edge_type: impl Into<String>,
48        target: Uuid,
49        is_active: bool,
50        weight: f32,
51        created_at: u64,
52        deleted_at: u64,
53    ) -> Self {
54        Self {
55            source,
56            edge_type: edge_type.into(),
57            target,
58            is_active,
59            weight,
60            created_at,
61            deleted_at,
62        }
63    }
64
65    /// Checks if this edge was active at the given timestamp
66    pub fn is_active_at(&self, timestamp: u64) -> bool {
67        self.created_at <= timestamp && (self.deleted_at == 0 || self.deleted_at > timestamp)
68    }
69}
70
71/// Returns the current timestamp in nanoseconds since Unix epoch
72pub fn current_timestamp_nanos() -> u64 {
73    std::time::SystemTime::now()
74        .duration_since(std::time::UNIX_EPOCH)
75        .expect("System time before Unix epoch")
76        .as_nanos() as u64
77}