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
pub mod bundle;
pub mod ocafile;
use crate::data_storage::DataStorage;

pub struct Graph {
    pub db: Box<dyn DataStorage>,
}

#[allow(dead_code)]
impl Graph {
    fn new(db: Box<dyn DataStorage>) -> Self {
        Self {
            db
        }
    }

    pub fn add(&self, new: &str, to: Option<&str>) -> Result<(), String> {
        self.db.insert(
            &format!("{}.upscending", new),
            &self.generate_parents_digests(to)
        )?;

        if let Some(to) = to {
            let k = &format!("{}.downscending", to);
            let children_digests: Option<Vec<u8>> = self.db.get(k)?;
            self.db.insert(
                &format!("{}.downscending", to),
                &self.update_children_digests(children_digests.as_deref(), new)
            )?;
        }

        Ok(())
    }

    fn generate_parents_digests(&self, parent_said: Option<&str>) -> Vec<u8> {
        let mut digests: Vec<u8> = Vec::new();
        if let Some(to) = parent_said {
            digests.push(to.len().try_into().unwrap());
            digests.extend(to.as_bytes());
        }
        digests
    }

    fn update_children_digests(&self, children_digest: Option<&[u8]>, new: &str) -> Vec<u8> {
        let mut digests: Vec<u8> = Vec::new();
        if let Some(children_digest) = children_digest {
            digests.extend(children_digest);
        }
        digests.push(new.len().try_into().unwrap());
        digests.extend(new.as_bytes());
        digests
    }

}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data_storage::{ DataStorage, SledDataStorage };

    #[test]
    fn test_history() {
        let db = SledDataStorage::open("db_test");
        let graph = Graph::new(Box::new(db));
        let _ = graph.add("dag1", None);
        let _ = graph.add("dag2", Some("dag1"));
        let _ = graph.add("dag3", Some("dag2"));
        let _ = graph.add("dag4", Some("dag2"));
        let _ = graph.add("dag5", Some("dag4"));

        /* dbg!(graph.db.get("dag1.upscending"));
        dbg!(graph.db.get("dag1.downscending"));
        dbg!(graph.db.get("dag2.upscending"));
        dbg!(graph.db.get("dag2.downscending"));
        dbg!(graph.db.get("dag3.upscending"));
        dbg!(graph.db.get("dag3.downscending"));
        dbg!(graph.db.get("dag4.upscending"));
        dbg!(graph.db.get("dag4.downscending"));
        dbg!(graph.db.get("dag5.upscending"));
        dbg!(graph.db.get("dag5.downscending")); */
    }
}