From 26166a63cdf884218aa6f44c5d78a280e27c2c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Tue, 2 Feb 2021 22:59:36 +0100 Subject: [PATCH] wip inserting data --- src/yopa.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/src/yopa.rs b/src/yopa.rs index 74029af..4d0c402 100644 --- a/src/yopa.rs +++ b/src/yopa.rs @@ -136,7 +136,7 @@ pub struct InMemoryStorage { data_objects: HashMap, data_relations: HashMap, - data_properties: HashMap, + data_values: HashMap, } fn next_id() -> ID { @@ -227,7 +227,7 @@ impl InMemoryStorage { let _ = map_drain_filter(&mut self.data_objects, |_k, v| v.object_tpl_id == id); // Remove property values - let _ = map_drain_filter(&mut self.data_properties, |_k, v| removed_prop_ids.contains(&v.prop_tpl_id)); + let _ = map_drain_filter(&mut self.data_values, |_k, v| removed_prop_ids.contains(&v.prop_tpl_id)); // Remove relations let _ = map_drain_filter(&mut self.data_relations, |_k, v| removed_relation_ids.contains(&v.rel_tpl_id)); @@ -248,7 +248,7 @@ impl InMemoryStorage { // Remove related property templates let removed_prop_tpl_ids = map_drain_filter(&mut self.tpl_properties, |_k, v| v.parent_tpl_id == id).keys(); - let _ = map_drain_filter(&mut self.data_properties, |_k, v| removed_prop_tpl_ids.contains(&v.prop_tpl_id)); + let _ = map_drain_filter(&mut self.data_values, |_k, v| removed_prop_tpl_ids.contains(&v.prop_tpl_id)); // Related object remain untouched, so there can be a problem with orphans. This is up to the application to deal with. @@ -261,10 +261,61 @@ impl InMemoryStorage { pub fn delete_property_template(&mut self, id : ID) -> Result { return if let Some(t) = self.tpl_properties.remove(&id) { // Remove relations - let _ = map_drain_filter(&mut self.data_properties, |_k, v| v.prop_tpl_id == id); + let _ = map_drain_filter(&mut self.data_values, |_k, v| v.prop_tpl_id == id); Ok(t) } else { Err(StorageError::NotExist("property template")) } } + + // DATA + + pub fn insert_object(&mut self, mut obj : data::Object) -> Result { + if !self.tpl_objects.contains_key(&obj.object_tpl_id) { + return Err(StorageError::NotExist("object template")); + } + + let id = next_id(); + obj.id = id; + self.data_objects.insert(id, obj); + Ok(id) + } + + pub fn insert_value(&mut self, mut obj : data::Value) -> Result { + if !self.tpl_properties.contains_key(&obj.prop_tpl_id) { + return Err(StorageError::NotExist("property template")); + } + + if !self.data_objects.contains_key(&obj.object_id) { + return Err(StorageError::NotExist("parent object")); + } + + // TODO validate if it already exists + + let id = next_id(); + obj.id = id; + self.data_values.insert(id, obj); + Ok(id) + } + + pub fn insert_relation(&mut self, mut rel: data::Relation) -> Result { + if !self.tpl_relations.contains_key(&rel.rel_tpl_id) { + return Err(StorageError::NotExist("relation template")); + } + + if !self.data_objects.contains_key(&rel.object_id) { + return Err(StorageError::NotExist("parent object")); + } + + if !self.data_objects.contains_key(&rel.related_id) { + return Err(StorageError::NotExist("related object")); + } + + // TODO validate if it already exists + + let id = next_id(); + rel.id = id; + self.data_relations.insert(id, rel); + Ok(id) + } }