Skip to content

Catalog: the snapshot in segments of about 2 MB and at most 4096 photos (format 11, stacked on #800) - #814

Closed
nledford wants to merge 36 commits into
storytold:mainfrom
nledford:catalog-segments
Closed

nledford wants to merge 36 commits into
storytold:mainfrom
nledford:catalog-segments

Conversation

@nledford

@nledford nledford commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #800 (compact records, format 10). Folds #846 (segment size and the checked folder sync), which is closed as folded.

Replaced by smaller parts, each a draft stacked on the one before: #2867 (upgrades keep what they replace), #2948, #2951, #2957, #2968 (compact records, catalog format 10), #2958, then the segments: #2978, #2983 (format 11), #2984, #2985. The compaction-by-snapshot-size rule that this PR holds is not in those parts.

Why this is stacked: it edits the snapshot code that #800 adds and main does not have: the FORMATS table that format 11 is a row of, Policy::due's snapshot-size parameter, snapshot_bytes, and the ReadingOver / reading_over settings reader in stored.rs. Format 11 is the point of this PR and is built on format 10, so it can't be rebuilt off main without #800's compact records and compaction policy. It is the bottom of the backup chain, and every PR above it needs format 11 (the segment files). Merge order: #800, then #814, then #841, #856, #870, #1004, #1025, #1034, #1043 / #1054, #1075, #1303. The chain is being unchained from the bottom: #800's parts and then #814's segment slices are being cut as PRs off main. When #800 lands, this PR is rebased onto main and the first line and the title lose "stacked on".

Problem

The snapshot is one file, so every compaction rewrites every photo however few changed, and opening reads it on one thread. On a library of 1,000,000 JPEGs, a compaction after 1,000 changes took 570 ms and an open 1,767 ms (see Measurements).

Change

See What below: the snapshot's photos move into segment files in the library folder (catalog format 11); a compaction writes only the segments whose photos changed, and opening reads the segments on several threads.

Not in this PR: the browser build, which keeps its photos in one catalog.snap; the two problems listed under Notes, left unchanged.

How it was checked

  • On the rebased branch (2026-10-14): cargo test -p lightcraft-catalog 282 passed, cargo test -p lightcraft-engine 854 passed, cargo fmt --check and clippy (the full list is under Tests below).
  • Three independent adversarial reviews (under Tests below).

What

The snapshot's photos move out of catalog.snap into segment files (segments/<first id>-<seq>.json in the library folder, each a run of photos in id order, plain JSON). catalog.snap keeps everything else and lists the segments with their size, CRC-32 and photo count. Catalog format 10 → 11. crates/catalog/src/segments.rs, docs/catalog-persistence.md.

  • A compaction writes only the segments whose photos changed. Which ones is not worked out from the ops: the journal keeps the photos as the segments on disk hold them (shared Arcs) and compares pointers. Changing a photo copies it, so a segment is rewritten exactly when one of its photos was changed, added or removed, whatever did it.
  • Opening reads segments on several threads, checking size, CRC, photo count, id range and the total. A damaged or missing segment fails the open with an error naming the file; nothing is modified.
  • Commit: new segments under names no snapshot lists (written on several threads, each fsynced, one folder sync), then catalog.snap replaced atomically, then the files it no longer lists removed.
  • Upgrade: a one-file library is split on first open; the segments are read back and compared with the loaded photos before catalog.snap is replaced. If they differ, the one file stays.
  • Stores without segments (Store::segmented() false: the browser build, whose storage holds a fixed set of files) keep the photos in catalog.snap. Store gains remove, list, segmented, write_new_with, sync, all with defaults.
  • When to compact now goes by what a compaction would rewrite (the segments holding changed photos), not the whole snapshot.

Measurements

persist_bench, Apple M1 Max; before = #800 (one file, format 10 as it is numbered now). "One place" = 1000 changes to the first 1000 photos; "all over" = a change to every 50th photo (touches every segment).

Library Photos Open Compaction, one place Compaction, all over Import (catalog only)
JPEG library 100,000 175 → 59 ms 81 → 49 ms (41 → 4.1 MB) 88 → 129 ms 1.9 → 3.1 s
JPEG library 1,000,000 1,767 → 518 ms 570 → 58 ms (414 → 4.1 MB) 577 → 886 ms 16.6 → 20.3 s
JPEG library 3,500,000 6,129 → 1,714 ms 2,009 → 75 ms (1,455 → 4.2 MB) 2,060 → 3,360 ms 57.5 → 61.0 s
Browsed raws 300,000 741 → 165 ms 575 → 63 ms 581 → 363 ms 9.2 → 10.6 s
Edited raws, 20 history steps 20,000 399 → 132 ms 2,742 → 326 ms (68 → 6.8 MB) 2,731 → 731 ms 14.0 → 6.4 s

Where it costs more: rewriting every segment at once is slower than one file where records are small (1.5× for JPEG libraries: each file is made durable on its own); and a bulk import of small records takes up to 1.6× as long in this catalog-only benchmark (smaller but more frequent compactions). Memory is unchanged: the catalog is still held whole in memory.

Safety

  • Never deletes what might be the catalog. Unlisted segment files are removed only on request, by an opener that holds the library lock, and only when the library was opened from a snapshot that lists segments. A folder with segment files but no catalog.snap is refused, not opened as a new library. Segment files beside an older one-file catalog.snap are left alone for that session.
  • A snapshot is never committed listing a file that is gone (checked before the commit), never written over an existing file, and a list of segments that doesn't add up to the stated photo count is refused.
  • No new panic paths: threads that can't be started or that panic are handled; hostile catalog.snap content (path-like names, duplicates, wrong order or ranges, huge numbers) is Corrupt with nothing read or modified.

Tests

  • tests_segments.rs (30 tests): layout, only-changed-segments (also after reopening), crash before every single write of a compaction, background compaction, parallel writes with one failing writer, damaged / missing / hostile inputs, the one-file fixture of Catalog: the log is compacted by the snapshot's size, not every 2000 records #800 (format 9 now, written by that build) upgraded, the split that doesn't read back, stores without segments and back, and a randomized model test (adds, gaps, removals, edits, foreground and background snapshots, reopening).
  • The background-compaction suite (tests_background.rs) now runs on the segmented layout; the one-file policy tests run on a store without segments.
  • On the rebased branch (2026-10-14): cargo test -p lightcraft-catalog 282 passed (main's tests_repair among them), cargo test -p lightcraft-engine 854 passed; cargo fmt --check; cargo clippy -p lightcraft-catalog -p lightcraft-engine -p lightcraft-web --all-targets -D warnings; cargo xtask parity, layers, assets. Not run on the rebase: the whole of cargo xtask ci (the wasm build and the UI crate's tests).

Three independent adversarial reviews (the third on the changes made after the first two: 20,000 randomized cases with injected failures, which found that a log-only library could be refused after an unfinished first snapshot; fixed, with a test). The first two ran over 740,000 simulated crash points (a copy of the files before every store mutation, sync and background, with and without injected failures, and from a one-file library) with no failure, and could not break the changed-segment detection. They found that cleaning up at open could delete good data in three situations (missing catalog.snap, an older snapshot restored over a newer folder, a second program opening an unlocked library mid-save), a thread-spawn panic path, and tests that didn't prove what they claimed (shown by mutation testing); all fixed here, and the mutations that slipped through are now caught.

Notes

🤖 Generated with Claude Code

Merging

Checklist

Summary by CodeRabbit

  • New Features

    • Large libraries store photo data in separate segments, allowing saves to update changed portions and segments to load concurrently.
    • Catalog records use less storage by omitting unset values and saving editing settings relative to defaults.
    • Catalog upgrades preserve replaced files where supported and identify preserved or damaged files when opening a library.
    • Libraries may be compacted on close according to save status and compaction policy; otherwise, changes may remain in a log until the next open.
    • Disk libraries upgrade to format 11, which earlier versions cannot open. Browser libraries continue storing photos in a single catalog file.
  • Documentation

    • Added guidance on large-library persistence, backups, catalog upgrades, and browser versus desktop libraries.

@nledford

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (0ba118de, after the UI.9 docking merge in #869) to keep the catalog stack in one line. No content change: this branch had no conflicts. cargo xtask ci passes locally (all 8 steps) on the tip of the stack (#870), which contains this commit.

@nledford nledford changed the title Catalog: the snapshot in segments (format 8, stacked on #800) Catalog: the snapshot in segments (format 9, stacked on #800) Oct 11, 2026
@nledford

Copy link
Copy Markdown
Contributor Author

Rebased onto the updated #800 and renumbered: segments are now format 9 (compact records are 8, 7 is left for #752). Changes here beyond the renumbering: segment files are read over the defaults the library states (on every reader thread), a library that states other defaults has every segment rewritten, the format 9 fixture is a library in segments, and the way back to an older version now says to move the segments folder too. Details for the whole series are in #800 (comment).

@nledford

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (995d5ea4) with the rest of the series; what changed is described in #800 (latest comment). Conflicts here were with today's merges in the locale files, docs/parity-checklist.md, the engine's module list and (in #856 / #870) library.rs, engine/src/lib.rs and menus.rs; both sides are kept in each.

Local verification: main itself does not pass cargo xtask ci on my Mac at the moment (#989; fixes for two of the four causes are in #990), so this is not a green run. I ran every CI step without stopping at the first failure, on plain main and on the tip of this series, and compared: the series fails exactly where main fails (the desktop app's compile error and the same 12 tests) and nowhere else; fmt, the heif steps, parity, layers, assets and wasm pass.

@nledford

Copy link
Copy Markdown
Contributor Author

A note for whoever merges this and #776 (smart albums kept off loops): each builds on its own, but the two together don't compile. I found it by merging all of my open PRs onto a local branch.

Catalog::without_photos, added here, names every field of Catalog so that a new one can't be forgotten; #776 adds one, the loops memo. Whichever of the two lands second needs this:

// crates/catalog/src/lib.rs, Catalog::without_photos
let Catalog { photos: _, albums, /* … */ folder_records, revision, loops: _ } = self;
Catalog {
    // …
    revision: *revision,
    // (worked out from the albums when asked for: not saved)
    loops: LoopsMemo::default(),
}

The memo is #[serde(skip)] and is rebuilt from the albums on demand, so there is nothing to save. I'm happy to push the change to whichever branch is rebased second; say which.

…put off is tested

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A sweep of segment sizes (docs/catalog-persistence.md, "Segment size") shows that what a
save of a few changes rewrites follows the size of a segment, and for edited photos so does
its time, while opening is the same from about 2000 photos per segment up. Halving the
sizes (they were 8192 photos and 4 MB) halves what scattered changes rewrite and what a
backup after them copies; a save that touches every segment is about 1.4 times as slow
where records are small.

A setting, not part of the format: existing segments are cut to the new size as they are
next rewritten.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…library that kept only one

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…al photos per segment

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… What's New states the close thresholds

On POSIX FsStore::sync returned Ok when syncing segments/ or the library folder failed, so
catalog.snap could name segments whose entries were not durable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
EINVAL, ENOTSUP and EBADF from fsync on a folder (network and FUSE file systems) mean "can't be
synced here", as on Windows; only real I/O errors fail Store::sync and write_atomic_with. A
write_atomic whose rename took effect no longer returns Err for a folder sync, so Err means nothing
was written (callers' state stays right).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… own

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 22, 2026
@nledford

Copy link
Copy Markdown
Contributor Author

Closing this draft as superseded: its parts now live in smaller PRs that can be reviewed one at a time.

Nothing here is lost; the branch stays on my fork if anything needs to come back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant