╭ 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 created_ts: u64 = 0, // the commit timestamp of the event that first created this repo 11 12 const Self = @This(); 13 14 pub const name_max_len = 32; 15 16 // `created_ts` comes from the commit timestamp, not the event 17 // payload, so it must not appear in the JSON we output 18 pub fn jsonStringify(self: @This(), jw: anytype) !void { 19 try jw.beginObject(); 20 inline for (std.meta.fields(@This())) |field| { 21 if (comptime std.mem.eql(u8, field.name, "created_ts")) continue; 22 try jw.objectField(field.name); 23 try jw.write(@field(self, field.name)); 24 } 25 try jw.endObject(); 26 } 27 28 pub fn consume( 29 comptime DB: type, 30 comptime hash_kind: hash.HashKind, 31 haxy_moment: DB.HashMap(.read_write), 32 event_id: *const [evt.event_id_size]u8, 33 event_maybe: ?@This(), 34 arena: *std.heap.ArenaAllocator, 35 created_ts: u64, 36 ) !void { 37 const repo_key = hash.hashInt(hash_kind, event_id); 38 39 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->repo")); 40 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 41 42 const name_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "name->repo-id")); 43 const name_to_repo_id = try DB.HashMap(.read_write).init(name_to_repo_id_cursor); 44 45 if (event_maybe) |event| { 46 if (event.name.len > name_max_len) return error.NameTooLong; 47 48 var event_to_write = event; 49 50 // the index is keyed by "user-id/repo-name": a repo is unique per 51 // owner, and the read side resolves the url's username to its user id. 52 const repo_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ event.user_id, event.name }); 53 54 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 55 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 56 57 // if this event_id already maps to a repo under a different key (its 58 // name or owner changed), drop the stale index entries first 59 const existing_cursor_maybe = try event_id_to_repo.getCursor(repo_key); 60 if (existing_cursor_maybe) |existing_cursor| { 61 const existing_repo = try DB.HashMap(.read_only).init(existing_cursor); 62 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_repo); 63 // updates preserve the original creation timestamp 64 event_to_write.created_ts = existing_event.created_ts; 65 const existing_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ existing_event.user_id, existing_event.name }); 66 if (!std.mem.eql(u8, existing_path, repo_path)) { 67 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 68 } 69 if (!std.mem.eql(u8, existing_event.user_id, event.user_id)) { 70 const old_user_repos_cursor = try user_id_to_repo_id_set.putCursor(hash.hashInt(hash_kind, existing_event.user_id)); 71 const old_user_repos = try DB.SortedSet(.read_write).init(old_user_repos_cursor); 72 const order_key = evt.orderKey(existing_event.created_ts, event_id); 73 _ = try old_user_repos.remove(&order_key); 74 } 75 } else { 76 // first time we've seen this repo: stamp it with the commit timestamp 77 event_to_write.created_ts = created_ts; 78 } 79 80 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 81 const repo = try DB.HashMap(.read_write).init(repo_cursor); 82 try evt.upsert(@This(), DB, hash_kind, repo, event_to_write); 83 84 try name_to_repo_id.put(hash.hashInt(hash_kind, repo_path), .{ .bytes = event_id }); 85 86 // first time we've seen this repo: add it to the ordered set the repos 87 // view paginates through. the key (orderKey) embeds the event id, so the 88 // set needs no value. 89 if (existing_cursor_maybe == null) { 90 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "repo-id-set")); 91 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 92 const order_key = evt.orderKey(event_to_write.created_ts, event_id); 93 try repo_id_set.put(&order_key); 94 } 95 96 // each user's repos are a set ordered by creation time (oldest first), so 97 // the user page paginates them the same way the home repos list does. 98 const user_repos_cursor = try user_id_to_repo_id_set.putCursor(hash.hashInt(hash_kind, event.user_id)); 99 const user_repos = try DB.SortedSet(.read_write).init(user_repos_cursor); 100 const user_repo_order_key = evt.orderKey(event_to_write.created_ts, event_id); 101 try user_repos.put(&user_repo_order_key); 102 } else { 103 if (try event_id_to_repo.getCursor(repo_key)) |existing_repo_cursor| { 104 const existing_repo = try DB.HashMap(.read_only).init(existing_repo_cursor); 105 const existing_repo_event = try evt.read(@This(), DB, hash_kind, arena, existing_repo); 106 107 const existing_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ existing_repo_event.user_id, existing_repo_event.name }); 108 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 109 110 // drop it from the ordered set using its recorded creation timestamp 111 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "repo-id-set")); 112 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 113 const order_key = evt.orderKey(existing_repo_event.created_ts, event_id); 114 _ = try repo_id_set.remove(&order_key); 115 116 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 117 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 118 119 const user_key = hash.hashInt(hash_kind, existing_repo_event.user_id); 120 const user_repos_cursor = try user_id_to_repo_id_set.putCursor(user_key); 121 const user_repos = try DB.SortedSet(.read_write).init(user_repos_cursor); 122 _ = try user_repos.remove(&order_key); 123 } 124 if (!try event_id_to_repo.remove(repo_key)) return error.EventNotFound; 125 } 126 } 127 128 // a repo plus its event id (the id is the on-disk repo directory name, so the 129 // caller can locate the working repo under <server>/repos/<hex event id>). 130 pub const RepoWithId = struct { 131 repo: Self, 132 event_id: [evt.event_id_size]u8, 133 }; 134 135 // read a repo by its owner's name and repo name. the owner name is resolved to 136 // a user id via name->user-id (the repo index is keyed by "user-id/repo-name"), 137 // matching the /repo/username/reponame url. 138 pub fn readByOwnerAndName( 139 comptime DB: type, 140 comptime hash_kind: hash.HashKind, 141 haxy_moment: DB.HashMap(.read_only), 142 arena: *std.heap.ArenaAllocator, 143 owner_name: []const u8, 144 repo_name: []const u8, 145 ) !?RepoWithId { 146 // owner name -> user id 147 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return null; 148 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 149 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, owner_name)) orelse return null; 150 var user_id: [evt.event_id_size]u8 = undefined; 151 _ = try user_id_cursor.readBytes(&user_id); 152 153 // "user-id/repo-name" -> repo event id 154 const repo_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ user_id[0..], repo_name }); 155 const name_to_repo_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->repo-id")) orelse return null; 156 const name_to_repo_id = try DB.HashMap(.read_only).init(name_to_repo_id_cursor); 157 const repo_id_cursor = try name_to_repo_id.getCursor(hash.hashInt(hash_kind, repo_path)) orelse return null; 158 var repo_id: [evt.event_id_size]u8 = undefined; 159 _ = try repo_id_cursor.readBytes(&repo_id); 160 161 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return null; 162 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 163 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, &repo_id)) orelse return null; 164 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 165 return .{ .repo = try evt.read(@This(), DB, hash_kind, arena, repo_map), .event_id = repo_id }; 166 } 167