|
|
|
@ -22,6 +22,7 @@ use futures::{
|
|
|
|
|
OptionFuture,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
use lzzzz::{
|
|
|
|
|
lz4f::{
|
|
|
|
|
self,
|
|
|
|
@ -51,11 +52,14 @@ async fn save_now_to(chain: &Chain<String>, to: impl AsRef<Path>) -> io::Result<
|
|
|
|
|
.truncate(true)
|
|
|
|
|
.open(to).await?;
|
|
|
|
|
let chain = serde_cbor::to_vec(chain).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
let mut file = AsyncWriteCompressor::new(&mut file, PreferencesBuilder::new()
|
|
|
|
|
.compression_level(lz4f::CLEVEL_HIGH).build())?;
|
|
|
|
|
.compression_level(lz4f::CLEVEL_HIGH).build())?;
|
|
|
|
|
file.write_all(&chain[..]).await?;
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
file.flush().await?;
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
file.shutdown().await?;
|
|
|
|
|
}
|
|
|
|
|
file.flush().await?;
|
|
|
|
@ -64,9 +68,9 @@ async fn save_now_to(chain: &Chain<String>, to: impl AsRef<Path>) -> io::Result<
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Start the save loop for this chain
|
|
|
|
|
pub async fn host(state: State)
|
|
|
|
|
pub async fn host(mut state: State)
|
|
|
|
|
{
|
|
|
|
|
let to = &state.config().file;
|
|
|
|
|
let to = state.config().file.to_owned();
|
|
|
|
|
let interval = state.config().save_interval();
|
|
|
|
|
while Arc::strong_count(state.when()) > 1 {
|
|
|
|
|
{
|
|
|
|
@ -78,11 +82,17 @@ pub async fn host(state: State)
|
|
|
|
|
info!("Saved chain to {:?}", to);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tokio::select!{
|
|
|
|
|
_ = OptionFuture::from(interval.map(|interval| time::delay_for(interval))) => {},
|
|
|
|
|
_ = state.on_shutdown() => {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
state.when().notified().await;
|
|
|
|
|
if state.has_shutdown() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
OptionFuture::from(interval.map(|interval| time::delay_for(interval))).await;
|
|
|
|
|
state.when().notified().await;
|
|
|
|
|
}
|
|
|
|
|
trace!("Saver exiting");
|
|
|
|
|
}
|
|
|
|
@ -91,10 +101,12 @@ pub async fn host(state: State)
|
|
|
|
|
pub async fn load(from: impl AsRef<Path>) -> io::Result<Chain<String>>
|
|
|
|
|
{
|
|
|
|
|
debug!("Loading chain from {:?}", from.as_ref());
|
|
|
|
|
let file = OpenOptions::new()
|
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
|
.read(true)
|
|
|
|
|
.open(from).await?;
|
|
|
|
|
let mut whole = Vec::new();
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
let mut file = AsyncReadDecompressor::new(file)?;
|
|
|
|
|
tokio::io::copy(&mut file, &mut whole).await?;
|
|
|
|
|
whole.flush().await?;
|
|
|
|
|