mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-20 19:11:08 +03:00
use extend_from_slice when appending multiple hashes to hash file (#3497)
* use extend_from_slice when appending multiple hashes to hash file * bump
This commit is contained in:
parent
055b684416
commit
2125c05020
5 changed files with 24 additions and 10 deletions
|
@ -27,7 +27,7 @@ pub trait Backend<T: PMMRable> {
|
||||||
/// associated data element to flatfile storage (for leaf nodes only). The
|
/// associated data element to flatfile storage (for leaf nodes only). The
|
||||||
/// position of the first element of the Vec in the MMR is provided to
|
/// position of the first element of the Vec in the MMR is provided to
|
||||||
/// help the implementation.
|
/// help the implementation.
|
||||||
fn append(&mut self, data: &T, hashes: Vec<Hash>) -> Result<(), String>;
|
fn append(&mut self, data: &T, hashes: &[Hash]) -> Result<(), String>;
|
||||||
|
|
||||||
/// Rewind the backend state to a previous position, as if all append
|
/// Rewind the backend state to a previous position, as if all append
|
||||||
/// operations after that had been canceled. Expects a position in the PMMR
|
/// operations after that had been canceled. Expects a position in the PMMR
|
||||||
|
|
|
@ -237,7 +237,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
// append all the new nodes and update the MMR index
|
// append all the new nodes and update the MMR index
|
||||||
self.backend.append(elmt, hashes)?;
|
self.backend.append(elmt, &hashes)?;
|
||||||
self.last_pos = pos;
|
self.last_pos = pos;
|
||||||
Ok(elmt_pos)
|
Ok(elmt_pos)
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,11 +35,11 @@ pub struct VecBackend<T: PMMRable> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: PMMRable> Backend<T> for VecBackend<T> {
|
impl<T: PMMRable> Backend<T> for VecBackend<T> {
|
||||||
fn append(&mut self, elmt: &T, hashes: Vec<Hash>) -> Result<(), String> {
|
fn append(&mut self, elmt: &T, hashes: &[Hash]) -> Result<(), String> {
|
||||||
if let Some(data) = &mut self.data {
|
if let Some(data) = &mut self.data {
|
||||||
data.push(elmt.clone());
|
data.push(elmt.clone());
|
||||||
}
|
}
|
||||||
self.hashes.append(&mut hashes.clone());
|
self.hashes.extend_from_slice(hashes);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -66,17 +66,15 @@ impl<T: PMMRable> Backend<T> for PMMRBackend<T> {
|
||||||
/// Append the provided data and hashes to the backend storage.
|
/// Append the provided data and hashes to the backend storage.
|
||||||
/// Add the new leaf pos to our leaf_set if this is a prunable MMR.
|
/// Add the new leaf pos to our leaf_set if this is a prunable MMR.
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
fn append(&mut self, data: &T, hashes: Vec<Hash>) -> Result<(), String> {
|
fn append(&mut self, data: &T, hashes: &[Hash]) -> Result<(), String> {
|
||||||
let size = self
|
let size = self
|
||||||
.data_file
|
.data_file
|
||||||
.append(&data.as_elmt())
|
.append(&data.as_elmt())
|
||||||
.map_err(|e| format!("Failed to append data to file. {}", e))?;
|
.map_err(|e| format!("Failed to append data to file. {}", e))?;
|
||||||
|
|
||||||
for h in &hashes {
|
self.hash_file
|
||||||
self.hash_file
|
.extend_from_slice(hashes)
|
||||||
.append(h)
|
.map_err(|e| format!("Failed to append hash to file. {}", e))?;
|
||||||
.map_err(|e| format!("Failed to append hash to file. {}", e))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.prunable {
|
if self.prunable {
|
||||||
// (Re)calculate the latest pos given updated size of data file
|
// (Re)calculate the latest pos given updated size of data file
|
||||||
|
|
|
@ -99,6 +99,14 @@ where
|
||||||
Ok(self.size_unsync())
|
Ok(self.size_unsync())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Append a slice of multiple elements to the file.
|
||||||
|
/// Will not be written to disk until flush() is subsequently called.
|
||||||
|
/// Alternatively discard() may be called to discard any pending changes.
|
||||||
|
pub fn extend_from_slice(&mut self, data: &[T]) -> io::Result<u64> {
|
||||||
|
self.file.append_elmts(data)?;
|
||||||
|
Ok(self.size_unsync())
|
||||||
|
}
|
||||||
|
|
||||||
/// Read an element from the file by position.
|
/// Read an element from the file by position.
|
||||||
/// Assumes we have already "shifted" the position to account for pruned data.
|
/// Assumes we have already "shifted" the position to account for pruned data.
|
||||||
/// Note: PMMR API is 1-indexed, but backend storage is 0-indexed.
|
/// Note: PMMR API is 1-indexed, but backend storage is 0-indexed.
|
||||||
|
@ -280,6 +288,14 @@ where
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Iterate over the slice and append each element.
|
||||||
|
fn append_elmts(&mut self, data: &[T]) -> io::Result<()> {
|
||||||
|
for x in data {
|
||||||
|
self.append_elmt(x)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Append data to the file. Until the append-only file is synced, data is
|
/// Append data to the file. Until the append-only file is synced, data is
|
||||||
/// only written to memory.
|
/// only written to memory.
|
||||||
pub fn append(&mut self, bytes: &mut [u8]) -> io::Result<()> {
|
pub fn append(&mut self, bytes: &mut [u8]) -> io::Result<()> {
|
||||||
|
|
Loading…
Reference in a new issue