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