Skip to content

Commit

Permalink
write_into_persistent_log
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Oct 13, 2021
1 parent dc669da commit fab2c11
Showing 1 changed file with 25 additions and 2 deletions.
@@ -1,7 +1,12 @@
use bincode::Options;
use chain_core::property::Serialize;
use chain_impl_mockchain::fragment::Fragment;
use jormungandr_lib::interfaces::load_persistent_fragments_logs_from_folder_path;
use std::path::PathBuf;
use jormungandr_lib::interfaces::{
load_persistent_fragments_logs_from_folder_path, PersistentFragmentLog,
};
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};

pub struct PersistentLogViewer {
dir: PathBuf,
Expand Down Expand Up @@ -30,9 +35,27 @@ impl PersistentLogViewer {
self.get_all().len()
}
}
#[allow(dead_code)]
pub fn write_into_persistent_log<P: AsRef<Path>>(
persistent_log: P,
entries: Vec<PersistentFragmentLog>,
) -> Result<(), Error> {
let mut output = BufWriter::with_capacity(128 * 1024, File::create(persistent_log.as_ref())?);

for entry in entries {
let codec = bincode::DefaultOptions::new().with_fixint_encoding();
let serialized = codec
.serialize(&entry)
.map_err(|_| Error::CannotSerializeEntry)?;
output.write_all(&serialized)?;
}
Ok(())
}

#[derive(custom_debug::Debug, thiserror::Error)]
pub enum Error {
#[error("cannot serialize entry of persistent log")]
CannotSerializeEntry,
#[error("cannot serialize entry of persistent log")]
Io(#[from] std::io::Error),
}

0 comments on commit fab2c11

Please sign in to comment.