|
|
|
@ -136,7 +136,7 @@ pub struct InMemoryStorage { |
|
|
|
|
|
|
|
|
|
data_objects: HashMap<ID, data::Object>, |
|
|
|
|
data_relations: HashMap<ID, data::Relation>, |
|
|
|
|
data_properties: HashMap<ID, data::Value>, |
|
|
|
|
data_values: HashMap<ID, data::Value>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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<model::PropertyTemplate, StorageError> { |
|
|
|
|
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<ID, StorageError> { |
|
|
|
|
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<ID, StorageError> { |
|
|
|
|
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<ID, StorageError> { |
|
|
|
|
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) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|