diff --git a/crates/catalog/src/index.rs b/crates/catalog/src/index.rs index 4db512354e..b40c4a698b 100644 --- a/crates/catalog/src/index.rs +++ b/crates/catalog/src/index.rs @@ -134,7 +134,8 @@ impl std::hash::Hasher for IdHasher { /// Where `p` is in memory. The contract of the kept texts: a text is used only for the photo at the /// address it was made for. The catalog's own photos (`Arc` contents) stay where they are -/// until `Arc::make_mut` copies one that others hold, which `photo_mut` reports ([`Indexes::text_moved`]); +/// until `Arc::make_mut` copies one that others hold, which `photo_mut` reports ([`Indexes::text_moved`]; `repair` +/// copies them in bulk and drops every lookup instead); /// every op that changes a searched field makes the text again ([`Indexes::text_changed`]). A copy /// of a photo (a candidate tried with other values) is at another address, so it is read from its /// own fields, never from a text kept for the photo with its id. Only ids and addresses are diff --git a/crates/catalog/src/lib.rs b/crates/catalog/src/lib.rs index 55b1e2d94f..605aeb4710 100644 --- a/crates/catalog/src/lib.rs +++ b/crates/catalog/src/lib.rs @@ -1231,6 +1231,10 @@ impl Catalog { /// wrote). pub fn repair(&mut self) -> Vec { let mut changed = Vec::new(); + // Photos others hold are copied below (`Arc::make_mut`), which moves them in memory and strands the text kept + // for their address ([`index::Indexes`]), and stacks and albums change outside `apply`: nothing derived is + // kept across a repair, and the lookups are built again when asked. + self.index = index::Indexes::default(); // Segmentation grids shared by the snapshot this was read from (issue #2154): each // reference takes its grid's data, and the table is not kept — a live catalog always // stores the data inline, so masks render and serialize without the snapshot's table. @@ -1341,9 +1345,8 @@ impl Catalog { self.next_stack = self.next_stack.max(stack); } if !changed.is_empty() { - // stacks and albums changed outside `apply`: the lookups are built again when asked - self.index = index::Indexes::default(); - // (and what was worked out from the albums, the guard of [`Self::keeps_written_order`] too) + // stacks and albums changed outside `apply`: what was worked out from the albums is made again (the guard of + // [`Self::keeps_written_order`] too) self.plans = PlansMemo::default(); } changed diff --git a/crates/catalog/src/tests_search_text.rs b/crates/catalog/src/tests_search_text.rs index 632f77e70a..2c9ae6c668 100644 --- a/crates/catalog/src/tests_search_text.rs +++ b/crates/catalog/src/tests_search_text.rs @@ -373,6 +373,27 @@ fn a_clone_builds_its_own() { assert_current(&c); } +/// `repair` copies the photos others hold (`Arc::make_mut`) without reporting a change, so the cache would +/// still keep a text for the address the retained photo has. The retained photo, changed on its own, is +/// read from its fields, never from the text kept for the photo with its id. +#[test] +fn repair_keeps_no_text_for_a_photo_it_copied() { + let mut c = library(); + warm(&c); + let mut held = c.photo(PhotoId(1)).unwrap().clone(); + assert!(c.repair().is_empty(), "a sound catalog"); + assert!(!std::ptr::eq(&*held, &**c.photo(PhotoId(1)).unwrap()), "repair copied the photo"); + std::sync::Arc::make_mut(&mut held).meta.title = "Trip to Nara".into(); + let nara = Filter { text: "nara".into(), ..Default::default() }; + let osaka = Filter { text: "ōsaka".into(), ..Default::default() }; + assert!(nara.matches(&held, &c), "the retained photo's own title"); + assert!(!osaka.matches(&held, &c), "not the title it had"); + assert!(text_rule("contains", "nara").matches(&held, &c), "the Text rule reads the retained photo's own title"); + assert!(!text_rule("contains", "ōsaka").matches(&held, &c)); + warm(&c); + assert_eq!((found(&c, "ōsaka"), found(&c, "nara")), (1, 0)); +} + /// The words of Text rules (not the search) read the same with the cache as without: what the fields give when each /// photo is read alone (a copy of a photo is not the catalog's own, so the text kept for its id is not used), and what the /// kept text gives.