Skip to content

PERF-CATALOG: segment files nothing lists are removed when a library is opened, and segments without a snapshot are refused (stacked on #2983) - #2984

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

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

Conversation

@nledford

@nledford nledford commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Part of #800. Stacked on #2983 (a draft: catalog format 11, the snapshot in segments, stacked on #2978): this PR's own commit is 46998f956e, the one on top of #2983's head b7257421fc. Chain to main: #2867, #2948, #2951, #2957, #2968 (with #2871), #2978, #2983, then this. It waits for them to land and is a draft until then: GitHub shows their commits in the diff until they do.

Problem

With the snapshot in segments (the part before this one), a crash between writing the segments of a compaction and replacing catalog.snap, or between that and removing the files it replaced, leaves segment files that no snapshot lists. They are never read and never written over, but nothing removes them, so they take room for as long as the library lives. And a library folder that has segment files but no catalog.snap (a folder copied or synced in part, a catalog.snap that was removed) opens as a new, empty library: the photos are in those files, and the next compaction writes a snapshot that does not list them.

Change

crates/catalog/src/journal.rs and the engine's library:

  • Journal::remove_unlisted_segments removes the segment files the snapshot doesn't list, and the engine calls it when a library is opened only if it holds the library's lock (to a second program, the segments the first is writing for its next snapshot look the same). It goes by catalog.snap as it is on disk together with what the journal takes it to list (the two differ after a snapshot whose outcome it never learned), removes nothing when that file can't be read, and only ever removes segment files: not other files in the folder, and, when the library was opened from a one-file catalog.snap (an older one put back over a newer folder), none at all for as long as the journal is open, since they may be all there is of the newer catalog.
  • Segment files without a catalog.snap are refused, not opened as a new library, unless the log holds every change from the first (a library whose first snapshot got as far as writing segments and no further). A folder whose segments can't be listed is refused as well.
  • docs/catalog-persistence.md and docs/whats-new.md say so.

Not in this PR: writing only the segments whose photos changed and sizing a segment by its bytes (the next part), and anything about when the log is compacted.

How it was checked

  • cargo xtask ci passes on this branch, rebased on current main: not run locally for this draft (the hosted CI gate runs it once its parents have landed); at 27948a405e cargo fmt --all --check, cargo check --workspace --all-targets and clippy with -D warnings pass, cargo test -p lightcraft-catalog --lib passes (369 passed, 3 ignored) and the engine's library and persistence tests pass (112 passed, 1 ignored)
  • A test that fails without this change: the 41 tests in tests_segments.rs run on the code of PERF-CATALOG: the catalog saves its photos in segment files and opens them on several threads (catalog format 11, stacked on #2978) #2983 with Journal::remove_unlisted_segments given its old behavior (it removes nothing): 7 fail on their assertions (unlisted_segment_files_are_removed_on_request_only: the two files a crash left are still there; segments_without_their_snapshot_are_not_a_new_library and a_log_only_library_survives_a_first_snapshot_that_did_not_finish: the folder opens as a new library; segments_beside_a_one_file_snapshot_are_left_alone; a_crash_at_any_point_of_a_compaction_loses_nothing: a segment file nothing lists is still there after opening; segments_are_json_files_in_a_folder_of_their_own; a_segments_folder_that_is_not_one_is_an_error_that_says_so), and cleaning_up_goes_by_the_snapshot_on_disk passes both ways: it guards against removing too much (a snapshot whose replace took effect and then reported an error) (These runs were made on this part before it was rebased onto PERF-CATALOG: the segments of a snapshot, with no caller yet (stacked on #2968 and #2871) #2978's final head, which changed only the segments module's tests and comments below it.)
  • No catalog compatibility check is needed: no file is written differently (the format stays 11); this removes files that no snapshot lists and refuses a folder that used to open as a new library
  • User-visible change: ran the app and looked at the result: not applicable, nothing changes on screen

A reviewer with a clean context read this part with the ones before and after it. It found two comments that were ahead of or behind the code (the dead-code allowance's reason and the doc about removing unlisted files), fixed.

Screenshots

None; nothing changes on screen.

Merging

Checklist

@coderabbitai

coderabbitai Bot commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@nledford

Copy link
Copy Markdown
Contributor Author

Reworked after a review: head now f57ebd57e1 (two comments made true at this commit), rebased on #2983.

@nledford
nledford force-pushed the catalog-segments-p3b branch from f57ebd5 to 716a57f Compare October 26, 2026 00:59
@nledford

Copy link
Copy Markdown
Contributor Author

The hosted gate was red on the missing ci_quick::EMBEDDED entries for the v10 catalog fixtures. The fix is in #2983 (9f5c944); this PR is rebased onto it with no change of its own, new head 716a57f.

nledford and others added 4 commits October 27, 2026 06:43
Opening a library in an older format rewrites it in this build's, and older
builds then refuse it: a one-way step. Journal::open now first keeps the
files the library was, byte for byte, as catalog.snap.v<format>-<seq> and
catalog.log.v<format>-<seq>, and reports their names (LoadReport::kept).
Put back under their own names they are the library as the older build
left it (docs/catalog-persistence.md, Going back to an older version).

A library whose files can't be kept is not upgraded (it opens as it is; the
next open tries again), and an upgrade that fails leaves no copy; kept files
stay whenever the upgrade may have been written. A library whose log was
damaged is an upgrade like any other: its snapshot is kept beside the
damaged log, and it is refused if that log can't be replaced. The person is
told once where the library as it was is kept. A library left in its old format
at open is upgraded by a later snapshot (compaction, or closing), which keeps its
files first and is not written when it can't. The browser build keeps none
(Store::keeps_replaced_formats).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A library left in an older format at open keeps its files when a later snapshot upgrades it. Those files are the library as it was at open (the snapshot, and the log up to its length then), under the names the open would have given them, so what is appended meanwhile in this build's ops is never in them. The snapshot hands the names on (Journal::take_kept) and the engine tells the person once, as the open does. Appends are not refused meanwhile (a library whose files can't be copied would save nothing): the module doc and the persistence doc say so.
…he later upgrade's tests and docs are exact

A library left in its old format whose snapshot is in this build's format already (a write that landed and reported a failure) no longer fails every snapshot until it is reopened: it is the upgrade done, with nothing to keep. Tests now cover the files kept at open's length and seq when changes were made before the first attempt, the snapshot that landed after its files were kept, no second copy on a retry, the background worker's result and telling once. The docs say what the kept files hold when a session already added changes in the old format, what going back loses, and that a snapshot at close or a library switch has no one to tell.
…n't be kept leaves nothing lying about

FsStore::write_atomic left `<name>.tmp` behind when the write, the sync or the rename failed, and write_atomic_with left it when the rename failed. The files an upgrade keeps are written through it, so a full disk or a taken name left a partial `catalog.snap.v6-8.tmp` beside the library, against the documented all-or-nothing keep and "no copy left lying about". Both now remove the staging file on any failure.

Co-Authored-By: Claude <noreply@anthropic.com>
@nledford
nledford force-pushed the catalog-segments-p3b branch from a48f65c to 28d6874 Compare October 27, 2026 15:10
nledford and others added 7 commits October 29, 2026 09:36
… files and sync folders it can

The catalog store gets what a snapshot in several files will need, with no caller yet: `Store::remove`, `Store::write_new_with` (a file that doesn't exist yet, durable in itself), `Store::sync` (one checked sync of the folders for all of them), `Store::list` and `Store::segmented` (the default can't hold segments: the browser's store). `FsStore` writes files into the library's `segments/` folder (made with its first file, and refused where `segments` is a file or a link to a folder elsewhere), lists them as `segments/<name>`, and syncs.

What exists today is unchanged: `write_atomic` and `write_atomic_with` still sync the folder on a best-effort basis and never fail for it. Only the new `sync` reports a folder sync that fails, except on a file system that can't sync folders (EBADF, EINVAL, "not supported": the platform's own number, and macOS's EOPNOTSUPP 102, which is ENETRESET on Linux), where it does nothing, as on Windows.

Nothing reads or writes `segments` yet: `crate::segments` holds only the names of the files, and the tests are of the store alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t a real folder

FsStore::read, append and truncate checked only the final path, so a segments folder that is a link (or a file) let them reach outside the library. They now use the same non-creating folder check as remove (symlink_metadata; a missing folder reads as no file).
…s: the files kept at open stay, as they were then
…when the log can't be replaced, and what a later snapshot keeps
…renames a kept catalog.log.damaged-<seq> like any kept log
A develop setting a record leaves out reads as its default, so a default
that changed in the code would change every photo relying on it. catalog.snap
now states the defaults it was written with ("defaults"), and they cover the
log beside it. A build whose own defaults differ reads the library's settings
over the library's (stored::reading_over) and rewrites it with its own before
anything is appended (LoadReport::other_defaults); a library that only lacks
newer settings, or knows more, is read as it is. What a missing value reads as
in this build is pinned by a test (develop_defaults.json).

Every record is still written in full, and the format stays 9: older builds
ignore the extra header field. This is the reading half of the compact records
of the next change.

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

The sentence said the library's files still state its own defaults. That holds when catalog.snap could not be written; when it was written and only the log could not be emptied, they state this build's, and the log's records are skipped as stale.
nledford and others added 3 commits October 30, 2026 08:06
…s the changed values out, and Journal::open's docs list the refusal when the rewrite can't be written
…fixture

The catalog formats were a table in journal.rs's module docs that nothing read.
They are now one list in code, journal::FORMATS: each row has the format's number,
the build that wrote it first, what it adds, and whether it adds an op (NEW_OP) or
only fields (FIELDS). VERSION is the newest row this build has. A test holds that
the numbers run 1.. without a gap, that a row that adds an op names it, and that the
newest op's format is the newest row that adds one (what NEWEST_FIELDS held by hand).

Every format from 6 on has a fixture, a small library written by the build of that
format, which every later build must open as it was: fixtures v7 to v9 are new (v6
was there), written by their builds from the fixture library of tests_stored.rs.

Nothing is written or read differently: VERSION stays 9.

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

The writing half of compact records, for develop settings only and with no
caller yet: the settings of a photo, and those of its history steps, versions
and import look, stored as their difference from the defaults ({} for an
unedited photo). A value is left out only where that reads back the same
(worked out from the types on first use: not the rendering process, not single
fields of the crop rectangle), every difference is read back before it is
written, and a negative zero is kept. writing_full() stores settings whole while
a library's header lacks a setting this build has, and states_every_setting()
says when that is so.

Nothing writes this form yet: records stay as they are, and the format stays 9.
The next change names these functions from the ops, stores the photos with them and brings
catalog format 10.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nledford and others added 7 commits October 30, 2026 08:08
A photo's record in catalog.snap / catalog.log states only what was set:
unset fields of the photo and its metadata are left out, and develop settings
(the photo's, its history steps', versions' and import look's) are stored as
their difference from the defaults, with the stored form of the previous change.
A record of a photo with no metadata goes from 3.2 kB to 0.2 kB. Reading is
unchanged, so formats 1-9 load as they are and are rewritten on open; the public
JSON of a photo (catalog.query, Local fingerprints, preview-cache keys) is
unchanged.

Format 10 is the first that leaves values out, so a library that is in a format
before 10 (also one that states defaults), or states other ones than this
build's, and can't be upgraded, is not opened (it is left as it was, and the
error says why). A library in format 10 that lacks a setting this build has is
rewritten to state it before a compact record is appended; where that can't be
written, its records state develop settings whole until a snapshot is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…compact records (storytold#2968, 24ba2c5)

# Conflicts:
#	crates/catalog/src/lib.rs
#	crates/catalog/src/store.rs
…eader, with no caller yet

`crate::segments` holds what the snapshot in segments (catalog format 11) is made of: the segment table, how a segment is named, which stretches of photos change and how they are cut into pieces of a size the caller states, the checksummed writer (on several threads where the store gives handles, and every new file in the folder before anything lists it), the check of a segment list a snapshot may state, and the reader that checks every segment against its listed size, CRC, photo count and id range and names the file that is wrong. `stored::PhotoRange` writes a run of photos in their stored form.

Nothing in the catalog calls it yet, and nothing reads or writes format 11: the journal that uses it, the format row, the segment size policy and the docs come in the next change. Its own unit tests exercise it (write and read back on one thread and on several; only the changed segments written; splits and removals; a damaged, missing, miscounted or foreign segment named in the error; a failed write leaving no new file behind, on one thread and on several; unlisted files discarded and nothing else), and the dead-code allowance on the module goes with the journal.

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

A library in a folder on disk keeps its photos in segment files (`segments/<first id>-<seq>.json`, at most 4096 photos each) that `catalog.snap` lists with their size, CRC-32 and photo count. Every compaction writes every segment, on several threads, under names no snapshot lists, then replaces `catalog.snap` atomically (the commit), then removes the files it no longer lists; opening reads the segments on several threads. A one-file library is split on first open, and its one file is replaced only once the segments read back the same. Stores without segments (the browser build) keep the photos in `catalog.snap`, as before. A damaged or missing segment fails the open with an error naming the file, and a list of segments that does not add up to the photos `catalog.snap` states is refused.

Format 11 is a new row of `FORMATS` (it adds fields, not an op) with a fixture library in segments, `v11-*`. An older build refuses it untouched, so going back takes the files the upgrade kept, and now takes the segments along. A library whose upgrade at open could not be written (or whose old files could not be kept) stays in its old format: a later compaction, by hand, on close or in the background, keeps the old files first and then writes the upgrade. A change that could not be appended to the log is then saved in the format on disk, in one file, so none is lost; closing such a library is not refused when everything is in the log and only the compaction failed.

This writes every segment each time: writing only the segments whose photos changed, removing the segment files a crash left, refusing segment files without a `catalog.snap`, and sizing a segment by its bytes are the next changes, and the module-level dead-code allowance of `segments` stays until the last of them. The compaction rule is the one that was there (2000 records or 16 MB of log).

300,000 browsed raws (180 MB as one file), one run each on an M1 Max with other builds running: opening 259 ms -> 78 ms; a synchronous compaction of 2000 records 840 ms -> 448 ms (median of 3); an append during a background compaction p95 7.7-7.9 ms -> 17.5-20.0 ms. Measured on this commit and on its parent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…mat 11's one-file store

Moved from storytold#2958 (ae75897, catalog-snapshot-scale-p4c), whose own change had become only these tests after storytold#2867. `tests_upgrade_attempts.rs` is the file as it was there, with the same `mod` line in `journal.rs`, and its opening comment no longer says that no format can be put off (a format 10 library can be on this build).

Ten tests that set up a put-off upgrade's state by hand (the library as it was at open, behind) and check that it keeps one pair of files however many attempts fail, writes them once, tells them once the snapshot that works is written, and takes a snapshot that landed all the same as the upgrade done. The store they use is one file, as the browser's is: they don't cover the segments of format 11.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…opened, and segments without a snapshot are refused

A crash between writing segments and committing `catalog.snap`, or between the commit and removing the files it replaced, leaves segment files nothing lists. They are never read and never written over, but they take room. `Journal::remove_unlisted_segments` removes them, and the engine calls it only when it holds the library's lock (to anyone else, the segments another program is writing for its next snapshot look the same). It goes by `catalog.snap` as it is on disk together with what the journal takes it to list (they differ after a snapshot whose outcome it never learned), removes nothing when that file can't be read, only ever removes segment files, and leaves them all alone for as long as the journal is open when the library was opened from a one-file `catalog.snap`: an older one put back over a newer folder may be all that is left of the newer catalog.

Segment files without a `catalog.snap` (a folder copied or synced in part, a `catalog.snap` that was removed) are refused instead of being opened as a new, empty library that would abandon them, unless the log holds every change from the first (a library whose first snapshot got as far as writing segments and no further). A folder whose segments can't be listed is refused as well.

No file is written differently: this only removes files that no snapshot lists, and refuses more. The format stays 11.

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

Copy link
Copy Markdown
Contributor Author

Folded into #2983 at 038f346 (the user's decision, 30 Oct): this PR's one commit, "segment files nothing lists are removed when the library is opened, and segments without a snapshot are refused", is now the last commit of #2983's branch (the same patch, rebased onto main with the rest of the chain). #2983's description has its Change and test lines, and #2985 is stacked on #2983 directly. Closing this PR.

@nledford nledford closed this Oct 30, 2026
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