From d5ad37cb8035622cd2af7e836c6aa99e170dcfe1 Mon Sep 17 00:00:00 2001 From: Joep Meindertsma Date: Fri, 30 Oct 2020 22:04:18 +0100 Subject: [PATCH] Explicit resource saving #24 --- lib/src/db.rs | 2 ++ lib/src/resources.rs | 10 ++++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/src/db.rs b/lib/src/db.rs index 79eb2083c..2ec1e27b7 100644 --- a/lib/src/db.rs +++ b/lib/src/db.rs @@ -219,6 +219,8 @@ mod test { new_property .set_by_shortname("description", "the age of a person") .unwrap(); + // Changes are only applied to the store after calling `.save()` + new_property.save().unwrap(); // The modified resource is saved to the store after this // A subject URL has been created automatically. diff --git a/lib/src/resources.rs b/lib/src/resources.rs index 4b0b540a2..fe3865672 100644 --- a/lib/src/resources.rs +++ b/lib/src/resources.rs @@ -11,7 +11,7 @@ use std::collections::HashMap; /// A Resource is a set of Atoms that shares a single Subject. /// A Resource only contains valid Values, but it _might_ lack required properties. -/// All changes to the Resource are immediately applied to the Store as well. +/// All changes to the Resource are applied after calling `.save()`. // #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Resource<'a> { /// A hashMap of all the Property Value combinations @@ -125,8 +125,6 @@ impl<'a> Resource<'a> { /// Validates the datatype. pub fn remove_propval(&mut self, property_url: &str) { self.propvals.remove_entry(property_url); - // Could fail, but unlikely - self.save().ok(); } /// Tries to resolve the shortname of a Property to a Property URL. @@ -150,8 +148,8 @@ impl<'a> Resource<'a> { } /// Saves the resource (with all the changes) to the store - /// Should be called automatically, but we might add some form of batching later - fn save(&self) -> AtomicResult<()> { + /// Should be run after any (batch of) changes to the Resource! + pub fn save(&self) -> AtomicResult<()> { self.store.add_resource(self) } @@ -169,7 +167,6 @@ impl<'a> Resource<'a> { /// Overwrites existing. pub fn set_propval(&mut self, property: String, value: Value) -> AtomicResult<()> { self.propvals.insert(property, value); - self.save()?; Ok(()) } @@ -272,6 +269,7 @@ mod test { new_resource.set_by_shortname("shortname", "person").unwrap(); assert!(new_resource.get_shortname("shortname").unwrap().to_string() == "person"); new_resource.set_by_shortname("shortname", "human").unwrap(); + new_resource.save().unwrap(); assert!(new_resource.get_shortname("shortname").unwrap().to_string() == "human"); let mut resource_from_store = store.get_resource(new_resource.get_subject()).unwrap(); assert!(resource_from_store.get_shortname("shortname").unwrap().to_string() == "human");