Skip to content

feat(store): store compact rewrites a closed store without dead terms - #825

Open
MathiasVDA wants to merge 5 commits into
mainfrom
claude/project-thread-04ivyo-759
Open

MathiasVDA wants to merge 5 commits into
mainfrom
claude/project-thread-04ivyo-759

Conversation

@MathiasVDA

Copy link
Copy Markdown
Contributor

Requested by Mathias · project thread

Closes #759

Phase 2 of #757, following decision record 0013 §1 and §4 (tripletrail-enterprise PR #131). Running inside the server is #760.

What

Store::compact(dir) and tripletrail store compact --location DIR [--dataset ds] [--memory-limit SIZE] rewrite a closed store with only the terms its quads still use.

  1. Count. The store is opened; its background merges stop and its writer is held throughout. One pass over the quads, which includes the log, sets a bit for every live dictionary ordinal.
  2. Renumber by rank (Ranks in compact.rs). A live ordinal's new number is the count of live ordinals up to and including it. A u32 count every 512 ordinals keeps this cheap; no map of every term is built. The map is monotone, so key order is kept.
  3. Write the next checkpoint with Disk::checkpoint(…, rewrite = true), the same path store upgrade takes:
    • Dictionary::keep copies the live keys in order as one piece, as stored, with the same namespaces, and the piece's index is rebuilt;
    • the saved geometries are read by their old ordinals (encode_geometry) and copied;
    • the quads are renumbered and every permutation (and stats.bin) is sorted again by the bulk load's Sorter, with spill under the memory limit.
  4. Crash safety. CURRENT names checkpoint n+1 only once it is complete. Until then the old checkpoint is untouched, and an unfinished one is removed at the next open, as a stale checkpoint is. The old log is retired at its full length, as at any checkpoint. Once compacted, the store's writer is dropped, so nothing more is written from the old data held in memory.

A store without dead terms is left as it is. The output is the current format; there is no new magic.

Differences from DR 0013, for the reviewer

Measured

examples/dict_churn.rs (1M triples, 10 rounds each replacing a tenth, release build) now compacts at the end and also loads the same quads fresh:

dict*.bin MB dict*.idx MB geoms*.bin MB segments MB dead terms
after 10 rounds 124.5 36.0 20.0 1,000,000 of 3,000,002
compacted (0.7 s) 71.2 24.0 10.0 22.0 0
fresh load 71.2 24.0 10.0 18.7 0

dict*.bin and geoms*.bin match a fresh load exactly, which meets the acceptance criterion. The segments are 18 % larger than a fresh load's, because ordinal order differs (DR 0013 §1: equal "up to ordinal order").

Tests

  • crates/tripletrail-store/tests/compact.rs:
    • churn on 20,000 subjects with strings, language strings, typed literals, WKT, blank nodes, triple terms and named graphs, plus a transaction left in the log. Checks the same quads after compaction, no dead terms left, geometries read back, writes afterwards (a dropped term comes back), and dict*.bin + geoms*.bin within 3 % of a fresh load;
    • a store with no dead terms is left as it is;
    • refused: a folder that is not a store, and a store open elsewhere;
    • the 0.3.0 CRS-tags fixture compacts to an up-to-date store with the same quads;
    • a kill test: a child process is SIGKILLed a moment after each step starts (reading, sorting, writing), two rounds per step. Each time the store opens with the same quads and no unfinished checkpoint, and a second compaction finishes.
  • Ranks unit test.
  • crates/tripletrail-server/tests/upgrade_in_place.rs, every_release_is_compacted_with_the_same_answers: each release fixture gets a dead term, runs store compact, then is served. It gives the release's answers (spatial and routing queries included), is up to date, and takes a write.
  • tests/offline.rs, store_compact_drops_dead_terms: the CLI output, the same dump, a second run that has nothing to do, and an unknown dataset refused.
  • Guide chapter 11: a tested example (11 of 108 terms dropped).

Local results:

  • cargo test -p tripletrail-store: all pass
  • --test offline, --test upgrade_in_place (the compaction test), and GUIDE_CHAPTER=11 --test guide: pass
  • clippy -D warnings (store, server): clean
  • the api_surface listing is updated for the new subcommand

Docs

  • docs/store.md: a Compaction entry with the design and the measurement.
  • docs/server.md: the command.
  • Guide chapter 11: a tdb2.tdbcompact row and a section with the example.
  • CHANGELOG: Unreleased, Added.

There is no docs/cli.md; CLI commands are documented in docs/server.md.


Generated by Claude Code

…#759)

Following DR 0013 §1 and §4. Store::compact opens the closed store and
counts the live ordinals: those that some quad uses. It renumbers each live
ordinal by its rank, through a bitset with a count every 512 ordinals, so
no map of every term is built.

It then writes checkpoint n+1 with:
- the live keys, copied in order as one dictionary piece, with its index;
- the saved geometries, read by their old ordinals;
- every permutation sorted again by the bulk load's sorter, under the
  memory limit.

The old checkpoint stays current until CURRENT names the new one, so a
crash leaves the store as it was. The log is folded in, as by any
checkpoint.

`tripletrail store compact --location --dataset [--memory-limit]` runs it.
@MathiasVDA MathiasVDA self-assigned this Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

The red audit check isn't caused by this PR. It fails on main as well (cab0f26), because of a new advisory, RUSTSEC-2026-0628, against calamine 0.38, the spreadsheet import's dependency. #828 bumps it to 0.39. Once that merges, I'll merge main into this branch.


Generated by Claude Code

@MathiasVDA
MathiasVDA marked this pull request as ready for review December 3, 2026 11:27
@MathiasVDA MathiasVDA added the review:changes Reviewer asks for changes label Dec 3, 2026 — with Claude

Copy link
Copy Markdown
Contributor Author

Changes requested at bfcdbb5. The core is right: rank renumbering, the checkpoint path, the crash rule and the three deviations from DR 0013 are fine with me (the DR text should follow, I note it on enterprise#131). I also ran a harder variant of your release test, because its dead term is the newest one and so no id moves: on every release fixture I served the dataset (so the routing network and spatial index are saved), deleted up/s0_0 and every rdfs:label (about 400 early terms dead), compacted, and compared the four fixture queries and a full ordered dump before and after. All seven releases agree, and a write afterwards works. Worth adding to the test in that form.

Two must-fix items.

1. Compacting a dataset whose files are of an older version upgrades it with no way back.
store compact on the 0.3.4 fixture (after one tripletrail update) rewrites dict.bin OXGSDIC7 to OXGSDIC9 and geoms.bin OXGSGEO3 to OXGSGEO4, removes the old checkpoint and logs, and store info then says up to date. store rollback answers has no restore point. The same on 0.1.0. So a dataset that store upgrade would have upgraded with a restore point (#516, #517) is upgraded one way by a command whose docs promise only "the same answers". Either refuse a dataset that is not up to date ("run store upgrade first", the simple choice) or keep the restore point as store upgrade does; then say which in docs/server.md, docs/store.md and the guide, with a test on an old fixture. check_no_rollback only covers an upgrade that already waits.

2. The whole live dictionary is copied onto the heap, whatever --memory-limit says.
Dictionary::keep builds one Vec with every live key, build_table adds a hash table over them, and live is 4 bytes per term. --memory-limit only bounds the sort of the quads. On a store where compaction matters most (1B triples: tens of GB of keys, normally mapped from disk) the command needs that much RAM or is killed; the DR says the keys are streamed. Please write the kept keys as they are read (or spill the piece as the bulk load does), or, if that is too much for this PR, state the memory need in the command help, docs/server.md and docs/store.md, and open a follow-up issue that #760 must close before compaction runs by itself in a server.

Small: the doc comment of store_upgrade in offline.rs now sits on store_compact (the new function went between the comment and its function), and the same happened to from_pieces in dict.rs.

audit is red as on main (the calamine advisory, #828); merge main in after #828 lands.


Generated by Claude Code

Review of #825:
- store compact refuses a dataset whose files are of an older version
  instead of upgrading it with no restore point: run store upgrade first.
- Dictionary::keep writes the kept keys to a mapped spill file as it reads
  them, and the old ordinal of a new one comes from Ranks::select, so the
  heap holds a bit per term besides the sort.
- The release test serves each fixture first, leaves about 400 early terms
  dead and compares the fixture queries and a full dump; a new test checks
  the refusal and compaction after store upgrade.
- Doc comments of store_upgrade and from_pieces back on their functions.

Refs #759
@MathiasVDA MathiasVDA removed the review:changes Reviewer asks for changes label Dec 3, 2026
@MathiasVDA MathiasVDA added the review:approved Reviewer approved label Dec 3, 2026 — with Claude

Copy link
Copy Markdown
Contributor Author

Approved at 6c1841f. Both must-fix items are done: store compact refuses a dataset whose files are of an older version and changes nothing, and the kept keys go to a mapped file as they are read, with Ranks::select in place of the live list. The store compact tests, the Ranks unit tests and the two compact tests on the release fixtures pass here. I merge it when the tests are green.

For #760: the automatic run needs the same rule for an older dataset, as noted on the decision record (tripletrail-enterprise#131).


Generated by Claude Code

This branch has not been deployed

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

Labels

review:approved Reviewer approved

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(store): store compact rewrites a closed store without dead terms

1 participant