|
|
@ -136,7 +136,7 @@ pub struct InMemoryStorage { |
|
|
|
|
|
|
|
|
|
|
|
data_objects: HashMap<ID, data::Object>, |
|
|
|
data_objects: HashMap<ID, data::Object>, |
|
|
|
data_relations: HashMap<ID, data::Relation>, |
|
|
|
data_relations: HashMap<ID, data::Relation>, |
|
|
|
data_properties: HashMap<ID, data::Value>, |
|
|
|
data_values: HashMap<ID, data::Value>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn next_id() -> ID { |
|
|
|
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); |
|
|
|
let _ = map_drain_filter(&mut self.data_objects, |_k, v| v.object_tpl_id == id); |
|
|
|
|
|
|
|
|
|
|
|
// Remove property values
|
|
|
|
// 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
|
|
|
|
// Remove relations
|
|
|
|
let _ = map_drain_filter(&mut self.data_relations, |_k, v| removed_relation_ids.contains(&v.rel_tpl_id)); |
|
|
|
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
|
|
|
|
// 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 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.
|
|
|
|
// 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> { |
|
|
|
pub fn delete_property_template(&mut self, id : ID) -> Result<model::PropertyTemplate, StorageError> { |
|
|
|
return if let Some(t) = self.tpl_properties.remove(&id) { |
|
|
|
return if let Some(t) = self.tpl_properties.remove(&id) { |
|
|
|
// Remove relations
|
|
|
|
// 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) |
|
|
|
Ok(t) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Err(StorageError::NotExist("property template")) |
|
|
|
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) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|