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