]> git.proxmox.com Git - proxmox-backup.git/blob - src/client/remote_chunk_reader.rs
src/client/remote_chunk_reader.rs: implement simple caching
[proxmox-backup.git] / src / client / remote_chunk_reader.rs
1 use failure::*;
2 use futures::future::Future;
3 use std::sync::Arc;
4 use std::collections::HashMap;
5
6 use super::BackupReader;
7 use crate::backup::{ReadChunk, DataChunk, 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_chunk(&mut self, digest:&[u8; 32]) -> Result<Vec<u8>, Error> {
35
36 let writer = Vec::with_capacity(4*1024*1024);
37
38 if let Some(raw_data) = self.cache.get(digest) {
39 return Ok(raw_data.to_vec());
40 }
41
42 let use_cache = self.cache_hint.contains_key(digest);
43
44 let chunk_data = self.client.download_chunk(&digest, writer).wait()?;
45
46 let chunk = DataChunk::from_raw(chunk_data, *digest)?;
47 chunk.verify_crc()?;
48
49 let raw_data = match self.crypt_config {
50 Some(ref crypt_config) => chunk.decode(Some(crypt_config))?,
51 None => chunk.decode(None)?,
52 };
53
54 if use_cache {
55 self.cache.insert(*digest, raw_data.to_vec());
56 }
57
58 Ok(raw_data)
59 }
60 }