╭ 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 const existing_cursor_maybe = try event_id_to_repo.getCursor(repo_key); 41 if (existing_cursor_maybe) |existing_cursor| { 42 const existing_repo = try DB.HashMap(.read_only).init(existing_cursor); 43 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_repo); 44 const existing_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ existing_event.user_id, existing_event.name }); 45 if (!std.mem.eql(u8, existing_path, repo_path)) { 46 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 47 } 48 } 49 50 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 51 const repo = try DB.HashMap(.read_write).init(repo_cursor); 52 try evt.upsert(@This(), DB, hash_kind, repo, event); 53 54 try name_to_repo_id.put(hash.hashInt(hash_kind, repo_path), .{ .bytes = event_id }); 55 56 // first time we've seen this repo: append its id to the ordered list 57 // the repos view paginates through (a delete leaves a tombstone here). 58 if (existing_cursor_maybe == null) { 59 const repo_list_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "repo-list")); 60 const repo_list = try DB.ArrayList(.read_write).init(repo_list_cursor); 61 try repo_list.append(.{ .bytes = event_id }); 62 } 63 64 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repos")); 65 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 66 67 const user_repos_cursor = try user_id_to_repos.putCursor(hash.hashInt(hash_kind, event.user_id)); 68 const user_repos = try DB.CountedHashSet(.read_write).init(user_repos_cursor); 69 try user_repos.put(repo_key, .{ .bytes = event_id }); 70 } else { 71 if (try event_id_to_repo.getCursor(repo_key)) |existing_repo_cursor| { 72 const existing_repo = try DB.HashMap(.read_only).init(existing_repo_cursor); 73 const existing_repo_event = try evt.read(@This(), DB, hash_kind, arena, existing_repo); 74 75 const existing_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ existing_repo_event.user_id, existing_repo_event.name }); 76 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 77 78 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repos")); 79 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 80 81 const user_key = hash.hashInt(hash_kind, existing_repo_event.user_id); 82 const user_repos_cursor = try user_id_to_repos.putCursor(user_key); 83 const user_repos = try DB.CountedHashSet(.read_write).init(user_repos_cursor); 84 _ = try user_repos.remove(repo_key); 85 } 86 if (!try event_id_to_repo.remove(repo_key)) return error.EventNotFound; 87 } 88 } 89 90 // a repo plus its event id (the id is the on-disk repo directory name, so the 91 // caller can locate the working repo under <server>/repos/<hex event id>). 92 pub const RepoWithId = struct { 93 repo: Self, 94 event_id: [evt.event_id_size]u8, 95 }; 96 97 // read a repo by its owner's name and repo name. the owner name is resolved to 98 // a user id via name->user-id (the repo index is keyed by "user-id/repo-name"), 99 // matching the /repo/username/reponame url. 100 pub fn readByOwnerAndName( 101 comptime DB: type, 102 comptime hash_kind: hash.HashKind, 103 haxy_moment: DB.HashMap(.read_only), 104 arena: *std.heap.ArenaAllocator, 105 owner_name: []const u8, 106 repo_name: []const u8, 107 ) !?RepoWithId { 108 // owner name -> user id 109 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return null; 110 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 111 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, owner_name)) orelse return null; 112 var user_id: [evt.event_id_size]u8 = undefined; 113 _ = try user_id_cursor.readBytes(&user_id); 114 115 // "user-id/repo-name" -> repo event id 116 const repo_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ user_id[0..], repo_name }); 117 const name_to_repo_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->repo-id")) orelse return null; 118 const name_to_repo_id = try DB.HashMap(.read_only).init(name_to_repo_id_cursor); 119 const repo_id_cursor = try name_to_repo_id.getCursor(hash.hashInt(hash_kind, repo_path)) orelse return null; 120 var repo_id: [evt.event_id_size]u8 = undefined; 121 _ = try repo_id_cursor.readBytes(&repo_id); 122 123 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return null; 124 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 125 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, &repo_id)) orelse return null; 126 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 127 return .{ .repo = try evt.read(@This(), DB, hash_kind, arena, repo_map), .event_id = repo_id }; 128 } 129