Skip to content

feat(store): order and filter store lists in SQL (#951) - #986

Merged
jimhoyd merged 2 commits into
mainfrom
claude/store-sql-order-951-v2
Sep 29, 2026
Merged

jimhoyd merged 2 commits into
mainfrom
claude/store-sql-order-951-v2

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Fixes #951

What

A sorted or filtered store list is now one counted keyset query through an index per sortable/filterable property. It no longer reads every record in scope and orders it in JS. The read/list path is in the new packages/store/src/listing.ts. collection.ts changes only open() (build the indexes) and listIn() (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() inside BEGIN IMMEDIATE, before record() 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=OFF and any other connection. So a string key is replace(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:

    • Numbers, integers and booleans key as CAST(... AS REAL).
    • An absent value or JSON null keys as x'', which sorts after all numbers and text, so absent values come last and a descending sort reverses everything, ties included.
    • The cursor seek is 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:

    • a stored value can't be keyed exactly: a lone surrogate (->> 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;
    • a cursor or filter string holds a lone surrogate.

    Cursor, limit, total, next and 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 against runList (the old path) on seeded random data (LIST_ORDER_SEED). The data covers:

    • code points on both sides of every UTF-8/UTF-16 boundary, surrogate pairs, NUL and empty strings, ties;
    • absent values and JSON null;
    • numbers including -0, subnormals, ±max, >2^53 and 1e21;
    • booleans and integers.

    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 PLAN of every sorted page uses a store_list_* index with no temp B-tree, and the cursor bounds the index range;
    • the anomaly fallback;
    • the indexes follow the declaration across activations.
  • 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.

10,000 records, page 100 before p50 / p99 ms after p50 / p99 ms
sort=title 11.2 / 14.0 0.63 / 1.31
kind=b&sort=title 10.4 / 13.0 0.73 / 1.53
sort=title, concurrency 16 169 / 386 (89 lists/s) 7.7 / 18.1 (1,887 lists/s)

The 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.

Limits

  • Measured on macOS only, not Linux. Collections with many owners were not measured.
  • In a multi-process setup, an activation with a different declaration drops the other process's list indexes, the same housekeeping the interval indexes already get. The other process's lists stay correct but get slower.
  • A filterable-only property still gets an index (one more entry per write).

🤖 Generated with Claude Code

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
jimhoyd enabled auto-merge (squash) September 29, 2026 22:21
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.

[Request]: Store: order sorted and filtered lists in SQL near the 10,000-record maximum

1 participant