2020-06-12 09:38:21 +00:00
|
|
|
use std::future::Future;
|
2019-07-05 08:42:46 +00:00
|
|
|
use std::collections::HashMap;
|
2020-06-12 09:38:21 +00:00
|
|
|
use std::pin::Pin;
|
2020-07-03 05:36:23 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-08-26 11:40:24 +00:00
|
|
|
|
2020-06-12 09:01:04 +00:00
|
|
|
use anyhow::Error;
|
2019-07-03 12:39:13 +00:00
|
|
|
|
|
|
|
use super::BackupReader;
|
2020-06-12 09:38:21 +00:00
|
|
|
use crate::backup::{AsyncReadChunk, CryptConfig, DataBlob, ReadChunk};
|
2020-01-20 11:52:22 +00:00
|
|
|
use crate::tools::runtime::block_on;
|
2019-07-03 12:39:13 +00:00
|
|
|
|
|
|
|
/// Read chunks from remote host using ``BackupReader``
|
2020-07-07 05:34:58 +00:00
|
|
|
#[derive(Clone)]
|
2019-07-03 12:39:13 +00:00
|
|
|
pub struct RemoteChunkReader {
|
|
|
|
client: Arc<BackupReader>,
|
|
|
|
crypt_config: Option<Arc<CryptConfig>>,
|
2019-07-05 08:42:46 +00:00
|
|
|
cache_hint: HashMap<[u8; 32], usize>,
|
2020-07-07 05:34:58 +00:00
|
|
|
cache: Arc<Mutex<HashMap<[u8; 32], Vec<u8>>>>,
|
2019-07-03 12:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RemoteChunkReader {
|
2019-07-05 08:42:46 +00:00
|
|
|
/// Create a new instance.
|
|
|
|
///
|
|
|
|
/// Chunks listed in ``cache_hint`` are cached and kept in RAM.
|
|
|
|
pub fn new(
|
|
|
|
client: Arc<BackupReader>,
|
|
|
|
crypt_config: Option<Arc<CryptConfig>>,
|
|
|
|
cache_hint: HashMap<[u8; 32], usize>,
|
|
|
|
) -> Self {
|
2020-06-12 09:01:04 +00:00
|
|
|
Self {
|
|
|
|
client,
|
|
|
|
crypt_config,
|
|
|
|
cache_hint,
|
2020-07-07 05:34:58 +00:00
|
|
|
cache: Arc::new(Mutex::new(HashMap::new())),
|
2020-06-12 09:01:04 +00:00
|
|
|
}
|
2019-07-03 12:39:13 +00:00
|
|
|
}
|
2020-06-12 09:38:21 +00:00
|
|
|
|
2020-07-03 05:36:23 +00:00
|
|
|
pub async fn read_raw_chunk(&self, digest: &[u8; 32]) -> Result<DataBlob, Error> {
|
2020-06-12 09:38:21 +00:00
|
|
|
let mut chunk_data = Vec::with_capacity(4 * 1024 * 1024);
|
|
|
|
|
|
|
|
self.client
|
|
|
|
.download_chunk(&digest, &mut chunk_data)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let chunk = DataBlob::from_raw(chunk_data)?;
|
|
|
|
chunk.verify_crc()?;
|
|
|
|
|
|
|
|
Ok(chunk)
|
|
|
|
}
|
2019-07-03 12:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ReadChunk for RemoteChunkReader {
|
2020-07-03 05:36:23 +00:00
|
|
|
fn read_raw_chunk(&self, digest: &[u8; 32]) -> Result<DataBlob, Error> {
|
2020-06-18 11:55:22 +00:00
|
|
|
block_on(Self::read_raw_chunk(self, digest))
|
2020-01-02 12:29:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 05:36:23 +00:00
|
|
|
fn read_chunk(&self, digest: &[u8; 32]) -> Result<Vec<u8>, Error> {
|
|
|
|
if let Some(raw_data) = (*self.cache.lock().unwrap()).get(digest) {
|
2020-01-02 12:29:10 +00:00
|
|
|
return Ok(raw_data.to_vec());
|
|
|
|
}
|
2019-10-06 08:31:06 +00:00
|
|
|
|
2020-06-12 09:38:21 +00:00
|
|
|
let chunk = ReadChunk::read_raw_chunk(self, digest)?;
|
2019-07-03 12:39:13 +00:00
|
|
|
|
2020-06-12 09:01:04 +00:00
|
|
|
let raw_data = chunk.decode(self.crypt_config.as_ref().map(Arc::as_ref))?;
|
2020-01-02 12:29:10 +00:00
|
|
|
|
|
|
|
// fixme: verify digest?
|
|
|
|
|
|
|
|
let use_cache = self.cache_hint.contains_key(digest);
|
2019-07-05 08:42:46 +00:00
|
|
|
if use_cache {
|
2020-07-03 05:36:23 +00:00
|
|
|
(*self.cache.lock().unwrap()).insert(*digest, raw_data.to_vec());
|
2019-07-05 08:42:46 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 12:39:13 +00:00
|
|
|
Ok(raw_data)
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 09:38:21 +00:00
|
|
|
|
|
|
|
impl AsyncReadChunk for RemoteChunkReader {
|
|
|
|
fn read_raw_chunk<'a>(
|
2020-07-03 05:36:23 +00:00
|
|
|
&'a self,
|
2020-06-12 09:38:21 +00:00
|
|
|
digest: &'a [u8; 32],
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<DataBlob, Error>> + Send + 'a>> {
|
|
|
|
Box::pin(Self::read_raw_chunk(self, digest))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_chunk<'a>(
|
2020-07-03 05:36:23 +00:00
|
|
|
&'a self,
|
2020-06-12 09:38:21 +00:00
|
|
|
digest: &'a [u8; 32],
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, Error>> + Send + 'a>> {
|
|
|
|
Box::pin(async move {
|
2020-07-03 05:36:23 +00:00
|
|
|
if let Some(raw_data) = (*self.cache.lock().unwrap()).get(digest) {
|
2020-06-12 09:38:21 +00:00
|
|
|
return Ok(raw_data.to_vec());
|
|
|
|
}
|
|
|
|
|
|
|
|
let chunk = Self::read_raw_chunk(self, digest).await?;
|
|
|
|
|
|
|
|
let raw_data = chunk.decode(self.crypt_config.as_ref().map(Arc::as_ref))?;
|
|
|
|
|
|
|
|
// fixme: verify digest?
|
|
|
|
|
|
|
|
let use_cache = self.cache_hint.contains_key(digest);
|
|
|
|
if use_cache {
|
2020-07-03 05:36:23 +00:00
|
|
|
(*self.cache.lock().unwrap()).insert(*digest, raw_data.to_vec());
|
2020-06-12 09:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(raw_data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|