Skip to content

Commit

Permalink
feat: raw iter data wipe (#265)
Browse files Browse the repository at this point in the history
* raw iterator delete

* wipe grovedb data with &self

* update comment

* cleanup
  • Loading branch information
iammadab authored Jul 21, 2023
1 parent c21af96 commit cc1b577
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
4 changes: 2 additions & 2 deletions grovedb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ impl GroveDb {
Ok(GroveDb { db })
}

/// Deletes GroveDB folder from disk and drop GroveDB instance
pub fn wipe(self) -> Result<(), Error> {
/// Uses raw iter to delete GroveDB key values pairs from rocksdb
pub fn wipe(&self) -> Result<(), Error> {
self.db.wipe()?;
Ok(())
}
Expand Down
3 changes: 0 additions & 3 deletions grovedb/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2934,9 +2934,6 @@ fn test_storage_wipe() {
// wipe the database
db.grove_db.wipe().unwrap();

// re-open database
let db = GroveDb::open(path).unwrap();

// retrieve key after wipe
let elem_result = db.get(&[TEST_LEAF.as_ref()], b"key", None).unwrap();
assert!(elem_result.is_err());
Expand Down
30 changes: 25 additions & 5 deletions storage/src/rocksdb_storage/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use integer_encoding::VarInt;
use lazy_static::lazy_static;
use rocksdb::{
checkpoint::Checkpoint, ColumnFamily, ColumnFamilyDescriptor, OptimisticTransactionDB, Options,
Transaction, WriteBatchWithTransaction, DB,
Transaction, WriteBatchWithTransaction, DB, DEFAULT_COLUMN_FAMILY_NAME,
};

use super::{
Expand Down Expand Up @@ -407,10 +407,30 @@ impl RocksDbStorage {
}

/// Destroys the OptimisticTransactionDB and drops instance
pub fn wipe(self) -> Result<(), Error> {
let path = self.db.path().to_path_buf();
drop(self);
DB::destroy(&Options::default(), path).map_err(|e| StorageError(e.into_string()))?;
pub fn wipe(&self) -> Result<(), Error> {
// TODO: fix this
// very inefficient way of doing this, time complexity is O(n)
// we can do O(1)
self.wipe_column_family(DEFAULT_COLUMN_FAMILY_NAME)?;
self.wipe_column_family(ROOTS_CF_NAME)?;
self.wipe_column_family(AUX_CF_NAME)?;
self.wipe_column_family(META_CF_NAME)?;
Ok(())
}

fn wipe_column_family(&self, column_family_name: &str) -> Result<(), Error> {
let cf_handle = self
.db
.cf_handle(column_family_name)
.ok_or(Error::StorageError(
"failed to get column family handle".to_string(),
))?;
let mut iter = self.db.raw_iterator_cf(&cf_handle);
iter.seek_to_first();
while iter.valid() {
self.db.delete(iter.key().expect("should have key"));
iter.next()
}
Ok(())
}
}
Expand Down

0 comments on commit cc1b577

Please sign in to comment.