Fix CalDAV/CardDAV multiget loading entire collections into memory
A REPORT multiget for a handful of resources previously listed the
whole calendar/address book (every row incl. ical_data/vcard) and
filtered by href in Rust with an O(N×M) substring scan. Large
collections paid full-table latency, RAM and DB CPU on every sync.
- Add find_events_by_ical_uids / get_contacts_by_uids through every
layer (domain repo trait → PG repo → storage port/adapter → use case
→ handler) using one indexed `= ANY($2)` query, mirroring the
existing single-UID lookups.
- Extract UIDs from multiget hrefs with a shared, tested
uid_from_multiget_href helper (percent-decoding, case-insensitive
extension strip, collection hrefs rejected). Exact UID matching also
removes the false positives the old substring filter allowed.
- CalDAV PROPFIND single-event path now uses the existing indexed
get_event_by_ical_uid instead of listing all events.
- Honour the already-documented limit/offset query params on
GET /api/address-books/{id}/contacts: optional ListQuery fields
thread through ContactUseCase::list_contacts to a paginated repo
query. Omitted params keep returning the full book (frontend and
DAV listing/sync paths unchanged).
https://claude.ai/code/session_0193Hff42gaA962wThxMGSd1
This commit is contained in:
@@ -27,6 +27,7 @@ use std::fmt::Write;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::application::adapters::caldav_adapter::{CalDavAdapter, CalDavReportType};
|
||||
use crate::application::adapters::uid_from_multiget_href;
|
||||
use crate::application::adapters::webdav_adapter::{PropFindRequest, PropFindType};
|
||||
use crate::application::dtos::calendar_dto::{
|
||||
CreateCalendarDto, CreateEventICalDto, UpdateCalendarDto,
|
||||
@@ -422,17 +423,13 @@ async fn handle_propfind(
|
||||
}
|
||||
};
|
||||
|
||||
// Individual event .ics
|
||||
// Individual event .ics — indexed lookup by iCalendar UID.
|
||||
let ical_uid = event_path.trim_end_matches(".ics");
|
||||
|
||||
let events = calendar_service
|
||||
.list_events(calendar_id, None, None, user.id)
|
||||
let event = calendar_service
|
||||
.get_event_by_ical_uid(calendar_id, ical_uid, user.id)
|
||||
.await
|
||||
.map_err(|e| AppError::internal_error(format!("Failed to list events: {}", e)))?;
|
||||
|
||||
let event = events
|
||||
.iter()
|
||||
.find(|e| e.ical_uid == ical_uid)
|
||||
.map_err(|e| AppError::internal_error(format!("Failed to look up event: {}", e)))?
|
||||
.ok_or_else(|| AppError::not_found(format!("Event not found: {}", ical_uid)))?;
|
||||
|
||||
let base_href = &format!("/caldav/{}/", calendar_id);
|
||||
@@ -444,7 +441,7 @@ async fn handle_propfind(
|
||||
let mut response_body = Vec::new();
|
||||
CalDavAdapter::generate_calendar_events_response(
|
||||
&mut response_body,
|
||||
std::slice::from_ref(event),
|
||||
std::slice::from_ref(&event),
|
||||
&report_type,
|
||||
base_href,
|
||||
)
|
||||
@@ -502,15 +499,18 @@ async fn handle_report(
|
||||
}
|
||||
}
|
||||
CalDavReportType::CalendarMultiget { hrefs, .. } => {
|
||||
let all_events = calendar_service
|
||||
.list_events(calendar_id, None, None, user.id)
|
||||
.await
|
||||
.map_err(|e| AppError::internal_error(format!("Failed to list events: {}", e)))?;
|
||||
// Indexed batch lookup (`ical_uid = ANY(...)`) — a multiget for
|
||||
// a handful of events must not pay for listing the whole
|
||||
// calendar and filtering client-side.
|
||||
let uids: Vec<String> = hrefs
|
||||
.iter()
|
||||
.filter_map(|href| uid_from_multiget_href(href, ".ics"))
|
||||
.collect();
|
||||
|
||||
all_events
|
||||
.into_iter()
|
||||
.filter(|evt| hrefs.iter().any(|href| href.contains(&evt.ical_uid)))
|
||||
.collect()
|
||||
calendar_service
|
||||
.get_events_by_ical_uids(calendar_id, &uids, user.id)
|
||||
.await
|
||||
.map_err(|e| AppError::internal_error(format!("Failed to fetch events: {}", e)))?
|
||||
}
|
||||
CalDavReportType::SyncCollection { .. } => calendar_service
|
||||
.list_events(calendar_id, None, None, user.id)
|
||||
|
||||
Reference in New Issue
Block a user