Add cacheable arg to Backend

This commit is contained in:
Alexander Weiss 2022-06-21 11:06:23 +02:00
parent d1c0e78a77
commit ed8561383a
11 changed files with 62 additions and 22 deletions

View File

@ -53,12 +53,13 @@ impl ReadBackend for ChooseBackend {
&self,
tpe: FileType,
id: &Id,
cacheable: bool,
offset: u32,
length: u32,
) -> Result<Vec<u8>> {
match self {
Local(local) => local.read_partial(tpe, id, offset, length).await,
Rest(rest) => rest.read_partial(tpe, id, offset, length).await,
Local(local) => local.read_partial(tpe, id, cacheable, offset, length).await,
Rest(rest) => rest.read_partial(tpe, id, cacheable, offset, length).await,
}
}
}
@ -72,10 +73,10 @@ impl WriteBackend for ChooseBackend {
}
}
async fn write_file(&self, tpe: FileType, id: &Id, f: File) -> Result<()> {
async fn write_file(&self, tpe: FileType, id: &Id, cacheable: bool, f: File) -> Result<()> {
match self {
Local(local) => local.write_file(tpe, id, f).await,
Rest(rest) => rest.write_file(tpe, id, f).await,
Local(local) => local.write_file(tpe, id, cacheable, f).await,
Rest(rest) => rest.write_file(tpe, id, cacheable, f).await,
}
}

View File

@ -20,6 +20,7 @@ pub trait DecryptReadBackend: ReadBackend {
&self,
tpe: FileType,
id: &Id,
cacheable: bool,
offset: u32,
length: u32,
) -> Result<Vec<u8>>;
@ -169,12 +170,16 @@ impl<R: ReadBackend, C: CryptoKey> DecryptReadBackend for DecryptBackend<R, C> {
&self,
tpe: FileType,
id: &Id,
cacheable: bool,
offset: u32,
length: u32,
) -> Result<Vec<u8>> {
Ok(self
.key
.decrypt_data(&self.backend.read_partial(tpe, id, offset, length).await?)?)
Ok(self.key.decrypt_data(
&self
.backend
.read_partial(tpe, id, cacheable, offset, length)
.await?,
)?)
}
}
@ -200,10 +205,13 @@ impl<R: ReadBackend, C: CryptoKey> ReadBackend for DecryptBackend<R, C> {
&self,
tpe: FileType,
id: &Id,
cacheable: bool,
offset: u32,
length: u32,
) -> Result<Vec<u8>> {
self.backend.read_partial(tpe, id, offset, length).await
self.backend
.read_partial(tpe, id, cacheable, offset, length)
.await
}
}
@ -213,8 +221,8 @@ impl<R: WriteBackend, C: CryptoKey> WriteBackend for DecryptBackend<R, C> {
self.backend.create().await
}
async fn write_file(&self, tpe: FileType, id: &Id, f: File) -> Result<()> {
self.backend.write_file(tpe, id, f).await
async fn write_file(&self, tpe: FileType, id: &Id, cacheable: bool, f: File) -> Result<()> {
self.backend.write_file(tpe, id, cacheable, f).await
}
async fn write_bytes(&self, tpe: FileType, id: &Id, buf: Vec<u8>) -> Result<()> {

View File

@ -29,11 +29,12 @@ impl<BE: DecryptFullBackend> DecryptReadBackend for DryRunBackend<BE> {
&self,
tpe: FileType,
id: &Id,
cacheable: bool,
offset: u32,
length: u32,
) -> Result<Vec<u8>> {
self.be
.read_encrypted_partial(tpe, id, offset, length)
.read_encrypted_partial(tpe, id, cacheable, offset, length)
.await
}
}
@ -56,10 +57,13 @@ impl<BE: DecryptFullBackend> ReadBackend for DryRunBackend<BE> {
&self,
tpe: FileType,
id: &Id,
cacheable: bool,
offset: u32,
length: u32,
) -> Result<Vec<u8>> {
self.be.read_partial(tpe, id, offset, length).await
self.be
.read_partial(tpe, id, cacheable, offset, length)
.await
}
}
@ -95,10 +99,10 @@ impl<BE: DecryptFullBackend> WriteBackend for DryRunBackend<BE> {
}
}
async fn write_file(&self, tpe: FileType, id: &Id, f: File) -> Result<()> {
async fn write_file(&self, tpe: FileType, id: &Id, cacheable: bool, f: File) -> Result<()> {
match self.dry_run {
true => Ok(()),
false => self.be.write_file(tpe, id, f).await,
false => self.be.write_file(tpe, id, cacheable, f).await,
}
}

View File

@ -101,6 +101,7 @@ impl ReadBackend for LocalBackend {
&self,
tpe: FileType,
id: &Id,
_cacheable: bool,
offset: u32,
length: u32,
) -> Result<Vec<u8>> {
@ -124,7 +125,13 @@ impl WriteBackend for LocalBackend {
Ok(())
}
async fn write_file(&self, tpe: FileType, id: &Id, mut f: File) -> Result<()> {
async fn write_file(
&self,
tpe: FileType,
id: &Id,
_cacheable: bool,
mut f: File,
) -> Result<()> {
v3!("writing tpe: {:?}, id: {}", &tpe, &id);
let filename = self.path(tpe, id);
let mut file = fs::OpenOptions::new()

View File

@ -76,6 +76,7 @@ pub trait ReadBackend: Clone + Send + Sync + 'static {
&self,
tpe: FileType,
id: &Id,
cacheable: bool,
offset: u32,
length: u32,
) -> Result<Vec<u8>>;
@ -133,7 +134,7 @@ pub trait ReadBackend: Clone + Send + Sync + 'static {
#[async_trait]
pub trait WriteBackend: ReadBackend {
async fn create(&self) -> Result<()>;
async fn write_file(&self, tpe: FileType, id: &Id, f: File) -> Result<()>;
async fn write_file(&self, tpe: FileType, id: &Id, cacheable: bool, f: File) -> Result<()>;
async fn write_bytes(&self, tpe: FileType, id: &Id, buf: Vec<u8>) -> Result<()>;
async fn remove(&self, tpe: FileType, id: &Id) -> Result<()>;
}

View File

@ -107,6 +107,7 @@ impl ReadBackend for RestBackend {
&self,
tpe: FileType,
id: &Id,
_cacheable: bool,
offset: u32,
length: u32,
) -> Result<Vec<u8>> {
@ -135,7 +136,7 @@ impl WriteBackend for RestBackend {
Ok(())
}
async fn write_file(&self, tpe: FileType, id: &Id, f: File) -> Result<()> {
async fn write_file(&self, tpe: FileType, id: &Id, _cacheable: bool, f: File) -> Result<()> {
v3!("writing tpe: {:?}, id: {}", &tpe, &id);
self.client
.post(self.url(tpe, id))

View File

@ -48,6 +48,7 @@ impl<BE: DecryptWriteBackend> Packer<BE> {
future: None,
be: be.clone(),
indexer: indexer.clone(),
cacheable: blob_type.is_cacheable(),
};
Ok(Self {
be,
@ -234,6 +235,7 @@ struct FileWriter<BE: DecryptWriteBackend> {
future: Option<JoinHandle<Result<()>>>,
be: BE,
indexer: SharedIndexer<BE>,
cacheable: bool,
}
impl<BE: DecryptWriteBackend> FileWriter<BE> {
@ -244,8 +246,9 @@ impl<BE: DecryptWriteBackend> FileWriter<BE> {
let be = self.be.clone();
let indexer = self.indexer.clone();
let cacheable = self.cacheable;
self.future = Some(spawn(async move {
be.write_file(FileType::Pack, &id, file).await?;
be.write_file(FileType::Pack, &id, cacheable, file).await?;
index.time = Some(Local::now());
indexer.write().await.add(index).await?;
Ok(())

View File

@ -730,7 +730,13 @@ impl Pruner {
continue;
}
let data = be
.read_partial(FileType::Pack, &pack.id, blob.offset, blob.length)
.read_partial(
FileType::Pack,
&pack.id,
blob.tpe.is_cacheable(),
blob.offset,
blob.length,
)
.await?;
match blob.tpe {
BlobType::Data => &mut data_packer,

View File

@ -114,7 +114,7 @@ async fn restore_contents(
stream.push(spawn(async move {
// read pack at blob_offset with length blob_length
let data = be
.read_encrypted_partial(FileType::Pack, &pack, bl.offset, bl.length)
.read_encrypted_partial(FileType::Pack, &pack, false, bl.offset, bl.length)
.await
.unwrap();

View File

@ -114,6 +114,7 @@ impl ReadIndex for Index {
vec.binary_search_by_key(id, |e| e.id).ok().map(|index| {
let be = &vec[index];
IndexEntry::new(
*tpe,
self.packs[be.pack_idx],
be.offset,
be.length,

View File

@ -24,6 +24,7 @@ pub use indexer::*;
#[derive(Debug, Clone, Constructor, Getters)]
pub struct IndexEntry {
blob_type: BlobType,
pack: Id,
offset: u32,
length: u32,
@ -33,6 +34,7 @@ pub struct IndexEntry {
impl IndexEntry {
pub fn from_index_blob(blob: &IndexBlob, pack: Id) -> Self {
Self {
blob_type: blob.tpe,
pack,
offset: blob.offset,
length: blob.length,
@ -43,7 +45,13 @@ impl IndexEntry {
/// Get a blob described by IndexEntry from the backend
pub async fn read_data<B: DecryptReadBackend>(&self, be: &B) -> Result<Vec<u8>> {
let data = be
.read_encrypted_partial(FileType::Pack, &self.pack, self.offset, self.length)
.read_encrypted_partial(
FileType::Pack,
&self.pack,
self.blob_type.is_cacheable(),
self.offset,
self.length,
)
.await?;
Ok(match self.uncompressed_length {
None => data,