Sfoglia il codice sorgente

install wal option

Danilo Fragoso 8 mesi fa
parent
commit
98241ac766
2 ha cambiato i file con 19 aggiunte e 0 eliminazioni
  1. 8 0
      main.zig
  2. 11 0
      persistence.zig

+ 8 - 0
main.zig

@@ -24,6 +24,7 @@ const PORT = 8085;
 var should_exit = std.atomic.Value(bool).init(false);
 var active_connections = std.atomic.Value(u32).init(0);
 var redis_mode = false;
+var instant_wal_mode = false;
 
 fn handleSignal(sig: c_int) callconv(.c) void {
     _ = sig;
@@ -38,6 +39,8 @@ pub fn main() !void {
     while (args.next()) |arg| {
         if (std.mem.eql(u8, arg, "-redis")) {
             redis_mode = true;
+        } else if (std.mem.eql(u8, arg, "-iwal")) {
+            instant_wal_mode = true;
         }
     }
 
@@ -65,6 +68,11 @@ pub fn main() !void {
     storage.init();
     try persistence.init();
 
+    if (instant_wal_mode) {
+        persistence.setInstantWal(true);
+        std.debug.print("Instant WAL mode enabled\n", .{});
+    }
+
     while (!should_exit.load(.seq_cst)) {
         var poll_fds = [_]posix.pollfd{
             .{

+ 11 - 0
persistence.zig

@@ -12,6 +12,7 @@ var mutex: std.Thread.Mutex = .{};
 
 var write_buffer: [BUFFER_SIZE]u8 = undefined;
 var buffer_position: usize = 0;
+var instant_wal: bool = false;
 
 const OPCode = enum {
     W,
@@ -74,6 +75,10 @@ pub fn init() !void {
     return;
 }
 
+pub fn setInstantWal(enabled: bool) void {
+    instant_wal = enabled;
+}
+
 pub fn persist(opcode: u8, key: []const u8, value: []const u8) void {
     const record_len = 1 + 1 + key.len + 1 + value.len + 1;
 
@@ -132,6 +137,12 @@ pub fn persist(opcode: u8, key: []const u8, value: []const u8) void {
     pos += 1;
 
     buffer_position = pos;
+
+    if (instant_wal) {
+        flushBuffer() catch |err| {
+            std.debug.print("Failed to flush buffer in instant WAL mode: {any}\n", .{err});
+        };
+    }
 }
 
 pub fn flush() !void {