diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 3ecfa06..ac492bf 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -1,7 +1,8 @@ -name: check - -on: push - +on: + pull_request: + push: + branches: + - master jobs: lint: runs-on: ubuntu-latest @@ -10,9 +11,9 @@ jobs: uses: actions/checkout@v2 - name: setup-zig - uses: mlugg/setup-zig@v1 + uses: mlugg/setup-zig@v2 with: - version: 0.14.0 + version: 0.15.1 - name: lint run: | @@ -27,13 +28,11 @@ jobs: steps: - name: checkout uses: actions/checkout@v2 - with: - submodules: recursive - name: setup-zig - uses: mlugg/setup-zig@v1 + uses: mlugg/setup-zig@v2 with: - version: 0.14.0 + version: 0.15.1 - name: test run: | diff --git a/README.md b/README.md index 04c3784..1c7a2ef 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Build and use RocksDB in zig. # Build Dependencies -`rocksdb-zig` is pinned to [Zig `0.13`](https://ziglang.org/download/), so you will need to have it installed. +`rocksdb-zig` is pinned to [Zig `0.15`](https://ziglang.org/download/), so you will need to have it installed. # Usage diff --git a/build.zig b/build.zig index 8122297..b7a48c7 100644 --- a/build.zig +++ b/build.zig @@ -16,11 +16,7 @@ pub fn build(b: *Build) void { }); bindings_mod.addImport("rocksdb", rocksdb_mod); - const tests = b.addTest(.{ - .target = target, - .optimize = optimize, - .root_source_file = b.path("src/lib.zig"), - }); + const tests = b.addTest(.{ .root_module = bindings_mod }); const test_step = b.step("test", "Run bindings tests"); tests.root_module.addImport("rocksdb", rocksdb_mod); test_step.dependOn(&b.addRunArtifact(tests).step); diff --git a/build.zig.zon b/build.zig.zon index c46aebe..6a1b215 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -8,6 +8,7 @@ .hash = "N-V-__8AAAKhcQL5PtDzqfPNCjk1LJCK8svO8KtC-f_3bj6z", }, }, + .minimum_zig_version = "0.15.0", .paths = .{ "build.zig", "build.zig.zon", diff --git a/src/data.zig b/src/data.zig index 8cf989c..2dc0d0d 100644 --- a/src/data.zig +++ b/src/data.zig @@ -6,19 +6,14 @@ const Allocator = std.mem.Allocator; /// data that was allocated by rocksdb and must be freed by rocksdb pub const Data = struct { data: []const u8, - free: *const fn (?*anyopaque) callconv(.C) void, + free: *const fn (?*anyopaque) callconv(.c) void, - pub fn deinit(self: @This()) void { + pub fn deinit(self: Data) void { self.free(@ptrCast(@constCast(self.data.ptr))); } - pub fn format( - self: @This(), - comptime _: []const u8, - options: std.fmt.FormatOptions, - writer: anytype, - ) !void { - try std.fmt.formatBuf(self.data, options, writer); + pub fn format(self: Data, writer: *std.io.Writer) !void { + try writer.print("{any}", .{self}); } }; diff --git a/src/database.zig b/src/database.zig index 2c35070..bf8def9 100644 --- a/src/database.zig +++ b/src/database.zig @@ -246,14 +246,18 @@ pub const DB = struct { return ri; } - pub fn liveFiles(self: *const Self, allocator: Allocator) Allocator.Error!std.ArrayList(LiveFile) { + pub fn liveFiles(self: *const Self, allocator: Allocator) Allocator.Error![]const LiveFile { const files = rdb.rocksdb_livefiles(self.db).?; + defer rdb.rocksdb_livefiles_destroy(files); const num_files: usize = @intCast(rdb.rocksdb_livefiles_count(files)); - var livefiles = std.ArrayList(LiveFile).init(allocator); + + var livefiles: std.ArrayList(LiveFile) = .empty; + defer livefiles.deinit(allocator); + var key_size: usize = 0; for (0..num_files) |i| { const file_num: c_int = @intCast(i); - try livefiles.append(.{ + try livefiles.append(allocator, .{ .allocator = allocator, .column_family_name = try copy(allocator, rdb.rocksdb_livefiles_column_family_name(files, file_num)), .name = try copy(allocator, rdb.rocksdb_livefiles_name(files, file_num)), @@ -265,8 +269,8 @@ pub const DB = struct { .num_deletions = rdb.rocksdb_livefiles_deletions(files, file_num), }); } - rdb.rocksdb_livefiles_destroy(files); - return livefiles; + + return try livefiles.toOwnedSlice(allocator); } pub fn propertyValueCf( @@ -506,15 +510,17 @@ test DB { var err_str: ?Data = null; defer if (err_str) |e| e.deinit(); runTest(&err_str) catch |e| { - std.debug.print("{}: {?}\n", .{ e, err_str }); + std.debug.print("{}: {?f}\n", .{ e, err_str }); return e; }; } fn runTest(err_str: *?Data) !void { + const allocator = std.testing.allocator; + { var db, const families = try DB.open( - std.testing.allocator, + allocator, "test-state", .{ .create_if_missing = true, @@ -527,7 +533,7 @@ fn runTest(err_str: *?Data) !void { err_str, ); defer db.deinit(); - defer std.testing.allocator.free(families); + defer allocator.free(families); const a_family = families[1].handle; _ = try db.put(a_family, "hello", "world", err_str); @@ -553,7 +559,7 @@ fn runTest(err_str: *?Data) !void { } var db, const families = try DB.open( - std.testing.allocator, + allocator, "test-state", .{ .create_if_missing = true, @@ -566,9 +572,12 @@ fn runTest(err_str: *?Data) !void { err_str, ); defer db.deinit(); - defer std.testing.allocator.free(families); - const lfs = try db.liveFiles(std.testing.allocator); - defer lfs.deinit(); - defer for (lfs.items) |lf| lf.deinit(); - try std.testing.expect(std.mem.eql(u8, "another", lfs.items[0].column_family_name)); + defer allocator.free(families); + + const lfs = try db.liveFiles(allocator); + defer { + for (lfs) |lf| lf.deinit(); + allocator.free(lfs); + } + try std.testing.expect(std.mem.eql(u8, "another", lfs[0].column_family_name)); }