Danilo Fragoso 10 달 전
부모
커밋
6d4faa6c49
4개의 변경된 파일66개의 추가작업 그리고 12개의 파일을 삭제
  1. 1 1
      hashing.zig
  2. 32 0
      index.zig
  3. 19 6
      persistence.zig
  4. 14 5
      storage.zig

+ 1 - 1
hashing.zig

@@ -1,7 +1,7 @@
 const std = @import("std");
 
 pub fn hashKey(k: []const u8) u32 {
-    return murmur3(k);
+    return xxhash32(k);
 }
 
 pub fn xoramasrosas(k: []const u8) u32 {

+ 32 - 0
index.zig

@@ -0,0 +1,32 @@
+const std = @import("std");
+const storage = @import("storage");
+
+var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
+const allocator = arena.allocator();
+
+const MAX_CHILDREN = 1024;
+
+const RadixNode = struct {
+    children: [MAX_CHILDREN]?*RadixNode,
+    eof: bool,
+    prefix: []const u8,
+};
+
+var root: RadixNode = .{
+    .children = [_]?*RadixNode{null} ** MAX_CHILDREN,
+    .eof = false,
+    .prefix = &[_]u8{},
+};
+
+fn commonPrefixLen(a: []const u8, b: []const u8) usize {
+    var len: usize = 0;
+    const min_len = if (a.len < b.len) a.len else b.len;
+
+    while (len < min_len) : (len += 1) {
+        if (a[len] != b[len]) {
+            break;
+        }
+    }
+
+    return len;
+}

+ 19 - 6
persistence.zig

@@ -9,6 +9,11 @@ const allocator = arena.allocator();
 
 var mutex: std.Thread.Mutex = .{};
 
+const OPCode = enum {
+    W,
+    D,
+};
+
 pub fn init() !void {
     try std.Thread.Pool.init(&thread_pool, .{ .allocator = allocator, .n_jobs = 1024 });
 
@@ -38,22 +43,30 @@ pub fn init() !void {
         if (record.len == 0) {
             continue;
         }
+
         record_count += 1;
         std.debug.print("Restoring record N:{d}\r", .{record_count});
 
-        var kv = std.mem.splitScalar(u8, record, '|');
-        const key = kv.first();
-        const value = kv.rest();
+        var recordIterator = std.mem.splitScalar(u8, record, '|');
+
+        const opcode = recordIterator.first();
+        const key = recordIterator.next() orelse continue;
+        const value = recordIterator.next() orelse continue;
 
-        _ = storage.writeVolatile(key, value);
+        const opcodeEnum = std.meta.stringToEnum(OPCode, opcode) orelse continue;
+
+        switch (opcodeEnum) {
+            .W => _ = storage.writeVolatile(key, value),
+            .D => _ = storage.deleteVolatile(key),
+        }
     }
     std.debug.print("Restored {d} records from persistence", .{record_count});
     return;
 }
 
-pub fn persist(key: []const u8, value: []const u8) void {
+pub fn persist(opcode: u8, key: []const u8, value: []const u8) void {
     mutex.lock();
-    const record = std.fmt.allocPrint(allocator, "{s}|{s}\r", .{ key, value }) catch {
+    const record = std.fmt.allocPrint(allocator, "{c}|{s}|{s}\r", .{ opcode, key, value }) catch {
         std.debug.print("Failed to format record for persistence\n", .{});
         mutex.unlock();
         return;

+ 14 - 5
storage.zig

@@ -52,7 +52,7 @@ pub fn write(key: []const u8, value: []const u8) bool {
         return false;
     }
 
-    persistence.persist(entry.?.key, entry.?.value);
+    persistence.persist('W', entry.?.key, entry.?.value);
     return true;
 }
 
@@ -72,10 +72,7 @@ pub fn read(key: []const u8) ?[]const u8 {
     return null;
 }
 
-pub fn delete(key: []const u8) bool {
-    mutex.lock();
-    defer mutex.unlock();
-
+pub fn deleteVolatile(key: []const u8) bool {
     const hash = hashing.hashKey(key);
     const index = hash % buf.len;
 
@@ -102,3 +99,15 @@ pub fn delete(key: []const u8) bool {
 
     return false;
 }
+
+pub fn delete(key: []const u8) bool {
+    mutex.lock();
+    defer mutex.unlock();
+
+    const deleted = deleteVolatile(key);
+    if (deleted) {
+        persistence.persist('D', key, "");
+    }
+
+    return deleted;
+}