srctree

Gregory Mullen parent 4f55f378 dd1b11fa
refactor types & add delta v1

src/database.zig added: 173, removed: 64, total 109
@@ -1,3 +1,5 @@
const std = @import("std");
 
const Types = @import("types.zig");
 
pub const FileSys = struct {
@@ -14,7 +16,7 @@ pub const Options = struct {
 
pub fn init(options: Options) !void {
switch (options.backing) {
.filesys => try Types.init("data"),
.filesys => |fs| try Types.init(try std.fs.cwd().makeOpenPath(fs.dir, .{ .iterate = true })),
}
}
 
 
src/types.zig added: 173, removed: 64, total 109
@@ -15,9 +15,9 @@ pub const Viewers = @import("types/viewers.zig");
 
pub const Writer = std.fs.File.Writer;
 
pub const TypeStorage = std.fs.Dir;
pub const Storage = std.fs.Dir;
 
pub fn init(dir: []const u8) !void {
pub fn init(dir: Storage) !void {
inline for (.{
Comment,
CommitMap,
@@ -30,10 +30,7 @@ pub fn init(dir: []const u8) !void {
Thread,
User,
}) |inc| {
var buf: [2048]u8 = undefined;
const filename = try std.fmt.bufPrint(&buf, inc.TYPE_PREFIX, .{dir});
inc.datad = try std.fs.cwd().makeOpenPath(filename, .{ .iterate = true });
try inc.initType();
try inc.initType(try dir.makeOpenPath(inc.TYPE_PREFIX, .{ .iterate = true }));
}
}
 
 
src/types/comment.zig added: 173, removed: 64, total 109
@@ -17,12 +17,14 @@ const Writer = Types.Writer;
 
const CMMT_VERSION: usize = 0;
 
pub const TYPE_PREFIX = "{s}/messages";
pub const TYPE_PREFIX = "messages";
 
pub var datad: std.fs.Dir = undefined;
var datad: Types.Storage = undefined;
 
pub fn init(_: []const u8) !void {}
pub fn initType() !void {}
pub fn initType(stor: Types.Storage) !void {
datad = stor;
}
 
pub fn raze() void {
datad.close();
 
src/types/commit-map.zig added: 173, removed: 64, total 109
@@ -6,11 +6,15 @@ const endian = builtin.cpu.arch.endian();
 
const Delta = @import("delta.zig");
 
const Types = @import("../types.zig");
 
const COMMITMAP_VERSION: usize = 0;
pub const TYPE_PREFIX = "{s}/commitmap";
pub const TYPE_PREFIX = "commitmap";
pub var datad: std.fs.Dir = undefined;
 
pub fn initType() !void {}
pub fn initType(stor: Types.Storage) !void {
datad = stor;
}
 
pub fn raze() void {}
 
 
src/types/delta.zig added: 173, removed: 64, total 109
@@ -11,11 +11,13 @@ const Template = @import("../template.zig");
 
pub const Delta = @This();
 
const DELTA_VERSION: usize = 0;
pub const TYPE_PREFIX = "{s}/deltas";
pub var datad: Types.TypeStorage = undefined;
const DELTA_VERSION: usize = 1;
pub const TYPE_PREFIX = "deltas";
var datad: Types.Storage = undefined;
 
pub fn initType() !void {}
pub fn initType(stor: Types.Storage) !void {
datad = stor;
}
 
/// while Zig specifies that the logical order of fields is little endian, I'm
/// not sure that's the layout I want to go use. So don't depend on that yet.
@@ -58,6 +60,25 @@ fn readVersioned(a: Allocator, idx: usize, reader: *std.io.AnyReader) !Delta {
},
.tags_id = try reader.readInt(usize, endian),
},
1 => Delta{
.index = idx,
.state = try reader.readStruct(State),
.created = try reader.readInt(i64, endian),
.updated = try reader.readInt(i64, endian),
.repo = try reader.readUntilDelimiterAlloc(a, 0, 0xFFFF),
.title = try reader.readUntilDelimiterAlloc(a, 0, 0xFFFF),
.message = try reader.readUntilDelimiterAlloc(a, 0, 0xFFFF),
.author = try reader.readUntilDelimiterAlloc(a, 0, 0xFFFF),
.thread_id = try reader.readInt(usize, endian),
.attach = switch (Attach.fromInt(try reader.readInt(u8, endian))) {
.nos => .{ .nos = try reader.readInt(usize, endian) },
.diff => .{ .diff = try reader.readInt(usize, endian) },
.issue => .{ .issue = try reader.readInt(usize, endian) },
.commit => .{ .issue = try reader.readInt(usize, endian) },
.line => .{ .issue = try reader.readInt(usize, endian) },
},
.tags_id = try reader.readInt(usize, endian),
},
else => return error.UnsupportedVersion,
};
}
@@ -108,7 +129,7 @@ pub fn commit(self: Delta) !void {
return self.writeOut(&writer);
}
 
pub fn writeOut(self: Delta, writer: *std.io.AnyWriter) !void {
fn writeOut(self: Delta, writer: *std.io.AnyWriter) !void {
try writer.writeInt(usize, DELTA_VERSION, endian);
try writer.writeStruct(self.state);
try writer.writeInt(i64, self.created, endian);
@@ -119,8 +140,8 @@ pub fn writeOut(self: Delta, writer: *std.io.AnyWriter) !void {
try writer.writeAll("\x00");
try writer.writeAll(self.message);
try writer.writeAll("\x00");
//try writer.writeAll(self.author);
//try writer.writeAll("\x00");
try writer.writeAll(self.author orelse "Unknown");
try writer.writeAll("\x00");
try writer.writeInt(usize, self.thread_id, endian);
 
try writer.writeInt(u8, @intFromEnum(self.attach), endian);
@@ -136,10 +157,9 @@ pub fn writeOut(self: Delta, writer: *std.io.AnyWriter) !void {
if (self.thread) |thread| {
try writer.writeAll(&thread.hash);
}
//try writer.writeAll("\x00");
 
try writer.writeAll("\x00");
 
if (self.thread) |t| try t.writeOut();
if (self.thread) |t| try t.commit();
}
 
pub fn readFile(a: std.mem.Allocator, idx: usize, file: std.fs.File) !Delta {
@@ -279,7 +299,7 @@ pub fn new(repo: []const u8, title: []const u8, msg: []const u8, author: []const
 
var thread = try Thread.new(d);
try currMaxSet(repo, max + 1);
try thread.writeOut();
try thread.commit();
d.thread_id = thread.index;
 
return d;
@@ -389,3 +409,40 @@ pub fn search(_: std.mem.Allocator, rules: []const SearchRule) SearchList(Delta)
.iterable = datad.iterate(),
};
}
 
test Delta {
const a = std.testing.allocator;
var tempdir = std.testing.tmpDir(.{});
defer tempdir.cleanup();
try Types.init(try tempdir.dir.makeOpenPath("datadir", .{ .iterate = true }));
 
var d = try Delta.new("repo_name", "title", "message", "author");
 
// LOL, you thought
const mask: i64 = ~@as(i64, 0xfffff);
d.created = std.time.timestamp() & mask;
d.updated = std.time.timestamp() & mask;
 
var out = std.ArrayList(u8).init(a);
defer out.clearAndFree();
var outw = out.writer().any();
try d.writeOut(&outw);
 
const v0: Delta = undefined;
const v0_bin: []const u8 = undefined;
const v1: Delta = undefined;
// TODO... eventually
_ = v0;
_ = v0_bin;
_ = v1;
 
const v1_bin: []const u8 = &[_]u8{
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x30, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x67, 0x00, 0x00, 0x00, 0x00,
0x72, 0x65, 0x70, 0x6F, 0x5F, 0x6E, 0x61, 0x6D, 0x65, 0x00, 0x74, 0x69, 0x74, 0x6C, 0x65, 0x00,
0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00, 0x61, 0x75, 0x74, 0x68, 0x6F, 0x72, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
try std.testing.expectEqualSlices(u8, out.items, v1_bin);
}
 
src/types/diff.zig added: 173, removed: 64, total 109
@@ -5,13 +5,17 @@ const Comment = @import("comment.zig");
const Delta = @import("delta.zig");
const Thread = @import("thread.zig");
 
const Types = @import("../types.zig");
 
pub const Diff = @This();
pub const TYPE_PREFIX = "{s}/diffs";
const DIFF_VERSION: usize = 0;
pub var datad: std.fs.Dir = undefined;
pub const TYPE_PREFIX = "diffs";
const DIFF_VERSION: usize = 1;
var datad: std.fs.Dir = undefined;
 
pub fn init(_: []const u8) !void {}
pub fn initType() !void {}
pub fn initType(stor: Types.Storage) !void {
datad = stor;
}
 
fn readVersioned(a: Allocator, idx: usize, file: std.fs.File) !Diff {
var reader = file.reader();
@@ -23,11 +27,24 @@ fn readVersioned(a: Allocator, idx: usize, file: std.fs.File) !Diff {
.created = try reader.readIntNative(i64),
.updated = try reader.readIntNative(i64),
.repo = try reader.readUntilDelimiterAlloc(a, 0, 0xFFFF),
.binsha = null,
.source_uri = try reader.readUntilDelimiterAlloc(a, 0, 0xFFFF),
 
.comment_data = try reader.readAllAlloc(a, 0xFFFF),
.file = file,
},
1 => return Diff{
.index = idx,
.state = try reader.readIntNative(usize),
.created = try reader.readIntNative(i64),
.updated = try reader.readIntNative(i64),
.repo = try reader.readUntilDelimiterAlloc(a, 0, 0xFFFF),
.binsha = try reader.readBoundedBytes(20),
.source_uri = try reader.readUntilDelimiterAlloc(a, 0, 0xFFFF),
.applies = (try reader.readByte() > 0),
.comment_data = try reader.readAllAlloc(a, 0xFFFF),
.file = file,
},
else => error.UnsupportedVersion,
};
}
@@ -37,12 +54,14 @@ state: usize,
created: i64 = 0,
updated: i64 = 0,
repo: []const u8,
binsha: ?[20]u8,
applies: bool = false,
delta_hash: [32]u8,
source_uri: ?[]const u8,
 
file: std.fs.File,
 
pub fn writeOut(self: Diff) !void {
pub fn commit(self: Diff) !void {
try self.file.seekTo(0);
var writer = self.file.writer();
try writer.writeIntNative(usize, DIFF_VERSION);
 
src/types/gist.zig added: 173, removed: 64, total 109
@@ -4,11 +4,13 @@ const Allocator = std.mem.Allocator;
const endian = builtin.cpu.arch.endian();
const Sha256 = std.crypto.hash.sha2.Sha256;
 
const Types = @import("../types.zig");
 
pub const Gist = @This();
 
pub const TYPE_PREFIX = "{s}/gist";
pub const TYPE_PREFIX = "gist";
const GIST_VERSION: usize = 0;
pub var datad: std.fs.Dir = undefined;
var datad: Types.Storage = undefined;
 
pub const File = struct {
name: []const u8,
@@ -21,7 +23,9 @@ created: i64,
files: []const File,
 
pub fn init(_: []const u8) !void {}
pub fn initType() !void {}
pub fn initType(stor: Types.Storage) !void {
datad = stor;
}
 
fn readVersioned(a: Allocator, hash: [32]u8, reader: std.io.AnyReader) !Gist {
const int: usize = try reader.readInt(usize, endian);
 
src/types/issue.zig added: 173, removed: 64, total 109
@@ -3,13 +3,17 @@ const Allocator = std.mem.Allocator;
 
const Comment = @import("comment.zig");
 
const Types = @import("../types.zig");
 
pub const Issue = @This();
 
pub const TYPE_PREFIX = "{s}/issues";
pub const TYPE_PREFIX = "issues";
const ISSUE_VERSION: usize = 0;
pub var datad: std.fs.Dir = undefined;
var datad: Types.Storage = undefined;
 
pub fn initType() !void {}
pub fn initType(stor: Types.Storage) !void {
datad = stor;
}
 
pub const Status = enum(u1) {
open = 0,
 
src/types/network.zig added: 173, removed: 64, total 109
@@ -1,14 +1,18 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
 
const Types = @import("../types.zig");
 
pub const Network = @This();
 
pub const TYPE_PREFIX = "{s}/networks";
pub const TYPE_PREFIX = "networks";
const NETWORK_VERSION: usize = 0;
pub var datad: std.fs.Dir = undefined;
var datad: std.fs.Dir = undefined;
 
pub fn init(_: []const u8) !void {}
pub fn initType() !void {}
pub fn initType(stor: Types.Storage) !void {
datad = stor;
}
 
name: []u8,
location: []u8,
 
src/types/read.zig added: 173, removed: 64, total 109
@@ -3,14 +3,18 @@ const builtin = @import("builtin");
const Allocator = std.mem.Allocator;
const endian = builtin.cpu.arch.endian();
 
const Types = @import("../types.zig");
 
pub const Read = @This();
 
pub const TYPE_PREFIX = "{s}/read";
pub const TYPE_PREFIX = "read";
const READ_VERSION: usize = 0;
 
pub var datad: std.fs.Dir = undefined;
var datad: std.fs.Dir = undefined;
pub fn init(_: []const u8) !void {}
pub fn initType() !void {}
pub fn initType(stor: Types.Storage) !void {
datad = stor;
}
 
pub fn readVersioned(a: Allocator, file: std.fs.File, _: [20]u8) !Read {
var reader = file.reader();
 
src/types/tags.zig added: 173, removed: 64, total 109
@@ -5,7 +5,7 @@ const endian = builtin.cpu.arch.endian();
 
pub const Tag = @This();
 
pub const TYPE_PREFIX = "{s}/tag";
pub const TYPE_PREFIX = "tag";
const READ_VERSION: usize = 0;
 
pub var datad: std.fs.Dir = undefined;
 
src/types/thread.zig added: 173, removed: 64, total 109
@@ -7,14 +7,18 @@ const Comment = @import("comment.zig");
const Delta = @import("delta.zig");
const State = Delta.State;
 
const Types = @import("../types.zig");
 
pub const Thread = @This();
 
pub const TYPE_PREFIX = "{s}/threads";
pub const TYPE_PREFIX = "threads";
const THREADS_VERSION: usize = 0;
pub var datad: std.fs.Dir = undefined;
var datad: Types.Storage = undefined;
 
pub fn init(_: []const u8) !void {}
pub fn initType() !void {}
pub fn initType(stor: Types.Storage) !void {
datad = stor;
}
 
fn readVersioned(a: Allocator, idx: usize, reader: *std.io.AnyReader) !Thread {
const int: usize = try reader.readInt(usize, endian);
@@ -45,10 +49,14 @@ hash: [32]u8 = [_]u8{0} ** 32,
comment_data: ?[]const u8 = null,
comments: ?[]Comment = null,
 
pub fn writeOut(self: Thread) !void {
pub fn commit(self: Thread) !void {
const file = try openFile(self.index);
defer file.close();
const writer = file.writer().any();
try self.writeOut(writer);
}
 
fn writeOut(self: Thread, writer: std.io.AnyWriter) !void {
try writer.writeInt(usize, THREADS_VERSION, endian);
try writer.writeStruct(self.state);
try writer.writeInt(i64, self.created, endian);
@@ -94,7 +102,7 @@ pub fn addComment(self: *Thread, a: Allocator, c: Comment) !void {
}
self.comments.?[self.comments.?.len - 1] = c;
self.updated = std.time.timestamp();
try self.writeOut();
try self.commit();
}
 
pub fn raze(self: Thread, a: std.mem.Allocator) void {
 
src/types/user.zig added: 173, removed: 64, total 109
@@ -3,14 +3,18 @@ const builtin = @import("builtin");
const Allocator = std.mem.Allocator;
const endian = builtin.cpu.arch.endian();
 
const Types = @import("../types.zig");
 
pub const User = @This();
 
pub const TYPE_PREFIX = "{s}/users";
pub const TYPE_PREFIX = "users";
const USER_VERSION: usize = 0;
 
pub var datad: std.fs.Dir = undefined;
var datad: std.fs.Dir = undefined;
pub fn init(_: []const u8) !void {}
pub fn initType() !void {}
pub fn initType(stor: Types.Storage) !void {
datad = stor;
}
 
pub fn readVersioned(a: Allocator, file: std.fs.File) !User {
var reader = file.reader();
 
src/types/viewers.zig added: 173, removed: 64, total 109
@@ -5,7 +5,7 @@ const endian = builtin.cpu.arch.endian();
 
pub const Viewers = @This();
 
pub const TYPE_PREFIX = "{s}/read";
pub const TYPE_PREFIX = "read";
const READ_VERSION: usize = 0;
 
pub var datad: std.fs.Dir = undefined;