diff --git a/CHANGELOG.md b/CHANGELOG.md index c6ec3db4..98dc7d71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ - A simple built-in HTTP client. In the Rust library, is disabled by default behind the `http_client` feature. It powers SPARQL federation and SPARQL UPDATE `LOAD` operations. - `std::str::FromStr` implementations to `NamedNode`, `BlankNode`, `Literal`, `Term` and `Variable` allowing to easily parse Turtle/SPARQL serialization of these terms. - Optional Sled storage for `oxigraph_server`. -- `(Memory|RocksDB|Sled)Store::drop_graph`. +- `(Memory|RocksDB|Sled)Store::drop_graph` and `(Memory|RocksDB|Sled)Store::clear`. ### Removed - The `default_graph_uris` and `named_graph_uris` parameters from `pyoxigraph` `query` methods. diff --git a/lib/src/store/memory.rs b/lib/src/store/memory.rs index 7eed33da..d7b5e522 100644 --- a/lib/src/store/memory.rs +++ b/lib/src/store/memory.rs @@ -484,6 +484,27 @@ impl MemoryStore { } } + /// Clears the store. + /// + /// Usage example: + /// ``` + /// use oxigraph::MemoryStore; + /// use oxigraph::model::{NamedNode, Quad}; + /// + /// let ex = NamedNode::new("http://example.com")?; + /// let quad = Quad::new(ex.clone(), ex.clone(), ex.clone(), ex); + /// let store = MemoryStore::new(); + /// store.insert(quad.clone()); + /// assert_eq!(1, store.len()); + /// + /// store.clear(); + /// assert_eq!(0, store.len()); + /// # Result::<_,Box>::Ok(()) + /// ``` + pub fn clear(&self) { + *self.indexes_mut() = MemoryStoreIndexes::default(); + } + #[allow(clippy::expect_used)] fn indexes(&self) -> RwLockReadGuard<'_, MemoryStoreIndexes> { self.indexes @@ -900,6 +921,7 @@ impl StrEncodingAware for MemoryStoreIndexes { type Error = Infallible; type StrId = LargeSpur; } + impl WritableEncodedStore for MemoryStoreIndexes { fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { if quad.graph_name.is_default_graph() { diff --git a/lib/src/store/rocksdb.rs b/lib/src/store/rocksdb.rs index 1beac4e7..c304a61a 100644 --- a/lib/src/store/rocksdb.rs +++ b/lib/src/store/rocksdb.rs @@ -367,6 +367,23 @@ impl RocksDbStore { } } + /// Clears the store. + /// + /// See [`MemoryStore`](super::memory::MemoryStore::clear()) for a usage example. + pub fn clear(&self) -> Result<(), io::Error> { + self.clear_cf(self.id2str_cf())?; + self.clear_cf(self.spog_cf())?; + self.clear_cf(self.posg_cf())?; + self.clear_cf(self.ospg_cf())?; + self.clear_cf(self.gspo_cf())?; + self.clear_cf(self.gpos_cf())?; + self.clear_cf(self.gosp_cf())?; + self.clear_cf(self.dspo_cf())?; + self.clear_cf(self.dpos_cf())?; + self.clear_cf(self.dosp_cf())?; + Ok(()) + } + fn id2str_cf(&self) -> &ColumnFamily { get_cf(&self.db, ID2STR_CF) } @@ -655,6 +672,34 @@ impl RocksDbStore { encoding, } } + + fn clear_cf(&self, cf: &ColumnFamily) -> Result<(), io::Error> { + self.db + .delete_range_cf( + cf, + [ + u8::MIN, + u8::MIN, + u8::MIN, + u8::MIN, + u8::MIN, + u8::MIN, + u8::MIN, + u8::MIN, + ], + [ + u8::MAX, + u8::MAX, + u8::MAX, + u8::MAX, + u8::MAX, + u8::MAX, + u8::MAX, + u8::MAX, + ], + ) + .map_err(map_err) + } } impl fmt::Display for RocksDbStore { diff --git a/lib/src/store/sled.rs b/lib/src/store/sled.rs index 1f5670ed..890786e3 100644 --- a/lib/src/store/sled.rs +++ b/lib/src/store/sled.rs @@ -398,6 +398,23 @@ impl SledStore { Ok(()) } + /// Clears the store. + /// + /// See [`MemoryStore`](super::memory::MemoryStore::clear()) for a usage example. + pub fn clear(&self) -> Result<(), io::Error> { + self.dspo.clear()?; + self.dpos.clear()?; + self.dosp.clear()?; + self.gspo.clear()?; + self.gpos.clear()?; + self.gosp.clear()?; + self.spog.clear()?; + self.posg.clear()?; + self.ospg.clear()?; + self.id2str.clear()?; + Ok(()) + } + fn contains_encoded(&self, quad: &EncodedQuad) -> Result { let mut buffer = Vec::with_capacity(4 * WRITTEN_TERM_MAX_SIZE); if quad.graph_name.is_default_graph() {