flatdb - a file-backed JSON store
import "flatdb.j" as flatdb;
A small JSON document store: load a file once into a value-semantic handle, query and edit it in memory through JSON Pointer, and write it back with a crash-atomic whole-file replace. Built from json (the data) and fs (the file); runs on either binary.
What it is - and isn't
flatdb is not a database engine; it is crash-atomic snapshotting of small data:
- Atomicity - whole-file, via a temp file + rename. A reader sees either the whole old file or the whole new one, never a torn write.
- Consistency - application-level (you decide what's valid).
- Isolation - optimistic, with an opt-in lock. Each writer reads the whole file;
savefails loudly if it changed sinceopen(no silent lost update between flatdb writers), andflatdb.updateadds a cross-process advisory lock so concurrent writers serialize with no loss. No multi-key transactions. - Durability - the data is fsynced before the atomic rename and the directory after it (
fs.syncDir), so a save that returned survives a crash. On Windows, which has no directory flush, only the data is fsynced.
For a real database, use a client over net (e.g. redis). flatdb is for embedding a small store - config, a readable cache, a benchmark history, a little app's saved state - where a single human-readable JSON file fits.
Handle, not a connection
A module holds no mutable state and spawn deep-copies scope, so a store can't be a shared open connection - it's a value you hold:
use io;
use json;
import "flatdb.j" as flatdb;
# flatdb.DB is { path as string, data as json.Value, version as string }
def db as flatdb.DB init flatdb.openString('{"n": 1}');
io.printf("path [%s], n %d\n", $db.path, json.asInt(flatdb.get($db, "/n")));Reading verbs leave the DB untouched; writing verbs return a fresh DB (thread it through, the same shape lists / maps / json use); save is the only side effect.
Surface
| Call | Returns | |
|---|---|---|
flatdb.open(path) | DB | Load the file (an empty store if it's absent, so first run never fails). |
flatdb.openWith(path, maxBytes) | DB | open, refusing a file over maxBytes (kind "limit"; a maxBytes below 1 is kind "flatdb") before decoding it; at most maxBytes + 1 bytes are read. For a file another party controls. |
flatdb.openString(text) | DB | Load from an in-memory JSON string - a read-only DB with no backing file (save throws). For a store fetched over the network or embedded in the program. |
flatdb.get(db, pointer) | json.Value | The sub-document at a JSON Pointer ("" = the whole document). |
flatdb.getOr(db, pointer, fallback) | json.Value | The node at the pointer, or fallback when nothing is there (a malformed pointer still throws). |
flatdb.has(db, pointer) | bool | Whether the pointer resolves. |
flatdb.keys(db, pointer) | list of string | Keys of the object at the pointer, in document order. |
flatdb.length(db, pointer) | int | Element / entry count at the pointer. |
flatdb.set(db, pointer, value) | DB | Upsert an object key / replace a list index (strict: no auto-vivify). |
flatdb.append(db, pointer, value) | DB | Push onto the list at the pointer (create it first with set). |
flatdb.remove(db, pointer) | DB | Drop the key / element at the pointer. |
flatdb.save(db) | DB | Write back atomically (temp + rename) and return a rebound DB (thread it - $db = flatdb.save($db) - to save again). Throws for a read-only DB, or a flatdb conflict if the file changed since open. |
flatdb.saveWith(db, opts) | DB | save with SaveOptions: pretty (indented JSON) and backup (keep the previous content as <path>.bak). Start from flatdb.saveDefaults(). |
flatdb.saveAs(db, path) | DB | Write the document to path and return a fresh DB bound to path (db unchanged). First dump for an openString DB, or a copy / new version of an on-disk one. |
flatdb.update(path, transform) | DB | Locked read-modify-write: take the lock, open the current doc, apply transform (a func(db as DB) -> DB), save, release (via defer). The safe path when concurrent writers may touch one store - no lost update. |
flatdb.updateWith(path, waitMs, transform) | DB | update with your own lock wait bound in milliseconds. |
flatdb.lock(dbPath) / flatdb.unlock(lk) | Lock / null | The raw advisory lock (cross-process, over fs.writeNew) behind update, for a multi-step transaction. Waits up to 5 s for a held lock. Prefer update. |
flatdb.lockWith(dbPath, waitMs) | Lock | lock with your own wait bound in milliseconds (0 = try once); for a writer that must queue behind a slow one. |
value is any JSON value - a json.Value. Build objects and lists with json.map() / json.list() (then json.set / json.append into them), and scalars with json.decode (json.decode("42"), json.decode("\"hi\"")). Addressing is JSON Pointer, identical to json's.
Example
use io;
use json;
import "flatdb.j" as flatdb;
def db as flatdb.DB init flatdb.open("state.json"); # empty on first run
$db = flatdb.set($db, "/runs", json.list());
def rec as json.Value init json.map();
$rec = json.set($rec, "/cpu", "Ryzen 5 7600X3D");
$rec = json.set($rec, "/ms", 118);
$db = flatdb.append($db, "/runs", $rec);
flatdb.save($db); # atomic replace
def store as flatdb.DB init flatdb.open("state.json");
io.printf("%d runs; first on %s\n",
flatdb.length($store, "/runs"),
json.asString(flatdb.get($store, "/runs/0/cpu")));A runnable version is examples/modules/flatdb_demo.j.
Hand-edited files and backups
saveWith takes a SaveOptions; start from saveDefaults() (compact, no backup) and set what you need:
use json;
import "flatdb.j" as flatdb;
def db as flatdb.DB init flatdb.set(flatdb.open("state.json"), "/n", json.decode("1"));
def opts as flatdb.SaveOptions init flatdb.saveDefaults();
$opts.pretty = true; # indented, for a config file people edit
$opts.backup = true; # keep the previous content as state.json.bak
$db = flatdb.saveWith($db, $opts);The backup is exactly the content the conflict check read, written crash-atomically before the store is replaced, with the store's own permissions (never an older backup's wider ones). A first save, with no previous file, writes none, and a save that fails its conflict check writes neither. One backup generation is kept. save, saveAs and update write compact JSON with no backup, so a pretty file saved through them comes out compact.
Read-only from a URL or a string
flatdb.openString(text) loads a store from an in-memory JSON string instead of a file. flatdb never imports http / net (it stays fs-only and builds on both binaries), so you bring the bytes. To read a database published at a URL:
use io;
use json;
import "http.j" as http;
import "flatdb.j" as flatdb;
def resp as http.Response init http.get("https://example.com/config.json", {});
def db as flatdb.DB init flatdb.openString($resp.body); # read-only
io.printf("theme: %s\n", json.asString(flatdb.get($db, "/ui/theme"), ""));The result has no backing path, so every reader works and the mutating verbs still return fresh in-memory copies, but save throws (kind: "flatdb") - there is nowhere local to write. To persist it (or fork any store to a new file), use saveAs:
use json;
import "flatdb.j" as flatdb;
def db as flatdb.DB init flatdb.openString('{"ui": {"theme": "light"}}');
def edited as flatdb.DB init flatdb.set($db, "/ui/theme", json.decode("\"dark\""));
def local as flatdb.DB init flatdb.saveAs($edited, "config.local.json");
flatdb.save($local); # now writable - $local is bound to config.local.jsonsaveAs(db, path) writes to path and returns a fresh DB bound to that path; the db you passed in is unchanged (value semantics, like set / append / remove). After a saveAs you hold both; the next save writes whichever handle you keep: reassign ($db = flatdb.saveAs($db, path);) to make the new file current, or keep the old handle to keep writing the original file. There is no hidden "active file" state.
Decoding untrusted input is safe against a nesting bomb: json.decode caps container depth (a deeply-nested payload is a catchable error, not a fatal stack overflow), and JSON has no entity/alias expansion to amplify size. Bound the download itself with http's body cap (64 MiB by default; set maxBytes in http.send's Options for more or less).
Concurrent writers
Crash-atomic is not write-safe: two writers that each open, edit different parts, and save would clobber one change (last rename wins). flatdb handles this two ways:
save fails loudly on a conflict. open records a SHA-256 of the text it decoded; save re-hashes the file and throws a flatdb error if the file moved since, so a lost update is never silent. Catch it and re-open, or retry:
use json;
import "flatdb.j" as flatdb;
def db as flatdb.DB init flatdb.set(flatdb.open("state.json"), "/n", json.decode("2"));
try {
flatdb.save($db);
} catch (e) {
# someone else wrote first - re-open, re-apply, retry
}A rewrite with identical content is not a conflict, since nothing would be lost. The check is content-based, so a second write in the same mtime tick is caught too. It runs twice: once before anything is written, and again after the new file is written and synced, under a short save lock (<path>.savelock), which save holds only for that last check and the rename. No other save can replace the file in between, so a lost update is always loud, never silent. The save lock is separate from the write lock lock / update take, so a writer that holds the write lock can still save. A save that crashed while holding it leaves the file behind; the next save breaks it after 10 s and goes through. The check guards against other save calls only: a program that writes the file without flatdb is not seen.
flatdb.update makes it impossible. It brackets the whole read-modify-write in a cross-process advisory lock (built on fs.writeNew, an atomic exclusive create), so concurrent writers serialize and no update is lost. transform is a func(db as DB) -> DB:
use json;
use convert;
import "flatdb.j" as flatdb;
func addVisit(db as flatdb.DB) {
def n as int init 0;
if (flatdb.has($db, "/visits")) { $n = json.asInt(flatdb.get($db, "/visits")); }
return flatdb.set($db, "/visits", json.decode(convert.toString($n + 1)));
}
flatdb.update("state.json", addVisit); # locked read -> edit -> save -> releaseThe lock coordinates across separate processes (a CLI and a server, two CLI runs) as well as spawned tasks - a symlink-free lock file created with fs.writeNew, whose holder id and timestamp let a lock leaked by a crashed writer be broken after a stale window (120 s). The timestamp is taken when the lock is acquired, not when the wait began; a lock file with no readable holder (a writer that crashed between creating it and writing its id) ages by its mtime instead. There is no lease renewal, so transform must finish well inside 120 s, or a waiter breaks the lock while it still runs. update refuses a transform that returns a DB bound to another file, which would write outside the lock. Waiters that find the same stale lock break it one at a time, under an exclusive .lock.break file, and remove the lock only while it still holds the holder they judged stale - so a lock a faster waiter has already re-taken is never removed. A waiter gives up after 5 s (flatdb.lockWith sets another bound) with a flatdb error naming the current holder. A lock file that cannot be created at all - a missing directory, a read-only mount, a data directory owned by another user - has no holder to wait for, so lock / update fail at once with the create's own error (cannot create the lock file ...: permission denied) instead of reporting contention after the wait (a dangling symbolic link left at the lock path is named as such). unlock checks and removes in one step, so it never removes a lock that a stale-break handed to someone else. flatdb.lock / flatdb.unlock are the raw primitives for a multi-step transaction, but prefer update, which releases on every exit path.
A note on save returning a DB: save returns the document rebound to the just-written file, so if you save the same handle more than once, thread the return - $db = flatdb.save($db); - or the next save sees the file it wrote as a conflict.
Many readers: load once, share the parsed tree
An opened store is a parsed json.Value tree, which takes from about the file's size (text-heavy data) up to roughly 15 times it (many small values) in memory. A server should therefore not open the store in every request handler (N requests in flight would hold N parsed copies). Open it once and share it:
- Read-only for the server's lifetime - a top-level
defis enough.def store as flatdb.DB init flatdb.open("registry.json");and handlers read$store. Passing theDBaround, or capturing it in aspawn, copies only the small struct around the tree; the tree itself is immutable and shared. - Updated while serving - publish each version through a
kvstore, which holds an opaque value without copying it, and rebuild theDBaround it where you need one:
use kv;
import "flatdb.j" as flatdb;
def cache as kv.Store init kv.open();
def first as flatdb.DB init flatdb.open("registry.json");
kv.setValue($cache, "registry", $first.data, 0);
# Readers (any handler / task): the current tree, no parse, no copy.
func current() {
return flatdb.DB{ path: "registry.json", data: kv.getValue($cache, "registry"), version: "" };
}
# The writer: update, save, and publish the new tree.
func publish(db as flatdb.DB) {
def saved as flatdb.DB init flatdb.save($db);
kv.setValue($cache, "registry", $saved.data, 0);
}A reader DB built this way is for reading: its empty version makes a save of it fail the conflict check, so writes go through a DB from flatdb.open (or flatdb.update). See kv values.
Atomic save, in detail
save creates a uniquely named sibling path + ".tmp." + uuid (empty, already carrying the target's permissions - 0600 for a new store), writes the encoded document to it, fsyncs it, fs.renames it over the target and fsyncs the directory (fs.syncDir), which makes the new name durable. On POSIX the rename is atomic, so a concurrent reader never sees a half-written file, and the document is never readable under a wider mode than the store's. A failed save removes its temp file; if the process dies mid-save, the original file is untouched and only a stray .tmp.* file remains (each save uses a new name).
See also
json- the value model and write surfaceflatdblayers over.fs- the file I/O (readString/writeString/rename) behindopen/save.redis- a real store, over the network, when you outgrow a single file.
Academy talks
| Talk | What it shows |
|---|---|
| A JSON store on disk with flatdb | a JSON file store with atomic saves and locked updates |
| Invoicing with web services: VAT, SOAP and GraphQL | a cache of VAT check results |
API reference
Every exported function, struct and constant of flatdb, with its signature, generated from the module's doc comments: the flatdb API reference.