Skip to content

Commit

Permalink
lock on foreach
Browse files Browse the repository at this point in the history
  • Loading branch information
ross96D committed Dec 21, 2024
1 parent 087b8c3 commit 4a412e7
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/db.zig
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ pub const DB = struct {
const result = self.tree.get(key);

if (result) |dataptr| {
// const entry = result.found;
if (value.len == dataptr.size) {
return self._inplace_update(value, dataptr);
}
Expand Down Expand Up @@ -294,13 +293,23 @@ pub const DB = struct {
}
}

/// Calling this function block the database completely.
pub fn for_each(
self: *DB,
allocator: std.mem.Allocator,
T: type,
ctx_: T,
fun: fn (T, []const u8, []const u8) anyerror!bool,
) !void {
try check_is_open(self);
if (!self.deinit_mut.tryLockShared()) {
return error.ClosedDB;
}
defer self.deinit_mut.unlockShared();

self.db_mut.lock();
defer self.db_mut.unlock();

const ctx_t = struct {
db: *DB,
allocator: std.mem.Allocator,
Expand Down

0 comments on commit 4a412e7

Please sign in to comment.