Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: correct constructors for SubtreeStateSyncInfo and MultiStateSyncInfo #298

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions grovedb/src/replication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ struct SubtreeStateSyncInfo<'db> {
num_processed_chunks: usize,
}

impl SubtreeStateSyncInfo<'_> {
// Function to create an instance of SubtreeStateSyncInfo with default values
fn new() -> Self {
let pending_chunks = BTreeSet::new();
Self {
restorer: None,
pending_chunks,
num_processed_chunks: 0,
}
}
ogabrielides marked this conversation as resolved.
Show resolved Hide resolved
}

// Struct governing state sync
pub struct MultiStateSyncInfo<'db> {
// Map of current processing subtrees
Expand All @@ -42,6 +54,25 @@ pub struct MultiStateSyncInfo<'db> {
version: u16,
}

impl MultiStateSyncInfo<'_> {
// Function to create an instance of MultiStateSyncInfo with default values
pub fn new() -> Self {
let processed_prefixes = BTreeSet::new();
let current_prefixes = BTreeMap::default();
Self {
current_prefixes,
processed_prefixes,
version: CURRENT_STATE_SYNC_VERSION,
}
}
}

impl Default for MultiStateSyncInfo<'_> {
fn default() -> Self {
Self::new()
}
}

// Struct containing information about current subtrees found in GroveDB
pub struct SubtreesMetadata {
// Map of Prefix (Path digest) -> (Actual path, Parent Subtree actual_value_hash, Parent
Expand Down Expand Up @@ -140,25 +171,6 @@ pub fn util_decode_vec_ops(chunk: Vec<u8>) -> Result<Vec<Op>, Error> {

#[cfg(feature = "full")]
impl GroveDb {
fn create_subtree_state_sync_info(&self) -> SubtreeStateSyncInfo {
let pending_chunks = BTreeSet::new();
SubtreeStateSyncInfo {
restorer: None,
pending_chunks,
num_processed_chunks: 0,
}
}

pub fn create_multi_state_sync_info(&self) -> MultiStateSyncInfo {
let processed_prefixes = BTreeSet::new();
let current_prefixes = BTreeMap::default();
MultiStateSyncInfo {
current_prefixes,
processed_prefixes,
version: CURRENT_STATE_SYNC_VERSION,
}
}

// Returns the discovered subtrees found recursively along with their associated
// metadata Params:
// tx: Transaction. Function returns the data by opening merks at given tx.
Expand Down Expand Up @@ -383,7 +395,7 @@ impl GroveDb {
replication::util_path_to_string(&[])
);

let mut root_prefix_state_sync_info = self.create_subtree_state_sync_info();
let mut root_prefix_state_sync_info = SubtreeStateSyncInfo::new();
let root_prefix = [0u8; 32];
if let Ok(merk) = self.open_merk_for_replication(SubtreePath::empty(), tx) {
let restorer = Restorer::new(merk, app_hash, None);
Expand Down Expand Up @@ -588,7 +600,7 @@ impl GroveDb {
replication::util_path_to_string(&prefix_metadata.0)
);

let mut subtree_state_sync_info = self.create_subtree_state_sync_info();
let mut subtree_state_sync_info = SubtreeStateSyncInfo::new();
if let Ok(merk) = self.open_merk_for_replication(path.into(), tx) {
let restorer =
Restorer::new(merk, *s_elem_value_hash, Some(*s_actual_value_hash));
Expand Down
2 changes: 1 addition & 1 deletion tutorials/src/bin/replication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn main() {
println!("{:?}", subtrees_metadata_source);

println!("\n######### db_checkpoint_0 -> db_destination state sync");
let state_info = db_destination.create_multi_state_sync_info();
let state_info = MultiStateSyncInfo::new();
let tx = db_destination.start_transaction();
sync_db_demo(&db_checkpoint_0, &db_destination, state_info, &tx).unwrap();
db_destination.commit_transaction(tx).unwrap().expect("expected to commit transaction");
Expand Down
Loading