LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
perf(catalog): #1136 index stacks and album membership, sort keys once per photo
Chad Elliott615d8a365e51
diff --git a/crates/catalog/proptest-regressions/tests_index.txt b/crates/catalog/proptest-regressions/tests_index.txtnew file mode 100644index 000000000..9c827f0c0--- /dev/null+++ b/crates/catalog/proptest-regressions/tests_index.txt@@ -0,0 +1,8 @@+# Seeds for failure cases proptest has generated in the past. It is+# automatically read and these particular cases re-run before any+# novel cases are generated.+#+# It is recommended to check this file in to source control so that+# everyone who runs the test benefits from these saved cases.+cc b91df6f4f23ec90cae3c0fb934b04b806181b61570f00ba25826e2232b7e1932 # shrinks to steps = [(3, 0, 0, false)]+cc b11415bdf5e4133b65e00645d4051129c71bf927747d6384aa50107440e92e07 # shrinks to steps = [(0, 10, 166, false), (7, 1, 0, false), (1, 14, 0, false)]diff --git a/crates/catalog/src/index.rs b/crates/catalog/src/index.rsnew file mode 100644index 000000000..2c347473b--- /dev/null+++ b/crates/catalog/src/index.rs@@ -0,0 +1,101 @@+//! Lookups derived from the catalog's own data, so questions asked once per photo of a big library+//! don't scan a list each time: which stack a photo is in, whether a regular album holds it.+//!+//! They are never saved (the on-disk format is unchanged). Each is built on first use from what+//! the catalog holds, so a catalog read from a snapshot, replayed from the journal or cloned starts+//! without them and builds them when asked. Once built, [`crate::Catalog::apply`] keeps them up to+//! date: the ops that change stacks or album photos (`AddStack`, `RemoveStack`, `SetStack`,+//! `AddAlbum`, `RemoveAlbum`, `SetAlbumPhotos`) update them as they apply, inverses and the+//! rollback of a failed batch included, as those are ops too.++use std::collections::{HashMap, HashSet};+use std::sync::OnceLock;++use crate::{AlbumId, Catalog, PhotoId, StackId};++#[derive(Default)]+pub(crate) struct Indexes {+ /// Photo → the stack it is in; `None` when the catalog was read with a photo in two stacks+ /// (only a hand-edited file can hold that: ops refuse it), where a scan says which one counts.+ stacks: OnceLock<Option<HashMap<PhotoId, StackId>>>,+ /// Album → its photos (regular albums; a smart album's list is empty).+ albums: OnceLock<HashMap<AlbumId, HashSet<PhotoId>>>,+}++/// A copy builds its own when asked (copies are made to be saved, not queried).+impl Clone for Indexes {+ fn clone(&self) -> Self {+ Indexes::default()+ }+}++/// Derived from the rest of the catalog: two catalogs with the same data are equal whatever was+/// built.+impl PartialEq for Indexes {+ fn eq(&self, _: &Self) -> bool {+ true+ }+}++impl std::fmt::Debug for Indexes {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ let stacks = self.stacks.get().map(|m| m.as_ref().map(HashMap::len));+ f.debug_struct("Indexes").field("stacks", &stacks).field("albums", &self.albums.get().map(HashMap::len)).finish()+ }+}++impl Indexes {+ /// Stack `id` held `old` and holds `new` now (empty: it was added, or is gone).+ pub(crate) fn stack_changed(&mut self, id: StackId, old: &[PhotoId], new: &[PhotoId]) {+ let Some(Some(m)) = self.stacks.get_mut() else { return };+ for p in old {+ if m.get(p) == Some(&id) {+ m.remove(p);+ }+ }+ for p in new {+ m.insert(*p, id);+ }+ }++ /// Album `id` holds `photos` now (`None`: it is gone).+ pub(crate) fn album_changed(&mut self, id: AlbumId, photos: Option<&[PhotoId]>) {+ let Some(m) = self.albums.get_mut() else { return };+ match photos {+ Some(photos) => {+ m.insert(id, photos.iter().copied().collect());+ }+ None => {+ m.remove(&id);+ }+ }+ }+}++impl Catalog {+ /// The stack photo `photo` is in, by the index (built on first use).+ pub(crate) fn stack_id_of(&self, photo: PhotoId) -> Option<StackId> {+ let m = self.index.stacks.get_or_init(|| {+ let mut m = HashMap::new();+ for s in self.stacks.values() {+ for p in &s.photos {+ if m.insert(*p, s.id).is_some_and(|other| other != s.id) {+ return None;+ }+ }+ }+ Some(m)+ });+ match m {+ Some(m) => m.get(&photo).copied(),+ // in id order, the first stack listing the photo has it+ None => self.stacks.values().find(|s| s.photos.contains(&photo)).map(|s| s.id),+ }+ }++ /// Whether album `album`'s own list holds `photo`, by the index (built on first use).+ pub(crate) fn album_lists(&self, album: AlbumId, photo: PhotoId) -> bool {+ let m = self.index.albums.get_or_init(|| self.albums.values().map(|a| (a.id, a.photos.iter().copied().collect())).collect());+ m.get(&album).is_some_and(|s| s.contains(&photo))+ }+}diff --git a/crates/catalog/src/lib.rs b/crates/catalog/src/lib.rsindex cc24266f2..eec22fa9c 100644--- a/crates/catalog/src/lib.rs+++ b/crates/catalog/src/lib.rs@@ -15,6 +15,7 @@pub mod dates;pub mod folders;+mod index;pub mod journal;pub mod keywords;pub mod local;@@ -275,6 +276,9 @@ pub struct Catalog {/// Increments on every applied op.#[serde(skip)]pub revision: u64,+ /// Lookups derived from the above (never saved), see [`index`].+ #[serde(skip)]+ index: index::Indexes,}impl Catalog {@@ -357,13 +361,18 @@ impl Catalog {/// no smart album).pub fn album_contains(&self, id: AlbumId, p: &Photo) -> bool {match self.albums.get(&id) {- // guarded: a smart album testing smart albums can't loop or recurse without end- Some(Album { smart: Some(rules), .. }) => !p.deleted && rules::smart_album_holds(id, p.id, || rules.matches(p, self)),- Some(a) => a.photos.contains(&p.id),+ Some(Album { smart: Some(rules), .. }) => self.smart_album_has(id, rules, &rules.prepare_one(), p),+ Some(a) => self.album_lists(a.id, p.id),None => false,}}+ /// Whether smart album `id` with `rules` (as [`Filter::prepare`]d) holds `p`.+ fn smart_album_has(&self, id: AlbumId, rules: &Filter, prepared: &query::Prepared, p: &Photo) -> bool {+ // guarded: a smart album testing smart albums can't loop or recurse without end+ !p.deleted && rules::smart_album_holds(id, p.id, || rules.matches_in(p, self, prepared))+ }+/// Whether smart album `from` tests `to`, directly or through the smart albums it tests (the/// rules of `from` would then change when those of `to` do). Loops in saved rules end the/// search, they don't repeat it.@@ -411,7 +420,10 @@ impl Catalog {/// The photos of an album: the stored list, or a smart album's current matches (id order).pub fn album_photos(&self, id: AlbumId) -> Vec<PhotoId> {match self.albums.get(&id) {- Some(Album { smart: Some(_), .. }) => self.photos.values().filter(|p| self.album_contains(id, p)).map(|p| p.id).collect(),+ Some(Album { smart: Some(rules), .. }) => {+ let prepared = rules.prepare();+ self.photos.values().filter(|p| self.smart_album_has(id, rules, &prepared, p)).map(|p| p.id).collect()+ }Some(a) => a.photos.clone(),None => Vec::new(),}@@ -421,7 +433,10 @@ impl Catalog {pub fn album_count(&self, id: AlbumId) -> usize {match self.albums.get(&id) {// counted in place: no id list is built just for its length- Some(Album { smart: Some(_), .. }) => self.photos.values().filter(|p| self.album_contains(id, p)).count(),+ Some(Album { smart: Some(rules), .. }) => {+ let prepared = rules.prepare();+ self.photos.values().filter(|p| self.smart_album_has(id, rules, &prepared, p)).count()+ }Some(a) => a.photos.iter().filter(|p| self.photos.get(p).is_some_and(|p| !p.deleted)).count(),None => 0,}@@ -569,6 +584,7 @@ impl Catalog {}let id = album.id;self.next_album = self.next_album.max(id.0 + 1);+ self.index.album_changed(id, Some(&album.photos));self.albums.insert(id, album);Op::RemoveAlbum { id }}@@ -577,6 +593,7 @@ impl Catalog {return Err(CatalogError::Invalid("folder is not empty".into()));}let a = self.albums.remove(&id).ok_or(CatalogError::NoAlbum(id))?;+ self.index.album_changed(id, None);Op::AddAlbum { album: a }}Op::RenameAlbum { id, name } => {@@ -614,13 +631,16 @@ impl Catalog {Op::SetAlbumOrder { id, order: std::mem::replace(&mut a.order, order) }}Op::SetAlbumPhotos { id, photos } => {- let a = self.album_mut(id)?;+ // the field, not `album_mut`: the index is updated while the album is borrowed+ let a = self.albums.get_mut(&id).ok_or(CatalogError::NoAlbum(id))?;if (a.folder || a.smart.is_some()) && !photos.is_empty() {return Err(CatalogError::Invalid(if a.folder { "folders can't hold photos" } else { "smart albums update automatically" }.into(),));}- Op::SetAlbumPhotos { id, photos: std::mem::replace(&mut a.photos, photos) }+ let old = std::mem::replace(&mut a.photos, photos);+ self.index.album_changed(id, Some(&a.photos));+ Op::SetAlbumPhotos { id, photos: old }}Op::SetAlbumCover { id, cover } => {let a = self.album_mut(id)?;@@ -641,11 +661,13 @@ impl Catalog {self.validate_stack(stack.id, &stack.photos)?;let id = stack.id;self.next_stack = self.next_stack.max(id.0 + 1);+ self.index.stack_changed(id, &[], &stack.photos);self.stacks.insert(id, stack);Op::RemoveStack { id }}Op::RemoveStack { id } => {let s = self.stacks.remove(&id).ok_or(CatalogError::NoStack(id))?;+ self.index.stack_changed(id, &s.photos, &[]);Op::AddStack { stack: s }}Op::SetStack { id, photos, collapsed } => {@@ -654,7 +676,9 @@ impl Catalog {}self.validate_stack(id, &photos)?;let s = self.stacks.get_mut(&id).ok_or(CatalogError::NoStack(id))?;- let old = Op::SetStack { id, photos: std::mem::replace(&mut s.photos, photos), collapsed: s.collapsed };+ let old_photos = std::mem::replace(&mut s.photos, photos);+ self.index.stack_changed(id, &old_photos, &s.photos);+ let old = Op::SetStack { id, photos: old_photos, collapsed: s.collapsed };s.collapsed = collapsed;old}@@ -853,10 +877,14 @@ mod tests_folders;#[cfg(test)]mod tests_format_version;#[cfg(test)]+mod tests_index;+#[cfg(test)]mod tests_journal;#[cfg(test)]mod tests_local;#[cfg(test)]mod tests_lock;#[cfg(test)]+mod tests_scale;+#[cfg(test)]mod tests_torn_append;diff --git a/crates/catalog/src/query.rs b/crates/catalog/src/query.rsindex 8b694a021..9d3eb90c7 100644--- a/crates/catalog/src/query.rs+++ b/crates/catalog/src/query.rs@@ -112,6 +112,21 @@ fn shuffle_rank(seed: u64, id: PhotoId) -> u64 {mix64(mix64(seed) ^ id.0)}+/// What [`Filter::prepare`] works out once for a pass over many photos.+pub(crate) struct Prepared {+ root: Option<String>,+ only: Option<std::collections::HashSet<PhotoId>>,+}++/// The ids of `photos` by `key`, ties by id (so the order is total and the same every time),+/// reversed when not `ascending`. Each photo's key is worked out once.+fn sorted<'a, K: Ord>(photos: &[&'a Photo], ascending: bool, key: impl Fn(&'a Photo) -> K) -> Vec<PhotoId> {+ let mut keyed: Vec<(K, PhotoId)> = photos.iter().map(|p| (key(p), p.id)).collect();+ // ids differ, so there are no ties and an unstable sort gives the one order there is+ keyed.sort_unstable_by(|a, b| if ascending { a.cmp(b) } else { b.cmp(a) });+ keyed.into_iter().map(|(_, id)| id).collect()+}+fn token_matches(p: &Photo, tok: &str) -> bool {let t = tok.to_lowercase();if let Some((field, val)) = t.split_once(':') {@@ -234,7 +249,18 @@ impl Filter {}pub fn matches(&self, p: &Photo, cat: &Catalog) -> bool {- self.matches_in(p, cat, self.library_root().as_deref())+ self.matches_in(p, cat, &self.prepare_one())+ }++ /// [`Filter::prepare`] for testing one photo (the `only` ids are looked through, not hashed).+ pub(crate) fn prepare_one(&self) -> Prepared {+ Prepared { root: self.library_root(), only: None }+ }++ /// What [`Filter::matches_in`] needs worked out once for a pass over many photos rather than+ /// once per photo: the library folder's identity and the `only` ids as a set.+ pub(crate) fn prepare(&self) -> Prepared {+ Prepared { root: self.library_root(), only: (!self.only.is_empty()).then(|| self.only.iter().copied().collect()) }}/// [`Filter::library_folder`] as a folder identity ([`folder_key`]); `None` without a choice.@@ -244,9 +270,9 @@ impl Filter {self.library_folder.as_deref().filter(|d| !d.trim().is_empty()).map(folder_key)}- /// [`Filter::matches`] with the library folder already turned into its identity, so a query- /// over a big library does that once, not once per photo.- fn matches_in(&self, p: &Photo, cat: &Catalog, root: Option<&str>) -> bool {+ /// [`Filter::matches`] with what [`Filter::prepare`] works out done already, so a query over a+ /// big library does that once, not once per photo.+ pub(crate) fn matches_in(&self, p: &Photo, cat: &Catalog, prepared: &Prepared) -> bool {if p.deleted != self.deleted {return false;}@@ -285,7 +311,7 @@ impl Filter {if !self.labels.is_empty() && !p.label.is_some_and(|l| self.labels.contains(&l)) {return false;}- if !self.only.is_empty() && !self.only.contains(&p.id) {+ if !self.only.is_empty() && !prepared.only.as_ref().map_or_else(|| self.only.contains(&p.id), |only| only.contains(&p.id)) {return false;}if let Some(want) = &self.merged {@@ -325,7 +351,7 @@ impl Filter {{return false;}- if let Some(root) = root+ if let Some(root) = prepared.root.as_deref()&& !photo_in_root(p, root){return false;@@ -362,22 +388,19 @@ impl Filter {impl Catalog {/// Photos matching `filter`, in `sort` order (ties broken by id for stability).pub fn query(&self, filter: &Filter, sort: &Sort) -> Vec<PhotoId> {- let root = filter.library_root();- let mut v: Vec<&Photo> = self.photos().map(|p| p.as_ref()).filter(|p| filter.matches_in(p, self, root.as_deref())).collect();- v.sort_by(|a, b| {- let o = match sort.key {- SortKey::CaptureDate => a.captured.cmp(&b.captured).then_with(|| a.imported.cmp(&b.imported)),- SortKey::ImportDate => a.imported.cmp(&b.imported),- SortKey::EditDate => a.edited.cmp(&b.edited),- SortKey::FileName => a.file_name.to_lowercase().cmp(&b.file_name.to_lowercase()),- SortKey::Rating => a.rating.cmp(&b.rating),- SortKey::FileSize => a.file_size.cmp(&b.file_size),- SortKey::Random => shuffle_rank(sort.seed, a.id).cmp(&shuffle_rank(sort.seed, b.id)),- }- .then(a.id.cmp(&b.id));- if sort.ascending { o } else { o.reverse() }- });- v.into_iter().map(|p| p.id).collect()+ let prepared = filter.prepare();+ let v: Vec<&Photo> = self.photos().map(|p| p.as_ref()).filter(|p| filter.matches_in(p, self, &prepared)).collect();+ let asc = sort.ascending;+ match sort.key {+ SortKey::CaptureDate => sorted(&v, asc, |p| (p.captured.as_deref(), p.imported.as_str())),+ SortKey::ImportDate => sorted(&v, asc, |p| p.imported.as_str()),+ SortKey::EditDate => sorted(&v, asc, |p| p.edited.as_deref()),+ // each name lower-cased once, not twice per comparison+ SortKey::FileName => sorted(&v, asc, |p| p.file_name.to_lowercase()),+ SortKey::Rating => sorted(&v, asc, |p| p.rating),+ SortKey::FileSize => sorted(&v, asc, |p| p.file_size),+ SortKey::Random => sorted(&v, asc, |p| shuffle_rank(sort.seed, p.id)),+ }}/// Year → month → day counts for the "By Date" section (newest first).@@ -430,8 +453,8 @@ impl Catalog {pub fn people_in(&self, filter: &Filter) -> Vec<Person> {let filter = Filter { person: None, ..filter.clone() };let mut m: std::collections::HashMap<String, (Person, f64)> = Default::default();- let root = filter.library_root();- for p in self.photos().filter(|p| filter.matches_in(p, self, root.as_deref())) {+ let prepared = filter.prepare();+ for p in self.photos().filter(|p| filter.matches_in(p, self, &prepared)) {let mut seen: Vec<String> = Vec::new();for r in p.meta.regions.iter().filter(|r| r.kind == lightcraft_meta::RegionKind::Face) {let Some(name) = r.name.as_deref().map(str::trim).filter(|n| !n.is_empty()) else { continue };diff --git a/crates/catalog/src/rules.rs b/crates/catalog/src/rules.rsindex f17f12e01..a87c9effc 100644--- a/crates/catalog/src/rules.rs+++ b/crates/catalog/src/rules.rs@@ -598,6 +598,35 @@ pub fn now() -> String {}}+/// A flag as rules name it (its variant, lower case), without building a string per photo.+fn flag_name(f: crate::Flag) -> &'static str {+ match f {+ crate::Flag::None => "none",+ crate::Flag::Pick => "pick",+ crate::Flag::Reject => "reject",+ }+}++/// A colour label's colour as rules name it (lower case).+fn label_colour(l: crate::ColorLabel) -> &'static str {+ match l {+ crate::ColorLabel::Red => "red",+ crate::ColorLabel::Yellow => "yellow",+ crate::ColorLabel::Green => "green",+ crate::ColorLabel::Blue => "blue",+ crate::ColorLabel::Purple => "purple",+ }+}++/// A media kind as rules name it (lower case).+fn kind_name(k: crate::MediaKind) -> &'static str {+ match k {+ crate::MediaKind::Image => "image",+ crate::MediaKind::Raw => "raw",+ crate::MediaKind::Video => "video",+ }+}+fn lower(v: &Value) -> String {match v {Value::String(s) => s.trim().to_lowercase(),@@ -778,16 +807,16 @@ impl Rule {match field {"rating" => num_op(op, Some(p.rating as f64), value),"flag" => {- let f = format!("{:?}", p.flag).to_lowercase();+ let f = flag_name(p.flag);(f == want || (want == "picked" && f == "pick") || (want == "rejected" && f == "reject")) == (op == "is")}"label" => {- let l = p.label.map(|l| format!("{l:?}").to_lowercase()).unwrap_or_else(|| "none".into());+ let l = p.label.map_or("none", label_colour);// custom label names count too- let named = p.label.is_some_and(|l| cat.label_name(l).to_lowercase() == want);+ let named = p.label.is_some_and(|l| cat.custom_label_name(l).map_or(label_colour(l) == want, |n| n.to_lowercase() == want));(l == want || named) == (op == "is")}- "kind" => (format!("{:?}", p.kind).to_lowercase() == want) == (op == "is"),+ "kind" => (kind_name(p.kind) == want) == (op == "is"),"edited" => yes(p.is_edited()),"hasGps" => yes(m.gps.is_some()),"virtualCopy" => yes(p.copy_of.is_some()),diff --git a/crates/catalog/src/stacks.rs b/crates/catalog/src/stacks.rsindex 304106aef..83dbc13ab 100644--- a/crates/catalog/src/stacks.rs+++ b/crates/catalog/src/stacks.rs@@ -20,7 +20,7 @@ impl Catalog {}/// The stack a photo belongs to.pub fn stack_of(&self, photo: PhotoId) -> Option<&Stack> {- self.stacks.values().find(|s| s.photos.contains(&photo))+ self.stack_id_of(photo).and_then(|id| self.stacks.get(&id))}/// Photo → (stack, position in the stack), for every stacked photo.pub fn stack_index(&self) -> HashMap<PhotoId, (StackId, usize)> {@@ -40,7 +40,7 @@ impl Catalog {return Err(CatalogError::NoPhoto(*p));}}- if self.stacks.values().any(|s| s.id != id && s.photos.iter().any(|p| seen.contains(p))) {+ if photos.iter().any(|p| self.stack_id_of(*p).is_some_and(|s| s != id)) {return Err(CatalogError::Invalid("a photo can be in only one stack".into()));}Ok(())diff --git a/crates/catalog/src/tests_index.rs b/crates/catalog/src/tests_index.rsnew file mode 100644index 000000000..abfd4ef51--- /dev/null+++ b/crates/catalog/src/tests_index.rs@@ -0,0 +1,222 @@+//! The derived indexes ([`crate::index`]) answer as a scan of the catalog would, through every op,+//! inverse, failed batch, snapshot, replay and clone; and the faster query paths give the same+//! results as before.++use proptest::prelude::*;++use super::*;++const PHOTOS: u64 = 8;++fn library() -> Catalog {+ let mut c = Catalog::new();+ for i in 0..PHOTOS {+ let p = Photo::new(PhotoId(i + 1), Source::Demo { scene: 1 }, &format!("p{i}.jpg"), "JPEG", 60, 40, "2026-01-01T00:00:00");+ c.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();+ }+ c+}++/// What the indexes must say, worked out by scanning (as the catalog did before it had them).+fn scanned(c: &Catalog) -> (Vec<Option<StackId>>, Vec<Vec<bool>>) {+ let photos: Vec<PhotoId> = (1..=PHOTOS + 1).map(PhotoId).collect();+ let stacks = photos.iter().map(|p| c.stacks().find(|s| s.photos.contains(p)).map(|s| s.id)).collect();+ let albums = (1..=4)+ .map(|a| {+ photos+ .iter()+ .map(|p| c.photo(*p).is_some() && c.album(AlbumId(a)).is_some_and(|al| al.smart.is_none() && al.photos.contains(p)))+ .collect()+ })+ .collect();+ (stacks, albums)+}++/// What the indexes say.+fn indexed(c: &Catalog) -> (Vec<Option<StackId>>, Vec<Vec<bool>>) {+ let photos: Vec<PhotoId> = (1..=PHOTOS + 1).map(PhotoId).collect();+ let stacks = photos.iter().map(|p| c.stack_of(*p).map(|s| s.id)).collect();+ let albums = (1..=4)+ .map(|a| {+ photos+ .iter()+ .map(|p| c.album(AlbumId(a)).is_some_and(|al| al.smart.is_none()) && c.photo(*p).is_some_and(|ph| c.album_contains(AlbumId(a), ph)))+ .collect()+ })+ .collect();+ (stacks, albums)+}++/// Photo ids from bits (the extra id names a photo that isn't there: such ops fail).+fn ids(bits: u16) -> Vec<PhotoId> {+ (0..=PHOTOS).filter(|i| bits & (1 << i) != 0).map(|i| PhotoId(i + 1)).collect()+}++fn op(c: &Catalog, kind: u8, n: u64, bits: u16, collapsed: bool) -> Op {+ match kind % 8 {+ 0 => Op::AddStack { stack: Stack { id: StackId(n % 4 + 1), photos: ids(bits), collapsed } },+ 1 => Op::RemoveStack { id: StackId(n % 4 + 1) },+ 2 => Op::SetStack { id: StackId(n % 4 + 1), photos: ids(bits), collapsed },+ 3 => {+ let mut a = Album::new(AlbumId(n % 4 + 1), "A");+ a.photos = ids(bits);+ Op::AddAlbum { album: a }+ }+ 4 => Op::RemoveAlbum { id: AlbumId(n % 4 + 1) },+ 5 => Op::SetAlbumPhotos { id: AlbumId(n % 4 + 1), photos: ids(bits) },+ // a batch whose last op fails half the time: what it did is rolled back+ 6 => Op::Batch {+ ops: vec![+ Op::SetAlbumPhotos { id: AlbumId(n % 4 + 1), photos: ids(bits) },+ Op::SetStack { id: StackId(n % 4 + 1), photos: ids(bits.rotate_left(3)), collapsed },+ Op::AddStack { stack: Stack { id: StackId(n % 4 + 1), photos: ids(!bits), collapsed } },+ ],+ },+ // as the engine deletes a photo: out of its albums and stacks too, in one batch+ _ => c.delete_permanently_ops(PhotoId(n % (PHOTOS + 1) + 1)),+ }+}++proptest! {+ #![proptest_config(ProptestConfig::with_cases(256))]++ #[test]+ fn indexes_agree_with_a_scan(steps in proptest::collection::vec((0u8..8, 0u64..16, 0u16..512, any::<bool>()), 1..40)) {+ let mut c = library();+ let start = c.to_snapshot();+ // built before the ops, so every op has to keep them up to date+ prop_assert_eq!(indexed(&c), scanned(&c));+ let mut log = String::new();+ let mut inverses = Vec::new();+ for (kind, n, bits, collapsed) in steps {+ let op = op(&c, kind, n, bits, collapsed);+ if let Ok(inv) = c.apply(op.clone()) {+ log.push_str(&Catalog::op_to_log_line(&op));+ inverses.push(inv);+ }+ prop_assert_eq!(indexed(&c), scanned(&c));+ }+ // a snapshot, a replayed journal and a clone build their own+ let snap = Catalog::from_snapshot(&c.to_snapshot()).unwrap();+ prop_assert_eq!(indexed(&snap), scanned(&c));+ let mut replayed = Catalog::from_snapshot(&start).unwrap();+ prop_assert_eq!(indexed(&replayed), scanned(&replayed));+ replayed.replay(&log).unwrap();+ prop_assert_eq!(indexed(&replayed), scanned(&c));+ prop_assert_eq!(indexed(&c.clone()), scanned(&c));+ prop_assert!(c.clone() == c, "the indexes don't take part in equality");+ // undone step by step+ for inv in inverses.into_iter().rev() {+ c.apply(inv).unwrap();+ prop_assert_eq!(indexed(&c), scanned(&c));+ }+ prop_assert!(c.stacks().next().is_none() && c.albums().next().is_none(), "all undone");+ }+}++#[test]+fn a_photo_in_another_stack_is_refused_by_the_index() {+ let mut c = library();+ c.apply(Op::AddStack { stack: Stack { id: StackId(1), photos: vec![PhotoId(1), PhotoId(2)], collapsed: false } }).unwrap();+ let e = c.apply(Op::AddStack { stack: Stack { id: StackId(2), photos: vec![PhotoId(2), PhotoId(3)], collapsed: false } }).unwrap_err();+ assert_eq!(e, CatalogError::Invalid("a photo can be in only one stack".into()));+ // its own stack may keep it+ c.apply(Op::SetStack { id: StackId(1), photos: vec![PhotoId(2), PhotoId(1), PhotoId(3)], collapsed: true }).unwrap();+ assert_eq!(c.stack_of(PhotoId(3)).map(|s| s.id), Some(StackId(1)));+}++/// The sort keys are worked out once per photo now; the order is the one the comparisons gave.+#[test]+fn sorted_queries_keep_their_order() {+ let mut c = Catalog::new();+ let names = ["b.jpg", "A.jpg", "a.jpg", "Été.jpg", "été.jpg", "C.JPG", "b.jpg"];+ for (i, n) in names.iter().enumerate() {+ let mut p = Photo::new(PhotoId(i as u64 + 1), Source::Demo { scene: 1 }, n, "JPEG", 60, 40, &format!("2026-01-0{}T00:00:00", 7 - i % 3));+ p.captured = (i % 2 == 0).then(|| format!("2025-0{}-01T00:00:00", 1 + i % 4));+ p.rating = (i % 3) as u8;+ c.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();+ }+ let all = Filter::default();+ for key in [SortKey::CaptureDate, SortKey::ImportDate, SortKey::EditDate, SortKey::FileName, SortKey::Rating, SortKey::FileSize, SortKey::Random]+ {+ for ascending in [true, false] {+ let sort = Sort { key, ascending, seed: 7, ..Default::default() };+ // the comparison the query used to sort by+ let mut want: Vec<&Photo> = c.photos().map(|p| p.as_ref()).collect();+ want.sort_by(|a, b| {+ let o = match key {+ SortKey::CaptureDate => a.captured.cmp(&b.captured).then_with(|| a.imported.cmp(&b.imported)),+ SortKey::ImportDate => a.imported.cmp(&b.imported),+ SortKey::EditDate => a.edited.cmp(&b.edited),+ SortKey::FileName => a.file_name.to_lowercase().cmp(&b.file_name.to_lowercase()),+ SortKey::Rating => a.rating.cmp(&b.rating),+ SortKey::FileSize => a.file_size.cmp(&b.file_size),+ SortKey::Random => mix64(mix64(7) ^ a.id.0).cmp(&mix64(mix64(7) ^ b.id.0)),+ }+ .then(a.id.cmp(&b.id));+ if ascending { o } else { o.reverse() }+ });+ let want: Vec<PhotoId> = want.iter().map(|p| p.id).collect();+ assert_eq!(c.query(&all, &sort), want, "{key:?} ascending {ascending}");+ }+ }+ // `only` as a set gives the photos it names+ let only = Filter { only: vec![PhotoId(3), PhotoId(1), PhotoId(3)], ..Default::default() };+ assert_eq!(c.query(&only, &Sort { key: SortKey::FileName, ascending: true, ..Default::default() }), vec![PhotoId(3), PhotoId(1)]);+}++/// Rules name flags, labels and kinds as their variants in lower case (what `{:?}` gave).+#[test]+fn rule_names_are_the_variants_in_lower_case() {+ let mut c = Catalog::new();+ let mut n = 0;+ for flag in [Flag::None, Flag::Pick, Flag::Reject] {+ for label in+ [None, Some(ColorLabel::Red), Some(ColorLabel::Yellow), Some(ColorLabel::Green), Some(ColorLabel::Blue), Some(ColorLabel::Purple)]+ {+ for kind in [MediaKind::Image, MediaKind::Raw, MediaKind::Video] {+ n += 1;+ let mut p = Photo::new(PhotoId(n), Source::Demo { scene: 1 }, "x.jpg", "JPEG", 60, 40, "2026-01-01T00:00:00");+ (p.flag, p.label, p.kind) = (flag, label, kind);+ c.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();+ }+ }+ }+ c.apply(Op::SetLabelName { label: ColorLabel::Blue, name: Some("Sky".into()) }).unwrap();+ let count = |field: &str, value: String| {+ let rules: RuleSet =+ serde_json::from_value(serde_json::json!({"match": "all", "rules": [{"field": field, "op": "is", "value": value}]})).unwrap();+ c.query(&Filter { rule_set: Some(rules), ..Default::default() }, &Sort::default()).len()+ };+ for flag in [Flag::None, Flag::Pick, Flag::Reject] {+ assert_eq!(count("flag", format!("{flag:?}").to_lowercase()), 18, "{flag:?}");+ }+ assert_eq!(count("flag", "picked".into()), 18);+ for label in ColorLabel::ALL {+ assert_eq!(count("label", format!("{label:?}").to_uppercase()), 9, "{label:?}");+ }+ assert_eq!(count("label", "sky".into()), 9, "a custom name");+ assert_eq!(count("label", "none".into()), 9);+ for kind in [MediaKind::Image, MediaKind::Raw, MediaKind::Video] {+ assert_eq!(count("kind", format!("{kind:?}")), 18, "{kind:?}");+ }+}++/// A hand-edited file can put a photo in two stacks (ops refuse it): the stack index then gives+/// the answers a scan of the stacks gives, before and after either stack goes.+#[test]+fn a_photo_in_two_stacks_read_from_a_file_answers_as_a_scan() {+ let mut c = library();+ c.apply(Op::AddStack { stack: Stack { id: StackId(1), photos: vec![PhotoId(1), PhotoId(2)], collapsed: false } }).unwrap();+ c.apply(Op::AddStack { stack: Stack { id: StackId(2), photos: vec![PhotoId(3), PhotoId(4)], collapsed: false } }).unwrap();+ let mut v: serde_json::Value = serde_json::from_str(&c.to_snapshot()).unwrap();+ v["stacks"]["2"]["photos"] = serde_json::json!([2, 3, 4]);+ let mut c = Catalog::from_snapshot(&v.to_string()).unwrap();+ assert_eq!(indexed(&c), scanned(&c));+ assert_eq!(c.stack_of(PhotoId(2)).map(|s| s.id), Some(StackId(1)));+ // the second stack can't be changed to keep it, as before+ assert!(c.apply(Op::SetStack { id: StackId(2), photos: vec![PhotoId(2), PhotoId(3)], collapsed: false }).is_err());+ c.apply(Op::RemoveStack { id: StackId(1) }).unwrap();+ assert_eq!(indexed(&c), scanned(&c));+ assert_eq!(c.stack_of(PhotoId(2)).map(|s| s.id), Some(StackId(2)));+}diff --git a/crates/catalog/src/tests_scale.rs b/crates/catalog/src/tests_scale.rsnew file mode 100644index 000000000..81ddccc41--- /dev/null+++ b/crates/catalog/src/tests_scale.rs@@ -0,0 +1,92 @@+//! What library queries cost at 100k photos (ignored:+//! `cargo test --release -p lightcraft-catalog --lib scale_100k -- --ignored --nocapture`).++use std::time::Instant;++use serde_json::json;++use super::*;++/// 100k photos, 10k two-photo stacks, a 20k-photo album, keywords and flags.+fn library(n: u64) -> (Catalog, AlbumId) {+ let mut c = Catalog::new();+ let mut ops: Vec<Op> = (0..n)+ .map(|i| {+ let mut p = Photo::new(+ PhotoId(i + 1),+ Source::Demo { scene: (i % 20) as u32 },+ &format!("IMG_{:06}.jpg", mix64(i) % 1_000_000),+ "JPEG",+ 6000,+ 4000,+ "2026-01-01T00:00:00",+ );+ p.captured = Some(format!("20{:02}-{:02}-{:02}T{:02}:00:00", 10 + i % 16, 1 + i % 12, 1 + i % 28, i % 24));+ p.rating = (i % 6) as u8;+ p.flag = if i % 7 == 0 { Flag::Pick } else { Flag::None };+ p.meta.keywords = vec![format!("kw{}", i % 500), "travel|italy".into()];+ Op::AddPhoto { photo: Box::new(p) }+ })+ .collect();+ ops.extend(+ (0..n / 10)+ .map(|s| Op::AddStack { stack: Stack { id: StackId(s + 1), photos: vec![PhotoId(s * 10 + 1), PhotoId(s * 10 + 2)], collapsed: false } }),+ );+ let album = AlbumId(1);+ let mut a = Album::new(album, "Big");+ a.photos = (0..n).step_by(5).map(|i| PhotoId(i + 1)).collect();+ ops.push(Op::AddAlbum { album: a });+ let t = Instant::now();+ c.apply(Op::Batch { ops }).unwrap();+ eprintln!("build {n} photos, {} stacks: {:.0} ms", n / 10, t.elapsed().as_secs_f64() * 1e3);+ (c, album)+}++fn smart(c: &mut Catalog, id: u64, rules: serde_json::Value) -> AlbumId {+ let mut a = Album::new(AlbumId(id), format!("Smart {id}"));+ a.smart = Some(Box::new(Filter { rule_set: Some(serde_json::from_value(rules).unwrap()), ..Default::default() }));+ c.apply(Op::AddAlbum { album: a }).unwrap();+ AlbumId(id)+}++/// Best of five runs (the machine may be busy with other work).+fn time<T>(what: &str, f: impl Fn() -> T) -> T {+ let mut best = f64::MAX;+ let mut r = None;+ for _ in 0..5 {+ let t = Instant::now();+ r = Some(f());+ best = best.min(t.elapsed().as_secs_f64() * 1e3);+ }+ eprintln!("{what}: {best:.1} ms");+ r.unwrap()+}++#[test]+#[ignore]+fn scale_100k() {+ let (mut c, album) = library(100_000);+ let all = Filter::default();+ time("query, capture date", || c.query(&all, &Sort::default()));+ time("query, file name", || c.query(&all, &Sort { key: SortKey::FileName, ..Default::default() }));+ time("query, text", || c.query(&Filter { text: "kw42".into(), ..Default::default() }, &Sort::default()));+ time("query, album of 20k", || c.query(&Filter { album: Some(album), ..Default::default() }, &Sort::default()));+ let only: Vec<PhotoId> = (0..5000).map(|i| PhotoId(i * 7 + 1)).collect();+ time("query, only 5k ids", || c.query(&Filter { only: only.clone(), ..Default::default() }, &Sort::default()));+ let stacked = smart(&mut c, 10, json!({"match": "all", "rules": [{"field": "stacked", "op": "is", "value": true}]}));+ let flagged = smart(+ &mut c,+ 11,+ json!({"match": "all", "rules": [{"field": "flag", "op": "is", "value": "pick"}, {"field": "kind", "op": "is", "value": "image"}]}),+ );+ let keyword = smart(&mut c, 12, json!({"match": "all", "rules": [{"field": "keywords", "op": "contains", "value": "kw42"}]}));+ let in_album = smart(&mut c, 13, json!({"match": "all", "rules": [{"field": "album", "op": "is", "value": album.0}]}));+ let recent = smart(&mut c, 14, json!({"match": "all", "rules": [{"field": "captureDate", "op": "inLast", "value": {"n": 2, "unit": "years"}}]}));+ for (what, a) in [("stacked", stacked), ("flag + kind", flagged), ("keyword", keyword), ("in album", in_album), ("in the last 2 years", recent)] {+ let n = time(&format!("smart album count, {what}"), || c.album_count(a));+ eprintln!(" = {n}");+ }+ time("albums_of", || c.albums_of(PhotoId(6)));+ time("arrange stacks", || c.arrange_stacks(&c.query(&all, &Sort::default())));+ time("stack_of × 1000", || (0..1000u64).filter(|i| c.stack_of(PhotoId(i * 97 + 1)).is_some()).count());+}