╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1195 comptime hash_kind: hash.HashKind, 1196 map: DB.HashMap(.read_write), 1197 event: T, 1198 ) !void { 1199 switch (@typeInfo(T)) { 1200 .@"struct" => |struct_info| { 1201 inline for (struct_info.fields) |field| { 1202 try upsertField(DB, hash_kind, map, field.name, field.type, @field(event, field.name)); 1203 } 1204 }, 1205 else => @compileError("upsert expects a struct"), 1206 } 1207 } 1208 1209 fn upsertField( 1210 comptime DB: type, 1211 comptime hash_kind: hash.HashKind, 1212 map: DB.HashMap(.read_write), 1213 comptime field_name: []const u8, 1214 comptime Field: type, 1215 value: Field, 1216 ) !void { 1217 const key = hash.hashInt(hash_kind, field_name); 1218 1219 switch (@typeInfo(Field)) { 1220 .pointer => |pointer_info| { 1221 if (pointer_info.size == .slice and pointer_info.child == u8) { 1222 try upsertBytes(DB, hash_kind, map, key, value); 1223 } else { 1224 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1225 } 1226 }, 1227 .array => |array_info| { 1228 if (array_info.child == u8) { 1229 try upsertBytes(DB, hash_kind, map, key, &value); 1230 } else { 1231 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1232 } 1233 }, 1234 .int => |int_info| switch (int_info.signedness) { 1235 .unsigned => { 1236 if (try map.getCursor(key)) |value_cursor| { 1237 if (try value_cursor.readUint() == value) { 1238 return; 1239 } 1240 } 1241 1242 try map.put(key, .{ .uint = value }); 1243 }, 1244 .signed => { 1245 if (try map.getCursor(key)) |value_cursor| { 1246 if (try value_cursor.readInt() == value) { 1247 return; 1248 } 1249 } 1250 1251 try map.put(key, .{ .int = value }); 1252 }, 1253 }, 1254 .bool => { 1255 const bytes = if (value) "true" else "false"; 1256 if (try map.getCursor(key)) |value_cursor| { 1257 var buffer: [5]u8 = undefined; 1258 const existing = try value_cursor.readBytesObject(&buffer); 1259 if (existing.format_tag) |format_tag| { 1260 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 1261 return; 1262 } 1263 } 1264 } 1265 1266 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 1267 }, 1268 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 1269 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 1270 } 1271 } 1272 1273 fn upsertBytes( 1274 comptime DB: type, 1275 comptime hash_kind: hash.HashKind, 1276 map: DB.HashMap(.read_write), 1277 key: hash.HashInt(hash_kind), 1278 value: []const u8, 1279 ) !void { 1280 var existing_cursor_maybe = try map.getCursor(key); 1281 if (existing_cursor_maybe) |*existing_cursor| { 1282 if (try bytesEqual(DB, existing_cursor, value)) { 1283 return; 1284 } 1285 } 1286 1287 var value_cursor = try map.putCursor(key); 1288 var write_buffer: [1024]u8 = undefined; 1289 var writer = try value_cursor.writer(&write_buffer); 1290 try writer.interface.writeAll(value); 1291 try writer.finish(); 1292 } 1293 1294 fn bytesEqual( 1295 comptime DB: type, 1296 cursor: *DB.Cursor(.read_only), 1297 value: []const u8, 1298 ) !bool { 1299 var read_buffer: [1024]u8 = undefined; 1300 var reader = try cursor.reader(&read_buffer); 1301 if (reader.size != value.len) { 1302 return false; 1303 } 1304 1305 var chunk_buffer: [1024]u8 = undefined; 1306 var offset: usize = 0; 1307 while (offset < value.len) { 1308 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1309 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1310 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1311 return false; 1312 } 1313 offset += chunk_len; 1314 } 1315 1316 return true; 1317 } 1318 1319 // 1320 // CommitIterator 1321 // 1322 // differs from xit's ObjectIterator in a few ways: 1323 // 1324 // 1. iterates parents first, rather than children first 1325 // 2. `next` only returns the object id, not the full Object 1326 // 3. stores its temporary state in a file on disk to avoid OOMs 1327 // 1328 1329 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1330 return struct { 1331 const DB = EventDB(repo_opts.hash); 1332 const db_name = "haxy-repo-events.db"; 1333 1334 repo_dir: std.Io.Dir, 1335 db_file: std.Io.File, 1336 db: *DB, 1337 // map of each object id to its children 1338 parent_to_children: DB.HashMap(.read_write), 1339 // tracking pending parents is necessary because a child can only 1340 // be returned after all of its unconsumed parents have been returned 1341 child_to_pending_parents: DB.HashMap(.read_write), 1342 // queue of object ids that are ready to be returned from `next` 1343 ready_queue: DB.ArrayList(.read_write), 1344 ready_queue_index: u64, 1345 // the object id at the tip of this branch 1346 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1347 // the object id furthest back in the history that hasn't been consumed 1348 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1349 1350 pub fn init( 1351 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1352 io: std.Io, 1353 allocator: std.mem.Allocator, 1354 consumed_object_ids: DB.HashMap(.read_only), 1355 ref: rf.Ref, 1356 ) !CommitIterator(repo_kind, repo_opts) { 1357 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 1358 errdefer { 1359 db_file.close(io); 1360 state.core.repo_dir.deleteFile(io, db_name) catch {}; 1361 } 1362 1363 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 1364 errdefer allocator.destroy(buffer_ptr); 1365 1366 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 1367 errdefer buffer_ptr.deinit(); 1368 1369 const db_ptr = try allocator.create(DB); 1370 errdefer allocator.destroy(db_ptr); 1371 // the db is scratch state that gets deleted after iteration, so 1372 // there is nothing worth fsyncing 1373 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr, .fsync = false }); 1374 1375 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 1376 1377 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 1378 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 1379 1380 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 1381 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 1382 1383 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 1384 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 1385 1386 var self = CommitIterator(repo_kind, repo_opts){ 1387 .repo_dir = state.core.repo_dir, 1388 .db_file = db_file, 1389 .db = db_ptr, 1390 .parent_to_children = parent_to_children, 1391 .child_to_pending_parents = child_to_pending_parents, 1392 .ready_queue = ready_queue, 1393 .ready_queue_index = 0, 1394 .newest_object_id = null, 1395 .oldest_object_id = null, 1396 }; 1397 errdefer self.deinit(io, allocator); 1398 1399 try self.collect(state, io, allocator, consumed_object_ids, ref); 1400 1401 return self; 1402 } 1403 1404 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 1405 self.db_file.close(io); 1406 self.db.core.memory.buffer.deinit(); 1407 allocator.destroy(self.db.core.memory.buffer); 1408 self.repo_dir.deleteFile(io, db_name) catch {}; 1409 allocator.destroy(self.db); 1410 } 1411 1412 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 1413 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 1414 1415 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 1416 self.ready_queue_index += 1; 1417 1418 try self.enqueueChildren(&oid); 1419 return oid; 1420 } 1421 1422 fn collect( 1423 self: *CommitIterator(repo_kind, repo_opts), 1424 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1425 io: std.Io, 1426 allocator: std.mem.Allocator, 1427 consumed_object_ids: DB.HashMap(.read_only), 1428 ref: rf.Ref, 1429 ) !void { 1430 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 1431 1432 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 1433 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 1434 var walk_queue_index: u64 = 0; 1435 1436 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 1437 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 1438 1439 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 1440 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1441 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 1442 try walk_queue.append(.{ .bytes = &head_oid }); 1443 1444 while (walk_queue_index < try walk_queue.count()) { 1445 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 1446 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 1447 1448 walk_queue_index += 1; 1449 1450 // if we've seen this object id, skip it 1451 if (null != try seen_object_ids.getCursor(oid_int)) { 1452 continue; 1453 } 1454 1455 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 1456 1457 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 1458 defer commit_object.deinit(); 1459 1460 // if this object id has already been consumed, skip it 1461 if (null != try consumed_object_ids.getCursor(oid_int)) { 1462 continue; 1463 } 1464 1465 if (self.newest_object_id == null) self.newest_object_id = oid; 1466 self.oldest_object_id = oid; 1467 1468 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 1469 var pending_parent_count: u64 = 0; 1470 for (parent_oids) |*parent_oid| { 1471 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1472 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 1473 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 1474 1475 // if this object id has already been consumed, skip it 1476 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 1477 continue; 1478 } 1479 1480 // if this object id hasn't already been seen, add it to the walk queue 1481 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 1482 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 1483 } 1484 1485 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 1486 const children = try DB.HashSet(.read_write).init(children_cursor); 1487 try children.put(oid_int, .{ .bytes = &oid }); 1488 pending_parent_count += 1; 1489 } 1490 1491 if (pending_parent_count == 0) { 1492 try self.ready_queue.append(.{ .bytes = &oid }); 1493 } else { 1494 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 1495 } 1496 } 1497 } 1498 1499 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 1500 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 1501 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 1502 const children = try DB.HashSet(.read_only).init(children_cursor); 1503 var children_iter = try children.iterator(); 1504 1505 while (try children_iter.next()) |*child_cursor| { 1506 const kv_pair = try child_cursor.readKeyValuePair(); 1507 const child_oid_int = kv_pair.hash; 1508 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 1509 const pending_parent_count = try pending_parent_cursor.readUint(); 1510 1511 if (pending_parent_count <= 1) { 1512 _ = try self.child_to_pending_parents.remove(child_oid_int); 1513 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 1514 try self.ready_queue.append(.{ .bytes = &child_oid }); 1515 } else { 1516 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 1517 } 1518 } 1519 } 1520 1521 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 1522 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 1523 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1524 _ = try oid_cursor.readBytes(&oid); 1525 return oid; 1526 } 1527 }; 1528 } 1529