Designing a narrow C ABI for BlazeDB, a Swift-native storage engine

BlazeDB’s Swift API is typed, KeyPath-friendly, and intentionally Swift-shaped. Its C API currently has six functions. That gap is deliberate.

I did not set out to port the Swift API into C. I wanted the smallest useful boundary through which another runtime could use the same storage engine without turning Swift’s type system, query DSL, or object layouts into a permanent interoperability contract. The question that kept coming back was simple: where should Swift stop?

Repo: https://github.com/Mikedan37/BlazeDB

The boundary

The published surface in blazedb.h is:

BlazeDB *blazedb_open(const char *path, const char *password);
void blazedb_close(BlazeDB *db);
BlazeDBResult blazedb_put(BlazeDB *db, const char *key, const void *data, size_t length);
BlazeDBResult blazedb_get(BlazeDB *db, const char *key, void **data, size_t *length);
BlazeDBResult blazedb_delete(BlazeDB *db, const char *key);
void blazedb_free(void *ptr);

BlazeDB is opaque. Keys are UTF-8 NUL-terminated C strings. Values are arbitrary bytes that the library never interprets. Most operations return a small numeric BlazeDBResult. blazedb_open returns NULL on failure.

On the Swift side, the same engine still has room to be expressive: BlazeStorable, KeyPath-oriented queries, observation through BlazeLiveQuery, SwiftUI helpers such as @BlazeQuery, concurrency-aware APIs, and the usual document model. None of that crosses the C boundary.

A tiny contrast:

let db = try BlazeDB.open(at: url, password: "DemoPass123!")
try db.put(key: "job:42", value: Data("queued".utf8))
let value = try db.get(key: "job:42")
BlazeDB *db = blazedb_open("hello.blaze", "DemoPass123!");
blazedb_put(db, "job:42", payload, strlen(payload));

void *data = NULL;
size_t len = 0;
if (blazedb_get(db, "job:42", &data, &len) == BLAZEDB_OK) {
    /* consume bytes */
    blazedb_free(data);
}
blazedb_close(db);

How to use it

Swift apps should keep using the Swift API. The C surface is for other runtimes.

From a clone:

swift build -c release --product BlazeDBC

That produces libBlazeDBC.dylib or libBlazeDBC.so, plus BlazeDBC/include/blazedb.h. The checked-in C sample is Examples/C/hello_blazedb.c. Link the shared library, not the static archive, so the Swift runtime comes in through the dynamic loader:

cd Examples/C
cc -o hello_blazedb hello_blazedb.c \
  -I../../BlazeDBC/include \
  -L../../.build/release -lBlazeDBC \
  -Wl,-rpath,../../.build/release
./hello_blazedb

On Linux, if the loader cannot find Swift runtime libs, add the toolchain’s Swift library path to LD_LIBRARY_PATH or rpath.

Go can call the same six functions through cgo against that header and shared library. BlazeDB currently ships the linking recipe under Examples/Go, not a published Go module.

What I deliberately kept out

I kept typed models, generics, KeyPaths, BlazeDataRecord, associated-value enums, async/await, actors, SwiftUI, live-query observation, and WAL/page internals out of blazedb.h.

Those exclusions are not a missing feature list. They are the point.

Swift generics and KeyPaths do not map cleanly into a C contract. Document records and associated-value enums would freeze Swift-shaped layouts and semantics into something foreign callers start depending on. Observation and callbacks drag foreign-runtime lifetimes into the ABI. Exposing WAL or page structures would freeze implementation details that should remain free to change behind an opaque handle.

The C API is meant to expose storage capabilities, not a serialized copy of the Swift implementation.

Why bytes

The C surface is a byte key/value façade over the same engine. Callers decide serialization. BlazeDB stores and returns opaque buffers.

Under the hood, the Swift byte-KV path still uses internal _kv records and deterministic IDs derived from the UTF-8 key. Foreign callers never see that. That is useful: Swift can evolve storage representation without asking Go, C, or anything else to adopt BlazeDataRecord, Codable, or Swift enums.

Ownership is the real API

Handle ownership and buffer ownership matter more than the function count.

blazedb_open retains a Swift box behind an opaque pointer. blazedb_close consumes that ownership. blazedb_get allocates a C-compatible buffer with malloc. On success, the caller owns the buffer and must release it with blazedb_free. blazedb_free(NULL) is a no-op. Empty values still return a buffer that must be freed.

blazedb_free is as much part of the API design as blazedb_get. Once another language is in the loop, allocator boundaries stop being documentation and become runtime behavior.

Dogfooding the boundary

I did not want the C boundary to exist only as an FFI exercise.

Outside the BlazeDB repository, I built a small Go workload appliance called InfraPanel. The worker daemon opens BlazeDB through cgo against the published blazedb_* symbols, persists job records as JSON under keys like job:{id}, maintains a jobs:index, and repeatedly crosses put/get/delete/free while scheduling work. The Linux cgo path links the shared BlazeDBC library built for aarch64-unknown-linux-gnu, and the project includes deployment tooling for running the worker on Raspberry Pi OS/aarch64. The touchscreen or Mac UI does not open the database file. It talks to the worker over HTTP and polls job state.

That is the useful dogfood lesson: a foreign-language process can treat BlazeDB as durable bytes, keep serialization in Go, and live with explicit get/free ownership. It is not proof of binary compatibility, and I am not claiming formal leak instrumentation or a measured multi-day soak from that setup.

BlazeDB itself currently ships a cgo linking recipe under Examples/Go, not a checked-in Go package or Go CI gate. Those are still open follow-ups.

A platform adapter is not automatically the ABI

There is also an experimental Android/KMM path in tree. It goes through a separate blazedb_bridge_* surface with JSON-oriented helpers and live-query callbacks, validated in CI as packaging scaffolding rather than a published Kotlin SDK.

That experiment reinforced the distinction. A platform adapter can be useful without becoming the long-lived ABI. I do not want every convenient bridge to accidentally promote itself into the compatibility contract.

What is still weak

The header has an add-only compatibility rule for published signatures, and the surface is intentionally opaque and small. I am not treating that policy as proof of binary drop-in stability. The repository does not yet expose a runtime ABI version query, and it does not have a binary compatibility suite.

What CI exercises today is narrower and more honest: Tier 1 tests call the @_cdecl entry points through BlazeDBCSmokeTests. What it does not yet cover is building and linking Examples/C/hello_blazedb.c in the PR gate, or keeping a checked-in Go/cgo smoke green. Open diagnostics are also weak: failed blazedb_open just returns NULL, with no blazedb_last_error yet.

There is no official Go, Kotlin, Rust, or Python SDK.

Open questions

The next practical pieces are fairly small: make the checked-in C consumer part of CI, add a real cgo smoke, and improve diagnostics around failed open. I am deliberately holding off on iterators and richer open-option structures until the ownership and compatibility rules are clearer.

I would be interested in how other people think about these edges:

  1. For Swift libraries that expose FFI, do you prefer a tiny capability/byte ABI or a richer mirror of the Swift API?
  2. What minimum testing would you expect before describing a C ABI as stable?
  3. For a surface this small, what error model has worked well: thread-local last_error, caller-supplied buffers, richer result structs, or something else?
  4. When does a platform-specific bridge become dangerous because people start depending on it as if it were the ABI?
1 Like