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