Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d10a9af
LR-LIB-SMARTALBUM: creating or giving a keyword no longer freezes the…
nledford Oct 16, 2026
83f0688
catalog: a failed keyword op no longer marks the keyword list as changed
nledford Oct 25, 2026
9ca0d4d
LR-LIB-FOLDER: a folder of albums holds the photos of the albums insi…
nledford Oct 10, 2026
fb1e00b
LR-LIB-FOLDER: library.source shows a folder of albums
nledford Oct 10, 2026
e1f64d6
LR-LIB-FOLDER: clicking a folder of albums shows its photos
nledford Oct 10, 2026
a45d188
LR-LIB-FOLDER: docs for folders of albums as a source
nledford Oct 10, 2026
bcf18f5
LR-LIB-FOLDER: review fixes in the catalog: one gathering per pass, o…
nledford Oct 10, 2026
bc89f59
LR-LIB-FOLDER: moving an album into folders that contain each other ends
nledford Oct 10, 2026
52fd3f9
LR-LIB-FOLDER: a smart album can't be made in or moved into the folde…
nledford Oct 10, 2026
ea72299
LR-LIB-FOLDER: the Move to menu says why a move was refused; the side…
nledford Oct 10, 2026
e3ee999
LR-LIB-FOLDER: docs for smart albums and the folder they show
nledford Oct 10, 2026
0f7c678
LR-LIB-FOLDER: second review fixes in the catalog
nledford Oct 10, 2026
eb8a53d
LR-LIB-FOLDER: editing a saved view's rules keeps the album or folder…
nledford Oct 10, 2026
0670a24
LR-LIB-FOLDER: a folder is no drop target or Move to choice for a sma…
nledford Oct 10, 2026
0cbb24d
LR-LIB-FOLDER: docs for the second review's fixes
nledford Oct 10, 2026
02cb82e
LR-LIB-FOLDER: a folder view follows edits, and the one-pass test cou…
nledford Oct 19, 2026
0035a8d
LR-LIB-FOLDER: the whole count follows loops through a folder and lea…
nledford Oct 24, 2026
c12dec1
LR-LIB-FOLDER: a folder shown as the source follows the clock of the …
nledford Oct 24, 2026
9de9377
LR-LIB-SMARTALBUM: albums.list answers at once from the counts the si…
nledford Oct 30, 2026
0b3f688
Merge #768 (c12dec194b67c3ff7d62ef42f94b21ca6e7de6ef) and #3387 (9de9…
nledford Oct 30, 2026
e048d41
LR-LIB-FOLDER: albums.list counts a folder from the counts the sideba…
nledford Oct 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 174 additions & 20 deletions crates/catalog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ impl Aspects {
pub const ANALYSIS: Aspects = Aspects(1 << 7);
/// Whether it is in the library at all: added, removed, deleted, restored, taken in from Local.
pub const PRESENCE: Aspects = Aspects(1 << 8);
/// The keyword list (keywords on their own and their attributes): of no photo, and read by no rule, filter or
/// sort, only by what lists keywords (the keyword tree, export names).
pub const KEYWORD_LIST: Aspects = Aspects(1 << 9);
/// Everything: the photo may count differently for any tally.
pub const ALL: Aspects = Aspects(u16::MAX);

Expand All @@ -340,9 +343,11 @@ struct Changes {
touched: std::collections::VecDeque<(u64, PhotoId, Aspects)>,
/// What the op being applied changes of its photo.
now: Aspects,
/// The latest revision made by an op that changed more than photos of its own (an album, a stack, a keyword of
/// the list…): what it changed for which photos is not known.
/// The latest revision made by an op that changed more than photos of its own (an album, a stack…): what it
/// changed for which photos is not known.
broad: u64,
/// The latest revision made by an op that changed the keyword list ([`Aspects::KEYWORD_LIST`]).
keyword_list: u64,
/// Changes up to this revision are no longer all in `touched`.
floor: u64,
/// The revision the last op recorded here made. When the catalog's revision is another, it was set from
Expand Down Expand Up @@ -432,16 +437,23 @@ impl Catalog {
self.albums.values().find(|a| a.quick).map(|a| a.id)
}

/// Albums (regular and smart) that contain the photo.
/// Albums (regular and smart) that contain the photo; not the folders those albums are in.
pub fn albums_of(&self, id: PhotoId) -> Vec<AlbumId> {
let Some(p) = self.photos.get(&id) else { return Vec::new() };
self.albums.values().filter(|a| self.album_contains(a.id, p)).map(|a| a.id).collect()
self.albums.values().filter(|a| !a.folder && self.album_contains(a.id, p)).map(|a| a.id).collect()
}

/// Whether album `id` contains `p` (a smart album evaluates its rules; deleted photos are in
/// no smart album).
/// no smart album). A folder contains what the albums inside it do ([`Self::album_members`]).
pub fn album_contains(&self, id: AlbumId, p: &Photo) -> bool {
match self.albums.get(&id) {
// a folder first: one that also has rules (a damaged library) is still a folder.
// Within a pass over the library its photos were gathered once; for one photo, no
// list is built
Some(a) if a.folder => match gathered(self, id, || self.folder_photos(id)) {
Some(folder) => folder.holds(self, p),
None => self.albums.values().any(|m| !m.folder && self.album_is_within(m, id) && self.album_contains(m.id, p)),
},
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,
Expand All @@ -454,21 +466,97 @@ impl Catalog {
!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.
/// The albums and smart albums whose photos `id` shows: those inside a folder, at any depth
/// (the folders themselves left out), or an album alone. Empty when there is no such album.
pub fn album_members(&self, id: AlbumId) -> Vec<AlbumId> {
match self.albums.get(&id) {
Some(a) if a.folder => self.albums.values().filter(|m| !m.folder && self.album_is_within(m, id)).map(|m| m.id).collect(),
Some(a) => vec![a.id],
None => Vec::new(),
}
}

/// Whether `album` sits inside `folder`, directly or through the folders between them, as the
/// sidebar lists it: a parent that is no folder, or is gone, leads nowhere. Parents that lead
/// round in a ring (a damaged library) end the search after one pass round it.
fn album_is_within(&self, album: &Album, folder: AlbumId) -> bool {
let mut parent = album.parent;
for _ in 0..=self.albums.len() {
match parent {
Some(p) if p == folder => return true,
Some(p) => match self.albums.get(&p) {
Some(between) if between.folder => parent = between.parent,
_ => return false,
},
None => return false,
}
}
false
}

/// What folder `id` shows, gathered for a question about many photos.
fn folder_photos(&self, id: AlbumId) -> FolderPhotos {
#[cfg(test)]
FOLDER_GATHERS.with(|n| n.set(n.get() + 1));
let mut shown = FolderPhotos::default();
for member in self.album_members(id).iter().filter_map(|m| self.albums.get(m)) {
match member.smart {
Some(_) => shown.smart.push(member.id),
None => shown.listed.extend(member.photos.iter().copied()),
}
}
shown
}

/// Whether smart album `from` tests `to`, directly or through the smart albums it tests and the
/// folders they are in (the rules of `from` would then change when those of `to` do). Loops in
/// saved rules end the search, they don't repeat it.
pub fn album_reaches(&self, from: AlbumId, to: AlbumId) -> bool {
self.album_search(self.album_leads_to(from), |a| a == to)
}

/// Whether a smart album with `rules` would include itself if it were in folder `parent`: its
/// rules lead (as in [`Self::album_reaches`]) to that folder or to one around it.
pub fn smart_album_would_loop_in(&self, rules: &Filter, parent: AlbumId) -> bool {
let Some(inside) = self.albums.get(&parent) else { return false };
self.album_search(rules.albums_tested(), |a| {
self.albums.get(&a).is_some_and(|f| f.folder) && (a == parent || self.album_is_within(inside, a))
})
}

/// The smart album that would include itself if album or folder `id` were moved into folder
/// `parent`: `id` itself or, for a folder, one inside it ([`Self::smart_album_would_loop_in`]).
/// `None` when the move makes no loop.
pub fn album_move_would_loop(&self, id: AlbumId, parent: AlbumId) -> Option<AlbumId> {
let moved = self.album_members(id);
moved
.into_iter()
.find(|m| self.albums.get(m).and_then(|a| a.smart.as_deref()).is_some_and(|rules| self.smart_album_would_loop_in(rules, parent)))
}

/// The albums whose photos decide those of `a`: the albums a smart album tests, or the albums
/// inside a folder.
fn album_leads_to(&self, a: AlbumId) -> Vec<AlbumId> {
match self.albums.get(&a) {
Some(al) if al.folder => self.album_members(a),
Some(al) => al.smart.as_deref().map(Filter::albums_tested).unwrap_or_default(),
None => Vec::new(),
}
}

/// Whether `found` holds for one of `start` or for an album they lead to
/// ([`Self::album_leads_to`]); each album is looked at once.
fn album_search(&self, start: Vec<AlbumId>, found: impl Fn(AlbumId) -> bool) -> bool {
let mut seen: std::collections::HashSet<AlbumId> = std::collections::HashSet::new();
let mut next = vec![from];
let mut next = start;
while let Some(a) = next.pop() {
if !seen.insert(a) {
continue;
}
let tested = self.albums.get(&a).and_then(|al| al.smart.as_deref()).map(Filter::albums_tested).unwrap_or_default();
if tested.contains(&to) {
if found(a) {
return true;
}
next.extend(tested);
next.extend(self.album_leads_to(a));
}
false
}
Expand Down Expand Up @@ -498,12 +586,14 @@ impl Catalog {
})
}

/// The photos of an album: the stored list, or a smart album's current matches (id order).
/// The photos of an album: the stored list, or a smart album's current matches (id order). A
/// folder's are those of the albums inside it, each once, without the deleted ones (id order).
pub fn album_photos(&self, id: AlbumId) -> Vec<PhotoId> {
match self.albums.get(&id) {
Some(a) if a.folder => gathering(|| self.photos.values().filter(|p| !p.deleted && 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()
gathering(|| 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(),
Expand All @@ -513,10 +603,11 @@ impl Catalog {
/// Number of photos shown for an album in the sources list (excludes deleted photos).
pub fn album_count(&self, id: AlbumId) -> usize {
match self.albums.get(&id) {
Some(a) if a.folder => gathering(|| self.photos.values().filter(|p| !p.deleted && self.album_contains(id, p)).count()),
// counted in place: no id list is built just for its length
Some(Album { smart: Some(rules), .. }) => {
let prepared = rules.prepare();
self.photos.values().filter(|p| self.smart_album_has(id, rules, &prepared, p)).count()
gathering(|| 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,
Expand Down Expand Up @@ -589,7 +680,8 @@ impl Catalog {
if !self.remembers(revision) {
return None;
}
Some(self.changes.touched.iter().filter(|(r, _, _)| *r > revision).fold(Aspects::NONE, |all, (_, _, a)| all.with(*a)))
let photos = self.changes.touched.iter().filter(|(r, _, _)| *r > revision).fold(Aspects::NONE, |all, (_, _, a)| all.with(*a));
Some(if self.changes.keyword_list > revision { photos.with(Aspects::KEYWORD_LIST) } else { photos })
}

/// Whether every change since the catalog was at `revision` is remembered photo by photo.
Expand Down Expand Up @@ -622,6 +714,7 @@ impl Catalog {
self.changes = Changes { floor: self.revision, seen: self.revision, ..Default::default() };
}
let broad = self.changes.broad;
let keyword_list = self.changes.keyword_list;
self.changes.torn = false;
let inv = match self.apply_inner(op) {
Ok(inv) => inv,
Expand All @@ -643,6 +736,7 @@ impl Catalog {
self.changes.touched.pop_back();
}
self.changes.broad = broad;
self.changes.keyword_list = keyword_list;
}
return Err(e);
}
Expand All @@ -653,9 +747,9 @@ impl Catalog {
}

fn apply_inner(&mut self, op: Op) -> Result<Op> {
// An op that changes one photo says which (`photo_mut`, and adding or removing it below). Every other kind
// can change what any photo matches or where it sorts: an album's photos or rules, a stack, a keyword's
// name. Listed kind by kind, so a new one has to be put on one side.
// An op that changes one photo says which (`photo_mut`, and adding or removing it below). Most other kinds
// can change what any photo matches or where it sorts: an album's photos or rules, a stack. The keyword
// list changes only lists of keywords. Listed kind by kind, so a new one has to be put on one side.
match &op {
// (a photo that comes, goes, or changes what it is to the library counts for every tally; so do the
// rare kinds whose reach nobody has to think about)
Expand Down Expand Up @@ -695,8 +789,9 @@ impl Catalog {
| Op::SetStack { .. }
| Op::SetLabelName { .. }
| Op::SetBrowsed { .. }
| Op::SetKeyword { .. }
| Op::SetFolderRecord { .. } => self.changes.broad = self.revision.saturating_add(1),
// (no photo matches, counts or sorts by it: only lists of keywords change)
Op::SetKeyword { .. } => self.changes.keyword_list = self.revision.saturating_add(1),
}
Ok(match op {
Op::AddPhoto { photo } => {
Expand Down Expand Up @@ -1186,9 +1281,68 @@ fn album_order_key(a: &Album) -> (bool, bool, u32, String, AlbumId) {
(!a.folder, a.order.is_none(), a.order.unwrap_or(0), a.name.to_lowercase(), a.id)
}

/// The photos a folder of albums shows: what the albums inside it list, and the smart albums
/// inside it, whose rules are asked per photo.
#[derive(Default)]
struct FolderPhotos {
listed: std::collections::HashSet<PhotoId>,
smart: Vec<AlbumId>,
}

impl FolderPhotos {
fn holds(&self, cat: &Catalog, p: &Photo) -> bool {
self.listed.contains(&p.id) || self.smart.iter().any(|s| cat.album_contains(*s, p))
}
}

#[cfg(test)]
thread_local! {
/// How many times a folder's photos were gathered on this thread (tests count passes, not time).
static FOLDER_GATHERS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}

thread_local! {
/// The folders asked about during a pass over the library ([`gathering`]), by catalog (where
/// it is, which holds for the pass: it is borrowed throughout) and id: `None` outside one.
static GATHERED: std::cell::RefCell<Option<std::collections::HashMap<(usize, AlbumId), std::rc::Rc<FolderPhotos>>>> =
const { std::cell::RefCell::new(None) };
}

/// Run `pass`, a question put to many photos of one catalog: each folder of albums it asks about
/// (the one shown, or one a smart album is limited to) has its photos gathered once, not looked
/// up album by album for every photo. Forgotten when the outermost pass ends, however it ends.
pub(crate) fn gathering<T>(pass: impl FnOnce() -> T) -> T {
struct Forget(bool);
impl Drop for Forget {
fn drop(&mut self) {
if self.0 {
GATHERED.with_borrow_mut(|g| *g = None);
}
}
}
let outermost = GATHERED.with_borrow_mut(|g| g.is_none().then(|| *g = Some(Default::default())).is_some());
let _forget = Forget(outermost);
pass()
}

/// Folder `id` of `cat`'s photos during a pass ([`gathering`]), made by `gather` the first time;
/// `None` outside one.
fn gathered(cat: &Catalog, id: AlbumId, gather: impl FnOnce() -> FolderPhotos) -> Option<std::rc::Rc<FolderPhotos>> {
let key = (std::ptr::from_ref(cat) as usize, id);
if let Some(known) = GATHERED.with_borrow(|g| g.as_ref().map(|folders| folders.get(&key).cloned()))? {
return Some(known);
}
// gathered with nothing borrowed, then kept
let folder = std::rc::Rc::new(gather());
GATHERED.with_borrow_mut(|g| g.as_mut().map(|folders| folders.insert(key, folder.clone())));
Some(folder)
}

#[cfg(test)]
mod tests;
#[cfg(test)]
mod tests_album_folder_photos;
#[cfg(test)]
mod tests_album_order;
#[cfg(test)]
mod tests_background;
Expand Down
37 changes: 22 additions & 15 deletions crates/catalog/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,11 +485,13 @@ impl Catalog {
let prepared = filter.prepare();
// `within` only decides for the few kept photos: worked out per photo, not hashed
let within_prepared = within.prepare_one();
let mut v: Vec<&Photo> = self
.photos()
.map(|p| p.as_ref())
.filter(|p| filter.matches_in(p, self, &prepared) || (keep.contains(&p.id) && within.matches_in(p, self, &within_prepared)))
.collect();
// one pass: a folder of albums it asks about is gathered once
let mut v: Vec<&Photo> = crate::gathering(|| {
self.photos()
.map(|p| p.as_ref())
.filter(|p| filter.matches_in(p, self, &prepared) || (keep.contains(&p.id) && within.matches_in(p, self, &within_prepared)))
.collect()
});
v.sort_by(|a, b| sort.order(a, b));
v.into_iter().map(|p| p.id).collect()
}
Expand All @@ -503,22 +505,26 @@ impl Catalog {
let listed: std::collections::HashSet<PhotoId> = was.iter().copied().filter(|id| touched.binary_search(id).is_ok()).collect();
let mut ids: Vec<PhotoId> = was.iter().copied().filter(|id| !listed.contains(id)).collect();
let (prepared, within_prepared) = (filter.prepare_one(), within.map(Filter::prepare_one));
for id in touched {
let Some(p) = self.photo(*id) else { continue };
let kept = within.zip(within_prepared.as_ref()).is_some_and(|(w, wp)| listed.contains(id) && w.matches_in(p, self, wp));
if filter.matches_in(p, self, &prepared) || kept {
// (the list is in order without the touched photos, and stays so as each is put back)
let at = ids.partition_point(|other| self.photo(*other).is_some_and(|o| sort.order(o, p).is_lt()));
ids.insert(at, *id);
// one pass: a folder of albums it asks about is gathered once
crate::gathering(|| {
for id in touched {
let Some(p) = self.photo(*id) else { continue };
let kept = within.zip(within_prepared.as_ref()).is_some_and(|(w, wp)| listed.contains(id) && w.matches_in(p, self, wp));
if filter.matches_in(p, self, &prepared) || kept {
// (the list is in order without the touched photos, and stays so as each is put back)
let at = ids.partition_point(|other| self.photo(*other).is_some_and(|o| sort.order(o, p).is_lt()));
ids.insert(at, *id);
}
}
}
});
ids
}

/// How many photos match `filter`: [`Catalog::query`]'s length without its sorting.
pub fn count(&self, filter: &Filter) -> usize {
let prepared = filter.prepare();
self.photos().filter(|p| filter.matches_in(p, self, &prepared)).count()
// one pass: a folder of albums it asks about is gathered once
crate::gathering(|| self.photos().filter(|p| filter.matches_in(p, self, &prepared)).count())
}

/// Year → month → day counts for the "By Date" section (newest first).
Expand Down Expand Up @@ -575,7 +581,8 @@ impl Catalog {
let filter = Filter { person: None, ..filter.clone() };
let mut m: std::collections::HashMap<String, (Person, f64)> = Default::default();
let prepared = filter.prepare();
for p in self.photos().filter(|p| filter.matches_in(p, self, &prepared)) {
let matching: Vec<_> = crate::gathering(|| self.photos().filter(|p| filter.matches_in(p, self, &prepared)).collect());
for p in matching {
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 };
Expand Down
Loading
Loading