From 72cd05b478c61fee660110d5a24552d1db3269a0 Mon Sep 17 00:00:00 2001 From: Odysseas Gabrielides Date: Mon, 30 Dec 2024 13:51:45 +0200 Subject: [PATCH] refactor: suggestions --- grovedb/src/lib.rs | 2 +- grovedb/src/replication.rs | 14 +++++++------- grovedb/src/replication/state_sync_session.rs | 19 +++++++------------ 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/grovedb/src/lib.rs b/grovedb/src/lib.rs index 038b8e83..33db70c7 100644 --- a/grovedb/src/lib.rs +++ b/grovedb/src/lib.rs @@ -219,7 +219,7 @@ pub use crate::error::Error; #[cfg(feature = "full")] use crate::operations::proof::util::hex_to_ascii; #[cfg(feature = "full")] -use crate::replication::replication_utils::util_encode_vec_ops; +use crate::replication::utils::encode_vec_ops; #[cfg(feature = "full")] use crate::util::{root_merk_optional_tx, storage_context_optional_tx}; #[cfg(feature = "full")] diff --git a/grovedb/src/replication.rs b/grovedb/src/replication.rs index 47b26b33..c2a6233d 100644 --- a/grovedb/src/replication.rs +++ b/grovedb/src/replication.rs @@ -83,7 +83,7 @@ impl GroveDb { let root_app_hash = self.root_hash(transaction, grove_version).value?; let (chunk_prefix, root_key, is_sum_tree, chunk_id) = - replication_utils::decode_global_chunk_id(global_chunk_id, &root_app_hash)?; + utils::decode_global_chunk_id(global_chunk_id, &root_app_hash)?; // TODO: Refactor this by writing fetch_chunk_inner (as only merk constructor // and type are different) @@ -125,7 +125,7 @@ impl GroveDb { e )) })?; - let op_bytes = replication_utils::util_encode_vec_ops(chunk).map_err(|e| { + let op_bytes = utils::encode_vec_ops(chunk).map_err(|e| { Error::CorruptedData(format!( "failed to encode chunk ops:{} with:{}", hex::encode(chunk_prefix), @@ -170,7 +170,7 @@ impl GroveDb { e )) })?; - let op_bytes = replication_utils::util_encode_vec_ops(chunk).map_err(|e| { + let op_bytes = utils::encode_vec_ops(chunk).map_err(|e| { Error::CorruptedData(format!( "failed to encode chunk ops:{} with:{}", hex::encode(chunk_prefix), @@ -238,7 +238,7 @@ impl GroveDb { println!( " starting:{:?}...", - replication_utils::path_to_string(&[]) + utils::path_to_string(&[]) ); let root_prefix = [0u8; 32]; @@ -258,7 +258,7 @@ impl GroveDb { } } -pub(crate) mod replication_utils { +pub(crate) mod utils { use grovedb_merk::{ ed::Encode, proofs::{Decoder, Op}, @@ -419,7 +419,7 @@ pub(crate) mod replication_utils { /// # Returns /// - `Ok(Vec)`: A byte vector representing the encoded operations. /// - `Err(Error)`: An error if the encoding process fails. - pub fn util_encode_vec_ops(chunk: Vec) -> Result, Error> { + pub fn encode_vec_ops(chunk: Vec) -> Result, Error> { let mut res = vec![]; for op in chunk { op.encode_into(&mut res) @@ -436,7 +436,7 @@ pub(crate) mod replication_utils { /// # Returns /// - `Ok(Vec)`: A vector of decoded `Op` operations. /// - `Err(Error)`: An error if the decoding process fails. - pub fn util_decode_vec_ops(chunk: Vec) -> Result, Error> { + pub fn decode_vec_ops(chunk: Vec) -> Result, Error> { let decoder = Decoder::new(&chunk); let mut res = vec![]; for op in decoder { diff --git a/grovedb/src/replication/state_sync_session.rs b/grovedb/src/replication/state_sync_session.rs index 13769351..e040908d 100644 --- a/grovedb/src/replication/state_sync_session.rs +++ b/grovedb/src/replication/state_sync_session.rs @@ -17,7 +17,7 @@ use grovedb_storage::{ use grovedb_version::version::GroveVersion; use super::{ - replication_utils::{encode_global_chunk_id, path_to_string, util_decode_vec_ops}, + utils::{encode_global_chunk_id, path_to_string, decode_vec_ops}, CURRENT_STATE_SYNC_VERSION, }; use crate::{replication, Element, Error, GroveDb, Transaction}; @@ -100,7 +100,7 @@ impl<'db> SubtreeStateSyncInfo<'db> { } self.pending_chunks.remove(chunk_id); if !chunk_data.is_empty() { - match util_decode_vec_ops(chunk_data) { + match decode_vec_ops(chunk_data) { Ok(ops) => { match self.restorer.process_chunk(chunk_id, ops, grove_version) { Ok(next_chunk_ids) => { @@ -209,13 +209,9 @@ impl<'db> MultiStateSyncSession<'db> { chunk_prefix: [u8; 32], grove_version: &GroveVersion, ) -> Result, Error> { - // SAFETY: we get an immutable reference of a transaction that stays behind - // `Pin` so this reference shall remain valid for the whole session - // object lifetime. let transaction_ref: &'db Transaction<'db> = unsafe { - let tx: &mut Transaction<'db> = - &mut Pin::into_inner_unchecked(self.as_mut()).transaction; - &*(tx as *mut _) + let tx: &Transaction<'db> = &self.as_ref().transaction; + &*(tx as *const _) }; if let Ok((merk, root_key, is_sum_tree)) = @@ -316,7 +312,7 @@ impl<'db> MultiStateSyncSession<'db> { let mut next_chunk_ids = vec![]; let (chunk_prefix, _, _, chunk_id) = - replication::replication_utils::decode_global_chunk_id( + replication::utils::decode_global_chunk_id( global_chunk_id, &self.app_hash, )?; @@ -431,9 +427,8 @@ impl<'db> MultiStateSyncSession<'db> { grove_version: &GroveVersion, ) -> Result { let transaction_ref: &'db Transaction<'db> = unsafe { - let tx: &mut Transaction<'db> = - &mut Pin::into_inner_unchecked(self.as_mut()).transaction; - &*(tx as *mut _) + let tx: &Transaction<'db> = &self.as_ref().transaction; + &*(tx as *const _) }; let subtree_path: Vec<&[u8]> = path_vec.iter().map(|vec| vec.as_slice()).collect(); let path: &[&[u8]] = &subtree_path;