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