╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const xit = @import("xit"); 4 const hash = xit.hash; 5 6 user_id: []const u8, 7 name: []const u8, 8 description: []const u8, 9 enable_issue: bool, 10 11 pub const name_max_len = 32; 12 13 pub fn consume( 14 comptime DB: type, 15 comptime hash_kind: hash.HashKind, 16 haxy_moment: DB.HashMap(.read_write), 17 event_id: *const [evt.event_id_size]u8, 18 event_maybe: ?@This(), 19 arena: *std.heap.ArenaAllocator, 20 ) !void { 21 const repo_key = hash.hashInt(hash_kind, event_id); 22 23 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->repo")); 24 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 25 26 const name_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "name->repo-id")); 27 const name_to_repo_id = try DB.HashMap(.read_write).init(name_to_repo_id_cursor); 28 29 if (event_maybe) |event| { 30 if (event.name.len > name_max_len) return error.NameTooLong; 31 32 // the index is keyed by "user-id/repo-name": a repo is unique per 33 // owner, and the read side resolves the url's username to its user id. 34 const repo_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ event.user_id, event.name }); 35 36 // if this event_id already maps to a repo under a different key (its 37 // name or owner changed), drop the stale name->id entry first 38 if (try event_id_to_repo.getCursor(repo_key)) |existing_cursor| { 39 const existing_repo = try DB.HashMap(.read_only).init(existing_cursor); 40 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_repo); 41 const existing_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ existing_event.user_id, existing_event.name }); 42 if (!std.mem.eql(u8, existing_path, repo_path)) { 43 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 44 } 45 } 46 47 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 48 const repo = try DB.HashMap(.read_write).init(repo_cursor); 49 try evt.upsert(@This(), DB, hash_kind, repo, event); 50 51 try name_to_repo_id.put(hash.hashInt(hash_kind, repo_path), .{ .bytes = event_id }); 52 53 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repos")); 54 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 55 56 const user_repos_cursor = try user_id_to_repos.putCursor(hash.hashInt(hash_kind, event.user_id)); 57 const user_repos = try DB.CountedHashSet(.read_write).init(user_repos_cursor); 58 try user_repos.put(repo_key, .{ .bytes = event_id }); 59 } else { 60 if (try event_id_to_repo.getCursor(repo_key)) |existing_repo_cursor| { 61 const existing_repo = try DB.HashMap(.read_only).init(existing_repo_cursor); 62 const existing_repo_event = try evt.read(@This(), DB, hash_kind, arena, existing_repo); 63 64 const existing_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ existing_repo_event.user_id, existing_repo_event.name }); 65 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 66 67 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repos")); 68 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 69 70 const user_key = hash.hashInt(hash_kind, existing_repo_event.user_id); 71 const user_repos_cursor = try user_id_to_repos.putCursor(user_key); 72 const user_repos = try DB.CountedHashSet(.read_write).init(user_repos_cursor); 73 _ = try user_repos.remove(repo_key); 74 } 75 if (!try event_id_to_repo.remove(repo_key)) return error.EventNotFound; 76 } 77 } 78 79 // read a repo by its owner's name and repo name. the owner name is resolved to 80 // a user id via name->user-id (the repo index is keyed by "user-id/repo-name"), 81 // matching the /repo/username/reponame url. 82 pub fn readByOwnerAndName( 83 comptime DB: type, 84 comptime hash_kind: hash.HashKind, 85 haxy_moment: DB.HashMap(.read_only), 86 arena: *std.heap.ArenaAllocator, 87 owner_name: []const u8, 88 repo_name: []const u8, 89 ) !?@This() { 90 // owner name -> user id 91 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return null; 92 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 93 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, owner_name)) orelse return null; 94 var user_id: [evt.event_id_size]u8 = undefined; 95 _ = try user_id_cursor.readBytes(&user_id); 96 97 // "user-id/repo-name" -> repo event id 98 const repo_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ user_id[0..], repo_name }); 99 const name_to_repo_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->repo-id")) orelse return null; 100 const name_to_repo_id = try DB.HashMap(.read_only).init(name_to_repo_id_cursor); 101 const repo_id_cursor = try name_to_repo_id.getCursor(hash.hashInt(hash_kind, repo_path)) orelse return null; 102 var repo_id: [evt.event_id_size]u8 = undefined; 103 _ = try repo_id_cursor.readBytes(&repo_id); 104 105 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return null; 106 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 107 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, &repo_id)) orelse return null; 108 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 109 return try evt.read(@This(), DB, hash_kind, arena, repo_map); 110 } 111