hit-data 0.0.5

Hierarchical Indexed Typed data structure
Documentation
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use crate::index::list_helpers::{
    get_parent_index_entry, get_parent_property_value, mutate_insert_in_reference_array,
};
use crate::index::move_object::move_object;
use crate::index::reference_helpers::remove_reference_from_parent_array_from_property;
use crate::index::reference_index_helpers::{
    index_object_references, index_reference, unindex_reference, unindex_reference_from_property,
};
use crate::index::remove_helpers::{get_references, remove_object_helper};
use crate::index::subobject_helpers::insert_subobject_in_array;
use crate::index::{IndexEntry, IndexEntryProperty, IndexEntryRef};
use crate::object_data::Id;
use crate::object_data::ObjectValue;
use crate::object_data::ObjectValues;
use crate::object_data::Reference;
use crate::HitError;
use std::collections::BTreeMap;
use std::collections::{btree_map::Iter, HashMap};

use super::{
    find_references_before_deletion::find_references_recursive,
    reference_helpers::mutate_remove_from_reference_array,
};

#[derive(Clone)]
pub struct Index {
    pub(in crate) index: BTreeMap<Id, IndexEntryRef>,
    id: Id,
}

impl Index {
    pub fn new_for_import(id: &str) -> Index {
        Index {
            index: BTreeMap::new(),
            id: id.to_string(),
        }
    }

    pub fn new(id: &str, values: ObjectValues) -> Result<Index, HitError> {
        let mut index = Index {
            index: BTreeMap::new(),
            id: id.to_string(),
        };
        //Disallow references and subobjects
        for (_, value) in values.iter() {
            match value {
                ObjectValue::Reference(_) => {
                    return Err(HitError::CanOnlySetScalarValuesInInsertedObject())
                }
                ObjectValue::VecReference(_) => {
                    return Err(HitError::CanOnlySetScalarValuesInInsertedObject())
                }
                ObjectValue::SubObject(_) => {
                    return Err(HitError::CanOnlySetScalarValuesInInsertedObject())
                }
                ObjectValue::VecSubObjects(_) => {
                    return Err(HitError::CanOnlySetScalarValuesInInsertedObject())
                }
                _ => {}
            }
        }
        index.insert_raw(id, values, None)?;
        Ok(index)
    }

    pub fn get_main_object_id(&self) -> &Id {
        return &self.id;
    }

    pub fn get(&self, id: &str) -> Option<IndexEntryRef> {
        match self.index.get(id) {
            Some(entry) => Some(entry.clone()),
            None => None,
        }
    }

    pub fn get_mut(&mut self, id: &str) -> Option<&mut IndexEntryRef> {
        return self.index.get_mut(id);
    }

    pub fn get_value(&self, id: &str, property: &str) -> Option<ObjectValue> {
        let obj = self.get(id)?;
        let obj = obj.borrow();
        let value = obj.get(&property);
        Some(value.clone())
    }

    pub fn set_value(
        &mut self,
        id: &str,
        property: &str,
        value: ObjectValue,
    ) -> Result<(), HitError> {
        //remove reference for old value
        // TODO : should be put in the ObjectValue::Reference case of the below match ?
        unindex_reference_from_property(self, id, property)?;

        match value.clone() {
            ObjectValue::Null => {}
            ObjectValue::Bool(_) => {}
            ObjectValue::Date(_) => {}
            ObjectValue::F32(_) => {}
            ObjectValue::Reference(value) => {
                index_reference(self, &value, property, id)?;
            }
            ObjectValue::String(_) => {}
            _ => return Err(HitError::CanOnlySetScalarValues()),
        }

        let entry = self.get(id).ok_or(HitError::IDNotFound(id.to_string(), "set_value".into()))?;
        entry.borrow_mut().set(property, value)?;
        Ok(())
    }

    pub fn iter(&self) -> Iter<Id, IndexEntryRef> {
        return self.index.iter();
    }

    pub fn contains(&self, id: &str) -> bool {
        return self.index.contains_key(id);
    }

    pub fn insert(
        &mut self,
        id: &str,
        values: ObjectValues,
        parent: IndexEntryProperty,
        before_id: Option<String>,
    ) -> Result<(), HitError> {
        self.insert_quietly(id, values, parent, before_id)?;
        //dispatch value to parent property
        let (entry, parent) =
            get_parent_index_entry(self, &id)?.ok_or(HitError::InvalidParentID(id.to_string()))?;
        Index::dispatch_value_property(entry, &parent.property);
        Ok(())
    }

    /**
     * Used for import
     */
    pub(in crate::index) fn insert_raw(
        &mut self,
        id: &str,
        values: ObjectValues,
        parent: Option<IndexEntryProperty>,
    ) -> Result<(), HitError> {
        //check id doesnt exist
        if self.index.contains_key(id) {
            return Err(HitError::DuplicateID(id.to_string()));
        }
        self.index.insert(
            id.to_string(),
            IndexEntry::new(id.to_string(), values, parent.clone()),
        );
        Ok(())
    }

    fn insert_quietly(
        &mut self,
        id: &str,
        values: ObjectValues,
        parent: IndexEntryProperty,
        before_id: Option<String>,
    ) -> Result<(), HitError> {
        // insert
        self.insert_raw(id, values.clone(), Some(parent.clone()))?;

        //index references to other objects
        index_object_references(self, values, id)?;

        // update the list of ids in the parent
        insert_subobject_in_array(self, parent, id, before_id)?;

        Ok(())
    }

    pub fn move_reference(
        &mut self,
        id: &str,
        target: IndexEntryProperty,
        before_id: Option<Id>,
    ) -> Result<(), HitError> {
        let target_entry = {
            self.get_mut(&target.id)
                .ok_or(HitError::IDNotFound(target.id.to_string(), "move_reference".into()))?
        };
        let data = get_parent_property_value(&target_entry, &target);

        // throw error if reference not found
        match &data {
            ObjectValue::VecReference(data) => {
                if !data.iter().any(|r| r.id == id) {
                    return Err(HitError::ReferenceNotFound());
                }
                match &before_id {
                    Some(before_id) => {
                        if !data.iter().any(|r| r.id == id) {
                            return Err(HitError::InvalidBeforeId(before_id.clone()));
                        }
                    }
                    None => {}
                }
            }
            ObjectValue::Null => {
                return Err(HitError::ReferenceNotFound());
            }
            _ => return Err(HitError::CannotInsertReferenceInThisDataType()),
        }

        let data = mutate_remove_from_reference_array(data, id)?.unwrap_or(vec![]);
        let data =
            mutate_insert_in_reference_array(ObjectValue::VecReference(data), id, before_id)?;
        //update the value in the index entry
        target_entry.borrow_mut().data.insert(
            target.clone().property,
            ObjectValue::VecReference(data.clone()),
        );

        Index::dispatch_value(
            target_entry.clone(),
            &target.property,
            ObjectValue::VecReference(data),
        );
        Ok(())
    }

    pub fn insert_reference(
        &mut self,
        id: &str,
        target: IndexEntryProperty,
        before_id: Option<Id>,
    ) -> Result<(), HitError> {
        {
            let target_entry = {
                self.get_mut(&target.id)
                    .ok_or(HitError::IDNotFound(target.id.to_string(), "insert_reference".into()))?
            };
            let data = get_parent_property_value(&target_entry, &target);

            match &data {
                ObjectValue::VecReference(data) => {
                    // TODO ? throw error if reference already exists
                    // if data.iter().any(|r| r.id == id) {
                    //  return Err(HitError::CannotInsertReferenceTwice());
                    // }
                }
                ObjectValue::Null => {}
                _ => return Err(HitError::CannotInsertReferenceInThisDataType()),
            }

            //generate mutated vector
            let data = mutate_insert_in_reference_array(data, id, before_id)?;
            let value = ObjectValue::VecReference(data);

            //update the value in the index entry
            target_entry
                .borrow_mut()
                .data
                .insert(target.clone().property, value.clone());
        }

        //update reference index
        index_reference(
            self,
            &Reference { id: id.to_string() },
            &target.property,
            &target.id,
        )?;
        //send the value as an event
        {
            let target_entry = {
                self.get_mut(&target.id)
                    .ok_or(HitError::IDNotFound(target.id.to_string(), "insert_reference".into()))?
            };
            let data = get_parent_property_value(&target_entry, &target);
            Index::dispatch_value(target_entry.clone(), &target.property, data);
        }

        Ok(())
    }

    pub fn remove_reference(
        &mut self,
        id: &str,
        parent: IndexEntryProperty,
    ) -> Result<(), HitError> {
        let value = remove_reference_from_parent_array_from_property(self, parent.clone(), id)?;

        unindex_reference(self, parent.clone(), id)?;
        //dispatch event
        let entry = self
            .get(&parent.clone().id)
            .ok_or(HitError::IDNotFound(parent.clone().id.to_string(), "remove_reference".into()))?;
        Index::dispatch_value(entry, &parent.property, value);
        Ok(())
    }

    pub(in crate) fn get_references(&self, id: &str) -> Result<Vec<IndexEntryProperty>, HitError> {
        get_references(&self, id)
    }

    pub fn find_references_recursive(
        &self,
        id: &str,
    ) -> Result<(HashMap<String, Vec<IndexEntryProperty>>, Vec<String>), HitError> {
        find_references_recursive(self, id)
    }

    pub fn remove_object(&mut self, id: &str) -> Result<Vec<String>, HitError> {
        let (parent_entry, parent) =
            get_parent_index_entry(self, &id)?.ok_or(HitError::CannotDeleteRootObject())?;

        let (refs, id_list) = find_references_recursive(self, id)?;
        if refs.len() > 0 {
            return Err(HitError::CannotDeleteObjectWithReferences(refs));
        }
        remove_object_helper(self, id)?;

        //remove from ref index the references in the object's data
        Index::dispatch_value_property(parent_entry, &parent.property);
        Ok(id_list)
    }

    pub fn move_object(
        &mut self,
        id: &str,
        property: IndexEntryProperty,
        before_id: Option<String>,
    ) -> Result<(), HitError> {
        move_object(self, id, property, before_id)
    }

    fn dispatch_value_property(entry: IndexEntryRef, property: &str) {
        let value = entry.borrow().get(property).clone();
        Index::dispatch_value(entry, property, value)
    }

    fn dispatch_value(entry: IndexEntryRef, property: &str, value: ObjectValue) {
        entry.borrow_mut().dispatch_value(property, value);
    }
}

#[cfg(test)]
mod tests {
    use linked_hash_map::LinkedHashMap;

    use crate::index::Index;
    use crate::HitError;
    use crate::ObjectValue;
    use crate::Reference;

    #[test]
    fn it_should_create_a_new_index_with_values() {
        let mut values = LinkedHashMap::new();
        values.insert("test".into(), ObjectValue::Bool(true));
        values.insert("testString".into(), ObjectValue::String("value".into()));
        assert!(Index::new("id", values).is_ok());
    }

    #[test]
    fn it_should_fail_creating_a_new_index_with_reference_values() {
        let mut values = LinkedHashMap::new();
        values.insert(
            "reference".into(),
            ObjectValue::Reference(Reference { id: "a".into() }),
        );
        assert!(matches!(
            Index::new("id", values).err(),
            Some(HitError::CanOnlySetScalarValuesInInsertedObject())
        ));
    }

    #[test]
    fn it_should_fail_creating_a_new_index_with_reference_array_values() {
        let mut values = LinkedHashMap::new();
        values.insert(
            "reference".into(),
            ObjectValue::VecReference(vec![Reference { id: "a".into() }]),
        );
        assert!(matches!(
            Index::new("id", values).err(),
            Some(HitError::CanOnlySetScalarValuesInInsertedObject())
        ));
    }
    #[test]
    fn it_should_fail_creating_a_new_index_with_subobject_values() {
        let mut values = LinkedHashMap::new();
        values.insert(
            "reference".into(),
            ObjectValue::SubObject(Reference { id: "a".into() }),
        );
        assert!(Index::new("id", values).is_err());
    }
    #[test]
    fn it_should_fail_creating_a_new_index_with_subobject_array_values() {
        let mut values = LinkedHashMap::new();
        values.insert(
            "reference".into(),
            ObjectValue::VecSubObjects(vec![Reference { id: "a".into() }]),
        );
        assert!(Index::new("id", values).is_err());
    }

    #[test]
    fn it_should_get_existing_data() {
        let mut values = LinkedHashMap::new();
        values.insert("test".into(), ObjectValue::Bool(true));
        let index = Index::new("id", values).ok().unwrap();

        let item = index.get("id").unwrap();
        let item = item.borrow();

        let prop = item.get("test");
        match prop {
            ObjectValue::Bool(value) => {
                assert_eq!(value, &true);
            }
            _ => panic!("Should be a boolean"),
        }
    }
}