Skip to content

Commit 089bd1b

Browse files
committed
wip? this is old, don't remember what it was
1 parent ace5e05 commit 089bd1b

File tree

8 files changed

+128
-39
lines changed

8 files changed

+128
-39
lines changed

build.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ pub fn build(b: *Build) void {
1212
const bindings_mod = b.addModule("bindings", .{
1313
.target = target,
1414
.optimize = optimize,
15-
.root_source_file = b.path("src/lib.zig"),
15+
.root_source_file = b.path("src/root.zig"),
1616
});
1717
bindings_mod.addImport("rocksdb", rocksdb_mod);
1818

1919
const tests = b.addTest(.{
2020
.target = target,
2121
.optimize = optimize,
22-
.root_source_file = b.path("src/lib.zig"),
22+
.root_source_file = b.path("src/root.zig"),
2323
});
2424
const test_step = b.step("test", "Run bindings tests");
2525
tests.root_module.addImport("rocksdb", rocksdb_mod);

src/batch.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const std = @import("std");
22
const rdb = @import("rocksdb");
3-
const lib = @import("lib.zig");
3+
const lib = @import("root.zig");
44

55
const Allocator = std.mem.Allocator;
66

src/database.zig

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const std = @import("std");
22
const rdb = @import("rocksdb");
3-
const lib = @import("lib.zig");
3+
const lib = @import("root.zig");
4+
const private = @import("private.zig");
45

56
const Allocator = std.mem.Allocator;
67
const RwLock = std.Thread.RwLock;
78

9+
const CallHandler = private.errors.CallHandler;
810
const Data = lib.Data;
911
const ErrorHandler = lib.ErrorHandler;
1012
const Iterator = lib.Iterator;
@@ -429,30 +431,6 @@ pub const LiveFile = struct {
429431
}
430432
};
431433

432-
const CallHandler = struct {
433-
/// The error string to pass into rocksdb.
434-
err_str_in: ?[*:0]u8 = null,
435-
/// The user's error string.
436-
handler: ErrorHandler,
437-
438-
fn init(handler: ErrorHandler) CallHandler {
439-
return .{ .handler = handler };
440-
}
441-
442-
fn handle(
443-
self: *CallHandler,
444-
ret: anytype,
445-
comptime err: anytype,
446-
) @TypeOf(err)!@TypeOf(ret) {
447-
if (self.err_str_in) |s| {
448-
self.handler.handle(err, s);
449-
return err;
450-
} else {
451-
return ret;
452-
}
453-
}
454-
};
455-
456434
const CfNameToHandleMap = struct {
457435
allocator: Allocator,
458436
map: std.StringHashMapUnmanaged(ColumnFamilyHandle),

src/errors.zig

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const std = @import("std");
22
const rdb = @import("rocksdb");
3-
const lib = @import("lib.zig");
3+
const lib = @import("root.zig");
44

55
const Data = lib.Data;
66

@@ -43,3 +43,27 @@ pub const ErrorHandler = union(enum) {
4343
}
4444
}
4545
};
46+
47+
pub const CallHandler = struct {
48+
/// The error string to pass into rocksdb.
49+
err_str_in: ?[*:0]u8 = null,
50+
/// The user's error string.
51+
handler: ErrorHandler,
52+
53+
pub fn init(handler: ErrorHandler) CallHandler {
54+
return .{ .handler = handler };
55+
}
56+
57+
pub fn handle(
58+
self: *CallHandler,
59+
ret: anytype,
60+
comptime err: anytype,
61+
) @TypeOf(err)!@TypeOf(ret) {
62+
if (self.err_str_in) |s| {
63+
self.handler.handle(err, s);
64+
return err;
65+
} else {
66+
return ret;
67+
}
68+
}
69+
};

src/iterator.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const std = @import("std");
22
const rdb = @import("rocksdb");
3-
const lib = @import("lib.zig");
3+
const lib = @import("root.zig");
44

55
const Allocator = std.mem.Allocator;
66

src/private.zig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//! This exposes all of the files within the library to be accessible within the
2+
//! library itself. This file should *not* be exposed publicly.
3+
4+
pub const batch = @import("batch.zig");
5+
pub const errors = @import("errors.zig");
6+
pub const data = @import("data.zig");
7+
pub const database = @import("database.zig");
8+
pub const iterator = @import("iterator.zig");
9+
pub const transaction = @import("transaction.zig");
10+
11+
/// The public facing API of the library.
12+
pub const root = @import("root.zig");
13+
14+
test {
15+
const std = @import("std");
16+
std.testing.refAllDecls(@This());
17+
}

src/lib.zig renamed to src/root.zig

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ pub const ErrorHandler = errors.ErrorHandler;
1818

1919
////////////
2020
// private
21-
pub const batch = @import("batch.zig");
22-
const errors = @import("errors.zig");
23-
pub const data = @import("data.zig");
24-
pub const database = @import("database.zig");
25-
pub const iterator = @import("iterator.zig");
21+
const private = @import("private.zig");
2622

27-
test {
28-
const std = @import("std");
29-
std.testing.refAllDecls(@This());
30-
}
23+
const batch = private.batch;
24+
const errors = private.errors;
25+
const data = private.data;
26+
const database = private.database;
27+
const iterator = private.iterator;
28+
const transaction = private.transaction;

src/transaction.zig

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const std = @import("std");
2+
const rdb = @import("rocksdb");
3+
const lib = @import("private.zig");
4+
5+
const Allocator = std.mem.Allocator;
6+
7+
const CallHandler = lib.errors.CallHandler;
8+
const ColumnFamilyHandle = lib.root.ColumnFamilyHandle;
9+
const Data = lib.root.Data;
10+
const ErrorHandler = lib.root.ErrorHandler;
11+
12+
pub const Transaction = struct {
13+
inner: *rdb.rocksdb_transaction_t,
14+
15+
const Self = @This();
16+
17+
pub fn deinit(self: Transaction) void {
18+
rdb.rocksdb_transaction_destroy(self.inner);
19+
}
20+
21+
pub fn commit(
22+
self: Transaction,
23+
error_handler: ErrorHandler,
24+
) error{RocksDBTransactionCommit}!void {
25+
var ch = CallHandler.init(error_handler);
26+
try ch.handle(rdb.rocksdb_transaction_commit(self.inner), error.RocksDBTransactionCommit);
27+
}
28+
29+
pub fn put(
30+
self: *const Self,
31+
column_family: ColumnFamilyHandle,
32+
key: []const u8,
33+
value: []const u8,
34+
error_handler: ErrorHandler,
35+
) error{RocksDBTransactionPut}!void {
36+
var ch = CallHandler.init(error_handler);
37+
try ch.handle(rdb.rocksdb_transaction_put_cf(
38+
self.inner,
39+
column_family,
40+
key.ptr,
41+
key.len,
42+
value.ptr,
43+
value.len,
44+
@ptrCast(&ch.err_str_in),
45+
), error.RocksDBTransactionPut);
46+
}
47+
48+
pub fn get(
49+
self: *const Self,
50+
column_family: ColumnFamilyHandle,
51+
key: []const u8,
52+
error_handler: ErrorHandler,
53+
) error{RocksDBTransactionGet}!Data {
54+
var valueLength: usize = 0;
55+
var ch = CallHandler.init(error_handler);
56+
const value = try ch.handle(rdb.rocksdb_transaction_get_cf(
57+
self.inner,
58+
column_family,
59+
key.ptr,
60+
key.len,
61+
&valueLength,
62+
@ptrCast(&ch.err_str_in),
63+
), error.RocksDBTransactionGet);
64+
if (value == 0) {
65+
return null;
66+
}
67+
return .{
68+
.free = rdb.rocksdb_free,
69+
.data = value[0..valueLength],
70+
};
71+
}
72+
};

0 commit comments

Comments
 (0)