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