From 69f2b3d9495cb4e8cff8eff4a49ac944dafee1ad Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 12 May 2022 21:21:19 +0300 Subject: use new ChunkStore for remote has_chunk Sponsored-by: author --- src/chunkstore.rs | 6 +++++- src/client.rs | 24 +++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/chunkstore.rs b/src/chunkstore.rs index f421805..1f67e2f 100644 --- a/src/chunkstore.rs +++ b/src/chunkstore.rs @@ -50,7 +50,11 @@ impl ChunkStore { /// Store a chunk in the store. /// /// The store chooses an id for the chunk. - pub async fn put(&mut self, chunk: EncryptedChunk, meta: &ChunkMeta) -> Result { + pub async fn put( + &mut self, + chunk: EncryptedChunk, + meta: &ChunkMeta, + ) -> Result { match self { Self::Local(store) => store.put(chunk, meta), Self::Remote(store) => store.put(chunk, meta).await, diff --git a/src/client.rs b/src/client.rs index bed5f1e..6b89991 100644 --- a/src/client.rs +++ b/src/client.rs @@ -5,6 +5,7 @@ use crate::chunk::{ }; use crate::chunkid::ChunkId; use crate::chunkmeta::ChunkMeta; +use crate::chunkstore::{ChunkStore, StoreError}; use crate::cipher::{CipherEngine, CipherError}; use crate::config::{ClientConfig, ClientConfigError}; use crate::generation::{FinishedGeneration, GenId, LocalGeneration, LocalGenerationError}; @@ -100,10 +101,15 @@ pub enum ClientError { /// Failed to write a file. #[error("failed to write to file {0}: {1}")] FileWrite(PathBuf, std::io::Error), + + /// Error from a chunk store. + #[error(transparent)] + ChunkStore(#[from] StoreError), } /// Client for the Obnam server HTTP API. pub struct BackupClient { + store: ChunkStore, client: reqwest::Client, base_url: String, cipher: CipherEngine, @@ -121,6 +127,7 @@ impl BackupClient { .build() .map_err(ClientError::ReqwestError)?; Ok(Self { + store: ChunkStore::remote(config)?, client, base_url: config.server_url.to_string(), cipher: CipherEngine::new(&pass), @@ -137,21 +144,8 @@ impl BackupClient { /// Does the server have a chunk? pub async fn has_chunk(&self, meta: &ChunkMeta) -> Result, ClientError> { - let body = match self.get("", &[("label", meta.label())]).await { - Ok((_, body)) => body, - Err(err) => return Err(err), - }; - - let hits: HashMap = - serde_json::from_slice(&body).map_err(ClientError::JsonParse)?; - let mut iter = hits.iter(); - let has = if let Some((chunk_id, _)) = iter.next() { - Some(chunk_id.into()) - } else { - None - }; - - Ok(has) + let mut ids = self.store.find_by_label(meta).await?; + Ok(ids.pop()) } /// Upload a data chunk to the server. -- cgit v1.2.1