perf(dav-collections): indexed UID lookups for single-object operations

Every CalDAV PUT/GET/DELETE of one .ics loaded the ENTIRE calendar —
every row including its ical_data — and filtered with .find() in Rust,
so importing N events cost O(N²) rows transferred. CardDAV did the exact
same in four places (PROPFIND of one .vcf, PUT existence check, GET,
DELETE), with three JSONB deserializations per discarded contact. The
indexed repo queries (find_event_by_ical_uid, get_contact_by_uid)
existed all along with zero callers.

Wire them end to end: new `get_event_by_ical_uid` /
`get_contact_by_uid` use-case methods (same access checks as
list_events / list_contacts, per the service-layer authz rule) exposed
through the storage ports and adapters, and the seven handler sites now
resolve one row instead of the whole collection.

https://claude.ai/code/session_01Dp3oWon5GBMVn4j3QXZdgx
This commit is contained in:
Claude
2026-06-10 09:52:28 +00:00
parent d56c2a3e13
commit 7687766bf7
8 changed files with 139 additions and 53 deletions
@@ -277,6 +277,29 @@ impl CalendarUseCase for CalendarService {
Ok(event)
}
async fn get_event_by_ical_uid(
&self,
calendar_id: &str,
ical_uid: &str,
user_id: Uuid,
) -> Result<Option<CalendarEventDto>, DomainError> {
let has_access = self
.calendar_storage
.check_calendar_access(calendar_id, user_id)
.await?;
let calendar = self.calendar_storage.get_calendar(calendar_id).await?;
if !has_access && !calendar.is_public {
return Err(DomainError::new(
ErrorKind::AccessDenied,
"Calendar",
"You don't have permission to view events in this calendar",
));
}
self.calendar_storage
.find_event_by_ical_uid(calendar_id, ical_uid)
.await
}
async fn list_events(
&self,
calendar_id: &str,
@@ -780,6 +780,22 @@ impl ContactUseCase for ContactService {
Ok(ContactDto::from(contact))
}
async fn get_contact_by_uid(
&self,
address_book_id: &str,
uid: &str,
user_id: Uuid,
) -> Result<Option<ContactDto>, DomainError> {
let id = Uuid::parse_str(address_book_id)
.map_err(|_| DomainError::validation_error("Invalid address book ID format"))?;
// Check if user has access to the address book
self.check_address_book_access(&id, &user_id).await?;
let contact = self.contact_repository.get_contact_by_uid(&id, uid).await?;
Ok(contact.map(ContactDto::from))
}
async fn list_contacts(
&self,
address_book_id: &str,