fix: batch N+1 queries in import duplicate check and watch event dismiss
Some checks failed
CI / Check / Test (push) Failing after 5m54s

apply_mapping: 2 batch queries instead of up to 2N per-row lookups
dismiss: single fetch + single update instead of 2N per-event queries
This commit is contained in:
2026-06-02 20:05:15 +02:00
parent ac7edd6953
commit b9210b6c4e
10 changed files with 367 additions and 49 deletions

View File

@@ -55,6 +55,18 @@ impl MovieRepository for RepoWithExternalMovie {
async fn delete_movie(&self, _: &MovieId) -> Result<(), DomainError> {
panic!("unexpected")
}
async fn existing_external_ids(
&self,
_: &[ExternalMetadataId],
) -> Result<std::collections::HashSet<String>, DomainError> {
panic!("unexpected")
}
async fn existing_title_year_pairs(
&self,
_: &[(MovieTitle, ReleaseYear)],
) -> Result<std::collections::HashSet<(String, u16)>, DomainError> {
panic!("unexpected")
}
async fn list_movies(
&self,
_: &domain::models::collections::PageParams,
@@ -89,6 +101,18 @@ impl MovieRepository for RepoEmpty {
async fn delete_movie(&self, _: &MovieId) -> Result<(), DomainError> {
panic!("unexpected")
}
async fn existing_external_ids(
&self,
_: &[ExternalMetadataId],
) -> Result<std::collections::HashSet<String>, DomainError> {
panic!("unexpected")
}
async fn existing_title_year_pairs(
&self,
_: &[(MovieTitle, ReleaseYear)],
) -> Result<std::collections::HashSet<(String, u16)>, DomainError> {
panic!("unexpected")
}
async fn list_movies(
&self,
_: &domain::models::collections::PageParams,
@@ -123,6 +147,18 @@ impl MovieRepository for RepoWithTitleMatch {
async fn delete_movie(&self, _: &MovieId) -> Result<(), DomainError> {
panic!("unexpected")
}
async fn existing_external_ids(
&self,
_: &[ExternalMetadataId],
) -> Result<std::collections::HashSet<String>, DomainError> {
panic!("unexpected")
}
async fn existing_title_year_pairs(
&self,
_: &[(MovieTitle, ReleaseYear)],
) -> Result<std::collections::HashSet<(String, u16)>, DomainError> {
panic!("unexpected")
}
async fn list_movies(
&self,
_: &domain::models::collections::PageParams,

View File

@@ -20,7 +20,6 @@ pub async fn execute(
.await?
.ok_or_else(|| DomainError::NotFound("import session".into()))?;
// clone to avoid borrow conflict when mutating session fields below
let parsed = session
.parsed_file
.clone()
@@ -31,11 +30,7 @@ pub async fn execute(
.document_parser
.apply_mapping(&parsed, &mappings);
for row in annotated.iter_mut() {
if let RowResult::Valid(ref import_row) = row.result {
row.is_duplicate = check_duplicate(ctx, import_row).await?;
}
}
mark_duplicates(ctx, &mut annotated).await?;
session.field_mappings = Some(mappings);
session.row_results = Some(annotated.clone());
@@ -45,33 +40,52 @@ pub async fn execute(
Ok(annotated)
}
async fn check_duplicate(
ctx: &AppContext,
row: &domain::models::ImportRow,
) -> Result<bool, DomainError> {
if let Some(ext_id) = &row.external_metadata_id
&& let Ok(eid) = ExternalMetadataId::new(ext_id.clone())
&& ctx
.repos
.movie
.get_movie_by_external_id(&eid)
.await?
.is_some()
{
return Ok(true);
}
if let (Some(title), Some(year_str)) = (&row.title, &row.release_year) {
let title_vo = MovieTitle::new(title.clone());
let year_vo = year_str
.parse::<u16>()
.ok()
.and_then(|y| ReleaseYear::new(y).ok());
if let (Ok(t), Some(y)) = (title_vo, year_vo) {
let matches = ctx.repos.movie.get_movies_by_title_and_year(&t, &y).await?;
if !matches.is_empty() {
return Ok(true);
async fn mark_duplicates(ctx: &AppContext, rows: &mut [AnnotatedRow]) -> Result<(), DomainError> {
let mut ext_ids = Vec::new();
let mut title_year_pairs = Vec::new();
for row in rows.iter() {
if let RowResult::Valid(ref r) = row.result {
if let Some(ext_id) = &r.external_metadata_id
&& let Ok(eid) = ExternalMetadataId::new(ext_id.clone())
{
ext_ids.push(eid);
}
if let (Some(title), Some(year_str)) = (&r.title, &r.release_year)
&& let Ok(t) = MovieTitle::new(title.clone())
&& let Some(y) = year_str
.parse::<u16>()
.ok()
.and_then(|y| ReleaseYear::new(y).ok())
{
title_year_pairs.push((t, y));
}
}
}
Ok(false)
let known_ext = ctx.repos.movie.existing_external_ids(&ext_ids).await?;
let known_ty = ctx
.repos
.movie
.existing_title_year_pairs(&title_year_pairs)
.await?;
for row in rows.iter_mut() {
if let RowResult::Valid(ref r) = row.result {
if let Some(ext_id) = &r.external_metadata_id
&& known_ext.contains(ext_id)
{
row.is_duplicate = true;
continue;
}
if let (Some(title), Some(year_str)) = (&r.title, &r.release_year)
&& let Ok(y) = year_str.parse::<u16>()
&& known_ty.contains(&(title.clone(), y))
{
row.is_duplicate = true;
}
}
}
Ok(())
}

View File

@@ -8,28 +8,34 @@ use crate::{context::AppContext, integrations::commands::DismissWatchEventsComma
pub async fn execute(ctx: &AppContext, cmd: DismissWatchEventsCommand) -> Result<u32, DomainError> {
let user_id = UserId::from_uuid(cmd.user_id);
let mut dismissed = 0u32;
if cmd.event_ids.is_empty() {
return Ok(0);
}
for id in cmd.event_ids {
let event_id = WatchEventId::from_uuid(id);
let event = ctx
.repos
.watch_event
.get_by_id(&event_id)
.await?
.ok_or_else(|| DomainError::NotFound(format!("WatchEvent {id}")))?;
let ids: Vec<WatchEventId> = cmd
.event_ids
.iter()
.map(|id| WatchEventId::from_uuid(*id))
.collect();
let events = ctx.repos.watch_event.get_by_ids(&ids).await?;
if events.len() != ids.len() {
return Err(DomainError::NotFound(
"one or more WatchEvents not found".into(),
));
}
for event in &events {
if event.user_id() != &user_id {
return Err(DomainError::Unauthorized("not your watch event".into()));
}
ctx.repos
.watch_event
.update_status(&event_id, WatchEventStatus::Dismissed)
.await?;
dismissed += 1;
}
Ok(dismissed)
let count = ctx
.repos
.watch_event
.update_status_batch(&ids, WatchEventStatus::Dismissed)
.await?;
Ok(count as u32)
}