From 89161d03c9dacd703cec78d9300cadf286cb7e73 Mon Sep 17 00:00:00 2001 From: asonix Date: Sat, 23 Nov 2024 15:37:28 -0600 Subject: [PATCH] Remove a couple redundant db accesses --- src/blurhash.rs | 10 ++-------- src/lib.rs | 27 +++++++++++++++++++-------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/blurhash.rs b/src/blurhash.rs index 9450923..d3e1344 100644 --- a/src/blurhash.rs +++ b/src/blurhash.rs @@ -8,25 +8,19 @@ use crate::{ formats::ProcessableFormat, magick::{MagickError, MAGICK_CONFIGURE_PATH, MAGICK_TEMPORARY_PATH}, process::Process, - repo::Alias, + repo::Hash, state::State, store::Store, }; pub(crate) async fn generate( state: &State, - alias: &Alias, + hash: Hash, original_details: &Details, ) -> Result where S: Store + 'static, { - let hash = state - .repo - .hash(alias) - .await? - .ok_or(UploadError::MissingIdentifier)?; - let identifier = if original_details.is_video() { crate::generate::ensure_motion_identifier(state, hash, original_details).await? } else { diff --git a/src/lib.rs b/src/lib.rs index e6e88b9..cf25314 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -120,6 +120,17 @@ async fn ensure_details( ensure_details_identifier(state, &identifier).await } +async fn ensure_details_hash( + state: &State, + hash: Hash, +) -> Result { + let Some(identifier) = state.repo.identifier(hash).await? else { + return Err(UploadError::MissingIdentifier.into()); + }; + + ensure_details_identifier(state, &identifier).await +} + #[tracing::instrument(skip(state))] async fn ensure_details_identifier( state: &State, @@ -1117,14 +1128,14 @@ async fn do_serve( alias: Alias, state: web::Data>, ) -> Result { - let (hash, alias, not_found) = if let Some(hash) = state.repo.hash(&alias).await? { - (hash, alias, false) + let (hash, not_found) = if let Some(hash) = state.repo.hash(&alias).await? { + (hash, false) } else { - let Some((alias, hash)) = not_found_hash(&state.repo).await? else { + let Some((_, hash)) = not_found_hash(&state.repo).await? else { return Ok(HttpResponse::NotFound().finish()); }; - (hash, alias, true) + (hash, true) }; let Some(identifier) = state.repo.identifier(hash.clone()).await? else { @@ -1133,7 +1144,7 @@ async fn do_serve( return Ok(HttpResponse::NotFound().finish()); }; - let details = ensure_details(&state, &alias).await?; + let details = ensure_details_identifier(&state, &identifier).await?; if let Some(public_url) = state.store.public_url(&identifier) { return Ok(HttpResponse::SeeOther() @@ -1174,7 +1185,7 @@ async fn do_serve_head( return Ok(HttpResponse::NotFound().finish()); }; - let details = ensure_details(&state, &alias).await?; + let details = ensure_details_identifier(&state, &identifier).await?; if let Some(public_url) = state.store.public_url(&identifier) { return Ok(HttpResponse::SeeOther() @@ -1385,8 +1396,8 @@ async fn blurhash( let blurhash = if let Some(blurhash) = state.repo.blurhash(hash.clone()).await? { blurhash } else { - let details = ensure_details(&state, &alias).await?; - let blurhash = blurhash::generate(&state, &alias, &details).await?; + let details = ensure_details_hash(&state, hash.clone()).await?; + let blurhash = blurhash::generate(&state, hash.clone(), &details).await?; let blurhash: Arc = Arc::from(blurhash); state.repo.relate_blurhash(hash, blurhash.clone()).await?;