Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fominok committed Nov 5, 2024
1 parent eb7b6b2 commit ea61c39
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions grovedb/src/merk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,24 @@ struct CachedMerk<'db> {
/// structure. Eventually we'll have enough info at the same place to perform
/// necessary propagations as well.
pub(crate) struct MerkCache<'db, 'b, B> {

Check warning on line 32 in grovedb/src/merk_cache.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `MerkCache` is never constructed

warning: struct `MerkCache` is never constructed --> grovedb/src/merk_cache.rs:32:19 | 32 | pub(crate) struct MerkCache<'db, 'b, B> { | ^^^^^^^^^
merks: BTreeMap<SubtreePath<'b, B>, CachedMerk<'db>>,
db: &'db GroveDb,
tx: &'db Transaction<'db>,
batch: &'db StorageBatch,
batch: &'static StorageBatch,
version: &'db GroveVersion,
merks: BTreeMap<SubtreePath<'b, B>, CachedMerk<'db>>,
}

impl<'db, 'b, B: AsRef<[u8]>> MerkCache<'db, 'b, B> {
pub(crate) fn new(

Check warning on line 41 in grovedb/src/merk_cache.rs

View workflow job for this annotation

GitHub Actions / clippy

associated items `new`, `get_merk_mut_internal`, `get_multi_mut`, and `finalize` are never used

warning: associated items `new`, `get_merk_mut_internal`, `get_multi_mut`, and `finalize` are never used --> grovedb/src/merk_cache.rs:41:19 | 40 | impl<'db, 'b, B: AsRef<[u8]>> MerkCache<'db, 'b, B> { | --------------------------------------------------- associated items in this implementation 41 | pub(crate) fn new( | ^^^ ... 58 | fn get_merk_mut_internal<'s>( | ^^^^^^^^^^^^^^^^^^^^^ ... 92 | pub(crate) fn get_multi_mut<'s, const N: usize>( | ^^^^^^^^^^^^^ ... 136 | pub(crate) fn finalize(self) -> Box<StorageBatch> { | ^^^^^^^^
db: &'db GroveDb,
tx: &'db Transaction<'db>,
batch: &'db StorageBatch,
version: &'db GroveVersion,
) -> Self {
MerkCache {
db,
tx,
batch,
version,
batch: Box::leak(Box::new(StorageBatch::default())),
merks: Default::default(),
}
}
Expand Down Expand Up @@ -131,6 +130,19 @@ impl<'db, 'b, B: AsRef<[u8]>> MerkCache<'db, 'b, B> {

Ok(result).wrap_with_cost(cost)
}

/// Summarizes all performed operations on this `MerkCache` with necessary
/// propagations into a `Storagebatch`.
pub(crate) fn finalize(self) -> Box<StorageBatch> {
let batch_ptr = self.batch as *const _;
// Remove all possible usages of the `StorageBatch`:
drop(self);

// SAFETY: The batch reference was created by `Box::leak` and restored into a
// `Box` form. No shared usage of that memory exists because the
// `MerkCache` structure was dropped above.
unsafe { Box::from_raw(batch_ptr as *mut _) }
}
}

/// Handle to a cached Merk.
Expand Down Expand Up @@ -193,7 +205,7 @@ mod tests {
let db = make_deep_tree(&version);
let tx = db.start_transaction();
let batch = StorageBatch::new();
let mut cache = MerkCache::new(&db, &tx, &batch, version);
let mut cache = MerkCache::new(&db, &tx, version);

let mut cost: OperationCost = Default::default();
let [test1, test2] = cache
Expand Down Expand Up @@ -231,7 +243,7 @@ mod tests {
let db = make_deep_tree(&version);
let tx = db.start_transaction();
let batch = StorageBatch::new();
let mut cache = MerkCache::new(&db, &tx, &batch, version);
let mut cache = MerkCache::new(&db, &tx, version);

let _ = cache.get_multi_mut([
SubtreePath::from(&[TEST_LEAF]),
Expand Down

0 comments on commit ea61c39

Please sign in to comment.