Skip to content

Commit

Permalink
feat: secondary rocksdb storage
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov committed Feb 8, 2024
1 parent 206bc63 commit ec24038
Show file tree
Hide file tree
Showing 12 changed files with 1,017 additions and 609 deletions.
2 changes: 1 addition & 1 deletion grovedb/src/estimated_costs/average_case_costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ mod test {
fn test_get_merk_node_average_case() {
// Open a merk and insert 10 elements.
let tmp_dir = TempDir::new().expect("cannot open tempdir");
let storage = RocksDbStorage::default_rocksdb_with_path(tmp_dir.path())
let storage = RocksDbStorage::default_primary_rocksdb(tmp_dir.path())
.expect("cannot open rocksdb storage");
let batch = StorageBatch::new();

Expand Down
21 changes: 19 additions & 2 deletions grovedb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,29 @@ pub type TransactionArg<'db, 'a> = Option<&'a Transaction<'db>>;

#[cfg(feature = "full")]
impl GroveDb {
/// Opens a given path
/// Opens a primary storage with a given path
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
let db = RocksDbStorage::default_rocksdb_with_path(path)?;
let db = RocksDbStorage::default_primary_rocksdb(path)?;
Ok(GroveDb { db })
}

/// Open a secondary storage with given paths
pub fn open_secondary<P: AsRef<Path>>(
primary_path: P,
secondary_storage: P,
) -> Result<Self, Error> {
let db = RocksDbStorage::default_secondary_rocksdb(primary_path, secondary_storage)?;
Ok(GroveDb { db })
}

/// Replicate recent changes from primary database
/// Available only for a secondary storage
pub fn try_to_catch_up_from_primary(&self) -> Result<(), Error> {
self.db.try_to_catch_up_from_primary()?;

Ok(())
}

/// Uses raw iter to delete GroveDB key values pairs from rocksdb
pub fn wipe(&self) -> Result<(), Error> {
self.db.wipe()?;
Expand Down
71 changes: 71 additions & 0 deletions grovedb/tests/secondary_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use grovedb::GroveDb;
use tempfile::TempDir;

#[test]
fn test_some_functionality() {
let primary_dir = TempDir::new().expect("should create temp dir");

let primary_grovedb = GroveDb::open(primary_dir.path()).expect("should open grovedb");

// Store value in primary

let key = b"key";
let value = vec![1, 2, 3];

primary_grovedb
.put_aux(key, &value, None, None)
.unwrap()
.expect("should put value to primary");

// Read value from primary

let primary_value = primary_grovedb
.get_aux(key, None)
.unwrap()
.expect("should get value from primary")
.expect("value should exist on primary");

assert_eq!(value, primary_value);

// Open secondary

let secondary_dir = TempDir::new().expect("should create temp dir");

let secondary_grovedb = GroveDb::open_secondary(primary_dir.path(), secondary_dir.path())
.expect("should open secondary");

// Read value on secondary

let secondary_value = secondary_grovedb
.get_aux(key, None)
.unwrap()
.expect("should get value from secondary")
.expect("value from primary should exist on secondary");

assert_eq!(primary_value, secondary_value);

// Update value on primary

let primary_value2 = vec![4, 5, 6];

primary_grovedb
.put_aux(key, &primary_value2, None, None)
.unwrap()
.expect("should put value to primary");

// Catch up secondary

secondary_grovedb
.try_to_catch_up_from_primary()
.expect("should catch up");

// Read updated value on secondary

let secondary_value2 = secondary_grovedb
.get_aux(key, None)
.unwrap()
.expect("should get value from secondary")
.expect("value from primary should exist on secondary");

assert_eq!(primary_value2, secondary_value2);
}
4 changes: 2 additions & 2 deletions merk/src/merk/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ mod tests {
fn chunks_from_reopen() {
let tmp_dir = TempDir::new().expect("cannot create tempdir");
let original_chunks = {
let storage = RocksDbStorage::default_rocksdb_with_path(tmp_dir.path())
let storage = RocksDbStorage::default_primary_rocksdb(tmp_dir.path())
.expect("cannot open rocksdb storage");
let batch = StorageBatch::new();
let mut merk = Merk::open_base(
Expand Down Expand Up @@ -307,7 +307,7 @@ mod tests {
.collect::<Vec<_>>()
.into_iter()
};
let storage = RocksDbStorage::default_rocksdb_with_path(tmp_dir.path())
let storage = RocksDbStorage::default_primary_rocksdb(tmp_dir.path())
.expect("cannot open rocksdb storage");
let merk = Merk::open_base(
storage
Expand Down
14 changes: 7 additions & 7 deletions merk/src/merk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ mod test {
#[test]
fn reopen_check_root_hash() {
let tmp_dir = TempDir::new().expect("cannot open tempdir");
let storage = RocksDbStorage::default_rocksdb_with_path(tmp_dir.path())
let storage = RocksDbStorage::default_primary_rocksdb(tmp_dir.path())
.expect("cannot open rocksdb storage");
let mut merk = Merk::open_base(
storage
Expand All @@ -763,7 +763,7 @@ mod test {
#[test]
fn test_get_node_cost() {
let tmp_dir = TempDir::new().expect("cannot open tempdir");
let storage = RocksDbStorage::default_rocksdb_with_path(tmp_dir.path())
let storage = RocksDbStorage::default_primary_rocksdb(tmp_dir.path())
.expect("cannot open rocksdb storage");
let mut merk = Merk::open_base(
storage
Expand Down Expand Up @@ -807,7 +807,7 @@ mod test {
let tmp_dir = TempDir::new().expect("cannot open tempdir");

let original_nodes = {
let storage = RocksDbStorage::default_rocksdb_with_path(tmp_dir.path())
let storage = RocksDbStorage::default_primary_rocksdb(tmp_dir.path())
.expect("cannot open rocksdb storage");
let batch = StorageBatch::new();
let mut merk = Merk::open_base(
Expand Down Expand Up @@ -846,7 +846,7 @@ mod test {
nodes
};

let storage = RocksDbStorage::default_rocksdb_with_path(tmp_dir.path())
let storage = RocksDbStorage::default_primary_rocksdb(tmp_dir.path())
.expect("cannot open rocksdb storage");
let merk = Merk::open_base(
storage
Expand Down Expand Up @@ -883,7 +883,7 @@ mod test {
let tmp_dir = TempDir::new().expect("cannot open tempdir");

let original_nodes = {
let storage = RocksDbStorage::default_rocksdb_with_path(tmp_dir.path())
let storage = RocksDbStorage::default_primary_rocksdb(tmp_dir.path())
.expect("cannot open rocksdb storage");
let batch = StorageBatch::new();
let mut merk = Merk::open_base(
Expand Down Expand Up @@ -918,7 +918,7 @@ mod test {
collect(&mut merk.storage.raw_iter(), &mut nodes);
nodes
};
let storage = RocksDbStorage::default_rocksdb_with_path(tmp_dir.path())
let storage = RocksDbStorage::default_primary_rocksdb(tmp_dir.path())
.expect("cannot open rocksdb storage");
let merk = Merk::open_base(
storage
Expand All @@ -939,7 +939,7 @@ mod test {
#[test]
fn update_node() {
let tmp_dir = TempDir::new().expect("cannot open tempdir");
let storage = RocksDbStorage::default_rocksdb_with_path(tmp_dir.path())
let storage = RocksDbStorage::default_primary_rocksdb(tmp_dir.path())
.expect("cannot open rocksdb storage");
let batch = StorageBatch::new();
let mut merk = Merk::open_base(
Expand Down
2 changes: 1 addition & 1 deletion merk/src/merk/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ mod test {
#[test]
fn test_reopen_root_hash() {
let tmp_dir = TempDir::new().expect("cannot open tempdir");
let storage = RocksDbStorage::default_rocksdb_with_path(tmp_dir.path())
let storage = RocksDbStorage::default_primary_rocksdb(tmp_dir.path())
.expect("cannot open rocksdb storage");
let test_prefix = [b"ayy"];

Expand Down
Loading

0 comments on commit ec24038

Please sign in to comment.