Skip to content

Add compatibility mode for files shared with other languages - #44

Merged
codeboost merged 3 commits into
masterfrom
compat-mode
Nov 3, 2026
Merged

codeboost merged 3 commits into
masterfrom
compat-mode

Conversation

@codeboost

@codeboost codeboost commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

xitdb-clj hashes keys with a format tag (header ID clj1), so it can't look up keys in files written by the Java or TypeScript libraries, and they can't look up keys in its files.

Compat mode hashes string keys as raw UTF-8, as Java's put(String) does, so both sides can read and write the same file.

Mode comes from the header. An existing file opens in the mode its header records: clj1 in Clojure mode, any other header in compat mode, so files from other languages open with no option. db-mode returns :clojure or :compat for an open database. New files are clj1 unless created with (xit-db path {:mode :compat}), which gives them the header ID sha1. An explicit :mode for an existing file is checked against its header, and a mismatch throws without modifying the file.

Compat mode rules. Hash map keys and set members must be strings at every depth. Writing anything else throws and leaves the database unchanged, reading one finds nothing, and removing one does nothing. Native maps and sets are written plain, so Java's ReadHashMap and ReadHashSet can read them. Clojure mode keeps its hashing and counted collections. Emptying a map or set in place now keeps its kind, because the wrapper keeps expecting it.

Interaction with 0.6.0.

  • Files written by xitdb-clj 0.5.0 and earlier also have header ID 0, like Java files. The new file format check rejects them in every mode before their header ID is read, so they never open in compat mode by mistake.
  • Files with non-SHA-1 key hashes are rejected in both modes, with a message that says so.
  • compact keeps the header, so a compacted database stays in its mode. A full copy during replication brings the source's header, so the replica takes the source's mode, even if it was opened with :mode.

The README documents all of this, including a table of how each kind of file opens with each :mode.

🤖 Generated with Claude Code

@xeubie

xeubie commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator

Big improvement suggestion: Automatically go into compat mode if the database isn't clj1. This should be fine since we're bumping the database format anyway in #43, so we don't need to worry about it going into compat mode when opening files made by the old xitdb-clj. It won't be able to open them anyway.

codeboost and others added 2 commits November 2, 2026 19:51
xitdb-clj hashes keys with a format tag and refuses files whose header
isn't clj1, so it can't look up keys in files written by the Java library.

`(xit-db path {:mode :compat})` opens those files. The mode is checked
against the header rather than picking the hashing, since a wrong choice
silently loses lookups and creates duplicate keys: clj1 files only open in
Clojure mode and every other file only opens in compat mode. New compat
files get the header ID sha1, which keeps them distinct from old unversioned
Clojure files.

In a non-clj1 file, string keys hash as raw UTF-8, as Java's put(String)
does. Writing any other key or set member throws, reading one finds nothing
and removing one does nothing. Native maps and sets are written plain so
Java's ReadHashMap and ReadHashSet can read them.

Clojure mode keeps its hashing and counted collections. Emptying a map or
set in place now keeps its kind, because the wrapper keeps expecting it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An existing file now opens in the mode its header records: clj1 in Clojure
mode, any other header in compat mode, so files written by other languages
open without passing :mode. New files are still created as clj1 unless
{:mode :compat} is given. An explicit :mode is still checked against the
header of an existing file, for code that relies on one mode.

Add db-mode, which returns :clojure or :compat for an open database.

Old unversioned xitdb-clj files have header ID 0 like Java files, but the
file format check from xitdb-java 0.46.0 rejects them before their header
ID is read, so they never open in compat mode.

A full copy during replication brings the source's header, so the replica
takes the source's mode. Document this, and test it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
map-contains-key? was the one read path still hashing through db-key-hash
and catching its exception; it now uses db-lookup-hash like the other
lookups, keeping the catch for key types no file can hold.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@codeboost
codeboost merged commit 4f1b0b2 into master Nov 3, 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.

2 participants