chore: implement parallel insertion

This commit is contained in:
Magamedrasul Ibragimov 2023-02-02 14:31:18 +04:00
parent 3544ff00bd
commit 824e643c77
6 changed files with 50 additions and 14 deletions

View File

@ -1,5 +1,7 @@
use crate::*;
use std::collections::HashMap;
/// Trait that must be implemented for a Database
pub trait Database {
/// Creates new instance of db
@ -17,4 +19,7 @@ pub trait Database {
/// Puts the value to the db by the key
fn put(&mut self, key: DBKey, value: Value) -> Result<()>;
/// Batc
fn put_batch(&mut self, subtree: &HashMap<DBKey, Value>) -> Result<()>;
}

View File

@ -1,9 +1,11 @@
use crate::*;
use std::fmt::Debug;
/// Trait that must be implemented for Hash Function
pub trait Hasher {
/// Native type for the hash-function
type Fr: Copy + Eq + Default + Sync + Send;
type Fr: Copy + Eq + Default + Sync + Send + Debug;
/// Serializes Self::Fr
fn serialize(value: Self::Fr) -> Value;

View File

@ -213,11 +213,23 @@ where
let subtree = Arc::new(Mutex::new(subtree));
let n = rayon::ThreadPoolBuilder::new()
let root_val = rayon::ThreadPoolBuilder::new()
.num_threads(8)
.build()
.unwrap()
.install(|| Self::batch_recalculate(root_key, subtree, self.depth));
.install(|| Self::batch_recalculate(root_key, Arc::clone(&subtree), self.depth));
let subtree = Mutex::into_inner(Arc::try_unwrap(subtree).unwrap()).unwrap();
// self.db.put_batch(&subtree.into_iter().)?;
// Update root value and next_index in memory
self.root = root_val;
self.next_index = end;
// Update next_index value in db
self.db
.put(NEXT_INDEX_KEY, self.next_index.to_be_bytes().to_vec())?;
Ok(())
}
@ -262,20 +274,20 @@ where
subtree: Arc<Mutex<HashMap<Key, H::Fr>>>,
depth: usize,
) -> H::Fr {
// if key.0 == depth {
// return subtree.into_inner().unwrap().get(&key).u;
// }
// let (left, right) = rayon::join(
// || Self::batch_recalculate(key, subtree, depth),
// || Self::batch_recalculate(key, subtree, depth),
// );
H::default_leaf()
if key.0 == depth {
return *subtree.lock().unwrap().get(&key).unwrap();
}
fn db_batch_insert(&mut self, subtree: HashMap<Key, H::Fr>) -> Result<()> {
Ok(())
let (left, right) = rayon::join(
|| Self::batch_recalculate(Key(key.0 + 1, key.1 * 2), Arc::clone(&subtree), depth),
|| Self::batch_recalculate(Key(key.0 + 1, key.1 * 2 + 1), Arc::clone(&subtree), depth),
);
let result = H::hash(&[left, right]);
subtree.lock().unwrap().insert(key, result);
result
}
/// Computes a Merkle proof for the leaf at the specified index

View File

@ -24,6 +24,10 @@ impl Database for MemoryDB {
Ok(())
}
fn put_batch(&mut self, subtree: &HashMap<DBKey, Value>) -> Result<()> {
Ok(())
}
}
impl Hasher for MyKeccak {

View File

@ -61,6 +61,10 @@ impl Database for MemoryDB {
Ok(())
}
fn put_batch(&mut self, subtree: &HashMap<DBKey, Value>) -> Result<()> {
Ok(())
}
}
impl Database for MySled {
@ -95,6 +99,10 @@ impl Database for MySled {
Ok(())
}
fn put_batch(&mut self, subtree: &HashMap<DBKey, Value>) -> Result<()> {
Ok(())
}
}
#[test]

View File

@ -1,5 +1,6 @@
use hex_literal::hex;
use pmtree::*;
use std::collections::HashMap;
use std::fs;
use tiny_keccak::{Hasher as _, Keccak};
@ -38,6 +39,10 @@ impl Database for MySled {
Ok(())
}
fn put_batch(&mut self, subtree: &HashMap<DBKey, Value>) -> Result<()> {
Ok(())
}
}
impl Hasher for MyKeccak {