ZypherDB¶
An embedded graph database in Zig, speaking openCypher, on LMDB or libmdbx.
ZypherDB is a graph database you embed in your program: no server, no configuration. You store
nodes and relationships, and you query them with openCypher, the language of graph databases —
from Python (pip install zypherdb), or from any language through its C ABI. Under the hood it is a
compact engine written in Zig, with storage on LMDB or libmdbx: transactions, crash safety, readers
that never wait.
Is a graph database the right tool?¶
Use one when the questions are about connections: who reports to whom however deep, which rules apply to this person through their teams, what is reachable from here, what links these two things. In a relational database those are recursive joins; in a graph they are a pattern. If your data is a set of independent records queried by their fields, a relational or document store is the simpler choice.
The tutorial, step by step¶
The tutorial builds one application from an empty directory: the access-control system of a biomedical research lab, where who may enter which room, when, with which training — and who is where right now — is not optional. Each step adds one capability and grows the same database. Every Python block on every page is run by the test suite, so you can copy, paste and run it as it is.
Tutorial
- 1. Your first database
- 2. People: nodes, labels and values
- 3. Teams and rooms: relationships
- 4. Asking precise questions: WHERE
- 5. Badges and parameters
- 6. The staff directory: shaping results
- 7. Reports: counting and grouping
- 8. Changing the lab: SET, REMOVE, DELETE, MERGE
- 9. Onboarding a cohort: UNWIND and WITH
- 10. Who is where: OPTIONAL MATCH and missing data
- 11. Hierarchies: paths of any length
- 12. May this badge open this door? EXISTS
- 13. Documents: lists and maps as properties
- 14. Time: windows, expiry and how long
- 15. A month of swipes: loading data in bulk
- 16. Errors, threads and processes
- 17. Fast queries
- 18. The door controller
- 19. The monitor: rules that are checked, not enforced
- 20. The safety manual: full-text search
- 21. Access reviews: vector search
Examples¶
Complete programs under examples/, built on the same lab: the smallest program that uses ZypherDB,
and a simulated day of the whole access-control system, with door controllers and a monitor in
separate processes. They run in the test suite too.
References¶
References
How it works inside¶
For reading and changing the engine: its design, how data is stored, how a query is parsed, planned and run, and how it is tested.
Reading the engine as a course¶
The technical guides can be read in order as a course on graph databases and systems programming in Zig: each chapter explains one layer, and then you read that layer’s code. The files below are listed in the order to read them. Most are under 500 lines. Learn the language first, from tutorials 01 to 09 and the openCypher reference, so the queries in the guides read easily.
The shape of the whole — architecture. Read
src/root.zig(the public surface),src/value.zig(the value every layer passes around) andsrc/diagnostics.zig(how errors carry a position).Storage: a graph as sorted keys — storage. Read
src/storage/engine/contract.zig(what an engine must provide),src/storage/engine/lmdb.zig,src/storage/key_value_store.zig(transactions and prefix scans),src/storage/key_encoding.zigandsrc/storage/value_encoding.zig(the bytes),src/storage/node_record.zig, thensrc/storage/graph.zig(the graph operations, each with its cost). Finish withsrc/storage/integrity.zig.The language: text to a tree — language. Read
src/query/lexer.zig,src/query/ast.zig(the arena tree),src/query/parser.zigandsrc/query/expression_parser.zig(recursive descent, one function per precedence level), thensrc/query/semantics.zig(the checks before planning).Planning and execution: a tree to rows — planning and execution. Read
src/planning/Plan.zig(operators in chains),src/planning/builder.zig,src/planning/patterns.zig(anchors, seeks, estimates),src/planning/expression_program.zig, thensrc/execution/Executor.zig,src/execution/bindings.zig(a row as slots),src/execution/operators.zig,src/execution/pattern_matching.zigandsrc/execution/expressions.zig. Thensrc/statement/Statement.zigandsrc/statement/Cache.zig(plan once, run many times).Where the time goes — performance. It has no new files: it goes back to the code of chapters 2 to 4 and names its known costs.
How the engine tests itself — testing. Its last section, Reading the code, lists the files in order, from
src/storage/integrity.zigtosrc/storage/layout.zig.Changing it — contributing: the build, the principles, and recipes for adding a clause, a function or an index.
The storage, language, planning and testing guides end with a “Why this way — and the alternatives” section. Read that section after the code: it makes sense only once you know the code. The architecture guide gives its reasons at the start, because they explain the layers that follow.