oca_rs/facade/
explore.rs

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use crate::data_storage::Namespace;
use oca_ast_semantics::ast::{BundleContent, CaptureContent, Content, ObjectKind, RefValue};
use oca_bundle_semantics::state::oca::OCABundle;
use serde::{ser::SerializeStruct, Serialize};
use std::collections::HashSet;

use super::Facade;

impl Facade {
    pub fn explore(&self, said: String) -> Option<Relationship> {
        let relations_u8_res = self.db.get(Namespace::OCARelations, &said);

        match relations_u8_res {
            Ok(Some(relations_u8)) => {
                let relationship: Relationship = relations_u8.into();
                Some(Relationship {
                    base_object: OCAObject::new(self, said),
                    relations: relationship.relations,
                })
            }
            _ => None,
        }
    }

    fn insert_oca_objects_metadata(&mut self, oca_bundle: OCABundle) -> Result<(), String> {
        self.db.insert(
            Namespace::OCARelations,
            &format!("{}.metadata", oca_bundle.said.clone().unwrap()),
            &[ObjectKind::OCABundle(BundleContent {
                said: oca_ast_semantics::ast::ReferenceAttrType::Reference(RefValue::Name("".to_string())),
            })
            .into()],
        )?;
        self.db.insert(
            Namespace::OCARelations,
            &format!("{}.metadata", oca_bundle.capture_base.said.clone().unwrap()),
            &[ObjectKind::CaptureBase(CaptureContent {
                attributes: None,
                properties: None,
                flagged_attributes: None,
            })
            .into()],
        )?;
        oca_bundle.overlays.iter().for_each(|overlay| {
            let _ = self.db.insert(
                Namespace::OCARelations,
                &format!("{}.metadata", overlay.said().clone().unwrap()),
                &[ObjectKind::Overlay(
                    overlay.overlay_type().clone(),
                    Content {
                        attributes: None,
                        properties: None,
                    },
                )
                .into()],
            );
        });

        Ok(())
    }

    pub fn add_relations(&mut self, oca_bundle: OCABundle) -> Result<(), String> {
        self.insert_oca_objects_metadata(oca_bundle.clone())?;

        let oca_bundle_said = oca_bundle.said.clone().unwrap().to_string();
        let capture_base_said = oca_bundle.capture_base.said.clone().unwrap().to_string();

        let mut oca_bundle_rel =
            self.explore(oca_bundle_said.clone())
                .unwrap_or(Relationship::new(OCAObject::new(
                    self,
                    oca_bundle_said.clone(),
                )));
        oca_bundle_rel.add_relation(OCAObject::new(self, capture_base_said.clone()));

        let mut capture_base_rel =
            self.explore(capture_base_said.clone())
                .unwrap_or(Relationship::new(OCAObject::new(
                    self,
                    capture_base_said.clone(),
                )));
        capture_base_rel.add_relation(OCAObject::new(self, oca_bundle_said.clone()));

        for overlay in oca_bundle.overlays {
            let overlay_said = overlay.said().clone().unwrap().to_string();

            oca_bundle_rel.add_relation(OCAObject::new(self, overlay_said.clone()));
            capture_base_rel.add_relation(OCAObject::new(self, overlay_said.clone()));

            let mut overlay_rel = self
                .explore(overlay_said.clone())
                .unwrap_or(Relationship::new(OCAObject::new(
                    self,
                    overlay_said.clone(),
                )));

            overlay_rel.add_relation(OCAObject::new(self, oca_bundle_said.clone()));
            overlay_rel.add_relation(OCAObject::new(self, capture_base_said.clone()));
            let overlay_rel_u8: Vec<u8> = overlay_rel.clone().into();
            self.db.insert(
                Namespace::OCARelations,
                &overlay_said.clone(),
                &overlay_rel_u8,
            )?;
        }

        let oca_bundle_rel_u8: Vec<u8> = oca_bundle_rel.clone().into();
        self.db.insert(
            Namespace::OCARelations,
            &oca_bundle_rel.base_object.said,
            &oca_bundle_rel_u8,
        )?;

        let capture_base_rel_u8: Vec<u8> = capture_base_rel.clone().into();
        self.db.insert(
            Namespace::OCARelations,
            &capture_base_rel.base_object.said,
            &capture_base_rel_u8,
        )?;

        Ok(())
    }

    fn object_type(&self, said: String) -> ObjectKind {
        let object_type = self
            .db
            .get(Namespace::OCARelations, &format!("{}.metadata", said))
            .unwrap();

        (*object_type.unwrap().first().unwrap()).into()
    }
}

#[derive(Clone)]
pub struct Relationship {
    pub base_object: OCAObject,
    pub relations: HashSet<OCAObject>,
}

impl Relationship {
    fn new(base_object: OCAObject) -> Self {
        Self {
            base_object,
            relations: HashSet::new(),
        }
    }

    fn add_relation(&mut self, object: OCAObject) {
        self.relations.insert(object);
    }
}

impl From<Relationship> for Vec<u8> {
    fn from(val: Relationship) -> Self {
        let mut result: Vec<u8> = Vec::new();

        val.relations.iter().for_each(|object| {
            result.push(object.object_type.clone().into());
            result.push(object.said.len().try_into().unwrap());
            result.extend(object.said.as_bytes());
        });

        result
    }
}

impl From<Vec<u8>> for Relationship {
    fn from(val: Vec<u8>) -> Self {
        let mut result = Relationship::new(OCAObject {
            said: "".to_string(),
            object_type: ObjectKind::OCABundle(BundleContent {
                said: oca_ast_semantics::ast::ReferenceAttrType::Reference(RefValue::Name("".to_string())),
            }),
        });

        let mut tmp_val = val.clone();
        while !tmp_val.is_empty() {
            let said_len = tmp_val[1];
            let (o_u8, split_val) = tmp_val.split_at(2 + said_len as usize);
            result.add_relation(OCAObject::from(o_u8.to_vec()));
            tmp_val = split_val.to_vec();
        }

        result
    }
}

impl From<Vec<u8>> for OCAObject {
    fn from(val: Vec<u8>) -> Self {
        let object_type = val[0];
        let said_len = val[1];
        let said = String::from_utf8(val[2..2 + said_len as usize].to_vec()).unwrap();
        Self {
            said,
            object_type: object_type.into(),
        }
    }
}

#[derive(Eq, PartialEq, Hash, Clone, Debug)]
pub struct OCAObject {
    pub said: String,
    pub object_type: ObjectKind,
}

impl Serialize for OCAObject {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        #[derive(Serialize)]
        struct OverlayMetadata {
            kind: ObjectKind,
        }

        let mut state = serializer.serialize_struct("OCAObject", 3)?;
        state.serialize_field("said", &self.said)?;
        match &self.object_type {
            ObjectKind::OCABundle(_) | ObjectKind::CaptureBase(_) => {
                state.serialize_field("object_type", &self.object_type)?
            }
            ObjectKind::Overlay(_, _) => {
                state.serialize_field("object_type", "Overlay")?;
                let overlay_metadata = OverlayMetadata {
                    kind: self.object_type.clone(),
                };
                state.serialize_field("metadata", &overlay_metadata)?
            }
        }
        state.end()
    }
}

impl OCAObject {
    fn new(facade: &Facade, said: String) -> Self {
        Self {
            said: said.clone(),
            object_type: facade.object_type(said),
        }
    }
}