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