test: implement batch_insert for memory_keccak

This commit is contained in:
Magamedrasul Ibragimov 2023-02-02 15:47:19 +04:00
parent 9055b14f07
commit 0d3f2e0657
5 changed files with 31 additions and 6 deletions

View File

@ -21,5 +21,5 @@ pub trait Database {
fn put(&mut self, key: DBKey, value: Value) -> Result<()>;
/// Batc
fn put_batch(&mut self, subtree: &HashMap<DBKey, Value>) -> Result<()>;
fn put_batch(&mut self, subtree: HashMap<DBKey, Value>) -> Result<()>;
}

View File

@ -221,7 +221,12 @@ where
let subtree = RwLock::into_inner(Arc::try_unwrap(subtree).unwrap()).unwrap();
self.db.put_batch(&subtree.into_iter().)?;
self.db.put_batch(
subtree
.into_iter()
.map(|(key, value)| (key.into(), H::serialize(value)))
.collect(),
)?;
// Update root value and next_index in memory
self.root = root_val;

View File

@ -25,7 +25,9 @@ impl Database for MemoryDB {
Ok(())
}
fn put_batch(&mut self, subtree: &HashMap<DBKey, Value>) -> Result<()> {
fn put_batch(&mut self, subtree: HashMap<DBKey, Value>) -> Result<()> {
self.0.extend(subtree.into_iter());
Ok(())
}
}
@ -101,3 +103,19 @@ fn insert_delete() -> Result<()> {
Ok(())
}
#[test]
fn batch_insertions() -> Result<()> {
let mut mt = MerkleTree::<MemoryDB, MyKeccak>::new(2, "abacaba")?;
let leaves = [
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("0000000000000000000000000000000000000000000000000000000000000002"),
hex!("0000000000000000000000000000000000000000000000000000000000000003"),
hex!("0000000000000000000000000000000000000000000000000000000000000004"),
];
mt.batch_insert(&leaves)?;
Ok(())
}

View File

@ -62,7 +62,9 @@ impl Database for MemoryDB {
Ok(())
}
fn put_batch(&mut self, subtree: &HashMap<DBKey, Value>) -> Result<()> {
fn put_batch(&mut self, subtree: HashMap<DBKey, Value>) -> Result<()> {
self.0.extend(subtree.into_iter());
Ok(())
}
}
@ -100,7 +102,7 @@ impl Database for MySled {
Ok(())
}
fn put_batch(&mut self, subtree: &HashMap<DBKey, Value>) -> Result<()> {
fn put_batch(&mut self, subtree: HashMap<DBKey, Value>) -> Result<()> {
Ok(())
}
}

View File

@ -40,7 +40,7 @@ impl Database for MySled {
Ok(())
}
fn put_batch(&mut self, subtree: &HashMap<DBKey, Value>) -> Result<()> {
fn put_batch(&mut self, subtree: HashMap<DBKey, Value>) -> Result<()> {
Ok(())
}
}