╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 20 // a repo's key in the name index. it's unique per owner, so the read side 21 // resolves the url's username to its user id first. 22 pub fn indexKey(self: Record, allocator: std.mem.Allocator) ![]const u8 { 23 return std.fmt.allocPrint(allocator, "{s}/{s}", .{ self.event.user_id, self.event.name }); 24 } 25 }; 26 27 const Self = @This(); 28 29 pub const Access = enum { 30 private, 31 public, 32 }; 33 34 pub const name_max_len = 32; 35 36 // the moment keys `evt.merge` reads and writes for this kind 37 pub const merge_policy: evt.MergePolicy = .target_wins; 38 pub const record_map_key = "event-id->repo"; 39 pub const id_set_key = "repo-id-set"; 40 pub const name_index_key = "name->repo-id"; 41 42 pub fn validateName(name: []const u8) !void { 43 if (name.len == 0) return error.NameEmpty; 44 if (name.len > name_max_len) return error.NameTooLong; 45 if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return error.InvalidName; 46 47 for (name) |c| { 48 if (!std.ascii.isAlphanumeric(c) and c != '-' and c != '_' and c != '.') 49 return error.InvalidName; 50 } 51 } 52 53 fn validateUserIds(user_ids: []const u8) !void { 54 var lines = std.mem.tokenizeScalar(u8, user_ids, '\n'); 55 while (lines.next()) |id| _ = try evt.parseEventId(id); 56 } 57 58 pub fn consume( 59 comptime DB: type, 60 comptime hash_kind: hash.HashKind, 61 haxy_moment: DB.HashMap(.read_write), 62 event_id: *const [evt.event_id_size]u8, 63 record_maybe: ?Record, 64 arena: *std.heap.ArenaAllocator, 65 _: ?[]const u8, 66 ) !void { 67 const repo_key = hash.hashInt(hash_kind, event_id); 68 69 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->repo")); 70 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 71 72 const name_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "name->repo-id")); 73 const name_to_repo_id = try DB.HashMap(.read_write).init(name_to_repo_id_cursor); 74 75 var existing_record_maybe: ?Record = null; 76 const existing_cursor_maybe = try event_id_to_repo.getCursor(repo_key); 77 if (existing_cursor_maybe) |existing_cursor| { 78 const existing_repo = try DB.HashMap(.read_only).init(existing_cursor); 79 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_repo); 80 } 81 82 var record_to_write = if (record_maybe) |record| 83 record 84 else blk: { 85 var record = existing_record_maybe orelse return error.EventNotFound; 86 record.removed = true; 87 break :blk record; 88 }; 89 90 if (!record_to_write.removed) { 91 try validateName(record_to_write.event.name); 92 try validateUserIds(record_to_write.event.read_user_ids); 93 try validateUserIds(record_to_write.event.write_user_ids); 94 } 95 96 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 97 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 98 99 if (existing_record_maybe) |existing_record| { 100 // updates preserve the original creation metadata 101 record_to_write.created_order = existing_record.created_order; 102 const order_key = evt.orderKeyDesc(existing_record.created_order, event_id); 103 104 // drop the old active indexes; active values are re-added below 105 if (!existing_record.removed) { 106 const existing_path = try existing_record.indexKey(arena.allocator()); 107 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 108 109 const old_user_repos_cursor = try user_id_to_repo_id_set.putCursor(hash.hashInt(hash_kind, existing_record.event.user_id)); 110 const old_user_repos = try DB.SortedSet(.read_write).init(old_user_repos_cursor); 111 _ = try old_user_repos.remove(&order_key); 112 } 113 } 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(Record, DB, hash_kind, repo, record_to_write); 118 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .repo, record_to_write.created_order, record_to_write.removed); 119 120 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 121 122 // the id set retains removed records so merges can carry removals 123 if (existing_cursor_maybe == null) { 124 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key)); 125 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 126 try repo_id_set.put(&order_key); 127 } 128 129 if (!record_to_write.removed) { 130 const repo_path = try record_to_write.indexKey(arena.allocator()); 131 try name_to_repo_id.put(hash.hashInt(hash_kind, repo_path), .{ .bytes = event_id }); 132 133 // each user's repos are ordered by creation time, newest first 134 const user_repos_cursor = try user_id_to_repo_id_set.putCursor(hash.hashInt(hash_kind, record_to_write.event.user_id)); 135 const user_repos = try DB.SortedSet(.read_write).init(user_repos_cursor); 136 try user_repos.put(&order_key); 137 } 138 } 139 140 // a repo plus its event id (the id is the on-disk repo directory name, so the 141 // caller can locate the working repo under <server>/repos/<hex event id>). 142 pub const RepoWithId = struct { 143 repo: Record, 144 event_id: [evt.event_id_size]u8, 145 }; 146 147 // read a repo by its owner's name and repo name. the owner name is resolved to 148 // a user id via name->user-id (the repo index is keyed by "user-id/repo-name"), 149 // matching the /repo/username/reponame url. 150 pub fn readByOwnerAndName( 151 comptime DB: type, 152 comptime hash_kind: hash.HashKind, 153 haxy_moment: DB.HashMap(.read_only), 154 arena: *std.heap.ArenaAllocator, 155 owner_name: []const u8, 156 repo_name: []const u8, 157 ) !?RepoWithId { 158 // owner name -> user id 159 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return null; 160 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 161 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, owner_name)) orelse return null; 162 var user_id: [evt.event_id_size]u8 = undefined; 163 _ = try user_id_cursor.readBytes(&user_id); 164 165 // "user-id/repo-name" -> repo event id 166 const repo_path = try (Record{ .event = .{ .user_id = user_id[0..], .name = repo_name, .description = "" } }).indexKey(arena.allocator()); 167 const name_to_repo_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->repo-id")) orelse return null; 168 const name_to_repo_id = try DB.HashMap(.read_only).init(name_to_repo_id_cursor); 169 const repo_id_cursor = try name_to_repo_id.getCursor(hash.hashInt(hash_kind, repo_path)) orelse return null; 170 var repo_id: [evt.event_id_size]u8 = undefined; 171 _ = try repo_id_cursor.readBytes(&repo_id); 172 173 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return null; 174 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 175 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, &repo_id)) orelse return null; 176 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 177 return .{ .repo = try evt.read(Record, DB, hash_kind, arena, repo_map), .event_id = repo_id }; 178 } 179