Contributing — how to extend the engine¶
The shape of the codebase makes each kind of change follow a fixed recipe. This page is that recipe, the principles the code follows, the build/test loop, and the Zig 0.17 gotchas that have already cost time.
Build and test¶
zig build test # unit tests in src/ + the suite in tests/
SNAP_UPDATE=1 zig build test # rewrite the snapshots that differ (review with git diff)
zig build fuzz -- storage --seeds 100 # the storage fuzzer, from a random seed
zig build fuzz -- faults --seeds 100 # the fault fuzzer: storage that fails on purpose
zig build fuzz -- crash --seeds 100 # the crash fuzzer: a writer killed with SIGKILL
# (CI runs them all on each change and nightly: see testing.md, "Fuzzing in CI")
zig build # static + shared libraries and zypherdb.h into zig-out/
make python # the Python extension module into zig-out/python/
make test-py # Python tests and documentation tests (pip install -e ".[dev]" once)
make tck # the openCypher TCK
make docs # the HTML documentation
make python and the targets that use it find the Python headers through PYTHON (default
python3); point it at a virtual environment with make test-py PYTHON=.venv/bin/python.
Wheels and the sdist come from an in-tree build backend, tools/build_backend/zypherdb_build.py
(declared in the root pyproject.toml): python -m build (or pip wheel .) runs
zig build python in ReleaseSafe, stripped, and packs the module with python/zypherdb into one
cp310-abi3 wheel for the platform. It uses a zig of the version build.zig.zon names, or
$ZIG, or else installs the ziglang package for the build. Linux wheels target glibc 2.17
(manylinux2014), macOS wheels 10.13 (x86_64) and 11.0 (arm64). pip install -e . only puts
python/ on the path; the package then loads the module make python built.
The version comes from git tags, not from pyproject.toml: on a tag v0.2.0 the build is
0.2.0; three commits later, 0.2.1.dev3. A release is a GitHub release with a new tag
vX.Y.Z, its notes the version’s section of CHANGELOG.md (## X.Y.Z — date, moved from
## Unreleased before tagging): publishing it runs .github/workflows/publish.yml, which builds and tests every wheel
and the sdist and uploads them to PyPI.
zig build test runs two executables:
The suite,
tests/. It imports thezypherdbmodule like any client and uses only the public API. It mirrors the source layers:tests/storage/,tests/query/,tests/end_to_end/(whole queries, one file per feature) andtests/c_api_test.zig.tests/support.zigholds the shared helpers: a temporary store, the “people and movies” fixture (loadMoviesGraph), andexpectQuery/expectOrdered/expectQueryError. Most feature work is verified with a fewexpectQuerycalls, not mocks.Unit tests in
src/. Atestblock stays next to its code only when it checks something private and small: the key encoding round trip, the laws ofValue’s order and hash, the generic algorithms, diagnostic truncation.The Python tests,
python/tests/, check the extension module: value conversion both ways, entities, errors, threads.The documentation tests,
python/tests/test_docs.py, run every example indocs/. The tutorial runs as one session, page after page; each other page runs on its own. Atextblock right after an example is its expected output and must match exactly. C examples with amainare compiled withzig ccand run. So a change that alters an output shown in the documentation fails until the page is updated, and every page states what the engine actually does.
A bug fix comes with a regression test that fails without the fix.
The sections below say how to run each kind of test. Why each one exists, what it guarantees and what it cannot see are in testing.
The openCypher TCK¶
make tck runs the openCypher TCK
(vendored in tests/tck/, see its README) and fails if a scenario in tests/tck/baseline.txt no
longer passes; CI does the same. Language work is measured there: pick a reason from
tests/tck/REPORT.md, implement it, run make tck, then record the newly passing scenarios with
python -I tests/tck/runner.py --update-baseline --report tests/tck/REPORT.md in the same commit.
tests/tck/deviations.txt lists the scenarios that ZypherDB answers differently on purpose (map
properties, for instance), each with its reason.
The storage fuzzer and the integrity checker¶
tests/fuzz/storage.zig runs random operations through the Graph API — nodes, labels,
properties of every type, relationships (loops and parallel ones too), commits and aborts,
full-text indexes and uniqueness constraints (as statements), closing and reopening the database —
and applies each to a model in memory (tests/fuzz/model.zig) that predicts the ones that must
fail (a constraint broken, a node still connected, a value that cannot be stored). After every
commit and abort it checks that the database equals the model, and runs the
integrity checker.
A run is determined by its seed. Each seed also picks what it exercises (swarm testing): which operations run and how often, with transactions or without, how many labels and properties, and whether values come from every type or from a narrow few that collide often. A failure prints the last operations, the problem, and the command that repeats it:
zig build fuzz -- storage --seed 42 --steps 1000 --verbose
zig build test runs eight fixed seeds; a long run takes random ones, on each engine:
zig build fuzz -Doptimize=ReleaseSafe -- storage --seeds 200 --steps 2000
zig build fuzz -Doptimize=ReleaseSafe -Dstorage-engine=mdbx -- storage --seeds 200 --steps 2000
At the end it prints how often each operation ran, and how often it failed as the model said it must: a fuzzer that never breaks a constraint does not test them. A new mutation of the graph, or a new kind of failure, gets an operation in the fuzzer and a prediction in the model.
The query fuzzer¶
tests/fuzz/query.zig builds a small random graph and runs generated read queries
(tests/fuzz/query_generator.zig), each compared with itself planned with optimizations off
(zypherdb.PlanOptions), prepared, and split by a random predicate into WHERE p, WHERE NOT p and
WHERE p IS NULL. --steps is the number of queries per seed. Each seed prints a digest of its
results; run the same seeds on both engines and compare the digests:
zig build fuzz -- query --seed 42 --steps 200 --verbose
zig build fuzz -Doptimize=ReleaseSafe -- query --seed 500 --seeds 100
zig build fuzz -Doptimize=ReleaseSafe -Dstorage-engine=mdbx -- query --seed 500 --seeds 100
A new planner optimization gets a switch in PlanOptions, so the fuzzer can compare the plan with
and without it.
The fault fuzzer¶
Every storage error must reach the caller as an error, without a panic, a leak, a transaction left
open or a half-written change. tests/fuzz/faults.zig checks this with a storage engine that fails
on purpose (src/storage/engine/faulty.zig, which wraps the real engine and fails at the N-th
operation). build.zig builds a second ZypherDB module with it (build option fault_injection) for
the fault test and the fuzzers; the library itself never includes it, so it pays nothing for it.
The fuzzer runs a random workload (tests/fuzz/fault_workload.zig: writes, MERGE, deletions,
constraints, full-text indexes and searches, and the query fuzzer’s reads; one-shot or prepared) on
two databases: one that fails and a twin that does not. Each statement runs on the failing database
once per fault point, until a run reaches none. After a failure, the statement must have failed
with a storage error (or with the twin’s own error), the database must be exactly as before it, and
the integrity checker must pass; once no fault is reached, the outcome and the whole database must
equal the twin’s. A seed chooses transient or persistent failures (a dead disk), and whether a
failure breaks the write transaction as a real engine does or leaves it usable: only then does a
write error that the code ignores show up, as a database unlike the twin’s.
zig build fuzz -- faults --seed 42 --steps 40 --verbose
zig build fuzz -Doptimize=ReleaseSafe -Dstorage-engine=mdbx -- faults --seeds 300 --steps 60
A new kind of statement that writes gets a template in the workload.
The crash fuzzer¶
A process that dies must leave a database that opens, passes the integrity checker and holds every
transaction it committed. tests/fuzz/crash.zig starts a writer (the fuzz program itself, run as
crash-writer) that runs a seed’s write workload, one statement per transaction, and reports each
statement done. The writer dies, a few times per seed, in one of two ways:
at a crash point: the failing engine (
src/storage/engine/faulty.zig) has one before each storage operation and one after each commit, and the writer sends itself SIGKILL at the one the fuzzer chose (half of them near a commit). These crashes are reproducible from the seed.killed by the fuzzer a random delay after a report, which can land inside the storage engine (in the middle of a commit’s writes or its sync), but depends on timing.
After each crash the fuzzer reopens the database, runs the integrity checker, and compares it with a twin that ran the same statements without crashing: it must equal the twin after the last statement reported, or after the next one (whose commit may have finished before the kill). The writer then starts again from there; the last one runs to the end and must reach the twin.
zig build fuzz -- crash --seed 42 --steps 80 --verbose
zig build fuzz -Doptimize=ReleaseSafe -Dstorage-engine=mdbx -- crash --seeds 200 --steps 80
On odd seeds another process stays on the database all along, and the writer holds a read open as it works: after each writer the one that stayed must still get the write lock, and its write transactions must have dropped the dead writer’s readers.
A process crash keeps what the operating system already holds, so every durability setting must keep every reported commit. Power failures, which lose what was not synced, are not tested: they need a simulated disk.
The real use test¶
The fuzzers and the simulators find what they model: the simulator has no processes, no TCP and no
bindings. tests/real_use/run.py runs what a user installs instead: the wheel, each node a process
of its own, on TCP. It kills masters, replicas and writers with SIGKILL, cuts and stalls connections
through a proxy, and checks each write against what it was answered: answered committed is there,
answered that it did not run is not, nothing is there twice, no batch is seen in part, and in the
end every replica equals its master. It also watches memory, file descriptors and lag.
tests/real_use/upgrade.py opens with the new version what the latest release on PyPI wrote: a
database closed, one whose writer was killed, one served to a new replica, and a master and replica
upgraded together. Each version has a Python of its own.
Both are too long for every change. Before each release, start the Real use workflow by hand
(gh workflow run real-use.yml --ref <branch>): it builds the wheels and runs both on Linux and
macOS, x86_64 and arm64. It also runs on a pull request that changes these tests. They are POSIX
only, so Windows has only CI’s tests. To run them on your machine:
python -m venv new && new/bin/pip install zypherdb-*.whl
python -m venv old && old/bin/pip install zypherdb
new/bin/python -I tests/real_use/upgrade.py old/bin/python new/bin/python
new/bin/python -I tests/real_use/run.py
Snapshot tests¶
Text that a reader checks as a whole — EXPLAIN and PROFILE plans
(tests/end_to_end/explain_test.zig) and the error messages a caller of the C ABI or Python
reads (tests/error_messages_test.zig) — is compared with a multiline string literal written
right after snap(@src(), ...) (tests/snapshot.zig):
try snap(@src(),
\\Return m.title [estimated 6]
\\NodeByLabelScan (m:Movie) [estimated 4]
).diff(plan_text);
When the text changes, the test prints both versions. SNAP_UPDATE=1 zig build test rewrites
every literal that differs with the new text; read the change with git diff before committing
it. To add a case, write an empty literal (\\ alone on its line) and run with SNAP_UPDATE=1.
If a test crashes during an update, the rest of the run can refuse to rewrite a file that changed
since it was compiled; run the update again.
Benchmarks¶
zig build bench -Doptimize=ReleaseFast measures the engine, and bench/python_overhead.py the
cost the Python layer adds. Results and their conditions are recorded in bench/RESULTS.md, and
known risks in performance. A change that claims a speed-up comes with numbers
from both runs.
A scenario that leaves the baseline needs an explanation in the commit message (for example, an
expected error that used to match only by accident).
Principles¶
The style follows Alexander Stepanov’s generic programming, within Zig’s conventions.
Regular types. A type that is compared, sorted, deduplicated or grouped defines
eql, a totalorderwhen ordering is meaningful, and ahashconsistent witheql, under the std names. Their laws (reflexive, antisymmetric, transitive, total; equal values hash equally) are checked by property tests over random values (seesrc/value.zig).Write an algorithm once. When the same algorithm appears for a second type, make it generic over the type (
comptime T: type) and a context with the operations it needs, following the std.HashMap context protocol (hash(ctx, T) u64,eql(ctx, T, T) bool). Examples:sequence_algorithms.removeDuplicates,aggregation.Grouping,projection.sortByKey. Do not generalize before there are two concrete uses.Costs are part of the interface. A public storage operation states its complexity (see
src/storage/graph.zig). A new operation states its own.Ownership is part of the interface. Say who owns returned memory: an owned copy (
getPropertyallocates with the caller’s allocator) or a view (an iterator’s slices are valid until the nextnext).Errors are values (
error unions), nevercatch unreachableon I/O. Every error that reaches the C ABI or Python carries aDiagnosticmessage and code.No class hierarchies, no Visitor. Data as concrete types, algorithms as free functions. The AST is an arena: no pointers between nodes; build bottom-up and return indices.
Layout and naming¶
One directory per layer:
storage/(engine, keys, graph),query/(text to AST),execution/(AST to rows),temporal/(calendar, zones, temporal types). Shared types and algorithms sit insrc/(value.zig,sequence_algorithms.zig,diagnostics.zig). The embeddings sit apart:src/c_api.zigwithinclude/zypherdb.h, andpython/(the native module inpython/native/, the package inpython/zypherdb/).One responsibility per file, as a guide under ~500 lines of code; functions under ~60 lines. A large
switchover token or node kinds (tokenize,evaluate) is the exception: it reads best in one place.Zig’s file convention: a file that is a struct (top-level fields) is
TitleCase(Executor.zig); any other file issnake_case. Zig cannot spread a struct’s methods over files, so a large struct keeps its state and splits its logic into free functions in other files that take it as the first parameter (expressions.evaluate(executor, …)). Calls go through the module name, so a call site says where the code lives.Descriptive names, no abbreviations (
edge_type,variable_name,children,diagnostic). Kept on purpose:id,src/dst, std’s own names (tmpDir), the LMDB and libmdbx C APIs’ own names inside the bindings, and one-letter names in short loops.
The recipes¶
Add a clause¶
A clause is a NodeKind, a parser rule, an operator and its executor arm.
src/query/ast.zig— add aNodeKindvalue (and aDatapayload if it carries state, e.g.unwind.variable_name). Document its children in the header comment.src/query/parser.zig— add aparse…Clausefunction and call it fromparseQueryin source order.src/planning/— its operator inPlan.Operator, and a case inbuilder.planClausethat adds it to the chain (and the variables it binds to the scope). A writing clause must be listed inast.isUpdatingClauseso the query opens a write transaction and gets itseagers.src/execution/operators.zig— a case infeed, which turns each row the operator receives into the rows it passes on; a writing clause’s work on one row goes inclauses.applyWriteandwrites.zig.src/query/semantics.zig— the variables it binds, if any, so later clauses see them in scope.tests/end_to_end/— queries that use it, in the feature’s file; thenmake tck.docs/guide/cypher.md— its row in the clause table, and a tutorial step if users need it.
Add an expression or operator¶
src/query/ast.zig— aNodeKind(expression) or aBinaryOperatorvalue.src/query/expression_parser.zig— aparseAtombranch (expression), or the loop of the precedence level where the operator belongs (see the language).src/execution/expressions.zig— a case inevaluate(expression) orapplyBinaryOperator(operator), with the null propagation of planning and execution.docs/guide/cypher.md— the operator table.
Add a function¶
Functions need no parser change: a call is parsed by name.
src/execution/functions.zig— an implementation,fn (*Executor, []const Value) ExecutionError!Value, and an entry inbuiltin_entrieswith its number of arguments (fixed(n, f), or aFunctionwith a range). Temporal functions are implemented intemporal_functions.zigand listed intemporal_table.checkCallsthen rejects wrong arities before the query runs.docs/guide/cypher.md— its row in the function table.
Add an index¶
Indexes are just more key ranges (see storage):
src/storage/key_encoding.zig— anappend…Key/append…Prefixpair for the new layout.src/storage/graph.zig— maintain it in the right mutation (setProperty,addEdge, …) and cleanup (deleteNodeOnly,deleteEdgesInDirection), and expose a lookup that returns an iterator (nodesByIndex/edgesByIndexare the models). State its cost.If it should drive planning, surface it in
patterns.zig(estimate,shouldEdgeAnchor) and in the pattern operator that reads it (pattern_matching.zig).src/storage/integrity.zig— rebuild its keys from the primary data, so the checker compares them (until then it reports them as unknown keys); then run the storage fuzzer.
Zig 0.17 gotchas (rediscovered once, recorded here)¶
@cImportis removed → hand-written bindings:extern "c" fn+opaque {}(seesrc/storage/engine/lmdb_bindings.zig), orb.addTranslateCinbuild.zigfor a large header (the Python module translatespython/native/python.h).@intFromEnumbecame@backingInt(zig fmtrewrites it); a struct’s field names are@typeInfo(T).@"struct".field_names.The clock and randomness come from
std.Io(std.Io.Threaded.global_single_threaded.io()where noIois passed in);maintakesstd.process.Init.std.fmt.bufPrintZis gone;std.heap.stackFallbackbecamestd.heap.BufferFirstAllocator.std.fswas reorganized intostd.Io;makePath/realpathAllocno longer exist.std.testing.tmpDir(.{})returns aTmpDir(not an error union); clean up with.cleanup().Allocator.dupeZis gone →dupeSentinel(u8, slice, 0).std.ArrayList(T)is now the unmanaged variant: init with.empty, and pass the allocator toappend(allocator, …)/deinit(allocator).ArrayList.init(allocator)no longer exists.Build API:
b.addModule/b.createModule/b.addLibrary/b.addTest;link_libclives inModule.CreateOptions.str ** n(comptime repetition) does not parse in this version; use@splator@memset.macOS file systems are case-insensitive: renaming
foo.zigtoFoo.zigneeds twogit mvsteps (through a temporary name), or git keeps the old case and Linux builds fail.