Repository navigation
Store compat-mode sorted keys as raw UTF-8 for xitdb-java interop - #46
Merged
Merged
Conversation
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>
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.
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: newdb-sorted-key-encode,db-sorted-key-lookup,db-sorted-key-decodeanddb-sorted-comparatorpick the codec from theDatabasehandle, asdb-key-hashdoes. Every sorted encode/decode call site (sorted_operations.clj, the sorted writers and the keypath write cursor) goes through them.IllegalArgumentExceptionand leaves the file byte-identical. Lookups,contains?,dissocanddisjwith a non-string key find nothing or do nothing.rankis 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 fornil. Asorted-key/compat-key-comparatorimplements it and is accepted bydefault-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.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/rsubseqwith a non-string bound to "find nothing". Clojure'ssubseqevaluates 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.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