srctree

Gregory Mullen parent b3bb88c8 24ff96df
attempt to auto restore thread when missing

src/types/delta.zig added: 19, removed: 9, total 10
@@ -161,8 +161,6 @@ fn writeOut(self: Delta, writer: *std.io.AnyWriter) !void {
try writer.writeAll(&thread.hash);
}
//try writer.writeAll("\x00");
 
if (self.thread) |t| try t.commit();
}
 
pub fn readFile(a: std.mem.Allocator, idx: usize, file: std.fs.File) !Delta {
@@ -174,7 +172,19 @@ pub fn readFile(a: std.mem.Allocator, idx: usize, file: std.fs.File) !Delta {
pub fn loadThread(self: *Delta, a: Allocator) !*Thread {
if (self.thread != null) return error.MemoryAlreadyLoaded;
const t = try a.create(Thread);
t.* = try Thread.open(a, self.thread_id) orelse return error.UnableToLoadThread;
t.* = Thread.open(a, self.thread_id) catch |err| t: {
std.debug.print("Error loading thread!! {}", .{err});
std.debug.print(" old thread_id {};", .{self.thread_id});
const thread = Thread.new(self.*) catch |err2| {
std.debug.print(" unable to create new {}\n", .{err2});
return error.UnableToLoadThread;
};
std.debug.print("new thread_id {}\n", .{thread.index});
self.thread_id = thread.index;
try self.commit();
break :t thread;
};
 
self.thread = t;
return t;
}
 
src/types/thread.zig added: 19, removed: 9, total 10
@@ -49,7 +49,7 @@ updated: i64 = 0,
delta_hash: HashType = [_]u8{0} ** 32,
hash: HashType = [_]u8{0} ** 32,
 
message_data: ?[]const u8 = null,
message_data: ?[]const u8 = &[0]u8{},
messages: ?[]Message = null,
 
pub fn commit(self: Thread) !void {
@@ -204,7 +204,7 @@ pub fn new(delta: Delta) !Thread {
.created = std.time.timestamp(),
.updated = std.time.timestamp(),
};
 
try thread.writeOut(file.writer().any());
return thread;
}
 
@@ -214,9 +214,9 @@ fn openFile(index: IDType) !std.fs.File {
return try datad.openFile(filename, .{ .mode = .read_write });
}
 
pub fn open(a: std.mem.Allocator, index: usize) !?Thread {
pub fn open(a: std.mem.Allocator, index: usize) !Thread {
const max = currMax() catch 0;
if (index > max) return null;
if (index > max) return error.ThreadDoesNotExist;
 
var file = openFile(index) catch return error.Other;
defer file.close();