2 use futures
::future
::Future
;
4 use std
::collections
::HashMap
;
6 use super::BackupReader
;
7 use crate::backup
::{ReadChunk, DataChunk, CryptConfig}
;
9 /// Read chunks from remote host using ``BackupReader``
10 pub struct RemoteChunkReader
{
11 client
: Arc
<BackupReader
>,
12 crypt_config
: Option
<Arc
<CryptConfig
>>,
13 cache_hint
: HashMap
<[u8; 32], usize>,
14 cache
: HashMap
<[u8; 32], Vec
<u8>>,
17 impl RemoteChunkReader
{
19 /// Create a new instance.
21 /// Chunks listed in ``cache_hint`` are cached and kept in RAM.
23 client
: Arc
<BackupReader
>,
24 crypt_config
: Option
<Arc
<CryptConfig
>>,
25 cache_hint
: HashMap
<[u8; 32], usize>,
28 Self { client, crypt_config, cache_hint, cache: HashMap::new() }
32 impl ReadChunk
for RemoteChunkReader
{
34 fn read_chunk(&mut self, digest
:&[u8; 32]) -> Result
<Vec
<u8>, Error
> {
36 let writer
= Vec
::with_capacity(4*1024*1024);
38 if let Some(raw_data
) = self.cache
.get(digest
) {
39 return Ok(raw_data
.to_vec());
42 let use_cache
= self.cache_hint
.contains_key(digest
);
44 let chunk_data
= self.client
.download_chunk(&digest
, writer
).wait()?
;
46 let chunk
= DataChunk
::from_raw(chunk_data
, *digest
)?
;
49 let raw_data
= match self.crypt_config
{
50 Some(ref crypt_config
) => chunk
.decode(Some(crypt_config
))?
,
51 None
=> chunk
.decode(None
)?
,
55 self.cache
.insert(*digest
, raw_data
.to_vec());