Skip to content

Commit

Permalink
feat(db, tree): add function to decompose the structure to allow acce…
Browse files Browse the repository at this point in the history
…ss back to the underlying db

If you need to access the underlying db after finishing work with the tree, you can call decompose()
on the tree to get its underlying storage object back.
  • Loading branch information
ChosunOne committed Jul 11, 2022
1 parent 1c75535 commit 914f63d
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/hash_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,11 @@ impl<const N: usize> HashTree<N> {
) -> BinaryMerkleTreeResult<Array<N>> {
self.tree.insert_one(previous_root, key, value)
}

#[inline]
#[must_use]
/// Decomposes the tree into the its DB and size
pub fn decompose(self) -> (HashTreeDB<N>, usize) {
self.tree.decompose()
}
}
8 changes: 8 additions & 0 deletions src/merkle_bit.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unused_qualifications)]

#[cfg(not(any(feature = "hashbrown")))]
use std::collections::HashMap;
use std::collections::VecDeque;
Expand Down Expand Up @@ -900,6 +902,12 @@ impl<M: MerkleTree<N>, const N: usize> MerkleBIT<M, N> {
let new_root = self.create_tree(tree_refs)?;
Ok(new_root)
}

/// Decomposes the tree into its underlying data structures
#[inline]
pub fn decompose(self) -> (M::Database, usize) {
(self.db, self.depth)
}
}

/// Enum used for splitting nodes into either the left or right path during tree traversal
Expand Down
6 changes: 6 additions & 0 deletions src/rocks_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,10 @@ impl<const N: usize, ValueType: Encode + Decode> RocksTree<N, ValueType> {
) -> BinaryMerkleTreeResult<()> {
Tree::verify_inclusion_proof(root, key, value, proof)
}

#[inline]
#[must_use]
pub fn decompose(self) -> (RocksDB<N>, usize) {
self.tree.decompose()
}
}
5 changes: 5 additions & 0 deletions src/tree_db/hashbrown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ impl<const N: usize> HashDB<N> {
pub fn new(map: HashMap<Array<N>, TreeNode<N>>) -> Self {
Self { map }
}
#[inline]
#[must_use]
pub fn decompose(self) -> HashMap<Array<N>, TreeNode<N>> {
self.map
}
}

impl<const N: usize> Database<N, TreeNode<N>> for HashDB<N> {
Expand Down
7 changes: 7 additions & 0 deletions src/tree_db/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ impl<const N: usize> HashDB<N> {
pub const fn new(map: HashMap<Array<N>, TreeNode<N>>) -> Self {
Self { map }
}

#[inline]
#[must_use]
/// Decomposes the `HashDB` into its underlying `HashMap`.
pub fn decompose(self) -> HashMap<Array<N>, TreeNode<N>> {
self.map
}
}

impl<const N: usize> Database<N, TreeNode<N>> for HashDB<N> {
Expand Down
5 changes: 5 additions & 0 deletions src/tree_db/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ impl<const N: usize> RocksDB<N> {
pending_inserts: Some(WriteBatch::default()),
}
}

#[inline]
pub fn decompose(self) -> DB {
self.db
}
}

impl<const N: usize> Database<N, TreeNode<N>> for RocksDB<N> {
Expand Down
28 changes: 28 additions & 0 deletions tests/merkle_bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,34 @@ pub mod integration_tests {
Ok(())
}

#[test]
fn it_actually_removes_things_from_the_db() -> BinaryMerkleTreeResult<()> {
let seed = [0x51u8; KEY_LEN];
let path = generate_path(seed);
let mut rng: StdRng = SeedableRng::from_seed(seed);

#[cfg(not(feature = "groestl"))]
let num_entries = 4096;
#[cfg(feature = "groestl")]
let num_entries = 512;

let (mut keys, values) = prepare_inserts(num_entries, &mut rng);

let mut bmt = Tree::open(&path, 160)?;

let root = bmt.insert(None, &mut keys, &values)?;

bmt.remove(&root)?;

let (db, _) = bmt.decompose();
let map = db.decompose();
#[cfg(not(any(feature = "rocksdb")))]
assert_eq!(map.keys().len(), 0);

tear_down(&path);
Ok(())
}

test_key_size!(it_handles_key_size_of_two, 2, [0x94u8; 32], 16, 16);
test_key_size!(it_handles_key_size_of_three, 3, [0x95u8; 32], 32, 32);
test_key_size!(it_handles_key_size_of_four, 4, [0x96u8; 32], 64, 64);
Expand Down

0 comments on commit 914f63d

Please sign in to comment.