mirror of https://github.com/vacp2p/pmtree.git
chore: refactor batch_insertions
This commit is contained in:
parent
c84e12f124
commit
0462068697
22
src/tree.rs
22
src/tree.rs
|
@ -205,13 +205,14 @@ where
|
||||||
let root_key = Key(0, 0);
|
let root_key = Key(0, 0);
|
||||||
|
|
||||||
subtree.insert(root_key, self.root);
|
subtree.insert(root_key, self.root);
|
||||||
self.fill_nodes(root_key, self.next_index, end, &mut subtree)?;
|
self.fill_nodes(
|
||||||
|
root_key,
|
||||||
////// TODO! Refactor later
|
self.next_index,
|
||||||
for i in self.next_index..end {
|
end,
|
||||||
subtree.insert(Key(self.depth, i), leaves[i - self.next_index]);
|
&mut subtree,
|
||||||
}
|
leaves,
|
||||||
//////
|
self.next_index,
|
||||||
|
)?;
|
||||||
|
|
||||||
let subtree = Arc::new(RwLock::new(subtree));
|
let subtree = Arc::new(RwLock::new(subtree));
|
||||||
|
|
||||||
|
@ -248,8 +249,11 @@ where
|
||||||
start: usize,
|
start: usize,
|
||||||
end: usize,
|
end: usize,
|
||||||
subtree: &mut HashMap<Key, H::Fr>,
|
subtree: &mut HashMap<Key, H::Fr>,
|
||||||
|
leaves: &[H::Fr],
|
||||||
|
from: usize,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
if key.0 == self.depth {
|
if key.0 == self.depth {
|
||||||
|
subtree.insert(key, leaves[key.1 - from]);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,11 +269,11 @@ where
|
||||||
let half = 1 << (self.depth - key.0 - 1);
|
let half = 1 << (self.depth - key.0 - 1);
|
||||||
|
|
||||||
if start < half {
|
if start < half {
|
||||||
self.fill_nodes(left, start, min(end, half), subtree)?;
|
self.fill_nodes(left, start, min(end, half), subtree, leaves, from)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if end > half {
|
if end > half {
|
||||||
self.fill_nodes(right, 0, end - half, subtree)?;
|
self.fill_nodes(right, 0, end - half, subtree, leaves, from)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -129,7 +129,7 @@ fn insert_delete() -> Result<()> {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn batch_insertions() -> Result<()> {
|
fn batch_insertions() -> Result<()> {
|
||||||
let mut mt = MerkleTree::<MySled, MyKeccak>::new(2, "abacabas")?;
|
let mut mt = MerkleTree::<MySled, MyKeccak>::new(2, "abacabasa")?;
|
||||||
|
|
||||||
let leaves = [
|
let leaves = [
|
||||||
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
|
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
|
||||||
|
@ -145,7 +145,7 @@ fn batch_insertions() -> Result<()> {
|
||||||
hex!("a9bb8c3f1f12e9aa903a50c47f314b57610a3ab32f2d463293f58836def38d36")
|
hex!("a9bb8c3f1f12e9aa903a50c47f314b57610a3ab32f2d463293f58836def38d36")
|
||||||
);
|
);
|
||||||
|
|
||||||
fs::remove_dir_all("abacabas").expect("Error removing db");
|
fs::remove_dir_all("abacabasa").expect("Error removing db");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue