Skip to content

Commit 094764d

Browse files
committed
Fix recursive delete #1084
1 parent 7af391e commit 094764d

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

lib/src/db.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,29 @@ impl Db {
479479
}
480480
Ok(())
481481
}
482+
483+
/// Recursively removes a resource and its children from the database
484+
fn recursive_remove(&self, subject: &str, transaction: &mut Transaction) -> AtomicResult<()> {
485+
if let Ok(found) = self.get_propvals(subject) {
486+
let resource = Resource::from_propvals(found, subject.to_string());
487+
transaction.push(Operation::remove_resource(subject));
488+
let mut children = resource.get_children(self)?;
489+
for child in children.iter_mut() {
490+
self.recursive_remove(child.get_subject(), transaction)?;
491+
}
492+
for (prop, val) in resource.get_propvals() {
493+
let remove_atom = crate::Atom::new(subject.into(), prop.clone(), val.clone());
494+
self.remove_atom_from_index(&remove_atom, &resource, transaction)?;
495+
}
496+
} else {
497+
return Err(format!(
498+
"Resource {} could not be deleted, because it was not found in the store.",
499+
subject
500+
)
501+
.into());
502+
}
503+
Ok(())
504+
}
482505
}
483506

484507
impl Drop for Db {
@@ -895,33 +918,10 @@ impl Storelike for Db {
895918
#[instrument(skip(self))]
896919
fn remove_resource(&self, subject: &str) -> AtomicResult<()> {
897920
let mut transaction = Transaction::new();
898-
transaction.push(Operation::remove_resource(subject));
899-
900-
let mut recursive_remove = |subject: &str| -> AtomicResult<()> {
901-
if let Ok(found) = self.get_propvals(subject) {
902-
let resource = Resource::from_propvals(found, subject.to_string());
903-
let mut children = resource.get_children(self)?;
904-
for child in children.iter_mut() {
905-
transaction.push(Operation::remove_resource(child.get_subject()));
906-
}
907-
for (prop, val) in resource.get_propvals() {
908-
let remove_atom = crate::Atom::new(subject.into(), prop.clone(), val.clone());
909-
self.remove_atom_from_index(&remove_atom, &resource, &mut transaction)?;
910-
}
911-
} else {
912-
return Err(format!(
913-
"Resource {} could not be deleted, because it was not found in the store.",
914-
subject
915-
)
916-
.into());
917-
}
918-
Ok(())
919-
};
920921

921-
recursive_remove(subject)?;
922+
self.recursive_remove(subject, &mut transaction)?;
922923

923-
self.apply_transaction(&mut transaction)?;
924-
Ok(())
924+
self.apply_transaction(&mut transaction)
925925
}
926926

927927
fn set_default_agent(&self, agent: crate::agents::Agent) {

0 commit comments

Comments
 (0)