src/client/remote_chunk_reader.rs: implement simple caching
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
use failure::*;
|
||||
use futures::future::Future;
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::BackupReader;
|
||||
use crate::backup::{ReadChunk, DataChunk, CryptConfig};
|
||||
@ -9,12 +10,22 @@ use crate::backup::{ReadChunk, DataChunk, CryptConfig};
|
||||
pub struct RemoteChunkReader {
|
||||
client: Arc<BackupReader>,
|
||||
crypt_config: Option<Arc<CryptConfig>>,
|
||||
cache_hint: HashMap<[u8; 32], usize>,
|
||||
cache: HashMap<[u8; 32], Vec<u8>>,
|
||||
}
|
||||
|
||||
impl RemoteChunkReader {
|
||||
|
||||
pub fn new(client: Arc<BackupReader>, crypt_config: Option<Arc<CryptConfig>>) -> Self {
|
||||
Self { client, crypt_config }
|
||||
/// 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 {
|
||||
|
||||
Self { client, crypt_config, cache_hint, cache: HashMap::new() }
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,6 +35,12 @@ impl ReadChunk for RemoteChunkReader {
|
||||
|
||||
let writer = Vec::with_capacity(4*1024*1024);
|
||||
|
||||
if let Some(raw_data) = self.cache.get(digest) {
|
||||
return Ok(raw_data.to_vec());
|
||||
}
|
||||
|
||||
let use_cache = self.cache_hint.contains_key(digest);
|
||||
|
||||
let chunk_data = self.client.download_chunk(&digest, writer).wait()?;
|
||||
|
||||
let chunk = DataChunk::from_raw(chunk_data, *digest)?;
|
||||
@ -34,6 +51,10 @@ impl ReadChunk for RemoteChunkReader {
|
||||
None => chunk.decode(None)?,
|
||||
};
|
||||
|
||||
if use_cache {
|
||||
self.cache.insert(*digest, raw_data.to_vec());
|
||||
}
|
||||
|
||||
Ok(raw_data)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user