╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1291 }, 1292 .int => |int_info| switch (int_info.signedness) { 1293 .unsigned => { 1294 if (try map.getCursor(key)) |value_cursor| { 1295 if (try value_cursor.readUint() == value) { 1296 return; 1297 } 1298 } 1299 1300 try map.put(key, .{ .uint = value }); 1301 }, 1302 .signed => { 1303 if (try map.getCursor(key)) |value_cursor| { 1304 if (try value_cursor.readInt() == value) { 1305 return; 1306 } 1307 } 1308 1309 try map.put(key, .{ .int = value }); 1310 }, 1311 }, 1312 .bool => { 1313 const bytes = if (value) "true" else "false"; 1314 if (try map.getCursor(key)) |value_cursor| { 1315 var buffer: [5]u8 = undefined; 1316 const existing = try value_cursor.readBytesObject(&buffer); 1317 if (existing.format_tag) |format_tag| { 1318 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 1319 return; 1320 } 1321 } 1322 } 1323 1324 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 1325 }, 1326 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 1327 .optional => |optional_info| { 1328 if (optional_info.child != []const u8) @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1329 // a missing key is null 1330 if (value) |bytes| { 1331 try upsertBytes(DB, hash_kind, map, key, bytes); 1332 } else { 1333 _ = try map.remove(key); 1334 } 1335 }, 1336 // a struct field's own fields are stored in the same map 1337 .@"struct" => try upsert(Field, DB, hash_kind, map, value), 1338 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 1339 } 1340 } 1341 1342 fn upsertBytes( 1343 comptime DB: type, 1344 comptime hash_kind: hash.HashKind, 1345 map: DB.HashMap(.read_write), 1346 key: hash.HashInt(hash_kind), 1347 value: []const u8, 1348 ) !void { 1349 var existing_cursor_maybe = try map.getCursor(key); 1350 if (existing_cursor_maybe) |*existing_cursor| { 1351 if (try bytesEqual(DB, existing_cursor, value)) { 1352 return; 1353 } 1354 } 1355 1356 var value_cursor = try map.putCursor(key); 1357 var write_buffer: [1024]u8 = undefined; 1358 var writer = try value_cursor.writer(&write_buffer); 1359 try writer.interface.writeAll(value); 1360 try writer.finish(); 1361 } 1362 1363 fn bytesEqual( 1364 comptime DB: type, 1365 cursor: *DB.Cursor(.read_only), 1366 value: []const u8, 1367 ) !bool { 1368 var read_buffer: [1024]u8 = undefined; 1369 var reader = try cursor.reader(&read_buffer); 1370 if (reader.size != value.len) { 1371 return false; 1372 } 1373 1374 var chunk_buffer: [1024]u8 = undefined; 1375 var offset: usize = 0; 1376 while (offset < value.len) { 1377 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1378 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1379 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1380 return false; 1381 } 1382 offset += chunk_len; 1383 } 1384 1385 return true; 1386 } 1387 1388 // 1389 // CommitIterator 1390 // 1391 // differs from xit's ObjectIterator in a few ways: 1392 // 1393 // 1. iterates parents first, rather than children first 1394 // 2. `next` only returns the object id, not the full Object 1395 // 3. stores its temporary state in a file on disk to avoid OOMs 1396 // 1397 1398 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1399 return struct { 1400 const DB = EventDB(repo_opts.hash); 1401 const db_name = "haxy-repo-events.db"; 1402 1403 repo_dir: std.Io.Dir, 1404 db_file: std.Io.File, 1405 db: *DB, 1406 // map of each object id to its children 1407 parent_to_children: DB.HashMap(.read_write), 1408 // tracking pending parents is necessary because a child can only 1409 // be returned after all of its unconsumed parents have been returned 1410 child_to_pending_parents: DB.HashMap(.read_write), 1411 // queue of object ids that are ready to be returned from `next` 1412 ready_queue: DB.ArrayList(.read_write), 1413 ready_queue_index: u64, 1414 // the object id at the tip of this branch 1415 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1416 // the object id furthest back in the history that hasn't been consumed 1417 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1418 1419 pub fn init( 1420 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1421 io: std.Io, 1422 allocator: std.mem.Allocator, 1423 consumed_object_ids: DB.HashMap(.read_only), 1424 ref: rf.Ref, 1425 ) !CommitIterator(repo_kind, repo_opts) { 1426 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 1427 errdefer { 1428 db_file.close(io); 1429 state.core.repo_dir.deleteFile(io, db_name) catch {}; 1430 } 1431 1432 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 1433 errdefer allocator.destroy(buffer_ptr); 1434 1435 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 1436 errdefer buffer_ptr.deinit(); 1437 1438 const db_ptr = try allocator.create(DB); 1439 errdefer allocator.destroy(db_ptr); 1440 // the db is scratch state that gets deleted after iteration, so 1441 // there is nothing worth fsyncing 1442 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr, .fsync = false }); 1443 1444 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 1445 1446 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 1447 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 1448 1449 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 1450 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 1451 1452 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 1453 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 1454 1455 var self = CommitIterator(repo_kind, repo_opts){ 1456 .repo_dir = state.core.repo_dir, 1457 .db_file = db_file, 1458 .db = db_ptr, 1459 .parent_to_children = parent_to_children, 1460 .child_to_pending_parents = child_to_pending_parents, 1461 .ready_queue = ready_queue, 1462 .ready_queue_index = 0, 1463 .newest_object_id = null, 1464 .oldest_object_id = null, 1465 }; 1466 errdefer self.deinit(io, allocator); 1467 1468 try self.collect(state, io, allocator, consumed_object_ids, ref); 1469 1470 return self; 1471 } 1472 1473 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 1474 self.db_file.close(io); 1475 self.db.core.memory.buffer.deinit(); 1476 allocator.destroy(self.db.core.memory.buffer); 1477 self.repo_dir.deleteFile(io, db_name) catch {}; 1478 allocator.destroy(self.db); 1479 } 1480 1481 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 1482 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 1483 1484 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 1485 self.ready_queue_index += 1; 1486 1487 try self.enqueueChildren(&oid); 1488 return oid; 1489 } 1490 1491 fn collect( 1492 self: *CommitIterator(repo_kind, repo_opts), 1493 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1494 io: std.Io, 1495 allocator: std.mem.Allocator, 1496 consumed_object_ids: DB.HashMap(.read_only), 1497 ref: rf.Ref, 1498 ) !void { 1499 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 1500 1501 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 1502 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 1503 var walk_queue_index: u64 = 0; 1504 1505 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 1506 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 1507 1508 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 1509 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1510 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 1511 try walk_queue.append(.{ .bytes = &head_oid }); 1512 1513 while (walk_queue_index < try walk_queue.count()) { 1514 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 1515 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 1516 1517 walk_queue_index += 1; 1518 1519 // if we've seen this object id, skip it 1520 if (null != try seen_object_ids.getCursor(oid_int)) { 1521 continue; 1522 } 1523 1524 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 1525 1526 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 1527 defer commit_object.deinit(); 1528 1529 // if this object id has already been consumed, skip it 1530 if (null != try consumed_object_ids.getCursor(oid_int)) { 1531 continue; 1532 } 1533 1534 if (self.newest_object_id == null) self.newest_object_id = oid; 1535 self.oldest_object_id = oid; 1536 1537 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 1538 var pending_parent_count: u64 = 0; 1539 for (parent_oids) |*parent_oid| { 1540 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1541 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 1542 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 1543 1544 // if this object id has already been consumed, skip it 1545 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 1546 continue; 1547 } 1548 1549 // if this object id hasn't already been seen, add it to the walk queue 1550 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 1551 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 1552 } 1553 1554 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 1555 const children = try DB.HashSet(.read_write).init(children_cursor); 1556 try children.put(oid_int, .{ .bytes = &oid }); 1557 pending_parent_count += 1; 1558 } 1559 1560 if (pending_parent_count == 0) { 1561 try self.ready_queue.append(.{ .bytes = &oid }); 1562 } else { 1563 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 1564 } 1565 } 1566 } 1567 1568 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 1569 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 1570 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 1571 const children = try DB.HashSet(.read_only).init(children_cursor); 1572 var children_iter = try children.iterator(); 1573 1574 while (try children_iter.next()) |*child_cursor| { 1575 const kv_pair = try child_cursor.readKeyValuePair(); 1576 const child_oid_int = kv_pair.hash; 1577 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 1578 const pending_parent_count = try pending_parent_cursor.readUint(); 1579 1580 if (pending_parent_count <= 1) { 1581 _ = try self.child_to_pending_parents.remove(child_oid_int); 1582 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 1583 try self.ready_queue.append(.{ .bytes = &child_oid }); 1584 } else { 1585 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 1586 } 1587 } 1588 } 1589 1590 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 1591 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 1592 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1593 _ = try oid_cursor.readBytes(&oid); 1594 return oid; 1595 } 1596 }; 1597 } 1598