From eb4fb8254ad767fdbf51d38cc1558385327b7189 Mon Sep 17 00:00:00 2001 From: Tpt Date: Sun, 28 Nov 2021 19:58:15 +0100 Subject: [PATCH] Lock the graph name when clearing a named graph Makes sure we conflict if a quad is inserted in the graph while we clear it --- lib/src/storage/mod.rs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/src/storage/mod.rs b/lib/src/storage/mod.rs index c92141d8..46c079f1 100644 --- a/lib/src/storage/mod.rs +++ b/lib/src/storage/mod.rs @@ -885,8 +885,22 @@ impl StorageWriter { } pub fn clear_graph(&mut self, graph_name: GraphNameRef<'_>) -> Result<()> { - for quad in self.reader().quads_for_graph(&graph_name.into()) { - self.remove_encoded(&quad?)?; + if graph_name.is_default_graph() { + for quad in self.reader().quads_for_graph(&EncodedTerm::DefaultGraph) { + self.remove_encoded(&quad?)?; + } + } else { + self.buffer.clear(); + write_term(&mut self.buffer, &graph_name.into()); + if self + .transaction + .contains_key_for_update(&self.storage.graphs_cf, &self.buffer)? + { + // The condition is useful to lock the graph itself and ensure no quad is inserted at the same time + for quad in self.reader().quads_for_graph(&graph_name.into()) { + self.remove_encoded(&quad?)?; + } + } } Ok(()) } @@ -910,15 +924,18 @@ impl StorageWriter { } fn remove_encoded_named_graph(&mut self, graph_name: &EncodedTerm) -> Result { - for quad in self.reader().quads_for_graph(graph_name) { - self.remove_encoded(&quad?)?; - } self.buffer.clear(); write_term(&mut self.buffer, graph_name); let result = if self .transaction .contains_key_for_update(&self.storage.graphs_cf, &self.buffer)? { + // The condition is done ASAP to lock the graph itself + for quad in self.reader().quads_for_graph(graph_name) { + self.remove_encoded(&quad?)?; + } + self.buffer.clear(); + write_term(&mut self.buffer, graph_name); self.transaction .remove(&self.storage.graphs_cf, &self.buffer)?; true