2
0

persistence.zig 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const std = @import("std");
  2. const storage = @import("storage.zig");
  3. const MAX_PERSISTENCE_SIZE = 10_000_000 * 100;
  4. var storage_file: ?std.fs.File = null;
  5. var thread_pool: std.Thread.Pool = undefined;
  6. var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
  7. const allocator = arena.allocator();
  8. const c_allocator = std.heap.c_allocator;
  9. var mutex: std.Thread.Mutex = .{};
  10. const OPCode = enum {
  11. W,
  12. D,
  13. };
  14. pub fn init() !void {
  15. try std.Thread.Pool.init(&thread_pool, .{ .allocator = allocator, .n_jobs = 4 });
  16. const cwd = std.fs.cwd();
  17. storage_file = cwd.openFile(".db", .{ .mode = .read_write }) catch |err| {
  18. if (err == std.fs.File.OpenError.FileNotFound) {
  19. std.debug.print("No persisted data found, starting fresh...\n", .{});
  20. storage_file = cwd.createFile(".db", .{ .read = true }) catch |ierr| {
  21. std.debug.print("Failed to create storage file: {any}\n", .{ierr});
  22. return;
  23. };
  24. std.debug.print("Created new storage file .db\n", .{});
  25. }
  26. return;
  27. };
  28. const storage_data = storage_file.?.readToEndAlloc(c_allocator, MAX_PERSISTENCE_SIZE) catch |err| {
  29. std.debug.print("Failed to read storage file: {any}\n", .{err});
  30. return;
  31. };
  32. defer c_allocator.free(storage_data);
  33. var records = std.mem.splitScalar(u8, storage_data, '\r');
  34. var record_count: usize = 0;
  35. while (records.next()) |record| {
  36. if (record.len == 0) {
  37. continue;
  38. }
  39. record_count += 1;
  40. std.debug.print("Restoring record N:{d}\r", .{record_count});
  41. var recordIterator = std.mem.splitScalar(u8, record, '|');
  42. const opcode = recordIterator.first();
  43. const key = recordIterator.next() orelse continue;
  44. const value = recordIterator.next() orelse continue;
  45. const opcodeEnum = std.meta.stringToEnum(OPCode, opcode) orelse continue;
  46. switch (opcodeEnum) {
  47. .W => _ = storage.writeVolatile(key, value),
  48. .D => _ = storage.deleteVolatile(key),
  49. }
  50. }
  51. std.debug.print("Restored {d} records from persistence", .{record_count});
  52. storage_file.?.close();
  53. storage_file = cwd.openFile(".db", .{ .mode = .write_only }) catch |err| {
  54. std.debug.print("Failed to reopen storage file in append mode: {any}\n", .{err});
  55. return;
  56. };
  57. try storage_file.?.seekFromEnd(0);
  58. return;
  59. }
  60. pub fn persist(opcode: u8, key: []const u8, value: []const u8) void {
  61. const record = std.fmt.allocPrint(c_allocator, "{c}|{s}|{s}\r", .{ opcode, key, value }) catch {
  62. std.debug.print("Failed to format record for persistence\n", .{});
  63. return;
  64. };
  65. thread_pool.spawn(persistRecord, .{record}) catch |err| {
  66. std.debug.print("Failed to spawn persistence job: {any}\n", .{err});
  67. return;
  68. };
  69. }
  70. fn persistRecord(record: []const u8) void {
  71. defer c_allocator.free(record);
  72. mutex.lock();
  73. defer mutex.unlock();
  74. _ = storage_file.?.write(record) catch |err| {
  75. std.debug.print("Failed to write record to storage file: {any}\n", .{err});
  76. return;
  77. };
  78. }