Skip to content

Store compat-mode sorted keys as raw UTF-8 for xitdb-java interop - #46

Merged
codeboost merged 1 commit into
masterfrom
compat-sorted-keys
Nov 5, 2026
Merged

codeboost merged 1 commit into
masterfrom
compat-sorted-keys

Conversation

@codeboost

Copy link
Copy Markdown
Owner

Summary

In compat mode (file header not clj1), sorted-map keys and sorted-set members must now be strings and are stored as their raw UTF-8 bytes, mirroring the existing hash-key rule. Sorted collections are now readable in both directions between xitdb-clj and xitdb-java. Clojure mode (clj1) is unchanged: same codec, same bytes, same tests.

  • conversion: new db-sorted-key-encode, db-sorted-key-lookup, db-sorted-key-decode and db-sorted-comparator pick the codec from the Database handle, as db-key-hash does. Every sorted encode/decode call site (sorted_operations.clj, the sorted writers and the keypath write cursor) goes through them.
  • Writing a non-string sorted key or member in compat mode throws IllegalArgumentException and leaves the file byte-identical. Lookups, contains?, dissoc and disj with a non-string key find nothing or do nothing.
  • Range queries follow one total order in compat mode: a non-string key is never present and sorts before every string. So rank is 0, (subseq m > :k) is the whole map and (rsubseq m < :k) is empty. This is the same model Clojure's own sorted-map uses for nil. A sorted-key/compat-key-comparator implements it and is accepted by default-sorted-comparator?, so materialized compat collections can be written back. Non-strings keep their clj1 order among themselves, so in-memory transforms of a materialized map (e.g. keys to keywords) lose no entries.
  • README: sorted-key rule next to the hash-key rule, a shared sorted-map Java/Clojure example, "Java SortedMaps" removed from "Not supported", and a note that compat files from 0.7.0/0.7.1 with prefixed sorted keys must be rewritten.

Deviation from the spec

The spec asked for subseq/rsubseq with a non-string bound to "find nothing". Clojure's subseq evaluates two of the four directions through the collection's comparator as (test (compare stored-key bound) 0), so no single comparator can return an empty result for all four. The consistent total order above was chosen instead; it is documented in the README.

Tests

New tests in test/xitdb/compat_mode_test.clj, writing and reading real files through the xitdb-java API: Java-written sorted map/set read by xitdb-clj; xitdb-clj-written sorted map/set read by Java with exact key bytes checked (including empty and non-ASCII keys); non-string keys rejected on write with the file unchanged; all four range directions with keyword, integer and nil bounds; swap! paths (assoc, dissoc, conj, disj, assoc-in, update-in); materialized compat collections keep distinct non-string keys.

clojure -M:test
Ran 302 tests containing 5709 assertions.
0 failures, 0 errors.

Reviewed with codex review (three passes); the two findings it raised (inconsistent range bounds, key collapse in materialized compat collections) are fixed and covered by tests. Final pass: no actionable regressions.

🤖 Generated with Claude Code

In compat mode (file header not clj1), sorted-map keys and sorted-set
members must be strings and are stored as their raw UTF-8 bytes, the
same rule hash keys already follow. This makes sorted collections
readable in both directions between xitdb-clj and xitdb-java.

- conversion: db-sorted-key-encode / db-sorted-key-lookup /
  db-sorted-key-decode / db-sorted-comparator pick the codec from the
  Database handle, as db-key-hash does. Every sorted encode/decode call
  site goes through them.
- Writing a non-string sorted key in compat mode throws
  IllegalArgumentException and leaves the file unchanged. Lookups,
  contains?, dissoc and disj with a non-string key find nothing.
- Range queries follow one total order in compat mode: a non-string key
  is never present and sorts before every string, so rank is 0,
  (subseq m > :k) is the whole map and (rsubseq m < :k) is empty.
  sorted-key/compat-key-comparator implements that order and is accepted
  by default-sorted-comparator?, so materialized compat collections can
  be written back; non-strings keep their clj1 order among themselves.
- Clojure mode (clj1) is unchanged.
- README: sorted-key rule, shared sorted-map example, note that compat
  files from 0.7.0/0.7.1 with prefixed sorted keys must be rewritten.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@codeboost
codeboost merged commit 0bda68a into master Nov 5, 2026
1 check passed
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