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