1 use std
::collections
::HashMap
;
6 use super::BackupReader
;
7 use crate::backup
::{ReadChunk, DataBlob, CryptConfig}
;
8 use crate::tools
::runtime
::block_on
;
10 /// Read chunks from remote host using ``BackupReader``
11 pub struct RemoteChunkReader
{
12 client
: Arc
<BackupReader
>,
13 crypt_config
: Option
<Arc
<CryptConfig
>>,
14 cache_hint
: HashMap
<[u8; 32], usize>,
15 cache
: HashMap
<[u8; 32], Vec
<u8>>,
18 impl RemoteChunkReader
{
20 /// Create a new instance.
22 /// Chunks listed in ``cache_hint`` are cached and kept in RAM.
24 client
: Arc
<BackupReader
>,
25 crypt_config
: Option
<Arc
<CryptConfig
>>,
26 cache_hint
: HashMap
<[u8; 32], usize>,
29 Self { client, crypt_config, cache_hint, cache: HashMap::new() }
33 impl ReadChunk
for RemoteChunkReader
{
35 fn read_raw_chunk(&mut self, digest
:&[u8; 32]) -> Result
<DataBlob
, Error
> {
37 let mut chunk_data
= Vec
::with_capacity(4*1024*1024);
39 //tokio::task::block_in_place(|| futures::executor::block_on(self.client.download_chunk(&digest, &mut chunk_data)))?;
41 // download_chunk returns the writer back to us, but we need to return a 'static value
43 .download_chunk(&digest
, &mut chunk_data
)
48 let chunk
= DataBlob
::from_raw(chunk_data
)?
;
54 fn read_chunk(&mut self, digest
:&[u8; 32]) -> Result
<Vec
<u8>, Error
> {
56 if let Some(raw_data
) = self.cache
.get(digest
) {
57 return Ok(raw_data
.to_vec());
60 let chunk
= self.read_raw_chunk(digest
)?
;
62 let raw_data
= chunk
.decode(self.crypt_config
.as_ref().map(Arc
::as_ref
))?
;
64 // fixme: verify digest?
66 let use_cache
= self.cache_hint
.contains_key(digest
);
68 self.cache
.insert(*digest
, raw_data
.to_vec());