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