╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
180 // the last_object_id to point to it. this is important in situations 181 // where we do a merge and then force-push to remove the merge. see the 182 // merge test for an example. 183 if (repo_events.items.len == 0) { 184 const head_oid_hex = (try rf.readRecur(.xit, repo_opts, state.readOnly(), io, .{ .ref = ref })) orelse return error.OidNotFound; 185 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 186 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 187 188 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &head_oid)) orelse return error.CursorNotFound; 189 const haxy_moment = try DB.HashMap(.read_only).init(haxy_moment_cursor); 190 191 const moment_index_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 192 const moment_index = try moment_index_cursor.readUint(); 193 194 try haxy.slice(moment_index + 1); 195 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = &head_oid }); 196 return; 197 } 198 199 // if this branch was rebased and force pushed, we need to detect that and 200 // properly revert the haxy state to the last valid state. we detect this 201 // by simply asking if the last event is a descendent of the last event 202 // we consumed. if it isn't, then we know the haxy state needs to be reverted. 203 if (last_object_id_maybe) |*last_object_id| { 204 var is_rebased = false; 205 206 _ = mrg.getDescendent( 207 .xit, 208 repo_opts, 209 state.readOnly(), 210 io, 211 allocator, 212 &std.fmt.bytesToHex(last_object_id, .lower), 213 &std.fmt.bytesToHex(repo_events.items[0].oid, .lower), 214 ) catch |err| switch (err) { 215 error.DescendentNotFound => is_rebased = true, 216 else => |e| return e, 217 }; 218 219 if (is_rebased) { 220 const parent_oids = repo_events.items[repo_events.items.len - 1].parent_oids; 221 switch (parent_oids.len) { 222 0 => { 223 // the branch was rebased all the way to the very beginning. 224 // we have a repo event with no parent, which means it is now 225 // the very first event. all we need to do is set the haxy list 226 // to be empty and make a new haxy_moments map to work with. 227 228 try haxy.slice(0); 229 haxy_moments_cursor = try haxy.appendCursor(); 230 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 231 232 last_object_id_maybe = null; 233 }, 234 1 => { 235 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 236 _ = try std.fmt.hexToBytes(&oid, &parent_oids[0]); 237 238 const old_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 239 const old_moment = try DB.HashMap(.read_only).init(old_moment_cursor); 240 241 const old_moment_index_cursor = try old_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 242 const old_moment_index = try old_moment_index_cursor.readUint(); 243 244 // resize the haxy list so we truncate all the moments that were 245 // created after the parent_oid was consumed 246 try haxy.slice(old_moment_index + 1); 247 248 // make a new haxy moment and set its initial value to the last haxy moment 249 haxy_moments_cursor = try haxy.appendCursor(); 250 const old_haxy_moments_cursor = try haxy.getCursor(old_moment_index) orelse return error.CursorNotFound; 251 try haxy_moments_cursor.write(.{ .slot = old_haxy_moments_cursor.slot() }); 252 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 253 254 last_object_id_maybe = oid; 255 }, 256 else => return error.UnexpectedParentCount, 257 } 258 } 259 } else { 260 if (repo_events.items[repo_events.items.len - 1].parent_oids.len != 0) { 261 // there is no last_object_id, but this event has a parent. 262 // this is an invalid state. if the event has a parent, that 263 // implies that an event has already been processed, but 264 // if that's true then there would be a last_object_id. 265 return error.UnexpectedParent; 266 } 267 } 268 269 // iterate over the events in reverse order 270 // (they are sorted by most recent events first) 271 for (0..repo_events.items.len) |i| { 272 const repo_event = repo_events.items[repo_events.items.len - i - 1]; 273 274 // create a moment for this object id 275 var haxy_moment_cursor = try haxy_moments.putCursor(hash.bytesToInt(repo_opts.hash, &repo_event.oid)); 276 277 // if there was a previous object id, set this haxy moment's initial value to it. 278 // this efficiently "clones" the map so we make further modifications based on it. 279 if (repo_event.parent_oids.len > 0) { 280 var first_parent_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 281 _ = try std.fmt.hexToBytes(&first_parent_oid, &repo_event.parent_oids[0]); 282 283 const first_parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &first_parent_oid)) orelse return error.CursorNotFound; 284 try haxy_moment_cursor.write(.{ .slot = first_parent_haxy_moment_cursor.slot() }); 285 } 286 287 const haxy_moment = try DB.HashMap(.read_write).init(haxy_moment_cursor); 288 289 // merge changes from every parent after the first parent. the common 290 // ancestor is the baseline, so a later parent only contributes values 291 // it changed relative to the merge base. 292 if (repo_event.parent_oids.len > 1) { 293 for (repo_event.parent_oids[1..]) |*parent_oid| { 294 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 295 _ = try std.fmt.hexToBytes(&oid, parent_oid); 296 297 const parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 298 const parent_haxy_moment = try DB.HashMap(.read_only).init(parent_haxy_moment_cursor); 299 300 const baseline_oid_hex = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), io, allocator, &repo_event.parent_oids[0], parent_oid); 301 var baseline_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 302 _ = try std.fmt.hexToBytes(&baseline_oid, &baseline_oid_hex); 303 const baseline_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &baseline_oid)) orelse return error.CursorNotFound; 304 const baseline_haxy_moment = try DB.HashMap(.read_only).init(baseline_haxy_moment_cursor); 305 306 try mergeChangedMapEntries(DB, haxy_moment, parent_haxy_moment, baseline_haxy_moment, true); 307 } 308 } 309 310 // associate this moment with the index it will first appear at in the haxy list. 311 // this will be important later so we can truncate that list if the user ever 312 // rebases starting at this object id. 313 try haxy_moment.put(hash.hashInt(repo_opts.hash, "moment-index"), .{ .uint = try haxy.count() - 1 }); 314 315 // consume the event unless it's a merge commit 316 if (repo_event.parent_oids.len <= 1) { 317 var commit_object = try obj.Object(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, &std.fmt.bytesToHex(repo_event.oid, .lower)); 318 defer commit_object.deinit(); 319 320 // read the message from the commit 321 try commit_object.object_reader.seekTo(commit_object.content.commit.message_position); 322 const message = try commit_object.object_reader.interface.allocRemaining(arena.allocator(), .unlimited); 323 324 const event = try std.json.parseFromSliceLeaky(Event, arena.allocator(), message, .{}); 325 326 // get the id of the current event as bytes 327 var current_event_id: [event_id_size]u8 = undefined; 328 _ = try std.fmt.hexToBytes(&current_event_id, &event.id); 329 330 switch (event.data) { 331 .user => |data| { 332 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->user")); 333 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 334 335 const user_cursor = try event_id_to_user.putCursor(hash.hashInt(repo_opts.hash, &current_event_id)); 336 const user_map = try DB.HashMap(.read_write).init(user_cursor); 337 338 try upsert(DB, repo_opts.hash, user_map, @TypeOf(data), data); 339 }, 340 .issue => |data| { 341 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->issue")); 342 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 343 344 const issue_cursor = try event_id_to_issue.putCursor(hash.hashInt(repo_opts.hash, &current_event_id)); 345 const issue = try DB.HashMap(.read_write).init(issue_cursor); 346 347 try upsert(DB, repo_opts.hash, issue, @TypeOf(data), data); 348 }, 349 } 350 } 351 352 // the current object id is now the last one 353 last_object_id_maybe = repo_event.oid; 354 355 // prevent any of the data created above from being mutated by future iterations of this loop 356 try state.core.db.freeze(); 357 } 358 359 if (last_object_id_maybe) |*last_object_id| { 360 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = last_object_id }); 361 } 362 } 363 364 fn upsert( 365 comptime DB: type, 366 comptime hash_kind: hash.HashKind, 367 map: DB.HashMap(.read_write), 368 comptime Data: type, 369 data: Data, 370 ) !void { 371 switch (@typeInfo(Data)) { 372 .@"struct" => |struct_info| { 373 inline for (struct_info.fields) |field| { 374 try upsertField(DB, hash_kind, map, field.name, field.type, @field(data, field.name)); 375 } 376 }, 377 else => @compileError("upsert expects a struct"), 378 } 379 } 380 381 fn mergeChangedMapEntries( 382 comptime DB: type, 383 target: DB.HashMap(.read_write), 384 parent: DB.HashMap(.read_only), 385 baseline: DB.HashMap(.read_only), 386 comptime is_top_level: bool, 387 ) !void { 388 var parent_iter = try parent.iterator(); 389 while (try parent_iter.next()) |kv_pair_cursor| { 390 const kv_pair = try kv_pair_cursor.readKeyValuePair(); 391 392 const baseline_value_cursor = try baseline.getCursor(kv_pair.hash) orelse { 393 if ((try target.getCursor(kv_pair.hash)) != null) { 394 return error.MergeConflict; 395 } 396 397 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 398 continue; 399 }; 400 401 const tag = kv_pair.value_cursor.slot().tag; 402 403 if (tag != baseline_value_cursor.slot().tag) { 404 return error.UnexpectedTag; 405 } 406 407 if (kv_pair.value_cursor.slot().value == baseline_value_cursor.slot().value) { 408 continue; 409 } 410 411 if (tag == .hash_map) { 412 const target_existing_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 413 if (target_existing_cursor.slot().tag != .hash_map) { 414 return error.MergeConflict; 415 } 416 417 const target_child_cursor = try target.putCursor(kv_pair.hash); 418 const target_child = try DB.HashMap(.read_write).init(target_child_cursor); 419 const parent_child = try DB.HashMap(.read_only).init(kv_pair.value_cursor); 420 const baseline_child = try DB.HashMap(.read_only).init(baseline_value_cursor); 421 try mergeChangedMapEntries(DB, target_child, parent_child, baseline_child, false); 422 } else if (is_top_level) { 423 // the moment-index is a top-level key in the haxy moment 424 // and is not a map. we don't want to do any merge of this. 425 continue; 426 } else { 427 const target_value_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 428 if (target_value_cursor.slot().value != baseline_value_cursor.slot().value) { 429 return error.MergeConflict; 430 } 431 432 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 433 } 434 } 435 } 436 437 fn upsertField( 438 comptime DB: type, 439 comptime hash_kind: hash.HashKind, 440 map: DB.HashMap(.read_write), 441 comptime field_name: []const u8, 442 comptime Field: type, 443 value: Field, 444 ) !void { 445 const key = hash.hashInt(hash_kind, field_name); 446 447 switch (@typeInfo(Field)) { 448 .pointer => |pointer_info| { 449 if (pointer_info.size == .slice and pointer_info.child == u8) { 450 try upsertBytes(DB, hash_kind, map, key, value); 451 } else { 452 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 453 } 454 }, 455 .array => |array_info| { 456 if (array_info.child == u8) { 457 try upsertBytes(DB, hash_kind, map, key, &value); 458 } else { 459 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 460 } 461 }, 462 .int => |int_info| switch (int_info.signedness) { 463 .unsigned => { 464 if (try map.getCursor(key)) |value_cursor| { 465 if (try value_cursor.readUint() == value) { 466 return; 467 } 468 } 469 470 try map.put(key, .{ .uint = value }); 471 }, 472 .signed => { 473 if (try map.getCursor(key)) |value_cursor| { 474 if (try value_cursor.readInt() == value) { 475 return; 476 } 477 } 478 479 try map.put(key, .{ .int = value }); 480 }, 481 }, 482 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 483 } 484 } 485 486 fn upsertBytes( 487 comptime DB: type, 488 comptime hash_kind: hash.HashKind, 489 map: DB.HashMap(.read_write), 490 key: hash.HashInt(hash_kind), 491 value: []const u8, 492 ) !void { 493 var existing_cursor_maybe = try map.getCursor(key); 494 if (existing_cursor_maybe) |*existing_cursor| { 495 if (try bytesEqual(DB, existing_cursor, value)) { 496 return; 497 } 498 } 499 500 var value_cursor = try map.putCursor(key); 501 var write_buffer: [1024]u8 = undefined; 502 var writer = try value_cursor.writer(&write_buffer); 503 try writer.interface.writeAll(value); 504 try writer.finish(); 505 } 506 507 fn bytesEqual( 508 comptime DB: type, 509 cursor: *DB.Cursor(.read_only), 510 value: []const u8, 511 ) !bool { 512 var read_buffer: [1024]u8 = undefined; 513 var reader = try cursor.reader(&read_buffer); 514 if (reader.size != value.len) { 515 return false; 516 } 517 518 var chunk_buffer: [1024]u8 = undefined; 519 var offset: usize = 0; 520 while (offset < value.len) { 521 const chunk_len = @min(chunk_buffer.len, value.len - offset); 522 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 523 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 524 return false; 525 } 526 offset += chunk_len; 527 } 528 529 return true; 530 } 531