1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::{
    core::utils::errors::GraphError,
    db::{
        api::view::internal::{Base, DynamicGraph, IntoDynamic},
        graph::{
            graph::{Graph, InternalGraph},
            views::deletion_graph::GraphWithDeletions,
        },
    },
};
use serde::{Deserialize, Serialize};
use std::path::Path;

#[derive(Serialize, Deserialize)]
pub enum MaterializedGraph {
    EventGraph(Graph),
    PersistentGraph(GraphWithDeletions),
}

impl From<Graph> for MaterializedGraph {
    fn from(value: Graph) -> Self {
        MaterializedGraph::EventGraph(value)
    }
}

impl From<GraphWithDeletions> for MaterializedGraph {
    fn from(value: GraphWithDeletions) -> Self {
        MaterializedGraph::PersistentGraph(value)
    }
}

impl IntoDynamic for MaterializedGraph {
    fn into_dynamic(self) -> DynamicGraph {
        match self {
            MaterializedGraph::EventGraph(g) => g.into_dynamic(),
            MaterializedGraph::PersistentGraph(g) => g.into_dynamic(),
        }
    }
}

impl MaterializedGraph {
    pub fn into_events(self) -> Option<Graph> {
        match self {
            MaterializedGraph::EventGraph(g) => Some(g),
            MaterializedGraph::PersistentGraph(_) => None,
        }
    }
    pub fn into_persistent(self) -> Option<GraphWithDeletions> {
        match self {
            MaterializedGraph::EventGraph(_) => None,
            MaterializedGraph::PersistentGraph(g) => Some(g),
        }
    }

    pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self, GraphError> {
        let f = std::fs::File::open(path)?;
        let mut reader = std::io::BufReader::new(f);
        Ok(bincode::deserialize_from(&mut reader)?)
    }

    pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), GraphError> {
        let f = std::fs::File::create(path)?;
        let mut writer = std::io::BufWriter::new(f);
        Ok(bincode::serialize_into(&mut writer, self)?)
    }
}

pub trait InternalMaterialize {
    fn new_base_graph(&self, graph: InternalGraph) -> MaterializedGraph;

    fn include_deletions(&self) -> bool;
}

pub trait InheritMaterialize: Base {}

impl<G: InheritMaterialize> InternalMaterialize for G
where
    G::Base: InternalMaterialize,
{
    fn new_base_graph(&self, graph: InternalGraph) -> MaterializedGraph {
        self.base().new_base_graph(graph)
    }

    fn include_deletions(&self) -> bool {
        self.base().include_deletions()
    }
}