]> git.proxmox.com Git - proxmox-backup.git/blob - src/client/remote_chunk_reader.rs
aeb821832047a382b898462e8f78b943e3b43b76
[proxmox-backup.git] / src / client / remote_chunk_reader.rs
1 use std::collections::HashMap;
2 use std::sync::Arc;
3
4 use failure::*;
5
6 use super::BackupReader;
7 use crate::backup::{ReadChunk, DataBlob, CryptConfig};
8
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>>,
15 }
16
17 impl RemoteChunkReader {
18
19 /// Create a new instance.
20 ///
21 /// Chunks listed in ``cache_hint`` are cached and kept in RAM.
22 pub fn new(
23 client: Arc<BackupReader>,
24 crypt_config: Option<Arc<CryptConfig>>,
25 cache_hint: HashMap<[u8; 32], usize>,
26 ) -> Self {
27
28 Self { client, crypt_config, cache_hint, cache: HashMap::new() }
29 }
30 }
31
32 impl ReadChunk for RemoteChunkReader {
33
34 fn read_raw_chunk(&mut self, digest:&[u8; 32]) -> Result<DataBlob, Error> {
35
36 let mut chunk_data = Vec::with_capacity(4*1024*1024);
37
38 tokio::task::block_in_place(|| futures::executor::block_on(self.client.download_chunk(&digest, &mut chunk_data)))?;
39
40 let chunk = DataBlob::from_raw(chunk_data)?;
41 chunk.verify_crc()?;
42
43 Ok(chunk)
44 }
45
46 fn read_chunk(&mut self, digest:&[u8; 32]) -> Result<Vec<u8>, Error> {
47
48 if let Some(raw_data) = self.cache.get(digest) {
49 return Ok(raw_data.to_vec());
50 }
51
52 let chunk = self.read_raw_chunk(digest)?;
53
54 let raw_data = chunk.decode(self.crypt_config.as_ref().map(Arc::as_ref))?;
55
56 // fixme: verify digest?
57
58 let use_cache = self.cache_hint.contains_key(digest);
59 if use_cache {
60 self.cache.insert(*digest, raw_data.to_vec());
61 }
62
63 Ok(raw_data)
64 }
65
66 }