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