╭ 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 11 pub const event_id_size: usize = 32; 12 13 pub const EventKind = enum { 14 user, 15 repo, 16 issue, 17 }; 18 19 pub const EventData = union(EventKind) { 20 user: User, 21 repo: Repo, 22 issue: Issue, 23 24 pub const User = struct { 25 name: []const u8, 26 email: []const u8, 27 password_hash: []const u8, 28 }; 29 30 pub const Repo = struct { 31 user_id: []const u8, 32 name: []const u8, 33 enable_issue: bool, 34 }; 35 36 pub const Issue = struct { 37 title: []const u8, 38 description: []const u8, 39 tags: []const u8, 40 }; 41 42 pub fn read( 43 comptime DB: type, 44 comptime hash_kind: hash.HashKind, 45 allocator: std.mem.Allocator, 46 map: DB.HashMap(.read_only), 47 kind: EventKind, 48 ) !EventData { 49 return switch (kind) { 50 .user => .{ 51 .user = .{ 52 .name = try readBytes(DB, hash_kind, allocator, map, "name"), 53 .email = try readBytes(DB, hash_kind, allocator, map, "email"), 54 .password_hash = try readBytes(DB, hash_kind, allocator, map, "password_hash"), 55 }, 56 }, 57 .repo => .{ 58 .repo = .{ 59 .user_id = try readBytes(DB, hash_kind, allocator, map, "user_id"), 60 .name = try readBytes(DB, hash_kind, allocator, map, "name"), 61 .enable_issue = try readBool(DB, hash_kind, map, "enable_issue"), 62 }, 63 }, 64 .issue => .{ 65 .issue = .{ 66 .title = try readBytes(DB, hash_kind, allocator, map, "title"), 67 .description = try readBytes(DB, hash_kind, allocator, map, "description"), 68 .tags = try readBytes(DB, hash_kind, allocator, map, "tags"), 69 }, 70 }, 71 }; 72 } 73 74 fn readBytes( 75 comptime DB: type, 76 comptime hash_kind: hash.HashKind, 77 allocator: std.mem.Allocator, 78 map: DB.HashMap(.read_only), 79 field_name: []const u8, 80 ) ![]const u8 { 81 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 82 return try cursor.readBytesAlloc(allocator, null); 83 } 84 85 fn readBool( 86 comptime DB: type, 87 comptime hash_kind: hash.HashKind, 88 map: DB.HashMap(.read_only), 89 field_name: []const u8, 90 ) !bool { 91 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 92 var buffer: [5]u8 = undefined; 93 const bytes = try cursor.readBytesObject(&buffer); 94 if (bytes.format_tag == null or !std.mem.eql(u8, &bytes.format_tag.?, "bl")) return error.InvalidFormatTag; 95 if (std.mem.eql(u8, bytes.value, "true")) return true; 96 if (std.mem.eql(u8, bytes.value, "false")) return false; 97 return error.InvalidBool; 98 } 99 }; 100 101 pub const Event = struct { 102 id: [event_id_size * 2]u8, 103 data: MaybeEventData, 104 105 pub const MaybeEventData = union(EventKind) { 106 user: ?EventData.User, 107 repo: ?EventData.Repo, 108 issue: ?EventData.Issue, 109 110 fn fromJson( 111 allocator: std.mem.Allocator, 112 kind: EventKind, 113 value_maybe: ?std.json.Value, 114 ) !MaybeEventData { 115 return switch (kind) { 116 .user => .{ 117 .user = if (value_maybe) |value| try std.json.parseFromValueLeaky(EventData.User, allocator, value, .{}) else null, 118 }, 119 .repo => .{ 120 .repo = if (value_maybe) |value| try std.json.parseFromValueLeaky(EventData.Repo, allocator, value, .{}) else null, 121 }, 122 .issue => .{ 123 .issue = if (value_maybe) |value| try std.json.parseFromValueLeaky(EventData.Issue, allocator, value, .{}) else null, 124 }, 125 }; 126 } 127 }; 128 129 pub fn jsonStringify(self: Event, jw: anytype) !void { 130 try jw.beginObject(); 131 try jw.objectField("id"); 132 try jw.write(self.id); 133 try jw.objectField("kind"); 134 try jw.write(@tagName(self.data)); 135 try jw.objectField("data"); 136 switch (self.data) { 137 inline else => |data_maybe| try jw.write(data_maybe), 138 } 139 try jw.endObject(); 140 } 141 142 fn fromString(allocator: std.mem.Allocator, message: []const u8) !Event { 143 const JsonEvent = struct { 144 id: [event_id_size * 2]u8, 145 kind: EventKind, 146 data: ?std.json.Value = null, 147 }; 148 const json_event = try std.json.parseFromSliceLeaky(JsonEvent, allocator, message, .{}); 149 return .{ 150 .id = json_event.id, 151 .data = try MaybeEventData.fromJson(allocator, json_event.kind, json_event.data), 152 }; 153 } 154 }; 155 156 pub fn randomId(random: std.Random) [event_id_size]u8 { 157 var id_bytes: [event_id_size]u8 = undefined; 158 random.bytes(&id_bytes); 159 return id_bytes; 160 } 161 162 pub fn consume( 163 comptime repo_opts: rp.RepoOpts(.xit), 164 io: std.Io, 165 allocator: std.mem.Allocator, 166 repo: *rp.Repo(.xit, repo_opts), 167 ref: rf.Ref, 168 ) !void { 169 const DB = rp.Repo(.xit, repo_opts).DB; 170 const State = rp.Repo(.xit, repo_opts).State; 171 172 const Ctx = struct { 173 core: *rp.Repo(.xit, repo_opts).Core, 174 io: std.Io, 175 allocator: std.mem.Allocator, 176 ref: rf.Ref, 177 178 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 179 var moment = try DB.HashMap(.read_write).init(cursor.*); 180 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 181 try consumeInTransaction(repo_opts, state, ctx.io, ctx.allocator, ctx.ref); 182 } 183 }; 184 185 try repo.core.db_file.lock(io, .exclusive); 186 defer repo.core.db_file.unlock(io); 187 188 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 189 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 190 .core = &repo.core, 191 .io = io, 192 .allocator = allocator, 193 .ref = ref, 194 }); 195 } 196 197 pub fn consumeInTransaction( 198 comptime repo_opts: rp.RepoOpts(.xit), 199 state: rp.Repo(.xit, repo_opts).State(.read_write), 200 io: std.Io, 201 allocator: std.mem.Allocator, 202 ref: rf.Ref, 203 ) !void { 204 const DB = rp.Repo(.xit, repo_opts).DB; 205 206 // the last_object_id represents the object id that was last consumed 207 var last_object_id_maybe: ?[hash.byteLen(repo_opts.hash)]u8 = null; 208 if (try state.extra.moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id"))) |last_object_id_cursor| { 209 var last_object_id_buffer: [hash.byteLen(repo_opts.hash)]u8 = undefined; 210 _ = try last_object_id_cursor.readBytes(&last_object_id_buffer); 211 last_object_id_maybe = last_object_id_buffer; 212 } 213 214 // the list with all of haxy's state, including materialized views. 215 // the reason it is a list is so we can keep every previous haxy 216 // state, making it easy to revert to an older state if the user 217 // rebases some of the past commits. 218 const haxy_cursor = try state.extra.moment.putCursor(hash.hashInt(repo_opts.hash, "haxy")); 219 const haxy = try DB.ArrayList(.read_write).init(haxy_cursor); 220 221 // add a new item to the haxy list created above. 222 // we call the item haxy_moments. it is a map of object id to haxy moment. 223 // in other words, it maps each object id to a hash map containing the 224 // state that the database was in when that object id was consumed. 225 var haxy_moments_cursor = try haxy.appendCursor(); 226 // use the previous haxy_moments as the basis for this one if it exists 227 if (try haxy.getCursor(-2)) |last_haxy_moments_cursor| { 228 try haxy_moments_cursor.write(.{ .slot = last_haxy_moments_cursor.slot() }); 229 } 230 var haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 231 232 var arena = std.heap.ArenaAllocator.init(allocator); 233 defer arena.deinit(); 234 235 // compute a list of events that haven't been consumed yet 236 const RepoEvent = struct { 237 parent_oids: []const [hash.hexLen(repo_opts.hash)]u8, 238 oid: [hash.byteLen(repo_opts.hash)]u8, 239 }; 240 var repo_events: std.ArrayList(RepoEvent) = .empty; 241 defer repo_events.deinit(allocator); 242 { 243 var commit_iter = try obj.ObjectIterator(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, .{ .kind = .commit }); 244 defer commit_iter.deinit(); 245 246 const head_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), io, .{ .ref = ref })) orelse return error.OidNotFound; 247 try commit_iter.include(&head_oid); 248 249 while (try commit_iter.next(arena.allocator())) |commit_object| { 250 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 251 _ = try std.fmt.hexToBytes(&oid, &commit_object.oid); 252 253 // if this object id has already been consumed, skip it 254 if (null != try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid))) { 255 continue; 256 } 257 258 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 259 try repo_events.append(allocator, .{ .parent_oids = parent_oids, .oid = oid }); 260 } 261 } 262 263 // if there are no events to process, look at the oid at HEAD and update 264 // the last_object_id to point to it. this is important in situations 265 // where we do a merge and then force-push to remove the merge. see the 266 // merge test for an example. 267 if (repo_events.items.len == 0) { 268 const head_oid_hex = (try rf.readRecur(.xit, repo_opts, state.readOnly(), io, .{ .ref = ref })) orelse return error.OidNotFound; 269 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 270 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 271 272 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &head_oid)) orelse return error.CursorNotFound; 273 const haxy_moment = try DB.HashMap(.read_only).init(haxy_moment_cursor); 274 275 const moment_index_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 276 const moment_index = try moment_index_cursor.readUint(); 277 278 try haxy.slice(moment_index + 1); 279 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = &head_oid }); 280 return; 281 } 282 283 // if this branch was rebased and force pushed, we need to detect that and 284 // properly revert the haxy state to the last valid state. we detect this 285 // by simply asking if the last event is a descendent of the last event 286 // we consumed. if it isn't, then we know the haxy state needs to be reverted. 287 if (last_object_id_maybe) |*last_object_id| { 288 var is_rebased = false; 289 290 _ = mrg.getDescendent( 291 .xit, 292 repo_opts, 293 state.readOnly(), 294 io, 295 allocator, 296 &std.fmt.bytesToHex(last_object_id, .lower), 297 &std.fmt.bytesToHex(repo_events.items[0].oid, .lower), 298 ) catch |err| switch (err) { 299 error.DescendentNotFound => is_rebased = true, 300 else => |e| return e, 301 }; 302 303 if (is_rebased) { 304 const parent_oids = repo_events.items[repo_events.items.len - 1].parent_oids; 305 switch (parent_oids.len) { 306 0 => { 307 // the branch was rebased all the way to the very beginning. 308 // we have a repo event with no parent, which means it is now 309 // the very first event. all we need to do is set the haxy list 310 // to be empty and make a new haxy_moments map to work with. 311 312 try haxy.slice(0); 313 haxy_moments_cursor = try haxy.appendCursor(); 314 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 315 316 last_object_id_maybe = null; 317 }, 318 1 => { 319 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 320 _ = try std.fmt.hexToBytes(&oid, &parent_oids[0]); 321 322 const old_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 323 const old_moment = try DB.HashMap(.read_only).init(old_moment_cursor); 324 325 const old_moment_index_cursor = try old_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 326 const old_moment_index = try old_moment_index_cursor.readUint(); 327 328 // resize the haxy list so we truncate all the moments that were 329 // created after the parent_oid was consumed 330 try haxy.slice(old_moment_index + 1); 331 332 // make a new haxy moment and set its initial value to the last haxy moment 333 haxy_moments_cursor = try haxy.appendCursor(); 334 const old_haxy_moments_cursor = try haxy.getCursor(old_moment_index) orelse return error.CursorNotFound; 335 try haxy_moments_cursor.write(.{ .slot = old_haxy_moments_cursor.slot() }); 336 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 337 338 last_object_id_maybe = oid; 339 }, 340 else => return error.UnexpectedParentCount, 341 } 342 } 343 } else { 344 if (repo_events.items[repo_events.items.len - 1].parent_oids.len != 0) { 345 // there is no last_object_id, but this event has a parent. 346 // this is an invalid state. if the event has a parent, that 347 // implies that an event has already been processed, but 348 // if that's true then there would be a last_object_id. 349 return error.UnexpectedParent; 350 } 351 } 352 353 // iterate over the events in reverse order 354 // (they are sorted by most recent events first) 355 for (0..repo_events.items.len) |i| { 356 const repo_event = repo_events.items[repo_events.items.len - i - 1]; 357 358 // create a moment for this object id 359 var haxy_moment_cursor = try haxy_moments.putCursor(hash.bytesToInt(repo_opts.hash, &repo_event.oid)); 360 361 // if there was a previous object id, set this haxy moment's initial value to it. 362 // this efficiently "clones" the map so we make further modifications based on it. 363 if (repo_event.parent_oids.len > 0) { 364 var first_parent_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 365 _ = try std.fmt.hexToBytes(&first_parent_oid, &repo_event.parent_oids[0]); 366 367 const first_parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &first_parent_oid)) orelse return error.CursorNotFound; 368 try haxy_moment_cursor.write(.{ .slot = first_parent_haxy_moment_cursor.slot() }); 369 } 370 371 const haxy_moment = try DB.HashMap(.read_write).init(haxy_moment_cursor); 372 373 // merge changes from every parent after the first parent. the common 374 // ancestor is the baseline, so a later parent only contributes values 375 // it changed relative to the merge base. 376 if (repo_event.parent_oids.len > 1) { 377 for (repo_event.parent_oids[1..]) |*parent_oid| { 378 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 379 _ = try std.fmt.hexToBytes(&oid, parent_oid); 380 381 const parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 382 const parent_haxy_moment = try DB.HashMap(.read_only).init(parent_haxy_moment_cursor); 383 384 const baseline_oid_hex = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), io, allocator, &repo_event.parent_oids[0], parent_oid); 385 var baseline_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 386 _ = try std.fmt.hexToBytes(&baseline_oid, &baseline_oid_hex); 387 const baseline_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &baseline_oid)) orelse return error.CursorNotFound; 388 const baseline_haxy_moment = try DB.HashMap(.read_only).init(baseline_haxy_moment_cursor); 389 390 try mergeChangedMapEntries(DB, haxy_moment, parent_haxy_moment, baseline_haxy_moment, true); 391 } 392 } 393 394 // associate this moment with the index it will first appear at in the haxy list. 395 // this will be important later so we can truncate that list if the user ever 396 // rebases starting at this object id. 397 try haxy_moment.put(hash.hashInt(repo_opts.hash, "moment-index"), .{ .uint = try haxy.count() - 1 }); 398 399 // consume the event unless it's a merge commit 400 if (repo_event.parent_oids.len <= 1) { 401 var commit_object = try obj.Object(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, &std.fmt.bytesToHex(repo_event.oid, .lower)); 402 defer commit_object.deinit(); 403 404 // read the message from the commit 405 try commit_object.object_reader.seekTo(commit_object.content.commit.message_position); 406 const message = try commit_object.object_reader.interface.allocRemaining(arena.allocator(), .unlimited); 407 408 const event = try Event.fromString(arena.allocator(), message); 409 410 // get the id of the current event as bytes 411 var current_event_id: [event_id_size]u8 = undefined; 412 _ = try std.fmt.hexToBytes(&current_event_id, &event.id); 413 414 switch (event.data) { 415 .user => |user_data_maybe| { 416 const user_key = hash.hashInt(repo_opts.hash, &current_event_id); 417 418 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->user")); 419 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 420 421 if (user_data_maybe) |user_data| { 422 const user_cursor = try event_id_to_user.putCursor(user_key); 423 const user_map = try DB.HashMap(.read_write).init(user_cursor); 424 try upsert(DB, repo_opts.hash, user_map, EventData.User, user_data); 425 } else { 426 if (!try event_id_to_user.remove(user_key)) return error.EventNotFound; 427 428 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "user-id->repos")); 429 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 430 _ = try user_id_to_repos.remove(user_key); 431 } 432 }, 433 .repo => |repo_data_maybe| { 434 const repo_key = hash.hashInt(repo_opts.hash, &current_event_id); 435 436 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->repo")); 437 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 438 439 if (repo_data_maybe) |repo_data| { 440 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 441 const repo_map = try DB.HashMap(.read_write).init(repo_cursor); 442 try upsert(DB, repo_opts.hash, repo_map, EventData.Repo, repo_data); 443 444 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "user-id->repos")); 445 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 446 447 const user_repos_cursor = try user_id_to_repos.putCursor(hash.hashInt(repo_opts.hash, repo_data.user_id)); 448 const user_repos = try DB.CountedHashSet(.read_write).init(user_repos_cursor); 449 try user_repos.put(repo_key, .{ .bytes = &current_event_id }); 450 } else { 451 if (try event_id_to_repo.getCursor(repo_key)) |existing_repo_cursor| { 452 const existing_repo_map = try DB.HashMap(.read_only).init(existing_repo_cursor); 453 const existing_repo = try EventData.read(DB, repo_opts.hash, arena.allocator(), existing_repo_map, .repo); 454 const data = existing_repo.repo; 455 456 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "user-id->repos")); 457 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 458 459 const user_key = hash.hashInt(repo_opts.hash, data.user_id); 460 const user_repos_cursor = try user_id_to_repos.putCursor(user_key); 461 const user_repos = try DB.CountedHashSet(.read_write).init(user_repos_cursor); 462 _ = try user_repos.remove(repo_key); 463 } 464 if (!try event_id_to_repo.remove(repo_key)) return error.EventNotFound; 465 } 466 }, 467 .issue => |issue_data_maybe| { 468 const issue_key = hash.hashInt(repo_opts.hash, &current_event_id); 469 470 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->issue")); 471 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 472 473 if (issue_data_maybe) |issue_data| { 474 const issue_cursor = try event_id_to_issue.putCursor(issue_key); 475 const issue = try DB.HashMap(.read_write).init(issue_cursor); 476 try upsert(DB, repo_opts.hash, issue, EventData.Issue, issue_data); 477 } else { 478 if (!try event_id_to_issue.remove(issue_key)) return error.EventNotFound; 479 } 480 }, 481 } 482 } 483 484 // the current object id is now the last one 485 last_object_id_maybe = repo_event.oid; 486 487 // prevent any of the data created above from being mutated by future iterations of this loop 488 try state.core.db.freeze(); 489 } 490 491 if (last_object_id_maybe) |*last_object_id| { 492 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = last_object_id }); 493 } 494 } 495 496 fn upsert( 497 comptime DB: type, 498 comptime hash_kind: hash.HashKind, 499 map: DB.HashMap(.read_write), 500 comptime Data: type, 501 data: Data, 502 ) !void { 503 switch (@typeInfo(Data)) { 504 .@"struct" => |struct_info| { 505 inline for (struct_info.fields) |field| { 506 try upsertField(DB, hash_kind, map, field.name, field.type, @field(data, field.name)); 507 } 508 }, 509 else => @compileError("upsert expects a struct"), 510 } 511 } 512 513 fn mergeChangedMapEntries( 514 comptime DB: type, 515 target: DB.HashMap(.read_write), 516 parent: DB.HashMap(.read_only), 517 baseline: DB.HashMap(.read_only), 518 comptime is_top_level: bool, 519 ) !void { 520 var parent_iter = try parent.iterator(); 521 while (try parent_iter.next()) |kv_pair_cursor| { 522 const kv_pair = try kv_pair_cursor.readKeyValuePair(); 523 524 const baseline_value_cursor = try baseline.getCursor(kv_pair.hash) orelse { 525 if ((try target.getCursor(kv_pair.hash)) != null) { 526 return error.MergeConflict; 527 } 528 529 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 530 continue; 531 }; 532 533 const tag = kv_pair.value_cursor.slot().tag; 534 535 if (tag != baseline_value_cursor.slot().tag) { 536 return error.UnexpectedTag; 537 } 538 539 if (kv_pair.value_cursor.slot().value == baseline_value_cursor.slot().value) { 540 continue; 541 } 542 543 if (tag == .hash_map) { 544 const target_existing_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 545 if (target_existing_cursor.slot().tag != .hash_map) { 546 return error.MergeConflict; 547 } 548 549 const target_child_cursor = try target.putCursor(kv_pair.hash); 550 const target_child = try DB.HashMap(.read_write).init(target_child_cursor); 551 const parent_child = try DB.HashMap(.read_only).init(kv_pair.value_cursor); 552 const baseline_child = try DB.HashMap(.read_only).init(baseline_value_cursor); 553 try mergeChangedMapEntries(DB, target_child, parent_child, baseline_child, false); 554 } else if (is_top_level) { 555 // the moment-index is a top-level key in the haxy moment 556 // and is not a map. we don't want to do any merge of this. 557 continue; 558 } else { 559 const target_value_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 560 if (target_value_cursor.slot().value != baseline_value_cursor.slot().value) { 561 return error.MergeConflict; 562 } 563 564 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 565 } 566 } 567 } 568 569 fn upsertField( 570 comptime DB: type, 571 comptime hash_kind: hash.HashKind, 572 map: DB.HashMap(.read_write), 573 comptime field_name: []const u8, 574 comptime Field: type, 575 value: Field, 576 ) !void { 577 const key = hash.hashInt(hash_kind, field_name); 578 579 switch (@typeInfo(Field)) { 580 .pointer => |pointer_info| { 581 if (pointer_info.size == .slice and pointer_info.child == u8) { 582 try upsertBytes(DB, hash_kind, map, key, value); 583 } else { 584 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 585 } 586 }, 587 .array => |array_info| { 588 if (array_info.child == u8) { 589 try upsertBytes(DB, hash_kind, map, key, &value); 590 } else { 591 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 592 } 593 }, 594 .int => |int_info| switch (int_info.signedness) { 595 .unsigned => { 596 if (try map.getCursor(key)) |value_cursor| { 597 if (try value_cursor.readUint() == value) { 598 return; 599 } 600 } 601 602 try map.put(key, .{ .uint = value }); 603 }, 604 .signed => { 605 if (try map.getCursor(key)) |value_cursor| { 606 if (try value_cursor.readInt() == value) { 607 return; 608 } 609 } 610 611 try map.put(key, .{ .int = value }); 612 }, 613 }, 614 .bool => { 615 const bytes = if (value) "true" else "false"; 616 if (try map.getCursor(key)) |value_cursor| { 617 var buffer: [5]u8 = undefined; 618 const existing = try value_cursor.readBytesObject(&buffer); 619 if (existing.format_tag != null and 620 std.mem.eql(u8, &existing.format_tag.?, "bl") and 621 std.mem.eql(u8, existing.value, bytes)) 622 { 623 return; 624 } 625 } 626 627 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 628 }, 629 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 630 } 631 } 632 633 fn upsertBytes( 634 comptime DB: type, 635 comptime hash_kind: hash.HashKind, 636 map: DB.HashMap(.read_write), 637 key: hash.HashInt(hash_kind), 638 value: []const u8, 639 ) !void { 640 var existing_cursor_maybe = try map.getCursor(key); 641 if (existing_cursor_maybe) |*existing_cursor| { 642 if (try bytesEqual(DB, existing_cursor, value)) { 643 return; 644 } 645 } 646 647 var value_cursor = try map.putCursor(key); 648 var write_buffer: [1024]u8 = undefined; 649 var writer = try value_cursor.writer(&write_buffer); 650 try writer.interface.writeAll(value); 651 try writer.finish(); 652 } 653 654 fn bytesEqual( 655 comptime DB: type, 656 cursor: *DB.Cursor(.read_only), 657 value: []const u8, 658 ) !bool { 659 var read_buffer: [1024]u8 = undefined; 660 var reader = try cursor.reader(&read_buffer); 661 if (reader.size != value.len) { 662 return false; 663 } 664 665 var chunk_buffer: [1024]u8 = undefined; 666 var offset: usize = 0; 667 while (offset < value.len) { 668 const chunk_len = @min(chunk_buffer.len, value.len - offset); 669 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 670 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 671 return false; 672 } 673 offset += chunk_len; 674 } 675 676 return true; 677 } 678