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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use crate::core::lazy_vec::{IllegalSet, LazyVec};
use crate::core::tprop::TProp;
use crate::core::Prop;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::Debug;

#[derive(thiserror::Error, Debug, PartialEq)]
#[error("cannot mutate static property '{name}'")]
pub struct IllegalMutate {
    pub name: String,
    pub source: IllegalSet<Option<Prop>>,
}

impl IllegalMutate {
    fn from(source: IllegalSet<Option<Prop>>, props: &Props) -> IllegalMutate {
        let id = PropId::Static(source.index);
        IllegalMutate {
            name: props.reverse_id(&id).to_string(),
            source,
        }
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone)]
enum PropId {
    Static(usize),
    Temporal(usize),
}

impl PropId {
    #[allow(dead_code)]
    pub(crate) fn new(id: usize, static_: bool) -> PropId {
        if static_ {
            PropId::Static(id)
        } else {
            PropId::Temporal(id)
        }
    }
    pub(crate) fn get_id(&self) -> usize {
        match self {
            PropId::Static(id) => *id,
            PropId::Temporal(id) => *id,
        }
    }
    pub(crate) fn is_static(&self) -> bool {
        match self {
            PropId::Static(_) => true,
            PropId::Temporal(_) => false,
        }
    }
}

#[derive(Default, Debug, Serialize, Deserialize, PartialEq)]
pub(crate) struct Props {
    // Mapping between property name and property id
    prop_ids: HashMap<String, PropId>, // TODO: change name back to prop_ids

    // Vector of vertices properties. Each index represents vertex local (physical) id
    static_props: Vec<LazyVec<Option<Prop>>>,
    temporal_props: Vec<LazyVec<TProp>>,
}

impl Props {
    // GETTERS:

    fn get_prop_id(&self, name: &str, should_be_static: bool) -> Option<usize> {
        match self.prop_ids.get(name) {
            Some(prop_id) if prop_id.is_static() == should_be_static => Some(prop_id.get_id()),
            _ => None,
        }
    }

    #[allow(unused_variables)]
    fn reverse_id(&self, id: &PropId) -> &str {
        self.prop_ids.iter().find(|&(k, v)| v == id).unwrap().0
    }

    fn get_or_default<A>(&self, vector: &Vec<LazyVec<A>>, id: usize, name: &str) -> A
    where
        A: PartialEq + Default + Clone + Debug,
    {
        match self.get_prop_id(name, true) {
            Some(prop_id) => {
                let props = vector.get(id).unwrap_or(&LazyVec::Empty);
                props.get(prop_id).cloned().unwrap_or(Default::default())
            }
            None => Default::default(),
        }
    }

    pub(crate) fn static_prop(&self, id: usize, name: &str) -> Option<Prop> {
        self.get_or_default(&self.static_props, id, name)
    }

    pub(crate) fn temporal_prop(&self, id: usize, name: &str) -> Option<&TProp> {
        // TODO: we should be able to use self.get_or_default() here
        let prop_id = self.get_prop_id(name, false)?;
        let props = self.temporal_props.get(id).unwrap_or(&LazyVec::Empty);
        props.get(prop_id)
    }

    fn get_names<A>(
        &self,
        vector: &Vec<LazyVec<A>>,
        id: usize,
        should_be_static: bool,
    ) -> Vec<String>
    where
        A: Clone + Default + PartialEq + Debug,
    {
        match vector.get(id) {
            Some(props) => {
                let ids = props.filled_ids().into_iter();
                if should_be_static {
                    ids.map(|id| self.reverse_id(&PropId::Static(id)).to_string())
                        .collect_vec()
                } else {
                    ids.map(|id| self.reverse_id(&PropId::Temporal(id)).to_string())
                        .collect_vec()
                }
            }
            None => vec![],
        }
    }

    pub fn static_names(&self, id: usize) -> Vec<String> {
        self.get_names(&self.static_props, id, true)
    }

    pub fn temporal_names(&self, id: usize) -> Vec<String> {
        self.get_names(&self.temporal_props, id, false)
    }

    // SETTERS:

    fn grow_and_get_slot<A>(vector: &mut Vec<A>, id: usize) -> &mut A
    where
        A: Default,
    {
        if vector.len() <= id {
            vector.resize_with(id + 1, || Default::default());
        }
        // now props_storage.len() >= id + 1:
        vector.get_mut(id).unwrap()
    }

    fn get_or_allocate_id(&mut self, name: &str, should_be_static: bool) -> Result<usize, ()> {
        match self.prop_ids.get(name) {
            None => {
                let new_prop_id = if should_be_static {
                    let static_prop_ids = self.prop_ids.iter().filter(|&(_, v)| v.is_static());
                    let new_id = static_prop_ids.count();
                    PropId::Static(new_id)
                } else {
                    let static_prop_ids = self.prop_ids.iter().filter(|&(_, v)| !v.is_static());
                    let new_id = static_prop_ids.count();
                    PropId::Temporal(new_id)
                };
                self.prop_ids.insert(name.to_string(), new_prop_id.clone());
                Ok(new_prop_id.get_id())
            }
            Some(id) if id.is_static() == should_be_static => Ok(id.get_id()),
            _ => Err(()),
        }
    }

    fn translate_props(
        &mut self,
        props: &Vec<(String, Prop)>,
        should_be_static: bool,
    ) -> Vec<(usize, Prop)> {
        // TODO: return Result
        props
            .iter()
            .map(|(name, prop)| {
                (
                    self.get_or_allocate_id(name, should_be_static).unwrap(),
                    prop.clone(),
                )
            })
            .collect_vec()
    }

    pub fn upsert_temporal_props(&mut self, t: i64, id: usize, props: &Vec<(String, Prop)>) {
        if !props.is_empty() {
            let translated_props = self.translate_props(props, false);
            let vertex_slot: &mut LazyVec<TProp> =
                Self::grow_and_get_slot(&mut self.temporal_props, id);
            for (prop_id, prop) in translated_props {
                vertex_slot.update_or_set(prop_id, |p| p.set(t, &prop), TProp::from(t, &prop));
            }
        }
    }

    pub fn set_static_props(
        &mut self,
        id: usize,
        props: &Vec<(String, Prop)>,
    ) -> Result<(), IllegalMutate> {
        if !props.is_empty() {
            let translated_props = self.translate_props(props, true);
            let vertex_slot: &mut LazyVec<Option<Prop>> =
                Self::grow_and_get_slot(&mut self.static_props, id);
            for (prop_id, prop) in translated_props {
                if let Err(e) = vertex_slot.set(prop_id, Some(prop)) {
                    return Err(IllegalMutate::from(e, &self));
                }
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod props_tests {
    use super::*;

    #[test]
    fn return_prop_id_if_prop_name_found() {
        let mut props = Props::default();
        props
            .prop_ids
            .insert(String::from("key1"), PropId::Temporal(0));
        props
            .prop_ids
            .insert(String::from("key2"), PropId::Temporal(1));

        assert_eq!(props.get_or_allocate_id("key2", false), Ok(1));
    }

    #[test]
    fn return_new_prop_id_if_prop_name_not_found() {
        let mut props = Props::default();
        assert_eq!(props.get_or_allocate_id("key1", false), Ok(0));
        assert_eq!(props.get_or_allocate_id("key2", false), Ok(1));
    }

    #[test]
    fn insert_new_vertex_prop() {
        let mut props = Props::default();
        props.upsert_temporal_props(1, 0, &vec![("bla".to_string(), Prop::I32(10))]);

        let prop_id = props.get_or_allocate_id("bla", false).unwrap();
        assert_eq!(
            props
                .temporal_props
                .get(0)
                .unwrap()
                .get(prop_id)
                .unwrap()
                .iter()
                .collect::<Vec<_>>(),
            vec![(&1, Prop::I32(10))]
        )
    }

    #[test]
    fn update_existing_vertex_prop() {
        let mut props = Props::default();
        props.upsert_temporal_props(1, 0, &vec![("bla".to_string(), Prop::I32(10))]);
        props.upsert_temporal_props(2, 0, &vec![("bla".to_string(), Prop::I32(10))]);

        let prop_id = props.get_or_allocate_id("bla", false).unwrap();
        assert_eq!(
            props
                .temporal_props
                .get(0)
                .unwrap()
                .get(prop_id)
                .unwrap()
                .iter()
                .collect::<Vec<_>>(),
            vec![(&1, Prop::I32(10)), (&2, Prop::I32(10))]
        )
    }

    #[test]
    fn new_update_with_the_same_time_to_a_vertex_prop_is_ignored() {
        let mut props = Props::default();
        props.upsert_temporal_props(1, 0, &vec![("bla".to_string(), Prop::I32(10))]);
        props.upsert_temporal_props(1, 0, &vec![("bla".to_string(), Prop::I32(20))]);

        let prop_id = props.get_or_allocate_id("bla", false).unwrap();
        assert_eq!(
            props
                .temporal_props
                .get(0)
                .unwrap()
                .get(prop_id)
                .unwrap()
                .iter()
                .collect::<Vec<_>>(),
            vec![(&1, Prop::I32(10))]
        )
    }
}