C reference¶
The engine is a C library: zig build produces zig-out/lib/libzypherdb.a (static),
libzypherdb.dylib or libzypherdb.so (shared), and the header zig-out/include/zypherdb.h. C, C++ and
any language with a C foreign-function interface can embed ZypherDB through it. Python does not need
it, since the Python package has its own native module.
Strings are passed as
(pointer, length)pairs in UTF-8; they do not need a terminating NUL.A context (
zypherdb_context *) and a result (zypherdb_result *) are opaque handles. Release them withzypherdb_closeandzypherdb_result_free.
A first program¶
#include <zypherdb.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
const char *path = argc > 1 ? argv[1] : "helix-db";
zypherdb_context *db = zypherdb_open(path, strlen(path));
if (db == NULL) return 1;
const char *create = "CREATE (:Person {name: 'Ana', badge: 101}), (:Person {name: 'Luis', badge: 102})";
zypherdb_result *created = zypherdb_query(db, create, strlen(create));
zypherdb_result_free(created);
const char *find = "MATCH (p:Person) WHERE p.badge = $badge RETURN p.name, p.badge";
const char *parameters = "{\"badge\": 102}";
zypherdb_result *found = zypherdb_query_with_parameters(db, find, strlen(find), parameters, strlen(parameters));
for (size_t row = 0; row < zypherdb_result_row_count(found); row++) {
size_t length;
const char *name = zypherdb_result_string(found, row, 0, &length);
printf("%.*s has badge %lld\n", (int)length, name, (long long)zypherdb_result_int(found, row, 1));
}
zypherdb_result_free(found);
const char *bad = "MATCH (p:Person) RETURN q";
if (zypherdb_query(db, bad, strlen(bad)) == NULL) {
size_t length;
const char *message = zypherdb_last_error(db, &length);
printf("error %d: %.*s\n", zypherdb_error_code(db), (int)length, message);
}
zypherdb_close(db);
return 0;
}
Luis has badge 102
error 2: unknown variable 'q'
Build it against the library (any C compiler works; zig cc is one):
zig cc -Izig-out/include first.c -Lzig-out/lib -lzypherdb -o first
Opening and closing¶
zypherdb_context *zypherdb_open(const char *path, size_t path_len);
zypherdb_context *zypherdb_open_with_durability(const char *path, size_t path_len, int durability);
int zypherdb_sync(zypherdb_context *context);
int zypherdb_set_import_directory(zypherdb_context *context, const char *path, size_t path_len);
void zypherdb_close(zypherdb_context *context);
zypherdb_open opens the database in directory path, creating the database if it does not exist;
the directory itself must exist. It returns NULL on failure. zypherdb_open uses full durability,
and zypherdb_open_with_durability chooses the mode:
Mode |
A commit returns when… |
A power failure can lose… |
|---|---|---|
|
its data is on disk |
nothing |
|
its data is on disk; its bookkeeping follows with the next commit |
the last commit |
|
it is in memory; the operating system writes it, or |
the commits since the last |
zypherdb_set_import_directory sets the directory LOAD CSV reads from (path_len 0: none, which
turns LOAD CSV off, as it is after opening): a query’s file:///name.csv is name.csv in it,
and nothing outside it can be read.
No mode can corrupt the database. zypherdb_sync writes every commit so far to disk and returns
ZYPHERDB_OK or ZYPHERDB_STORAGE_ERROR.
Errors¶
const char *zypherdb_last_error(zypherdb_context *context, size_t *out_len);
int zypherdb_error_code(zypherdb_context *context);
When a query fails, these two functions describe the failure: the message, and its category. The message stays valid until the next call on the context.
Code |
Constant |
Meaning |
|---|---|---|
0 |
|
no error |
1 |
|
the query is not valid Cypher |
2 |
|
the query uses a variable it never bound |
4 |
|
an operator or function got a value of the wrong type |
5 |
|
an expression is not allowed where it is |
6 |
|
|
7 |
|
the storage failed |
8 |
|
a |
9 |
|
integer overflow or division by zero |
10 |
|
a write would break a uniqueness constraint |
11 |
|
|
12 |
|
a replica that forwards writes cannot forward this one (replication) |
13 |
|
a forwarded write did not run: the master could not be reached, or turned it away |
14 |
|
a forwarded write was sent, then the connection was lost or the time ran out: it may have run or not |
Code 3 is reserved: it was once “missing property”, and a missing property now reads as null.
Queries¶
zypherdb_result *zypherdb_query(zypherdb_context *context, const char *query, size_t query_len);
zypherdb_result *zypherdb_query_with_parameters(zypherdb_context *context, const char *query, size_t query_len,
const char *parameters_json, size_t parameters_len);
void zypherdb_result_free(zypherdb_result *result);
A query runs one openCypher statement (see the reference) as one transaction, and returns
its result, or NULL if it fails. The result owns all of its values; it stays valid until
zypherdb_result_free, whatever happens to the database meanwhile.
Parameters are a JSON object, one entry per $name:
{"badge": 102, "zones": ["A", "B"], "limits": {"max": 4}, "at": {"~datetime": "2025-03-10T09:00+01:00[Europe/Madrid]"}}
JSON numbers without a fraction or exponent are integers; the rest are floats. Temporal values are tagged objects, the same as in JSON results.
Prepared statements. zypherdb_query keeps what it prepares (parse, checks, plan) for the 64
query texts used most recently, so a text run again with other parameters skips that work. To keep
one regardless, prepare it:
zypherdb_statement *zypherdb_prepare(zypherdb_context *context, const char *query, size_t query_len);
zypherdb_result *zypherdb_statement_query(zypherdb_context *context, zypherdb_statement *statement,
const char *parameters_json, size_t parameters_len);
void zypherdb_statement_free(zypherdb_statement *statement);
size_t zypherdb_statement_column_count(const zypherdb_statement *statement);
const char *zypherdb_statement_column_name(const zypherdb_statement *statement, size_t column, size_t *out_len);
size_t zypherdb_statement_parameter_count(const zypherdb_statement *statement);
const char *zypherdb_statement_parameter_name(const zypherdb_statement *statement, size_t parameter, size_t *out_len);
zypherdb_prepare returns NULL when the query does not parse or check (the error is the
context’s). zypherdb_statement_query runs it as zypherdb_query_with_parameters would
(parameters_json may be NULL); a statement is planned again when the label or relationship-type
counts it was planned on change at least twofold (and by 100 or more). A statement belongs to the
context it was prepared with: free it before closing that context, and use it from one thread at a
time, as the context.
Several queries in one transaction.
int zypherdb_write_many(zypherdb_context *context, size_t count, const char *const *queries,
const size_t *query_lens, const char *const *parameters_json,
const size_t *parameters_lens, zypherdb_result **out_results);
size_t zypherdb_error_statement(zypherdb_context *context);
zypherdb_write_many runs count queries in order, in one transaction: each sees what the ones
before it wrote, and they commit together or, if one fails, none does. parameters_json (or any
entry of it) may be NULL: no parameters. It returns 0 and fills out_results[0..count] (free
each), or 1 and fills nothing; zypherdb_error_statement then gives the index of the query that
failed (SIZE_MAX: none did, the commit failed). A batch holds queries only: no schema command,
EXPLAIN, PROFILE or LOAD CSV (ZYPHERDB_ERROR_INVALID_EXPRESSION). On a replica that
forwards writes, a batch that writes goes to the master as one request.
Plans. EXPLAIN query and PROFILE query are queries too: their result is the plan, one row
per operator, with the columns depth, operator, details, estimated_rows, rows and
time_ms (see EXPLAIN and PROFILE). PROFILE runs the query and
commits its writes; its own rows are not returned.
Reading a result¶
Cell by cell¶
size_t zypherdb_result_row_count(const zypherdb_result *result);
size_t zypherdb_result_column_count(const zypherdb_result *result);
int zypherdb_result_type(const zypherdb_result *result, size_t row, size_t col);
int64_t zypherdb_result_int(const zypherdb_result *result, size_t row, size_t col);
double zypherdb_result_float(const zypherdb_result *result, size_t row, size_t col);
bool zypherdb_result_bool(const zypherdb_result *result, size_t row, size_t col);
const char *zypherdb_result_string(const zypherdb_result *result, size_t row, size_t col, size_t *out_len);
zypherdb_result_type gives a cell’s type, or -1 for a row or column out of range. Each reader
returns the cell’s value when the cell has that type, and 0, false or the empty string when it
has another. Unlike zypherdb_result_type, the readers do not check the range: call them only with a
row and column inside the result.
Type |
Constant |
Read with |
|---|---|---|
0 |
|
|
1 |
|
|
2 |
|
|
3 |
|
(no value) |
4 |
|
the list readers below, or JSON |
5 |
|
JSON |
6 |
|
JSON |
7 |
|
JSON |
8 |
|
JSON |
9 |
|
|
10–15 |
|
JSON |
The list readers read the elements of a list cell whose elements are integers, floats, booleans or strings:
size_t zypherdb_result_list_length(const zypherdb_result *result, size_t row, size_t col);
int zypherdb_result_list_item_type(const zypherdb_result *result, size_t row, size_t col, size_t index);
int64_t zypherdb_result_list_item_int(const zypherdb_result *result, size_t row, size_t col, size_t index);
double zypherdb_result_list_item_float(const zypherdb_result *result, size_t row, size_t col, size_t index);
bool zypherdb_result_list_item_bool(const zypherdb_result *result, size_t row, size_t col, size_t index);
const char *zypherdb_result_list_item_string(const zypherdb_result *result, size_t row, size_t col, size_t index,
size_t *out_len);
Results as JSON¶
const char *zypherdb_result_to_json(const zypherdb_result *result, size_t *out_len);
This function returns the whole result as one JSON array of rows, each row an array of values. It reads any value, and it is the fastest way to read a large result. The text belongs to the result.
#include <zypherdb.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
zypherdb_context *db = zypherdb_open(argv[1], strlen(argv[1]));
const char *query = "CREATE (d:Door {name: 'Cleanroom', floors: [2, 3]}) RETURN d, $at + duration('PT30M'), 1.0";
const char *parameters = "{\"at\": {\"~datetime\": \"2025-03-10T09:00+01:00[Europe/Madrid]\"}}";
zypherdb_result *result = zypherdb_query_with_parameters(db, query, strlen(query), parameters, strlen(parameters));
size_t length;
const char *json = zypherdb_result_to_json(result, &length);
printf("%.*s\n", (int)length, json);
zypherdb_result_free(result);
zypherdb_close(db);
return 0;
}
[[{"~node":{"id":0,"labels":["Door"],"properties":{"floors":[2,3],"name":"Cleanroom"}}},{"~datetime":"2025-03-10T09:30+01:00[Europe/Madrid]"},1.0]]
Values that JSON has no form for are tagged objects, with a key that starts with ~:
Value |
JSON |
|---|---|
node |
|
relationship |
|
path |
|
date, local time, time, local date-time, date-time, duration |
|
A float always has a
.or an exponent (1.0), and its special values are writtenNaN,Infinityand-Infinity.A node or relationship shows its state when the query finished. A node the query deleted keeps its id but has no labels or properties.
Direct writes¶
uint64_t zypherdb_allocate_node_id(zypherdb_context *context);
int zypherdb_add_node(zypherdb_context *context, uint64_t id, const char *label, size_t label_len);
int zypherdb_add_edge(zypherdb_context *context, uint64_t source, uint64_t target, const char *type, size_t type_len);
int zypherdb_set_property_int64(zypherdb_context *context, uint64_t id, const char *name, size_t name_len, int64_t value);
int zypherdb_set_property_float64(zypherdb_context *context, uint64_t id, const char *name, size_t name_len, double value);
int zypherdb_set_property_string(zypherdb_context *context, uint64_t id, const char *name, size_t name_len,
const char *value, size_t value_len);
int zypherdb_begin_write(zypherdb_context *context);
int zypherdb_commit_write(zypherdb_context *context);
void zypherdb_abort_write(zypherdb_context *context);
These functions write without parsing a query, for loading data in bulk:
zypherdb_allocate_node_idreturns a node id never handed out before, orUINT64_MAXon failure.zypherdb_add_nodecreates a node with that id and label if it does not exist, and adds the label if it does. An empty label adds none. Ids chosen by the caller are reserved, sozypherdb_allocate_node_idnever returns them.zypherdb_add_edgeadds a relationship fromsourcetotarget; each call adds one, even between nodes already joined by a relationship of that type.zypherdb_set_property_*set a node’s property.
Each function returns ZYPHERDB_OK, ZYPHERDB_STORAGE_ERROR, ZYPHERDB_CONSTRAINT_VIOLATION when a
uniqueness constraint refuses the write (zypherdb_last_error says which), ZYPHERDB_INVALID_NAME
when a label, relationship type or property name is empty, longer than 1024 bytes or contains a 0x00
byte (an empty label in zypherdb_add_node means none), ZYPHERDB_TOO_LARGE when a value is over
64 MiB stored (a node with all its properties) or a node would have more than 65535 labels, or ZYPHERDB_NODE_NOT_FOUND when a node it
names does not exist.
On its own, each call is a transaction that waits for the disk. Between zypherdb_begin_write and
zypherdb_commit_write, all the calls form one transaction instead, committed at the end with a
single disk write; zypherdb_abort_write undoes it. Do not run queries while such a transaction is
open.
Exporting the graph¶
const char *zypherdb_export_json(zypherdb_context *context, size_t *out_len);
This function returns the whole graph as JSON, read straight from storage, or NULL on failure:
{"nodes": [{"id": 0, "labels": ["Door"], "properties": {"name": "Cleanroom"}}],
"relationships": [{"id": 0, "source": 0, "target": 1, "type": "LEADS_TO", "properties": {}}]}
The text belongs to the context, and stays valid until the next export or zypherdb_close. It is
meant for tests and debugging, since it reads everything.
Checking the database¶
const char *zypherdb_check(zypherdb_context *context, size_t *out_len);
This function checks that every index, adjacency key and counter agrees with the stored records
(see storage), and returns the report as JSON, or NULL on
failure:
{"ok": false, "problem_count": 1, "problems": ["missing key lDoor\\x00..."]}
problems describes the first 32 problems found. The text belongs to the context, and stays
valid until the next check or zypherdb_close.
Warning. It is a diagnostic, not a routine call. Run it only with a reason: a query that misses data it should find or counts that do not add up (a suspected bug in ZypherDB), to be sure after a crash, or to attach its output to a bug report. It reads the whole database and holds memory in proportion to it, so on a large database it is slow and heavy; it repairs nothing. A problem it reports is a bug in ZypherDB: please report it with the output.
Replication¶
zypherdb_context *zypherdb_open_replicated(const char *path, size_t path_len, int durability, int role,
const char *address, size_t address_len, const char *token,
size_t token_len, const char *name, size_t name_len,
bool forward_writes, uint64_t request_timeout_ms, bool encrypted);
bool zypherdb_result_visible(const zypherdb_result *result);
uint64_t zypherdb_last_lsn(zypherdb_context *context);
bool zypherdb_wait_for(zypherdb_context *context, uint64_t lsn, uint64_t timeout_ms);
const char *zypherdb_replication_status(zypherdb_context *context, size_t *out_len);
zypherdb_open_replicated opens the database with a replication role (see
replication): ZYPHERDB_ROLE_MASTER (0) serves replicas at address
("host:port", the host an IP address or a name), and ZYPHERDB_ROLE_REPLICA (1) follows the master at address and refuses
writes. token is the secret master and replicas share, and may not be empty. With encrypted the
conversation is encrypted, and the token never travels; both sides must agree. name (at most 255 bytes) is what a replica calls
itself in its master’s status; NULL takes the name of its host, and a master ignores it. A
replica opened with forward_writes sends the statements that write to its master instead of
refusing them, each taking at most request_timeout_ms (0: 30 s); zypherdb_result_visible is
false for a forwarded write the replica had not applied in that time. The
context keeps its own copy of address, token and name. It returns NULL when the database cannot be opened or replication cannot start (the
address is taken, the token is empty, a database that was a master or holds data is opened as a replica, or the name is too long). While another process
runs replication on the database the context stands by ("standby": true in its status) and
takes over when that process closes it or dies. Closing the context
stops replication.
Warning. Without
encrypted, the token and the data travel in clear: run replication only on a trusted network, or inside a VPN or a tunnel. See replication.
zypherdb_last_lsn is the number of the last change the database has: on a master the last
committed, on a replica the last applied, also in a context opened without a role; 0 on a
database that is neither. zypherdb_wait_for waits until the master’s change lsn is applied, at
most timeout_ms, and returns whether it is: to read on a replica a write made on the master, pass
it the master’s zypherdb_last_lsn after the write. A replica opened with a role waits for its
first copy too, whatever lsn. It works in a context opened without a role too, and returns true
at once on a database that is no replica.
zypherdb_replication_status returns how replication goes as JSON, or NULL without replication.
A master’s:
{"role": "master", "standby": false, "address": "0.0.0.0:7000", "last_lsn": 120,
"replicas": [{"name": "eu-1", "address": "10.0.0.7:51234", "connected_ms": 86400000,
"phase": "streaming", "acked_lsn": 118, "lag": 2}]}
A replica’s (phase is "connecting", "copying", "streaming" or "halted";
last_contact_ms is how long ago the master was last heard from, null if never; halted and
last_refusal are a reason’s name, such as "token", or null; master_unresolved is true when
the last try found no address for the master’s host name):
{"role": "replica", "standby": false, "name": "eu-1", "master_address": "10.0.0.5:7000",
"phase": "streaming", "last_contact_ms": 120, "applied_lsn": 118, "master_lsn": 120, "lag": 2,
"master_unresolved": false, "last_refusal": null, "halted": null}
The text belongs to the context, and stays valid until the next status or zypherdb_close.
Threads and processes¶
A context is a session: one thread uses it at a time. For another thread, open another session on
the same database with zypherdb_open_session(context); it takes the context’s import directory,
and its replication role, if any. Sessions run at once: reads in parallel, never waiting for a
write; writes take turns, one write transaction at a time (a zypherdb_begin_write holds the
writer until its commit or abort). A session reads its own writes, and waits for no other
session unless it writes too. Close each session with zypherdb_close: the database closes with
the last one, whichever it is.
zypherdb_context *db = zypherdb_open(path, strlen(path));
zypherdb_context *worker = zypherdb_open_session(db); /* hand it to another thread */
/* ... */
zypherdb_close(worker);
zypherdb_close(db);
Each process opens the database itself: reads run in parallel across processes, and writes take turns as they do between sessions. Do not open the same directory twice in one process: open sessions instead.
Why this way — and the alternatives¶
A C ABI, not a C++ or Rust API. Every language can call C, and a C ABI does not change with the compiler that built the library. The header is written by hand, so it says exactly what is promised.
Strings as pointer and length. Callers in most languages hold strings with a length and no terminating NUL; requiring one would mean a copy per call, and a NUL inside a string could not be passed at all.
A result that owns its values. A result is copied out of the database before the transaction ends, so it stays valid whatever the database does next, and the caller never holds a pointer into storage. The price is the copy; the gain is that no misuse of a result can corrupt or block the database.
JSON for structured values, not a C struct per type. Nodes, maps, paths and temporals would need many structs and functions to walk them; JSON is one function and every language reads it. The common scalar types still have direct readers, which avoid the JSON round trip.
Status codes and a context’s last error, not callbacks or
errno: every function says whether it failed in its return value, and the message is there to read, in the context, until the next call.