A minimal, idiomatic Zig wrapper around RocksDB's C API.
- Zig fetch
zig fetch --save=rocksdb git+https://github.com/chainsafe/rocksdb.zig-
In your
build.zig, link the dependencyconst rocksdb_module = b.dependency("rocksdb", .{}).module("rocksdb"); my_module.addImport("rocksdb", rocksdb_module);
-
Import and use in your code:
const rocksdb = @import("rocksdb");
Here's a simple example to get you started:
const std = @import("std");
const rocksdb = @import("rocksdb");
pub fn main() !void {
// Open database
var db = try rocksdb.Database.open("/tmp/testdb", .{});
defer db.close();
// Put a key-value pair
try db.put("hello", "world");
// Get the value
const value = try db.get("hello");
if (value) |v| {
std.debug.print("Value: {s}\n", .{v});
// IMPORTANT: Free the memory allocated by LevelDB
rocksdb.free(v.ptr);
}
}var db = try Database.open(db_path, .{});
defer db.close();
var batch = try WriteBatch.init();
defer batch.deinit();
batch.put("a", "one");
batch.put("b", "two");
try db.write(&batch);-
Fetch the latest version via following command.
zig fetch --save=rocksdb https://github.com/facebook/rocksdb/archive/refs/tags/v10.5.1.zip
-
Go the errors file and take a look at the error status.
-
Update the version macros in build.zig
We recommend to use style guide from TigerBeetle