From: patrick-scho Date: Sun, 20 Oct 2024 16:28:38 +0000 (+0200) Subject: inital commit X-Git-Url: https://gitweb.ps.run/ziglmdb/commitdiff_plain/1adf3709493825c0d304efb99ba520740d8d69e3?ds=sidebyside inital commit --- 1adf3709493825c0d304efb99ba520740d8d69e3 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..576c295 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lmdb"] + path = lmdb + url = ./lmdb/ diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..65bca19 --- /dev/null +++ b/build.zig @@ -0,0 +1,79 @@ +const std = @import("std"); + +// Although this function looks imperative, note that its job is to +// declaratively construct a build graph that will be executed by an external +// runner. +pub fn build(b: *std.Build) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard optimization options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not + // set a preferred release mode, allowing the user to decide how to optimize. + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "lmdb", + // In this case the main source file is merely a path, however, in more + // complicated build scripts, this could be a generated file. + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + + exe.addIncludePath(.{ .path = "./lmdb/libraries/liblmdb" }); + + exe.addCSourceFiles(.{ .files = &.{ + "./lmdb/libraries/liblmdb/midl.c", + "./lmdb/libraries/liblmdb/mdb.c", + } }); + + exe.linkLibC(); + + // This declares intent for the executable to be installed into the + // standard location when the user invokes the "install" step (the default + // step when running `zig build`). + b.installArtifact(exe); + + // This *creates* a Run step in the build graph, to be executed when another + // step is evaluated that depends on it. The next line below will establish + // such a dependency. + const run_cmd = b.addRunArtifact(exe); + + // By making the run step depend on the install step, it will be run from the + // installation directory rather than directly from within the cache directory. + // This is not necessary, however, if the application depends on other installed + // files, this ensures they will be present and in the expected location. + run_cmd.step.dependOn(b.getInstallStep()); + + // This allows the user to pass arguments to the application in the build + // command itself, like this: `zig build run -- arg1 arg2 etc` + if (b.args) |args| { + run_cmd.addArgs(args); + } + + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build run` + // This will evaluate the `run` step rather than the default, which is "install". + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + + // Creates a step for unit testing. This only builds the test executable + // but does not run it. + const unit_tests = b.addTest(.{ + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + + const run_unit_tests = b.addRunArtifact(unit_tests); + + // Similar to creating the run step earlier, this exposes a `test` step to + // the `zig build --help` menu, providing a way for the user to request + // running the unit tests. + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&run_unit_tests.step); +} diff --git a/lmdb b/lmdb new file mode 160000 index 0000000..9c9d345 --- /dev/null +++ b/lmdb @@ -0,0 +1 @@ +Subproject commit 9c9d34558cc438f99aebd1ab58f83fd7faeabc0a diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..ce9cde9 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,90 @@ +const std = @import("std"); +const lmdb = @cImport(@cInclude("lmdb.h")); + +const print = std.debug.print; + +pub fn Lmdb(comptime KeySize: comptime_int) type { + _ = KeySize; + return struct { + pub fn init(comptime path: []const u8) @This() { + var res: @This() = undefined; + + _ = lmdb.mdb_env_create(&res.env); + // mdb_env_set_maxreaders(env, 1); + // mdb_env_set_maxdbs(env, 1); // named databases + // mdb_env_set_mapsize(env, 1024*1024); + + _ = lmdb.mdb_env_open(res.env, path.ptr, 0, 0o664); + // /*MDB_FIXEDMAP |MDB_NOSYNC |MDB_NOSUBDIR*/ + + _ = lmdb.mdb_txn_begin(res.env, null, 0, &res.txn); + _ = lmdb.mdb_dbi_open(res.txn, null, 0, &res.dbi); + + return res; + } + + pub fn deinit(self: @This()) void { + _ = lmdb.mdb_txn_commit(self.txn); + _ = lmdb.mdb_dbi_close(self.env, self.dbi); + _ = lmdb.mdb_env_close(self.env); + } + + pub fn get(self: @This(), comptime T: type, key: []const u8) ?T { + var k = lmdb.MDB_val{ + .mv_data = @ptrFromInt(@intFromPtr(key.ptr)), + .mv_size = key.len, + }; + + var v: lmdb.MDB_val = undefined; + + const res = lmdb.mdb_get(self.txn, self.dbi, &k, &v); + + if (res == 0 and v.mv_size == @sizeOf(T)) { + if (v.mv_data) |data| { + return @as(*T, @ptrFromInt(@intFromPtr(data))).*; + } + } + + return null; + } + + pub fn put(self: @This(), comptime T: type, key: []const u8, val: T) void { + var k = lmdb.MDB_val{ + .mv_data = @ptrFromInt(@intFromPtr(key.ptr)), + .mv_size = key.len, + }; + + var v = lmdb.MDB_val{ + .mv_data = @ptrFromInt(@intFromPtr(&val)), + .mv_size = @sizeOf(T), + }; + + const res = lmdb.mdb_put(self.txn, self.dbi, &k, &v, 0); + _ = res; + + // return val; + } + + env: ?*lmdb.MDB_env, + dbi: lmdb.MDB_dbi, + txn: ?*lmdb.MDB_txn, + }; +} + +pub fn main() !void { + var db = Lmdb(16).init("./db"); + defer db.deinit(); + + var testKey = [_]u8{0} ** 16; + @memcpy(testKey[0..5], "abcde"); + // @memcpy(testKey[5..10], "abcde"); + + const u_1 = db.get(u8, &testKey); + print("u1: {?}\n", .{u_1}); + + var u_2 = db.get(u8, "abcde" ++ "12345"); + db.put(u8, "abcde" ++ "12345", u_2.? + 1); + + u_2 = db.get(u8, "abcde" ++ "12345"); + print("u2: {?}\n", .{u_2}); +}