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
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::btree_map::Iter;
use std::collections::BTreeMap;

use super::find_references_before_deletion::find_references_recursive;

pub struct Index {
    pub(in crate::index) 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()))?;
        entry.borrow_mut().set(property, value)?;
        Ok(())
    }

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

    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 insert_reference(
        &mut self,
        id: &str,
        target: IndexEntryProperty,
    ) -> Result<(), HitError> {
        //update reference index
        index_reference(
            self,
            &Reference { id: id.to_string() },
            &target.property,
            &target.id,
        )?;

        //generate mutated vector
        let target_entry = {
            self.get_mut(&target.id)
                .ok_or(HitError::IDNotFound(target.id.to_string()))?
        };
        let data = get_parent_property_value(&target_entry, &target);
        let data = mutate_insert_in_reference_array(data, id, None)?;
        let value = ObjectValue::VecReference(data);

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

        //send the value as an event
        Index::dispatch_value(target_entry.clone(), &target.property, value);

        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()))?;
        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 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"),
        }
    }
}