Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
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
171 changes: 158 additions & 13 deletions crates/catalog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,16 +432,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 +461,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 +581,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 +598,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 @@ -1186,9 +1272,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
21 changes: 14 additions & 7 deletions crates/catalog/src/tally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::collections::{HashMap, HashSet};

use crate::query::Filter;
use crate::rules::{AlbumPass, MAX_ALBUM_DEPTH};
use crate::{Album, AlbumId, Aspects, Catalog, PhotoId};
use crate::{AlbumId, Aspects, Catalog, PhotoId};

/// The most photos an edit may touch for the counts to follow it photo by photo.
const MAX_FOLLOWED: usize = 256;
Expand Down Expand Up @@ -104,7 +104,8 @@ impl Members {
}
}

/// [`Catalog::album_count`] of every album, as of one revision of the catalog.
/// [`Catalog::album_count`] of every album, as of one revision of the catalog. Folders of albums are not counted:
/// the sidebar shows them no count, and theirs would cost a pass each.
#[derive(Clone, Debug)]
pub struct AlbumTally {
revision: u64,
Expand All @@ -122,9 +123,14 @@ impl AlbumTally {
/// An album whose rules lead into a loop, or through a chain too deep to be sure of, is counted by its rules
/// as before: the pass ends before the first of them (they sort last), so nothing they evaluate reads it.
pub fn of(cat: &Catalog) -> AlbumTally {
// one pass: a folder of albums a smart album is limited to is gathered once
crate::gathering(|| AlbumTally::counted(cat))
}

fn counted(cat: &Catalog) -> AlbumTally {
let heights = chain_heights(cat);
let height_of = |id: AlbumId| heights.get(&id).copied().flatten().filter(|h| *h <= MAX_ALBUM_DEPTH);
let mut smart: Vec<AlbumId> = cat.albums().filter(|a| a.is_smart()).map(|a| a.id).collect();
let mut smart: Vec<AlbumId> = cat.albums().filter(|a| a.is_smart() && !a.folder).map(|a| a.id).collect();
smart.sort_by_key(|id| height_of(*id).unwrap_or(usize::MAX));
let held_by = |id: AlbumId| {
let mut held = Members::none();
Expand Down Expand Up @@ -152,7 +158,7 @@ impl AlbumTally {
counts.insert(*id, held.len());
members.insert(*id, held);
}
for album in cat.albums().filter(|a| !a.is_smart()) {
for album in cat.albums().filter(|a| !a.is_smart() && !a.folder) {
counts.insert(album.id, cat.album_count(album.id));
}
AlbumTally { revision: cat.revision, counts, members }
Expand Down Expand Up @@ -202,7 +208,7 @@ impl AlbumTally {
self.counts.insert(*album, held.len());
}
if changed.any_of(Aspects::PRESENCE) {
for album in cat.albums().filter(|a| !a.is_smart()) {
for album in cat.albums().filter(|a| !a.is_smart() && !a.folder) {
self.counts.insert(album.id, cat.album_count(album.id));
}
}
Expand All @@ -222,12 +228,13 @@ fn chain_heights(cat: &Catalog) -> HashMap<AlbumId, Option<usize>> {
}
let tests = |id: AlbumId| -> Vec<AlbumId> {
let tested = cat.album(id).and_then(|a| a.smart.as_deref()).map(Filter::albums_tested).unwrap_or_default();
tested.into_iter().filter(|t| cat.album(*t).is_some_and(Album::is_smart)).collect()
// (a folder of albums leads to the smart albums inside it, which its photos are gathered from)
tested.into_iter().flat_map(|t| cat.album_members(t)).filter(|t| cat.album(*t).is_some_and(|a| a.is_smart() && !a.folder)).collect()
};
let frame = |id: AlbumId| Frame { id, tests: tests(id), next: 0, tallest: 0, loops: false };
let mut done: HashMap<AlbumId, Option<usize>> = HashMap::new();
let mut open: HashSet<AlbumId> = HashSet::new();
for root in cat.albums().filter(|a| a.is_smart()).map(|a| a.id) {
for root in cat.albums().filter(|a| a.is_smart() && !a.folder).map(|a| a.id) {
if done.contains_key(&root) {
continue;
}
Expand Down
Loading
Loading