╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
23 // a repo's key in the name index. it's unique per owner, so the read side 24 // resolves the url's username to its user id first. 25 pub fn indexKey(self: Record, allocator: std.mem.Allocator) ![]const u8 { 26 return std.fmt.allocPrint(allocator, "{s}/{s}", .{ self.event.user_id, self.event.name }); 27 } 28 }; 29 30 const Self = @This(); 31 32 pub const Access = enum { 33 private, 34 public, 35 }; 36 37 // what a user may do in a repo, ordered so a higher role includes the lower ones 38 pub const Role = enum { 39 none, 40 read, 41 write, 42 owner, 43 44 pub fn atLeast(self: Role, min: Role) bool { 45 return @intFromEnum(self) >= @intFromEnum(min); 46 } 47 48 // whether a user may change an event: anyone's with the write role, 49 // otherwise only one they authored 50 pub fn canModify(self: Role, user_email: []const u8, author_email_maybe: ?[]const u8) bool { 51 if (self.atLeast(.write)) return true; 52 const author_email = author_email_maybe orelse return false; 53 return self.atLeast(.read) and std.mem.eql(u8, user_email, author_email); 54 } 55 }; 56 57 pub const name_max_len = 32; 58 59 // the role `user_id_maybe` holds in the repo. null is a logged-out user, who 60 // gets the base role the access fields give everyone. 61 pub fn roleOf(record: Record, user_id_maybe: ?[evt.event_id_size]u8) Role { 62 const event = record.event; 63 const base: Role = if (event.write_access == .public) .write else if (event.read_access == .public) .read else .none; 64 const user_id = user_id_maybe orelse return base; 65 if (std.mem.eql(u8, event.user_id, &user_id)) return .owner; 66 67 const user_id_hex = std.fmt.bytesToHex(user_id, .lower); 68 if (containsUserId(event.owner_user_ids, &user_id_hex)) return .owner; 69 if (base == .write or containsUserId(event.write_user_ids, &user_id_hex)) return .write; 70 if (base == .read or containsUserId(event.read_user_ids, &user_id_hex)) return .read; 71 return .none; 72 } 73 74 fn containsUserId(user_ids: []const u8, user_id_hex: []const u8) bool { 75 var lines = std.mem.tokenizeScalar(u8, user_ids, '\n'); 76 while (lines.next()) |id| { 77 if (std.mem.eql(u8, id, user_id_hex)) return true; 78 } 79 return false; 80 } 81 82 // the moment keys `evt.merge` reads and writes for this kind 83 pub const merge_policy: evt.MergePolicy = .target_wins; 84 pub const record_map_key = "event-id->repo"; 85 pub const all_id_set_key = "repo-id-set"; 86 pub const active_id_set_key = "active-repo-id-set"; 87 pub const name_index_key = "name->repo-id"; 88 89 pub fn validateName(name: []const u8) !void { 90 if (name.len == 0) return error.NameEmpty; 91 if (name.len > name_max_len) return error.NameTooLong; 92 if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return error.InvalidName; 93 94 for (name) |c| { 95 if (!std.ascii.isAlphanumeric(c) and c != '-' and c != '_' and c != '.') 96 return error.InvalidName; 97 } 98 } 99 100 fn validateUserIds(user_ids: []const u8) !void { 101 var lines = std.mem.tokenizeScalar(u8, user_ids, '\n'); 102 while (lines.next()) |id| _ = try evt.parseEventId(id); 103 } 104 105 pub fn consume( 106 comptime DB: type, 107 comptime hash_kind: hash.HashKind, 108 haxy_moment: DB.HashMap(.read_write), 109 event_id: *const [evt.event_id_size]u8, 110 record_maybe: ?Record, 111 arena: *std.heap.ArenaAllocator, 112 _: ?[]const u8, 113 ) !void { 114 const repo_key = hash.hashInt(hash_kind, event_id); 115 116 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->repo")); 117 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 118 119 const name_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "name->repo-id")); 120 const name_to_repo_id = try DB.HashMap(.read_write).init(name_to_repo_id_cursor); 121 122 var existing_record_maybe: ?Record = null; 123 const existing_cursor_maybe = try event_id_to_repo.getCursor(repo_key); 124 if (existing_cursor_maybe) |existing_cursor| { 125 const existing_repo = try DB.HashMap(.read_only).init(existing_cursor); 126 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_repo); 127 } 128 129 var record_to_write = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 130 131 if (!record_to_write.removed) { 132 try validateName(record_to_write.event.name); 133 try validateUserIds(record_to_write.event.read_user_ids); 134 try validateUserIds(record_to_write.event.write_user_ids); 135 try validateUserIds(record_to_write.event.owner_user_ids); 136 } 137 138 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 139 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 140 141 if (existing_record_maybe) |existing_record| { 142 // updates preserve the original creation metadata 143 record_to_write.created_order = existing_record.created_order; 144 const order_key = evt.orderKeyDesc(existing_record.created_order, event_id); 145 146 // drop the old active indexes; active values are re-added below 147 if (!existing_record.removed) { 148 const existing_path = try existing_record.indexKey(arena.allocator()); 149 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 150 151 const old_user_repos_cursor = try user_id_to_repo_id_set.putCursor(hash.hashInt(hash_kind, existing_record.event.user_id)); 152 const old_user_repos = try DB.SortedSet(.read_write).init(old_user_repos_cursor); 153 _ = try old_user_repos.remove(&order_key); 154 } 155 } 156 157 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 158 const repo = try DB.HashMap(.read_write).init(repo_cursor); 159 try evt.upsert(Record, DB, hash_kind, repo, record_to_write); 160 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .repo, existing_record_maybe, record_to_write); 161 162 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 163 164 // the id set retains removed records so merges can carry removals 165 if (existing_cursor_maybe == null) { 166 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 167 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 168 try repo_id_set.put(&order_key); 169 } 170 171 const active = try DB.SortedSet(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, active_id_set_key))); 172 if (record_to_write.removed) { 173 _ = try active.remove(&order_key); 174 } else { 175 try active.put(&order_key); 176 const repo_path = try record_to_write.indexKey(arena.allocator()); 177 try name_to_repo_id.put(hash.hashInt(hash_kind, repo_path), .{ .bytes = event_id }); 178 179 // each user's repos are ordered by creation time, newest first 180 const user_repos_cursor = try user_id_to_repo_id_set.putCursor(hash.hashInt(hash_kind, record_to_write.event.user_id)); 181 const user_repos = try DB.SortedSet(.read_write).init(user_repos_cursor); 182 try user_repos.put(&order_key); 183 } 184 } 185 186 // a repo plus its event id (the id is the on-disk repo directory name, so the 187 // caller can locate the working repo under <server>/repos/<hex event id>). 188 pub const RepoWithId = struct { 189 repo: Record, 190 event_id: [evt.event_id_size]u8, 191 }; 192 193 // read a repo by its owner's name and repo name. the owner name is resolved to 194 // a user id via name->user-id (the repo index is keyed by "user-id/repo-name"), 195 // matching the /repo/username/reponame url. 196 pub fn readByOwnerAndName( 197 comptime DB: type, 198 comptime hash_kind: hash.HashKind, 199 haxy_moment: DB.HashMap(.read_only), 200 arena: *std.heap.ArenaAllocator, 201 owner_name: []const u8, 202 repo_name: []const u8, 203 ) !?RepoWithId { 204 // owner name -> user id 205 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return null; 206 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 207 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, owner_name)) orelse return null; 208 var user_id: [evt.event_id_size]u8 = undefined; 209 _ = try user_id_cursor.readBytes(&user_id); 210 211 // "user-id/repo-name" -> repo event id 212 const repo_path = try (Record{ .event = .{ .user_id = user_id[0..], .name = repo_name, .description = "" } }).indexKey(arena.allocator()); 213 const name_to_repo_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->repo-id")) orelse return null; 214 const name_to_repo_id = try DB.HashMap(.read_only).init(name_to_repo_id_cursor); 215 const repo_id_cursor = try name_to_repo_id.getCursor(hash.hashInt(hash_kind, repo_path)) orelse return null; 216 var repo_id: [evt.event_id_size]u8 = undefined; 217 _ = try repo_id_cursor.readBytes(&repo_id); 218 219 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return null; 220 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 221 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, &repo_id)) orelse return null; 222 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 223 return .{ .repo = try evt.read(Record, DB, hash_kind, arena, repo_map), .event_id = repo_id }; 224 } 225 226 pub fn readById( 227 comptime DB: type, 228 comptime hash_kind: hash.HashKind, 229 haxy_moment: DB.HashMap(.read_only), 230 arena: *std.heap.ArenaAllocator, 231 id: []const u8, 232 ) !?Record { 233 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 234 const records = try DB.HashMap(.read_only).init(records_cursor); 235 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 236 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 237 } 238