|
|
|
@ -23,18 +23,24 @@ use futures::{
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
use lzzzz::{
|
|
|
|
|
lz4f::{
|
|
|
|
|
self,
|
|
|
|
|
AsyncWriteCompressor,
|
|
|
|
|
PreferencesBuilder,
|
|
|
|
|
AsyncReadDecompressor,
|
|
|
|
|
use async_compression::{
|
|
|
|
|
tokio_02::{
|
|
|
|
|
write::{
|
|
|
|
|
BzEncoder,
|
|
|
|
|
BzDecoder,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const SAVE_INTERVAL: Option<Duration> = Some(Duration::from_secs(2));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
type Compressor<T> = BzEncoder<T>;
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
type Decompressor<T> = BzDecoder<T>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn save_now(state: &State) -> io::Result<()>
|
|
|
|
|
{
|
|
|
|
|
let chain = state.chain().read().await;
|
|
|
|
@ -54,12 +60,11 @@ async fn save_now_to(chain: &Chain<String>, to: impl AsRef<Path>) -> io::Result<
|
|
|
|
|
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())?;
|
|
|
|
|
let mut file = Compressor::new(&mut file);
|
|
|
|
|
file.write_all(&chain[..]).await?;
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
file.flush().await?;
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
file.shutdown().await?;
|
|
|
|
|
}
|
|
|
|
|
file.flush().await?;
|
|
|
|
@ -105,11 +110,16 @@ pub async fn load(from: impl AsRef<Path>) -> io::Result<Chain<String>>
|
|
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
|
.read(true)
|
|
|
|
|
.open(from).await?;
|
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
|
let mut whole = Vec::new();
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
let mut file = AsyncReadDecompressor::new(file)?;
|
|
|
|
|
let mut whole = Decompressor::new(whole);
|
|
|
|
|
tokio::io::copy(&mut file, &mut whole).await?;
|
|
|
|
|
whole.flush().await?;
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
whole.shutdown().await?;
|
|
|
|
|
#[cfg(feature="compress-chain")]
|
|
|
|
|
let whole = whole.into_inner();
|
|
|
|
|
serde_cbor::from_slice(&whole[..])
|
|
|
|
|
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))
|
|
|
|
|
}
|
|
|
|
|