diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-05-12 21:21:19 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-10-26 17:19:55 +0300 |
commit | 69f2b3d9495cb4e8cff8eff4a49ac944dafee1ad (patch) | |
tree | 6943cc3a21d482c75c38fe36eabbb3d95e333a93 | |
parent | 383cff243c8937e31c48145a2422a99ff2109f6e (diff) | |
download | obnam2-69f2b3d9495cb4e8cff8eff4a49ac944dafee1ad.tar.gz |
use new ChunkStore for remote has_chunk
Sponsored-by: author
-rw-r--r-- | src/chunkstore.rs | 6 | ||||
-rw-r--r-- | 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<ChunkId, StoreError> { + pub async fn put( + &mut self, + chunk: EncryptedChunk, + meta: &ChunkMeta, + ) -> Result<ChunkId, StoreError> { 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<Option<ChunkId>, ClientError> { - let body = match self.get("", &[("label", meta.label())]).await { - Ok((_, body)) => body, - Err(err) => return Err(err), - }; - - let hits: HashMap<String, ChunkMeta> = - 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. |