╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const xit = @import("xit"); 3 const rp = xit.repo; 4 const hash = xit.hash; 5 const mrg = xit.merge; 6 const obj = xit.object; 7 const rf = xit.ref; 8 9 pub const User = @import("event/User.zig"); 10 pub const Repo = @import("event/Repo.zig"); 11 pub const Issue = @import("event/Issue.zig"); 12 13 pub const event_id_size: usize = 32; 14 15 // the branch haxy events are committed to before being consumed 16 pub const events_ref: rf.Ref = .{ .kind = .head, .name = "haxy/meta" }; 17 18 // options + db type for *the admin repo* — the single event store that holds 19 // users, repos, issues, etc. the functions below stay parameterized over 20 // repo_opts because they also operate on individual repos in the repos dir, 21 // which may use different options 22 pub const admin_repo_opts: rp.RepoOpts(.xit) = .{}; 23 pub const AdminDB = rp.Repo(.xit, admin_repo_opts).DB; 24 25 pub const EventKind = enum { 26 user, 27 repo, 28 issue, 29 }; 30 31 pub const EventWithId = struct { 32 id: [event_id_size * 2]u8, 33 event: union(EventKind) { 34 user: ?User, 35 repo: ?Repo, 36 issue: ?Issue, 37 }, 38 timestamp: u64 = 0, // not serialized, because it comes from the commit timestamp 39 40 pub fn jsonStringify(self: EventWithId, jw: anytype) !void { 41 try jw.beginObject(); 42 try jw.objectField("id"); 43 try jw.write(self.id); 44 try jw.objectField("kind"); 45 try jw.write(@tagName(self.event)); 46 try jw.objectField("data"); 47 switch (self.event) { 48 inline else => |event_maybe| try jw.write(event_maybe), 49 } 50 try jw.endObject(); 51 } 52 53 fn fromString(arena: *std.heap.ArenaAllocator, message: []const u8) !EventWithId { 54 const JsonEvent = struct { 55 id: [event_id_size * 2]u8, 56 kind: EventKind, 57 data: ?std.json.Value = null, 58 }; 59 const json_event = try std.json.parseFromSliceLeaky(JsonEvent, arena.allocator(), message, .{}); 60 return .{ 61 .id = json_event.id, 62 .event = switch (json_event.kind) { 63 .user => .{ 64 .user = if (json_event.data) |value| 65 try std.json.parseFromValueLeaky(User, arena.allocator(), value, .{}) 66 else 67 null, 68 }, 69 .repo => .{ 70 .repo = if (json_event.data) |value| 71 try std.json.parseFromValueLeaky(Repo, arena.allocator(), value, .{}) 72 else 73 null, 74 }, 75 .issue => .{ 76 .issue = if (json_event.data) |value| 77 try std.json.parseFromValueLeaky(Issue, arena.allocator(), value, .{}) 78 else 79 null, 80 }, 81 }, 82 }; 83 } 84 85 pub fn randomId(random: std.Random) [event_id_size]u8 { 86 var id_bytes: [event_id_size]u8 = undefined; 87 random.bytes(&id_bytes); 88 return id_bytes; 89 } 90 }; 91 92 pub fn consume( 93 comptime repo_opts: rp.RepoOpts(.xit), 94 io: std.Io, 95 allocator: std.mem.Allocator, 96 repo: *rp.Repo(.xit, repo_opts), 97 ref: rf.Ref, 98 ) !void { 99 const DB = rp.Repo(.xit, repo_opts).DB; 100 const State = rp.Repo(.xit, repo_opts).State; 101 102 const Ctx = struct { 103 core: *rp.Repo(.xit, repo_opts).Core, 104 io: std.Io, 105 allocator: std.mem.Allocator, 106 ref: rf.Ref, 107 108 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 109 var moment = try DB.HashMap(.read_write).init(cursor.*); 110 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 111 try consumeInTransaction(repo_opts, state, ctx.io, ctx.allocator, ctx.ref); 112 } 113 }; 114 115 try repo.core.db_file.lock(io, .exclusive); 116 defer repo.core.db_file.unlock(io); 117 118 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 119 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 120 .core = &repo.core, 121 .io = io, 122 .allocator = allocator, 123 .ref = ref, 124 }); 125 } 126 127 pub fn consumeInTransaction( 128 comptime repo_opts: rp.RepoOpts(.xit), 129 state: rp.Repo(.xit, repo_opts).State(.read_write), 130 io: std.Io, 131 allocator: std.mem.Allocator, 132 ref: rf.Ref, 133 ) !void { 134 const DB = rp.Repo(.xit, repo_opts).DB; 135 136 // the last_object_id represents the object id that was last consumed 137 var last_object_id_maybe: ?[hash.byteLen(repo_opts.hash)]u8 = null; 138 if (try state.extra.moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id"))) |last_object_id_cursor| { 139 var last_object_id_buffer: [hash.byteLen(repo_opts.hash)]u8 = undefined; 140 _ = try last_object_id_cursor.readBytes(&last_object_id_buffer); 141 last_object_id_maybe = last_object_id_buffer; 142 } 143 144 // the list with all of haxy's state, including materialized views. 145 // the reason it is a list is so we can keep every previous haxy 146 // state, making it easy to revert to an older state if the user 147 // rebases some of the past commits. 148 const haxy_cursor = try state.extra.moment.putCursor(hash.hashInt(repo_opts.hash, "haxy")); 149 const haxy = try DB.ArrayList(.read_write).init(haxy_cursor); 150 151 // add a new item to the haxy list created above. 152 // we call the item haxy_moments. it is a map of object id to haxy moment. 153 // in other words, it maps each object id to a hash map containing the 154 // state that the database was in when that object id was consumed. 155 var haxy_moments_cursor = try haxy.appendCursor(); 156 // use the previous haxy_moments as the basis for this one if it exists 157 if (try haxy.getCursor(-2)) |last_haxy_moments_cursor| { 158 try haxy_moments_cursor.write(.{ .slot = last_haxy_moments_cursor.slot() }); 159 } 160 var haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 161 162 var commit_iter = try CommitIterator(repo_opts).init(state.readOnly(), io, allocator, haxy_moments.readOnly(), ref); 163 defer commit_iter.deinit(io, allocator); 164 165 // if there are no events to process, look at the oid at HEAD and update 166 // the last_object_id to point to it. this is important in situations 167 // where we do a merge and then force-push to remove the merge. see the 168 // merge test for an example. 169 if (commit_iter.newest_object_id == null) { 170 const head_oid_hex = (try rf.readRecur(.xit, repo_opts, state.readOnly(), io, .{ .ref = ref })) orelse return error.OidNotFound; 171 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 172 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 173 174 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &head_oid)) orelse return error.CursorNotFound; 175 const haxy_moment = try DB.HashMap(.read_only).init(haxy_moment_cursor); 176 177 const moment_index_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 178 const moment_index = try moment_index_cursor.readUint(); 179 180 try haxy.slice(moment_index + 1); 181 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = &head_oid }); 182 return; 183 } 184 185 // if this branch was rebased and force pushed, we need to detect that and 186 // properly revert the haxy state to the last valid state. we detect this 187 // by simply asking if the last event is a descendent of the last event 188 // we consumed. if it isn't, then we know the haxy state needs to be reverted. 189 if (last_object_id_maybe) |*last_object_id| { 190 var is_rebased = false; 191 const newest_object_id = commit_iter.newest_object_id orelse return error.OidNotFound; 192 193 _ = mrg.getDescendent( 194 .xit, 195 repo_opts, 196 state.readOnly(), 197 io, 198 allocator, 199 &std.fmt.bytesToHex(last_object_id, .lower), 200 &std.fmt.bytesToHex(&newest_object_id, .lower), 201 ) catch |err| switch (err) { 202 error.DescendentNotFound => is_rebased = true, 203 else => |e| return e, 204 }; 205 206 if (is_rebased) { 207 const oldest_object_id = commit_iter.oldest_object_id orelse return error.OidNotFound; 208 var oldest_object = try obj.Object(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, &std.fmt.bytesToHex(oldest_object_id, .lower)); 209 defer oldest_object.deinit(); 210 211 const parent_oids = oldest_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 212 switch (parent_oids.len) { 213 0 => { 214 // the branch was rebased all the way to the very beginning. 215 // we have a repo event with no parent, which means it is now 216 // the very first event. all we need to do is set the haxy list 217 // to be empty and make a new haxy_moments map to work with. 218 219 try haxy.slice(0); 220 haxy_moments_cursor = try haxy.appendCursor(); 221 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 222 223 last_object_id_maybe = null; 224 }, 225 1 => { 226 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 227 _ = try std.fmt.hexToBytes(&oid, &parent_oids[0]); 228 229 const old_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 230 const old_moment = try DB.HashMap(.read_only).init(old_moment_cursor); 231 232 const old_moment_index_cursor = try old_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 233 const old_moment_index = try old_moment_index_cursor.readUint(); 234 235 // resize the haxy list so we truncate all the moments that were 236 // created after the parent_oid was consumed 237 try haxy.slice(old_moment_index + 1); 238 239 // make a new haxy moment and set its initial value to the last haxy moment 240 haxy_moments_cursor = try haxy.appendCursor(); 241 const old_haxy_moments_cursor = try haxy.getCursor(old_moment_index) orelse return error.CursorNotFound; 242 try haxy_moments_cursor.write(.{ .slot = old_haxy_moments_cursor.slot() }); 243 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 244 245 last_object_id_maybe = oid; 246 }, 247 else => return error.UnexpectedParentCount, 248 } 249 } 250 } 251 252 while (try commit_iter.next()) |repo_event_oid| { 253 var commit_object = try obj.Object(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, &std.fmt.bytesToHex(repo_event_oid, .lower)); 254 defer commit_object.deinit(); 255 256 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 257 258 // create a moment for this object id 259 var haxy_moment_cursor = try haxy_moments.putCursor(hash.bytesToInt(repo_opts.hash, &repo_event_oid)); 260 261 // if there was a previous object id, set this haxy moment's initial value to it. 262 // this efficiently "clones" the map so we make further modifications based on it. 263 if (parent_oids.len > 0) { 264 var first_parent_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 265 _ = try std.fmt.hexToBytes(&first_parent_oid, &parent_oids[0]); 266 267 const first_parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &first_parent_oid)) orelse return error.CursorNotFound; 268 try haxy_moment_cursor.write(.{ .slot = first_parent_haxy_moment_cursor.slot() }); 269 } 270 271 const haxy_moment = try DB.HashMap(.read_write).init(haxy_moment_cursor); 272 273 // merge changes from every parent after the first parent. the common 274 // ancestor is the baseline, so a later parent only contributes values 275 // it changed relative to the merge base. 276 if (parent_oids.len > 1) { 277 for (parent_oids[1..]) |*parent_oid| { 278 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 279 _ = try std.fmt.hexToBytes(&oid, parent_oid); 280 281 const parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 282 const parent_haxy_moment = try DB.HashMap(.read_only).init(parent_haxy_moment_cursor); 283 284 const baseline_oid_hex = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), io, allocator, &parent_oids[0], parent_oid); 285 var baseline_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 286 _ = try std.fmt.hexToBytes(&baseline_oid, &baseline_oid_hex); 287 const baseline_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &baseline_oid)) orelse return error.CursorNotFound; 288 const baseline_haxy_moment = try DB.HashMap(.read_only).init(baseline_haxy_moment_cursor); 289 290 try mergeChangedMapEntries(DB, haxy_moment, parent_haxy_moment, baseline_haxy_moment, true); 291 } 292 } 293 294 // associate this moment with the index it will first appear at in the haxy list. 295 // this will be important later so we can truncate that list if the user ever 296 // rebases starting at this object id. 297 try haxy_moment.put(hash.hashInt(repo_opts.hash, "moment-index"), .{ .uint = try haxy.count() - 1 }); 298 299 // consume the event unless it's a merge commit 300 if (parent_oids.len <= 1) { 301 var arena = std.heap.ArenaAllocator.init(allocator); 302 defer arena.deinit(); 303 304 // read the message from the commit 305 try commit_object.object_reader.seekTo(commit_object.content.commit.message_position); 306 const message = try commit_object.object_reader.interface.allocRemaining(arena.allocator(), .unlimited); 307 308 const event_with_id = try EventWithId.fromString(&arena, message); 309 310 // get the id of the current event as bytes 311 var current_event_id: [event_id_size]u8 = undefined; 312 _ = try std.fmt.hexToBytes(&current_event_id, &event_with_id.id); 313 314 const created_ts = commit_object.content.commit.metadata.timestamp; 315 316 switch (event_with_id.event) { 317 .user => |event_maybe| try User.consume(DB, repo_opts.hash, haxy_moment, &current_event_id, event_maybe, &arena, created_ts), 318 .repo => |event_maybe| try Repo.consume(DB, repo_opts.hash, haxy_moment, &current_event_id, event_maybe, &arena, created_ts), 319 .issue => |event_maybe| try Issue.consume(DB, repo_opts.hash, haxy_moment, &current_event_id, event_maybe), 320 } 321 } 322 323 // the current object id is now the last one 324 last_object_id_maybe = repo_event_oid; 325 326 // prevent any of the data created above from being mutated by future iterations of this loop 327 try state.core.db.freeze(); 328 } 329 330 if (last_object_id_maybe) |*last_object_id| { 331 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = last_object_id }); 332 } 333 } 334 335 pub fn currentMoment( 336 comptime repo_opts: rp.RepoOpts(.xit), 337 repo: *rp.Repo(.xit, repo_opts), 338 ) !rp.Repo(.xit, repo_opts).DB.HashMap(.read_only) { 339 const DB = rp.Repo(.xit, repo_opts).DB; 340 const history = try DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 341 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 342 const moment = try DB.HashMap(.read_only).init(moment_cursor); 343 const last_object_id_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id")) orelse return error.NotFound; 344 var last_object_id: [hash.byteLen(repo_opts.hash)]u8 = undefined; 345 _ = try last_object_id_cursor.readBytes(&last_object_id); 346 const haxy_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy")) orelse return error.NotFound; 347 const haxy = try DB.ArrayList(.read_only).init(haxy_cursor); 348 const haxy_moments_cursor = try haxy.getCursor(-1) orelse return error.NotFound; 349 const haxy_moments = try DB.HashMap(.read_only).init(haxy_moments_cursor); 350 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &last_object_id)) orelse return error.NotFound; 351 return try DB.HashMap(.read_only).init(haxy_moment_cursor); 352 } 353 354 pub fn commitAndConsume( 355 comptime repo_opts: rp.RepoOpts(.xit), 356 io: std.Io, 357 allocator: std.mem.Allocator, 358 repo: *rp.Repo(.xit, repo_opts), 359 ref: rf.Ref, 360 events: []const EventWithId, 361 ) !void { 362 var json: std.Io.Writer.Allocating = .init(allocator); 363 defer json.deinit(); 364 365 for (events) |event| { 366 json.clearRetainingCapacity(); 367 try std.json.Stringify.value(event, .{}, &json.writer); 368 _ = try repo.commitAtRef(io, allocator, .{ .message = json.written(), .timestamp = event.timestamp }, null, ref); 369 } 370 371 try consume(repo_opts, io, allocator, repo, ref); 372 } 373 374 // build the key for SortedMaps sorted by timestamp. the big-endian timestamp 375 // makes byte order match creation order; the event id breaks ties and keeps 376 // keys unique within the same timestamp. 377 pub fn orderKey(timestamp: u64, event_id: *const [event_id_size]u8) [@sizeOf(u64) + event_id_size]u8 { 378 var key: [@sizeOf(u64) + event_id_size]u8 = undefined; 379 std.mem.writeInt(u64, key[0..@sizeOf(u64)], timestamp, .big); 380 @memcpy(key[@sizeOf(u64)..], event_id); 381 return key; 382 } 383 384 // split a pushed "<owner>/<repo>" path into its two components, or null if it 385 // isn't exactly two non-empty segments. 386 pub fn parseOwnerRepoPath(path: []const u8) ?struct { owner: []const u8, name: []const u8 } { 387 var it = std.mem.splitScalar(u8, path, '/'); 388 const owner = it.next() orelse return null; 389 const name = it.next() orelse return null; 390 if (it.next() != null) return null; 391 if (owner.len == 0 or name.len == 0) return null; 392 393 return .{ .owner = owner, .name = name }; 394 } 395 396 // resolve a pushed `<owner>/<repo>` to the hex event id that names its on-disk 397 // directory under the repos dir 398 pub fn resolveOrCreateRepo( 399 io: std.Io, 400 allocator: std.mem.Allocator, 401 admin_repo_path: []const u8, 402 owner_name: []const u8, 403 repo_name: []const u8, 404 create_if_missing: bool, 405 ) !?[event_id_size * 2]u8 { 406 var repo = rp.Repo(.xit, admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 407 error.RepoNotFound => return null, 408 else => |e| return e, 409 }; 410 defer repo.deinit(io, allocator); 411 412 var arena = std.heap.ArenaAllocator.init(allocator); 413 defer arena.deinit(); 414 415 const moment = try currentMoment(admin_repo_opts, &repo); 416 417 // an already-registered repo reuses its event id, so a re-push (or a clone) 418 // lands in the same repo 419 if (try Repo.readByOwnerAndName(AdminDB, admin_repo_opts.hash, moment, &arena, owner_name, repo_name)) |found| { 420 return std.fmt.bytesToHex(found.event_id, .lower); 421 } 422 423 if (!create_if_missing) return null; 424 425 // the new repo is owned by the named user, read from the name index; an 426 // unknown owner can't own a repo, so the push is rejected 427 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(admin_repo_opts.hash, "name->user-id")) orelse return null; 428 const name_to_user_id = try AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 429 const owner_id_cursor = try name_to_user_id.getCursor(hash.hashInt(admin_repo_opts.hash, owner_name)) orelse return null; 430 var owner_user_id: [event_id_size]u8 = undefined; 431 _ = try owner_id_cursor.readBytes(&owner_user_id); 432 433 var id_bytes: [event_id_size]u8 = undefined; 434 io.random(&id_bytes); 435 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 436 437 try commitAndConsume(admin_repo_opts, io, allocator, &repo, events_ref, &[_]EventWithId{.{ 438 .id = event_id_hex, 439 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 440 .event = .{ .repo = .{ 441 .user_id = &owner_user_id, 442 .name = repo_name, 443 .description = "", 444 .enable_issue = true, 445 } }, 446 }}); 447 448 return event_id_hex; 449 } 450 451 // 452 // reading from xitdb 453 // 454 455 pub fn read( 456 comptime T: type, 457 comptime DB: type, 458 comptime hash_kind: hash.HashKind, 459 arena: *std.heap.ArenaAllocator, 460 map: DB.HashMap(.read_only), 461 ) !T { 462 var event: T = undefined; 463 464 switch (@typeInfo(T)) { 465 .@"struct" => |struct_info| { 466 inline for (struct_info.fields) |field| { 467 switch (@typeInfo(field.type)) { 468 .pointer => |pointer_info| { 469 if (pointer_info.size == .slice and pointer_info.child == u8 and pointer_info.is_const) { 470 @field(event, field.name) = try readBytes(DB, hash_kind, arena.allocator(), map, field.name); 471 } else { 472 @compileError("unsupported read field type: " ++ @typeName(field.type)); 473 } 474 }, 475 .array => |array_info| { 476 if (array_info.child == u8) { 477 const bytes = try readBytes(DB, hash_kind, arena.allocator(), map, field.name); 478 if (bytes.len != array_info.len) return error.InvalidByteArrayLength; 479 @memcpy(@field(event, field.name)[0..], bytes); 480 } else { 481 @compileError("unsupported read field type: " ++ @typeName(field.type)); 482 } 483 }, 484 .bool => @field(event, field.name) = try readBool(DB, hash_kind, map, field.name), 485 else => @compileError("unsupported read field type: " ++ @typeName(field.type)), 486 } 487 } 488 }, 489 else => @compileError("read expects a struct"), 490 } 491 492 return event; 493 } 494 495 fn readBytes( 496 comptime DB: type, 497 comptime hash_kind: hash.HashKind, 498 allocator: std.mem.Allocator, 499 map: DB.HashMap(.read_only), 500 field_name: []const u8, 501 ) ![]const u8 { 502 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 503 return try cursor.readBytesAlloc(allocator, null); 504 } 505 506 fn readBool( 507 comptime DB: type, 508 comptime hash_kind: hash.HashKind, 509 map: DB.HashMap(.read_only), 510 field_name: []const u8, 511 ) !bool { 512 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 513 var buffer: [5]u8 = undefined; 514 const bytes = try cursor.readBytesObject(&buffer); 515 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 516 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 517 if (std.mem.eql(u8, bytes.value, "true")) return true; 518 if (std.mem.eql(u8, bytes.value, "false")) return false; 519 return error.InvalidBool; 520 } 521 522 // 523 // writing to xitdb 524 // 525 526 pub fn upsert( 527 comptime T: type, 528 comptime DB: type, 529 comptime hash_kind: hash.HashKind, 530 map: DB.HashMap(.read_write), 531 event: T, 532 ) !void { 533 switch (@typeInfo(T)) { 534 .@"struct" => |struct_info| { 535 inline for (struct_info.fields) |field| { 536 try upsertField(DB, hash_kind, map, field.name, field.type, @field(event, field.name)); 537 } 538 }, 539 else => @compileError("upsert expects a struct"), 540 } 541 } 542 543 fn mergeChangedMapEntries( 544 comptime DB: type, 545 target: DB.HashMap(.read_write), 546 parent: DB.HashMap(.read_only), 547 baseline: DB.HashMap(.read_only), 548 comptime is_top_level: bool, 549 ) !void { 550 var parent_iter = try parent.iterator(); 551 while (try parent_iter.next()) |kv_pair_cursor| { 552 const kv_pair = try kv_pair_cursor.readKeyValuePair(); 553 554 const baseline_value_cursor = try baseline.getCursor(kv_pair.hash) orelse { 555 if ((try target.getCursor(kv_pair.hash)) != null) { 556 return error.MergeConflict; 557 } 558 559 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 560 continue; 561 }; 562 563 const tag = kv_pair.value_cursor.slot().tag; 564 565 if (tag != baseline_value_cursor.slot().tag) { 566 return error.UnexpectedTag; 567 } 568 569 if (kv_pair.value_cursor.slot().value == baseline_value_cursor.slot().value) { 570 continue; 571 } 572 573 if (tag == .hash_map) { 574 const target_existing_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 575 if (target_existing_cursor.slot().tag != .hash_map) { 576 return error.MergeConflict; 577 } 578 579 const target_child_cursor = try target.putCursor(kv_pair.hash); 580 const target_child = try DB.HashMap(.read_write).init(target_child_cursor); 581 const parent_child = try DB.HashMap(.read_only).init(kv_pair.value_cursor); 582 const baseline_child = try DB.HashMap(.read_only).init(baseline_value_cursor); 583 try mergeChangedMapEntries(DB, target_child, parent_child, baseline_child, false); 584 } else if (is_top_level) { 585 // the moment-index is a top-level key in the haxy moment 586 // and is not a map. we don't want to do any merge of this. 587 continue; 588 } else { 589 const target_value_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 590 if (target_value_cursor.slot().value != baseline_value_cursor.slot().value) { 591 return error.MergeConflict; 592 } 593 594 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 595 } 596 } 597 } 598 599 fn upsertField( 600 comptime DB: type, 601 comptime hash_kind: hash.HashKind, 602 map: DB.HashMap(.read_write), 603 comptime field_name: []const u8, 604 comptime Field: type, 605 value: Field, 606 ) !void { 607 const key = hash.hashInt(hash_kind, field_name); 608 609 switch (@typeInfo(Field)) { 610 .pointer => |pointer_info| { 611 if (pointer_info.size == .slice and pointer_info.child == u8) { 612 try upsertBytes(DB, hash_kind, map, key, value); 613 } else { 614 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 615 } 616 }, 617 .array => |array_info| { 618 if (array_info.child == u8) { 619 try upsertBytes(DB, hash_kind, map, key, &value); 620 } else { 621 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 622 } 623 }, 624 .int => |int_info| switch (int_info.signedness) { 625 .unsigned => { 626 if (try map.getCursor(key)) |value_cursor| { 627 if (try value_cursor.readUint() == value) { 628 return; 629 } 630 } 631 632 try map.put(key, .{ .uint = value }); 633 }, 634 .signed => { 635 if (try map.getCursor(key)) |value_cursor| { 636 if (try value_cursor.readInt() == value) { 637 return; 638 } 639 } 640 641 try map.put(key, .{ .int = value }); 642 }, 643 }, 644 .bool => { 645 const bytes = if (value) "true" else "false"; 646 if (try map.getCursor(key)) |value_cursor| { 647 var buffer: [5]u8 = undefined; 648 const existing = try value_cursor.readBytesObject(&buffer); 649 if (existing.format_tag) |format_tag| { 650 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 651 return; 652 } 653 } 654 } 655 656 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 657 }, 658 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 659 } 660 } 661 662 fn upsertBytes( 663 comptime DB: type, 664 comptime hash_kind: hash.HashKind, 665 map: DB.HashMap(.read_write), 666 key: hash.HashInt(hash_kind), 667 value: []const u8, 668 ) !void { 669 var existing_cursor_maybe = try map.getCursor(key); 670 if (existing_cursor_maybe) |*existing_cursor| { 671 if (try bytesEqual(DB, existing_cursor, value)) { 672 return; 673 } 674 } 675 676 var value_cursor = try map.putCursor(key); 677 var write_buffer: [1024]u8 = undefined; 678 var writer = try value_cursor.writer(&write_buffer); 679 try writer.interface.writeAll(value); 680 try writer.finish(); 681 } 682 683 fn bytesEqual( 684 comptime DB: type, 685 cursor: *DB.Cursor(.read_only), 686 value: []const u8, 687 ) !bool { 688 var read_buffer: [1024]u8 = undefined; 689 var reader = try cursor.reader(&read_buffer); 690 if (reader.size != value.len) { 691 return false; 692 } 693 694 var chunk_buffer: [1024]u8 = undefined; 695 var offset: usize = 0; 696 while (offset < value.len) { 697 const chunk_len = @min(chunk_buffer.len, value.len - offset); 698 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 699 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 700 return false; 701 } 702 offset += chunk_len; 703 } 704 705 return true; 706 } 707 708 // 709 // CommitIterator 710 // 711 // differs from xit's ObjectIterator in a few ways: 712 // 713 // 1. iterates parents first, rather than children first 714 // 2. `next` only returns the object id, not the full Object 715 // 3. stores its temporary state in a file on disk to avoid OOMs 716 // 717 718 pub fn CommitIterator(comptime repo_opts: rp.RepoOpts(.xit)) type { 719 return struct { 720 const DB = rp.Repo(.xit, repo_opts).DB; 721 const db_name = "haxy-repo-events.db"; 722 723 repo_dir: std.Io.Dir, 724 db_file: std.Io.File, 725 db: *DB, 726 // map of each object id to its children 727 parent_to_children: DB.HashMap(.read_write), 728 // tracking pending parents is necessary because a child can only 729 // be returned after all of its unconsumed parents have been returned 730 child_to_pending_parents: DB.HashMap(.read_write), 731 // queue of object ids that are ready to be returned from `next` 732 ready_queue: DB.ArrayList(.read_write), 733 ready_queue_index: u64, 734 // the object id at the tip of this branch 735 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 736 // the object id furthest back in the history that hasn't been consumed 737 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 738 739 pub fn init( 740 state: rp.Repo(.xit, repo_opts).State(.read_only), 741 io: std.Io, 742 allocator: std.mem.Allocator, 743 consumed_object_ids: DB.HashMap(.read_only), 744 ref: rf.Ref, 745 ) !CommitIterator(repo_opts) { 746 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 747 errdefer { 748 db_file.close(io); 749 state.core.repo_dir.deleteFile(io, db_name) catch {}; 750 } 751 752 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 753 errdefer allocator.destroy(buffer_ptr); 754 755 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 756 errdefer buffer_ptr.deinit(); 757 758 const db_ptr = try allocator.create(DB); 759 errdefer allocator.destroy(db_ptr); 760 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr }); 761 762 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 763 764 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 765 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 766 767 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 768 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 769 770 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 771 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 772 773 var self = CommitIterator(repo_opts){ 774 .repo_dir = state.core.repo_dir, 775 .db_file = db_file, 776 .db = db_ptr, 777 .parent_to_children = parent_to_children, 778 .child_to_pending_parents = child_to_pending_parents, 779 .ready_queue = ready_queue, 780 .ready_queue_index = 0, 781 .newest_object_id = null, 782 .oldest_object_id = null, 783 }; 784 errdefer self.deinit(io, allocator); 785 786 try self.collect(state, io, allocator, consumed_object_ids, ref); 787 788 return self; 789 } 790 791 pub fn deinit(self: *CommitIterator(repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 792 self.db_file.close(io); 793 self.db.core.memory.buffer.deinit(); 794 allocator.destroy(self.db.core.memory.buffer); 795 self.repo_dir.deleteFile(io, db_name) catch {}; 796 allocator.destroy(self.db); 797 } 798 799 pub fn next(self: *CommitIterator(repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 800 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 801 802 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 803 self.ready_queue_index += 1; 804 805 try self.enqueueChildren(&oid); 806 return oid; 807 } 808 809 fn collect( 810 self: *CommitIterator(repo_opts), 811 state: rp.Repo(.xit, repo_opts).State(.read_only), 812 io: std.Io, 813 allocator: std.mem.Allocator, 814 consumed_object_ids: DB.HashMap(.read_only), 815 ref: rf.Ref, 816 ) !void { 817 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 818 819 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 820 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 821 var walk_queue_index: u64 = 0; 822 823 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 824 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 825 826 const head_oid_hex = (try rf.readRecur(.xit, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 827 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 828 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 829 try walk_queue.append(.{ .bytes = &head_oid }); 830 831 while (walk_queue_index < try walk_queue.count()) { 832 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 833 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 834 835 walk_queue_index += 1; 836 837 // if we've seen this object id, skip it 838 if (null != try seen_object_ids.getCursor(oid_int)) { 839 continue; 840 } 841 842 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 843 844 var commit_object = try obj.Object(.xit, repo_opts, .full).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 845 defer commit_object.deinit(); 846 847 // if this object id has already been consumed, skip it 848 if (null != try consumed_object_ids.getCursor(oid_int)) { 849 continue; 850 } 851 852 if (self.newest_object_id == null) self.newest_object_id = oid; 853 self.oldest_object_id = oid; 854 855 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 856 var pending_parent_count: u64 = 0; 857 for (parent_oids) |*parent_oid| { 858 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 859 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 860 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 861 862 // if this object id has already been consumed, skip it 863 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 864 continue; 865 } 866 867 // if this object id hasn't already been seen, add it to the walk queue 868 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 869 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 870 } 871 872 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 873 const children = try DB.HashSet(.read_write).init(children_cursor); 874 try children.put(oid_int, .{ .bytes = &oid }); 875 pending_parent_count += 1; 876 } 877 878 if (pending_parent_count == 0) { 879 try self.ready_queue.append(.{ .bytes = &oid }); 880 } else { 881 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 882 } 883 } 884 } 885 886 fn enqueueChildren(self: *CommitIterator(repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 887 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 888 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 889 const children = try DB.HashSet(.read_only).init(children_cursor); 890 var children_iter = try children.iterator(); 891 892 while (try children_iter.next()) |*child_cursor| { 893 const kv_pair = try child_cursor.readKeyValuePair(); 894 const child_oid_int = kv_pair.hash; 895 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 896 const pending_parent_count = try pending_parent_cursor.readUint(); 897 898 if (pending_parent_count <= 1) { 899 _ = try self.child_to_pending_parents.remove(child_oid_int); 900 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 901 try self.ready_queue.append(.{ .bytes = &child_oid }); 902 } else { 903 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 904 } 905 } 906 } 907 908 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 909 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 910 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 911 _ = try oid_cursor.readBytes(&oid); 912 return oid; 913 } 914 }; 915 } 916