Skip to main content

marsdb_graph/
model.rs

1use std::collections::BTreeMap;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
4pub struct NodeId(pub u64);
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct EdgeId(pub u64);
8
9#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
10pub enum PropertyValue {
11    Null,
12    Bool(bool),
13    Int(i64),
14    Float(f64),
15    String(String),
16}
17
18#[derive(Debug, Clone, PartialEq)]
19pub struct Node {
20    pub id: NodeId,
21    pub labels: Vec<String>,
22    pub props: BTreeMap<String, PropertyValue>,
23}
24
25#[derive(Debug, Clone, PartialEq)]
26pub struct Edge {
27    pub id: EdgeId,
28    pub label: String,
29    pub src: NodeId,
30    pub dst: NodeId,
31    pub props: BTreeMap<String, PropertyValue>,
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub enum Direction {
36    Out,
37    In,
38}
39
40/// A traversal-hop candidate read directly from an adjacency multimap entry,
41/// without touching the `edges`/`nodes` tables.
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub struct AdjEntry {
44    pub edge_id: EdgeId,
45    pub other: NodeId,
46    pub label_id: u32,
47}
48
49impl AdjEntry {
50    pub(crate) fn encode(&self) -> [u8; 20] {
51        let mut buf = [0u8; 20];
52        buf[0..8].copy_from_slice(&self.edge_id.0.to_be_bytes());
53        buf[8..16].copy_from_slice(&self.other.0.to_be_bytes());
54        buf[16..20].copy_from_slice(&self.label_id.to_be_bytes());
55        buf
56    }
57
58    pub(crate) fn decode(bytes: &[u8]) -> Self {
59        let edge_id = u64::from_be_bytes(bytes[0..8].try_into().unwrap());
60        let other = u64::from_be_bytes(bytes[8..16].try_into().unwrap());
61        let label_id = u32::from_be_bytes(bytes[16..20].try_into().unwrap());
62        AdjEntry {
63            edge_id: EdgeId(edge_id),
64            other: NodeId(other),
65            label_id,
66        }
67    }
68}