|
@@ -6,6 +6,7 @@ var storage_file: ?std.fs.File = null;
|
|
|
var thread_pool: std.Thread.Pool = undefined;
|
|
var thread_pool: std.Thread.Pool = undefined;
|
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
|
const allocator = arena.allocator();
|
|
const allocator = arena.allocator();
|
|
|
|
|
+const c_allocator = std.heap.c_allocator;
|
|
|
|
|
|
|
|
var mutex: std.Thread.Mutex = .{};
|
|
var mutex: std.Thread.Mutex = .{};
|
|
|
|
|
|
|
@@ -15,14 +16,14 @@ const OPCode = enum {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
pub fn init() !void {
|
|
pub fn init() !void {
|
|
|
- try std.Thread.Pool.init(&thread_pool, .{ .allocator = allocator, .n_jobs = 1024 });
|
|
|
|
|
|
|
+ try std.Thread.Pool.init(&thread_pool, .{ .allocator = allocator, .n_jobs = 4 });
|
|
|
|
|
|
|
|
const cwd = std.fs.cwd();
|
|
const cwd = std.fs.cwd();
|
|
|
storage_file = cwd.openFile(".db", .{ .mode = .read_write }) catch |err| {
|
|
storage_file = cwd.openFile(".db", .{ .mode = .read_write }) catch |err| {
|
|
|
if (err == std.fs.File.OpenError.FileNotFound) {
|
|
if (err == std.fs.File.OpenError.FileNotFound) {
|
|
|
std.debug.print("No persisted data found, starting fresh...\n", .{});
|
|
std.debug.print("No persisted data found, starting fresh...\n", .{});
|
|
|
|
|
|
|
|
- storage_file = cwd.createFile(".db", .{}) catch |ierr| {
|
|
|
|
|
|
|
+ storage_file = cwd.createFile(".db", .{ .read = true }) catch |ierr| {
|
|
|
std.debug.print("Failed to create storage file: {any}\n", .{ierr});
|
|
std.debug.print("Failed to create storage file: {any}\n", .{ierr});
|
|
|
return;
|
|
return;
|
|
|
};
|
|
};
|
|
@@ -32,11 +33,11 @@ pub fn init() !void {
|
|
|
return;
|
|
return;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- const storage_data = storage_file.?.readToEndAlloc(allocator, MAX_PERSISTENCE_SIZE) catch |err| {
|
|
|
|
|
|
|
+ const storage_data = storage_file.?.readToEndAlloc(c_allocator, MAX_PERSISTENCE_SIZE) catch |err| {
|
|
|
std.debug.print("Failed to read storage file: {any}\n", .{err});
|
|
std.debug.print("Failed to read storage file: {any}\n", .{err});
|
|
|
return;
|
|
return;
|
|
|
};
|
|
};
|
|
|
- defer allocator.free(storage_data);
|
|
|
|
|
|
|
+ defer c_allocator.free(storage_data);
|
|
|
var records = std.mem.splitScalar(u8, storage_data, '\r');
|
|
var records = std.mem.splitScalar(u8, storage_data, '\r');
|
|
|
var record_count: usize = 0;
|
|
var record_count: usize = 0;
|
|
|
while (records.next()) |record| {
|
|
while (records.next()) |record| {
|
|
@@ -61,17 +62,22 @@ pub fn init() !void {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
std.debug.print("Restored {d} records from persistence", .{record_count});
|
|
std.debug.print("Restored {d} records from persistence", .{record_count});
|
|
|
|
|
+
|
|
|
|
|
+ storage_file.?.close();
|
|
|
|
|
+ storage_file = cwd.openFile(".db", .{ .mode = .write_only }) catch |err| {
|
|
|
|
|
+ std.debug.print("Failed to reopen storage file in append mode: {any}\n", .{err});
|
|
|
|
|
+ return;
|
|
|
|
|
+ };
|
|
|
|
|
+ try storage_file.?.seekFromEnd(0);
|
|
|
|
|
+
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
pub fn persist(opcode: u8, 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, "{c}|{s}|{s}\r", .{ opcode, key, value }) catch {
|
|
|
|
|
|
|
+ const record = std.fmt.allocPrint(c_allocator, "{c}|{s}|{s}\r", .{ opcode, key, value }) catch {
|
|
|
std.debug.print("Failed to format record for persistence\n", .{});
|
|
std.debug.print("Failed to format record for persistence\n", .{});
|
|
|
- mutex.unlock();
|
|
|
|
|
return;
|
|
return;
|
|
|
};
|
|
};
|
|
|
- mutex.unlock();
|
|
|
|
|
|
|
|
|
|
thread_pool.spawn(persistRecord, .{record}) catch |err| {
|
|
thread_pool.spawn(persistRecord, .{record}) catch |err| {
|
|
|
std.debug.print("Failed to spawn persistence job: {any}\n", .{err});
|
|
std.debug.print("Failed to spawn persistence job: {any}\n", .{err});
|
|
@@ -80,6 +86,8 @@ pub fn persist(opcode: u8, key: []const u8, value: []const u8) void {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fn persistRecord(record: []const u8) void {
|
|
fn persistRecord(record: []const u8) void {
|
|
|
|
|
+ defer c_allocator.free(record);
|
|
|
|
|
+
|
|
|
mutex.lock();
|
|
mutex.lock();
|
|
|
defer mutex.unlock();
|
|
defer mutex.unlock();
|
|
|
|
|
|