diff --git a/lib/benches/store.rs b/lib/benches/store.rs index 37f479d6..1d43ff96 100644 --- a/lib/benches/store.rs +++ b/lib/benches/store.rs @@ -43,6 +43,7 @@ fn do_load(store: &Store, data: &[u8]) { None, ) .unwrap(); + store.optimize().unwrap(); } fn store_query_and_update(c: &mut Criterion) { diff --git a/lib/src/storage/backend/rocksdb.rs b/lib/src/storage/backend/rocksdb.rs index 6b0ff260..151c9969 100644 --- a/lib/src/storage/backend/rocksdb.rs +++ b/lib/src/storage/backend/rocksdb.rs @@ -304,7 +304,6 @@ impl Db { } } - #[cfg(test)] #[allow(clippy::unnecessary_wraps)] pub fn compact(&self, column_family: &ColumnFamily) -> Result<()> { unsafe { diff --git a/lib/src/storage/mod.rs b/lib/src/storage/mod.rs index 38258100..30f496d3 100644 --- a/lib/src/storage/mod.rs +++ b/lib/src/storage/mod.rs @@ -841,6 +841,21 @@ impl Storage { self.db.flush(&self.id2str_cf) } + #[cfg(not(target_arch = "wasm32"))] + pub fn compact(&self) -> Result<()> { + self.db.compact(&self.default_cf)?; + self.db.compact(&self.gpos_cf)?; + self.db.compact(&self.gpos_cf)?; + self.db.compact(&self.gosp_cf)?; + self.db.compact(&self.spog_cf)?; + self.db.compact(&self.posg_cf)?; + self.db.compact(&self.ospg_cf)?; + self.db.compact(&self.dspo_cf)?; + self.db.compact(&self.dpos_cf)?; + self.db.compact(&self.dosp_cf)?; + self.db.compact(&self.id2str_cf) + } + pub fn get_str(&self, key: &StrHash) -> Result> { self.db .get(&self.id2str_cf, &key.to_be_bytes())? diff --git a/lib/src/store.rs b/lib/src/store.rs index 02ff24bd..f371a6c8 100644 --- a/lib/src/store.rs +++ b/lib/src/store.rs @@ -561,6 +561,16 @@ impl Store { pub fn flush(&self) -> io::Result<()> { self.storage.flush() } + + /// Optimizes the database for future workload. + /// + /// Useful to call after a batch upload or an other similar operation. + /// + /// Warning: Can take hours on huge databases. + #[cfg(not(target_arch = "wasm32"))] + pub fn optimize(&self) -> io::Result<()> { + self.storage.compact() + } } impl fmt::Display for Store {