Guides

Storage formats and files

XML, JSON or compressed binary - what each format is for, how to choose one, what the files in a database folder are, and how to convert between formats.

The format is a setting. Your code, queries and transactions are the same whichever you pick.

The three formats

Format Files you can read Best at Trade-off
XML (default) yes the fastest row reads larger than binary
JSON yes working with JSON tools and pipelines larger on disk, slower on a cold read
Binary no (Brotli-compressed) the smallest files: archives, devices each row read decompresses its page

The benchmarks page compares all three, operation by operation.

Choosing a format

For a new database opened without StorageFormat, the file extension picks the format:

Extension Format
.xod XML
.xodj JSON
.xodb Binary
using var xml = new XodContext("data/app.xod");
using var json = new XodContext("data/app.xodj");

A path that doesn’t end in one of these names a folder, creating data.xod (XML) inside it unless StorageFormat is set.

For an existing database, XOD writes in the format recorded in its database file (the format it was created in or converted to), unless you override it explicitly with DatabaseOptions.StorageFormat:

var options = new DatabaseOptions { StorageFormat = StorageFormat.Binary };
using var db = new XodContext("data/app.xod", options: options);

An undefined StorageFormat value throws ArgumentOutOfRangeException. Each file is always read in the format it was written in, and db.StorageFormat reports the active write format.

What’s in the folder

A database is a folder of small files. For a database app.xod with Customer and Order tables:

File What it holds
app.xod the database file: its format, settings, and property encryption key (when configured)
Customer.xtab, Order.xtab one per table: the table’s pages, autonumber high-water mark, and <Schema> element (kept current by every write so xod query can read tables without your classes)
<id>.xpag the pages of rows (512 KB each by default, see PageSize)
Customer.xidx, Order.xidx the table’s indexes (rebuilt automatically if missing or stale)
transaction.xwal a transaction’s undo journal while a commit is in progress
<file>.tmp a file being atomically written beside its target before moving into place
app.xod.lock, xod.lock exclusive lock files for the database and its folder; they stay in the folder after close
app.xod.<id>.xidxdirty open-writer marker tracking whether indexes have caught up with pages (removed on clean close)
app.xod.xpwlog, <file>.xpwnew transient files created while a password change is in progress

Leave these files alone while the database is open. Because every open holds the database exclusively, it automatically tidies anything a crash left behind before any read or write: undoing an interrupted commit, finishing or discarding an interrupted password change, deleting unnamed pages a crash left behind, and rebuilding indexes whose writer stopped mid-flight. Crash recovery touches only this database’s own files, leaving unrelated files in the folder untouched.

Every writer writes pages before tables and deletes emptied or dropped pages afterwards, so a crash never leaves a table naming a missing page. A missing page file named by a table, or an index entry whose row isn’t on its page, throws DatabaseFileException. Opening a missing database with InitialCreate = false fails immediately without creating lock files.

To back up a database, close the context and copy the folder. To version it, commit the folder: with XML or JSON, git diff shows the rows that changed.

One database per folder

A folder holds one database: tables are named by their type, so two database files in one folder would share them. You can open a database by passing its folder path instead of its file; XOD opens the database file in it, or creates data.xod.

Opening a folder that holds more than one database file (.xod, .xodj, .xodb), or a path naming a new database file beside an existing one, throws XodException. Move each database to its own folder. Creating a new database holds the folder lock exclusively until the database file is written, so two concurrent opens can’t create two databases in the same empty folder.

Read-only folders

When a folder can’t be written and has no lock files (such as read-only media), XOD opens the database for reading only without holding a lock. Writes throw XodException explaining why. If crash leftovers are present in such a folder, opening throws XodException because recovery requires write access and an exclusive lock.

Because a read-only open without lock files holds no lock, a writer that can open the lock files can still write beside it, and the reader won’t see those writes.

Converting between formats

XodMigrator converts a database from one format to another without needing your model classes:

int files = XodMigrator.ConvertDatabase(
    sourceDirectoryOrFile: "data",
    targetDirectory: "data-binary",
    targetFormat: StorageFormat.Binary);

The converted database keeps its file names and records its new format in the database file, so reopening it requires no StorageFormat option:

using var db = new XodContext("data-binary/app.xod");

Pass password to convert a password-protected database. The command-line tool does the same with xod convert (see Command-line tool).

  • Exclusive access: refused with DatabaseLockedException while any context or process has the source open; no context can open the source until conversion finishes.
  • Crash recovery: recovers interrupted commits, password changes, or crash-staged database files in the source before converting.
  • Clean target: the target must be a new or empty folder (XodException otherwise), and an undefined StorageFormat throws ArgumentOutOfRangeException.
  • Crash-safe output: converting to another folder publishes the target whole (a crash leaves a .<target>.converting-<id> staging folder you can delete). Passing the source folder as the target converts in place file by file, leaving the database openable if interrupted.

Page size

DatabaseOptions.PageSize (default 512 KB) sets how many bytes of rows go in each page file. With a warm cache, page size makes little difference to lookups; larger pages speed up bulk loads, and very small pages (64 to 128 KB) make indexed queries much slower. Keep the default unless you’ve measured.

Source on GitHub · Report a problem with this page