╭ 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 // walk the commits and add all new events we haven't consumed yet. 250 // the `next` call is using the arena because we need the parent_oids 251 // slice to live beyond this scope. 252 // TODO: `repo_events` could in theory contain an unbounded number 253 // of events, so there is an OOM risk here. we definitely need to 254 // guard against this. 255 while (try commit_iter.next(arena.allocator())) |commit_object| { 256 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 257 _ = try std.fmt.hexToBytes(&oid, &commit_object.oid); 258 259 // if this object id has already been consumed, prune its ancestry from 260 // the iterator. other queued paths, such as the second parent of a 261 // merge commit, can still be walked until they hit a consumed commit 262 // or run out of history. 263 if (null != try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid))) { 264 try commit_iter.exclude(&commit_object.oid); 265 continue; 266 } 267 268 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 269 try repo_events.append(allocator, .{ .parent_oids = parent_oids, .oid = oid }); 270 } 271 } 272 273 // if there are no events to process, look at the oid at HEAD and update 274 // the last_object_id to point to it. this is important in situations 275 // where we do a merge and then force-push to remove the merge. see the 276 // merge test for an example. 277 if (repo_events.items.len == 0) { 278 const head_oid_hex = (try rf.readRecur(.xit, repo_opts, state.readOnly(), io, .{ .ref = ref })) orelse return error.OidNotFound; 279 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 280 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 281 282 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &head_oid)) orelse return error.CursorNotFound; 283 const haxy_moment = try DB.HashMap(.read_only).init(haxy_moment_cursor); 284 285 const moment_index_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 286 const moment_index = try moment_index_cursor.readUint(); 287 288 try haxy.slice(moment_index + 1); 289 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = &head_oid }); 290 return; 291 } 292 293 // if this branch was rebased and force pushed, we need to detect that and 294 // properly revert the haxy state to the last valid state. we detect this 295 // by simply asking if the last event is a descendent of the last event 296 // we consumed. if it isn't, then we know the haxy state needs to be reverted. 297 if (last_object_id_maybe) |*last_object_id| { 298 var is_rebased = false; 299 300 _ = mrg.getDescendent( 301 .xit, 302 repo_opts, 303 state.readOnly(), 304 io, 305 allocator, 306 &std.fmt.bytesToHex(last_object_id, .lower), 307 &std.fmt.bytesToHex(repo_events.items[0].oid, .lower), 308 ) catch |err| switch (err) { 309 error.DescendentNotFound => is_rebased = true, 310 else => |e| return e, 311 }; 312 313 if (is_rebased) { 314 const parent_oids = repo_events.items[repo_events.items.len - 1].parent_oids; 315 switch (parent_oids.len) { 316 0 => { 317 // the branch was rebased all the way to the very beginning. 318 // we have a repo event with no parent, which means it is now 319 // the very first event. all we need to do is set the haxy list 320 // to be empty and make a new haxy_moments map to work with. 321 322 try haxy.slice(0); 323 haxy_moments_cursor = try haxy.appendCursor(); 324 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 325 326 last_object_id_maybe = null; 327 }, 328 1 => { 329 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 330 _ = try std.fmt.hexToBytes(&oid, &parent_oids[0]); 331 332 const old_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 333 const old_moment = try DB.HashMap(.read_only).init(old_moment_cursor); 334 335 const old_moment_index_cursor = try old_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 336 const old_moment_index = try old_moment_index_cursor.readUint(); 337 338 // resize the haxy list so we truncate all the moments that were 339 // created after the parent_oid was consumed 340 try haxy.slice(old_moment_index + 1); 341 342 // make a new haxy moment and set its initial value to the last haxy moment 343 haxy_moments_cursor = try haxy.appendCursor(); 344 const old_haxy_moments_cursor = try haxy.getCursor(old_moment_index) orelse return error.CursorNotFound; 345 try haxy_moments_cursor.write(.{ .slot = old_haxy_moments_cursor.slot() }); 346 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 347 348 last_object_id_maybe = oid; 349 }, 350 else => return error.UnexpectedParentCount, 351 } 352 } 353 } else { 354 if (repo_events.items[repo_events.items.len - 1].parent_oids.len != 0) { 355 // there is no last_object_id, but this event has a parent. 356 // this is an invalid state. if the event has a parent, that 357 // implies that an event has already been processed, but 358 // if that's true then there would be a last_object_id. 359 return error.UnexpectedParent; 360 } 361 } 362 363 // iterate over the events in reverse order 364 // (they are sorted by most recent events first) 365 for (0..repo_events.items.len) |i| { 366 const repo_event = repo_events.items[repo_events.items.len - i - 1]; 367 368 // create a moment for this object id 369 var haxy_moment_cursor = try haxy_moments.putCursor(hash.bytesToInt(repo_opts.hash, &repo_event.oid)); 370 371 // if there was a previous object id, set this haxy moment's initial value to it. 372 // this efficiently "clones" the map so we make further modifications based on it. 373 if (repo_event.parent_oids.len > 0) { 374 var first_parent_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 375 _ = try std.fmt.hexToBytes(&first_parent_oid, &repo_event.parent_oids[0]); 376 377 const first_parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &first_parent_oid)) orelse return error.CursorNotFound; 378 try haxy_moment_cursor.write(.{ .slot = first_parent_haxy_moment_cursor.slot() }); 379 } 380 381 const haxy_moment = try DB.HashMap(.read_write).init(haxy_moment_cursor); 382 383 // merge changes from every parent after the first parent. the common 384 // ancestor is the baseline, so a later parent only contributes values 385 // it changed relative to the merge base. 386 if (repo_event.parent_oids.len > 1) { 387 for (repo_event.parent_oids[1..]) |*parent_oid| { 388 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 389 _ = try std.fmt.hexToBytes(&oid, parent_oid); 390 391 const parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 392 const parent_haxy_moment = try DB.HashMap(.read_only).init(parent_haxy_moment_cursor); 393 394 const baseline_oid_hex = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), io, allocator, &repo_event.parent_oids[0], parent_oid); 395 var baseline_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 396 _ = try std.fmt.hexToBytes(&baseline_oid, &baseline_oid_hex); 397 const baseline_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &baseline_oid)) orelse return error.CursorNotFound; 398 const baseline_haxy_moment = try DB.HashMap(.read_only).init(baseline_haxy_moment_cursor); 399 400 try mergeChangedMapEntries(DB, haxy_moment, parent_haxy_moment, baseline_haxy_moment, true); 401 } 402 } 403 404 // associate this moment with the index it will first appear at in the haxy list. 405 // this will be important later so we can truncate that list if the user ever 406 // rebases starting at this object id. 407 try haxy_moment.put(hash.hashInt(repo_opts.hash, "moment-index"), .{ .uint = try haxy.count() - 1 }); 408 409 // consume the event unless it's a merge commit 410 if (repo_event.parent_oids.len <= 1) { 411 var commit_object = try obj.Object(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, &std.fmt.bytesToHex(repo_event.oid, .lower)); 412 defer commit_object.deinit(); 413 414 // read the message from the commit 415 try commit_object.object_reader.seekTo(commit_object.content.commit.message_position); 416 const message = try commit_object.object_reader.interface.allocRemaining(arena.allocator(), .unlimited); 417 418 const event = try Event.fromString(arena.allocator(), message); 419 420 // get the id of the current event as bytes 421 var current_event_id: [event_id_size]u8 = undefined; 422 _ = try std.fmt.hexToBytes(&current_event_id, &event.id); 423 424 switch (event.data) { 425 .user => |user_data_maybe| { 426 const user_key = hash.hashInt(repo_opts.hash, &current_event_id); 427 428 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->user")); 429 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 430 431 if (user_data_maybe) |user_data| { 432 const user_cursor = try event_id_to_user.putCursor(user_key); 433 const user_map = try DB.HashMap(.read_write).init(user_cursor); 434 try upsert(DB, repo_opts.hash, user_map, EventData.User, user_data); 435 } else { 436 if (!try event_id_to_user.remove(user_key)) return error.EventNotFound; 437 438 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "user-id->repos")); 439 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 440 _ = try user_id_to_repos.remove(user_key); 441 } 442 }, 443 .repo => |repo_data_maybe| { 444 const repo_key = hash.hashInt(repo_opts.hash, &current_event_id); 445 446 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->repo")); 447 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 448 449 if (repo_data_maybe) |repo_data| { 450 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 451 const repo_map = try DB.HashMap(.read_write).init(repo_cursor); 452 try upsert(DB, repo_opts.hash, repo_map, EventData.Repo, repo_data); 453 454 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "user-id->repos")); 455 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 456 457 const user_repos_cursor = try user_id_to_repos.putCursor(hash.hashInt(repo_opts.hash, repo_data.user_id)); 458 const user_repos = try DB.CountedHashSet(.read_write).init(user_repos_cursor); 459 try user_repos.put(repo_key, .{ .bytes = &current_event_id }); 460 } else { 461 if (try event_id_to_repo.getCursor(repo_key)) |existing_repo_cursor| { 462 const existing_repo_map = try DB.HashMap(.read_only).init(existing_repo_cursor); 463 const existing_repo = try EventData.read(DB, repo_opts.hash, arena.allocator(), existing_repo_map, .repo); 464 const data = existing_repo.repo; 465 466 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "user-id->repos")); 467 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 468 469 const user_key = hash.hashInt(repo_opts.hash, data.user_id); 470 const user_repos_cursor = try user_id_to_repos.putCursor(user_key); 471 const user_repos = try DB.CountedHashSet(.read_write).init(user_repos_cursor); 472 _ = try user_repos.remove(repo_key); 473 } 474 if (!try event_id_to_repo.remove(repo_key)) return error.EventNotFound; 475 } 476 }, 477 .issue => |issue_data_maybe| { 478 const issue_key = hash.hashInt(repo_opts.hash, &current_event_id); 479 480 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->issue")); 481 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 482 483 if (issue_data_maybe) |issue_data| { 484 const issue_cursor = try event_id_to_issue.putCursor(issue_key); 485 const issue = try DB.HashMap(.read_write).init(issue_cursor); 486 try upsert(DB, repo_opts.hash, issue, EventData.Issue, issue_data); 487 } else { 488 if (!try event_id_to_issue.remove(issue_key)) return error.EventNotFound; 489 } 490 }, 491 } 492 } 493 494 // the current object id is now the last one 495 last_object_id_maybe = repo_event.oid; 496 497 // prevent any of the data created above from being mutated by future iterations of this loop 498 try state.core.db.freeze(); 499 } 500 501 if (last_object_id_maybe) |*last_object_id| { 502 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = last_object_id }); 503 } 504 } 505 506 fn upsert( 507 comptime DB: type, 508 comptime hash_kind: hash.HashKind, 509 map: DB.HashMap(.read_write), 510 comptime Data: type, 511 data: Data, 512 ) !void { 513 switch (@typeInfo(Data)) { 514 .@"struct" => |struct_info| { 515 inline for (struct_info.fields) |field| { 516 try upsertField(DB, hash_kind, map, field.name, field.type, @field(data, field.name)); 517 } 518 }, 519 else => @compileError("upsert expects a struct"), 520 } 521 } 522 523 fn mergeChangedMapEntries( 524 comptime DB: type, 525 target: DB.HashMap(.read_write), 526 parent: DB.HashMap(.read_only), 527 baseline: DB.HashMap(.read_only), 528 comptime is_top_level: bool, 529 ) !void { 530 var parent_iter = try parent.iterator(); 531 while (try parent_iter.next()) |kv_pair_cursor| { 532 const kv_pair = try kv_pair_cursor.readKeyValuePair(); 533 534 const baseline_value_cursor = try baseline.getCursor(kv_pair.hash) orelse { 535 if ((try target.getCursor(kv_pair.hash)) != null) { 536 return error.MergeConflict; 537 } 538 539 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 540 continue; 541 }; 542 543 const tag = kv_pair.value_cursor.slot().tag; 544 545 if (tag != baseline_value_cursor.slot().tag) { 546 return error.UnexpectedTag; 547 } 548 549 if (kv_pair.value_cursor.slot().value == baseline_value_cursor.slot().value) { 550 continue; 551 } 552 553 if (tag == .hash_map) { 554 const target_existing_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 555 if (target_existing_cursor.slot().tag != .hash_map) { 556 return error.MergeConflict; 557 } 558 559 const target_child_cursor = try target.putCursor(kv_pair.hash); 560 const target_child = try DB.HashMap(.read_write).init(target_child_cursor); 561 const parent_child = try DB.HashMap(.read_only).init(kv_pair.value_cursor); 562 const baseline_child = try DB.HashMap(.read_only).init(baseline_value_cursor); 563 try mergeChangedMapEntries(DB, target_child, parent_child, baseline_child, false); 564 } else if (is_top_level) { 565 // the moment-index is a top-level key in the haxy moment 566 // and is not a map. we don't want to do any merge of this. 567 continue; 568 } else { 569 const target_value_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 570 if (target_value_cursor.slot().value != baseline_value_cursor.slot().value) { 571 return error.MergeConflict; 572 } 573 574 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 575 } 576 } 577 } 578 579 fn upsertField( 580 comptime DB: type, 581 comptime hash_kind: hash.HashKind, 582 map: DB.HashMap(.read_write), 583 comptime field_name: []const u8, 584 comptime Field: type, 585 value: Field, 586 ) !void { 587 const key = hash.hashInt(hash_kind, field_name); 588 589 switch (@typeInfo(Field)) { 590 .pointer => |pointer_info| { 591 if (pointer_info.size == .slice and pointer_info.child == u8) { 592 try upsertBytes(DB, hash_kind, map, key, value); 593 } else { 594 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 595 } 596 }, 597 .array => |array_info| { 598 if (array_info.child == u8) { 599 try upsertBytes(DB, hash_kind, map, key, &value); 600 } else { 601 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 602 } 603 }, 604 .int => |int_info| switch (int_info.signedness) { 605 .unsigned => { 606 if (try map.getCursor(key)) |value_cursor| { 607 if (try value_cursor.readUint() == value) { 608 return; 609 } 610 } 611 612 try map.put(key, .{ .uint = value }); 613 }, 614 .signed => { 615 if (try map.getCursor(key)) |value_cursor| { 616 if (try value_cursor.readInt() == value) { 617 return; 618 } 619 } 620 621 try map.put(key, .{ .int = value }); 622 }, 623 }, 624 .bool => { 625 const bytes = if (value) "true" else "false"; 626 if (try map.getCursor(key)) |value_cursor| { 627 var buffer: [5]u8 = undefined; 628 const existing = try value_cursor.readBytesObject(&buffer); 629 if (existing.format_tag != null and 630 std.mem.eql(u8, &existing.format_tag.?, "bl") and 631 std.mem.eql(u8, existing.value, bytes)) 632 { 633 return; 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