Repository navigation
feat(store): order and filter store lists in SQL (#951) - #986
Merged
Merged
Conversation
A sorted or filtered page is now one counted keyset query through an index per sortable and filterable property, instead of reading every record in scope and ordering it in memory. A string's index key maps UTF-8 lead bytes EE/EF above the four-byte sequences, so SQLite byte order equals the documented UTF-16 code unit order; numbers, integers and booleans key as doubles. Indexes are derived from the declaration, built at activation in a BEGIN IMMEDIATE transaction before the declaration fence is recorded, and dropped (with stale interval indexes) once no live activation declares them. Rows the key cannot order exactly (lone surrogates, off-type values) are found through a normally empty partial index and served by the in-memory path. A differential test compares pages, totals, cursors and next against the in-memory reference on seeded random data. bench:store gains --lists; results in docs/CAPACITY.md: 10k sort=title c=16 from 169/386 ms to 7.7/18.1 ms. Co-Authored-By: Claude Opus 5.5 <[email protected]>
jimhoyd
enabled auto-merge (squash)
September 29, 2026 22:21
…generate references Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Sep 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #951
What
A sorted or filtered store list is now one counted keyset query through an index per
sortable/filterableproperty. It no longer reads every record in scope and orders it in JS. The read/list path is in the newpackages/store/src/listing.ts.collection.tschanges onlyopen()(build the indexes) andlistIn()(call the SQL path). The write path is untouched.Indexes: expression indexes over
data, partial on the collection name (a literal), created in the same way as the interval index:(collection, owner, key, id)on an owned collection;(collection, key, id)on a shared collection, or on an owned one with readers mounts.They are built in
open()insideBEGIN IMMEDIATE, beforerecord()writes the declaration fence. Stale ones are dropped with the stale interval indexes, now also in one write transaction (dropStaleIndexes).UTF-16 collation, built-ins only. node:sqlite has no collation API, and a user function in an index would break
trusted_schema=OFFand any other connection. So a string key isreplace(replace(CAST(v AS BLOB), x'EE', x'F5'), x'EF', x'F6'). UTF-8 byte order differs from UTF-16 code-unit order only between U+E000–U+FFFF (lead bytes EE/EF) and supplementary code points (F0–F4). Moving EE/EF to F5/F6, which are never valid bytes, gives UTF-16 order. Other key rules:CAST(... AS REAL).x'', which sorts after all numbers and text, so absent values come last and a descending sort reverses everything, ties included.k >= P AND (k > P OR id > ?), so the index bounds the range.Fallback to the in-memory path, which gives the same results. It is used when:
->>decodes it as WTF-8) or a type other than the declared one. These rows are found through a normally empty partial index, so the check is one lookup;Cursor,
limit,total,nextand filter semantics are unchanged. Readers mounts, projections and owner scoping pass through unchanged, and a projected mount still sorts and filters only on the properties it lists.Evidence
New
packages/store/test/list-order.test.ts: a differential test of SQL pages againstrunList(the old path) on seeded random data (LIST_ORDER_SEED). The data covers:It runs in shared, owner and readers scopes, with filters and random limits, walks every page, and deletes or inserts records mid-walk (cursor stability). It passed under 20 extra seeds, and it fails when the EE/EF mapping is removed (checked by mutation).
Also covered:
EXPLAIN QUERY PLANof every sorted page uses astore_list_*index with no temp B-tree, and the cursor bounds the index range;Before and after, measured with
npm run bench:store -- --lists(new flag: only the list parts) in the same session, Apple M4 Pro, Node 26.10, SQLite 3.53.4. [Request]: Store: order sorted and filtered lists in SQL near the 10,000-record maximum #951 cites 152/712 ms from the 2026-09-28 run; the same measurement gave 169/386 ms here.sort=titlekind=b&sort=titlesort=title, concurrency 16The write cost of four list indexes is +0.03–0.04 ms per create at p50. Details are in docs/CAPACITY.md ("Measured: sorted lists in SQL (#951)"); docs/STORE.md, the store README and CHANGELOG are updated.
npm run verifyandnpm run test:packagepass. The store package measures 147932 packed / 572071 unpacked bytes, 36 entries, which is inside main's measurement-plus-margin budget (Flaky store test: operator-audit's final deleteOwnerless races the server lease heartbeat #971), so the budget is unchanged.Limits
filterable-only property still gets an index (one more entry per write).🤖 Generated with Claude Code