╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
79 // `user_moment` is the owner's, which holds the grants. a null user is logged 80 // out. 81 pub fn roleOf( 82 comptime DB: type, 83 comptime hash_kind: hash.HashKind, 84 user_moment: DB.HashMap(.read_only), 85 arena: *std.heap.ArenaAllocator, 86 record: Record, 87 repo_id: *const [evt.event_id_size]u8, 88 user_id_maybe: ?[evt.event_id_size]u8, 89 ) !?Role { 90 // what read access gives everyone, logged in or not 91 const base: ?Role = if (record.event.read_access == .public) .read else null; 92 const user_id = user_id_maybe orelse return base; 93 if (std.mem.eql(u8, &record.event.user_id, &std.fmt.bytesToHex(user_id, .lower))) return .owner; 94 95 // a grant is never below the base role 96 return (try evt.Grant.readRole(DB, hash_kind, user_moment, arena, repo_id, &user_id)) orelse base; 97 } 98 99 // the moment keys `evt.merge` reads and writes for this kind 100 pub const merge_policy: evt.MergePolicy = .target_wins; 101 pub const record_map_key = "event-id->repo"; 102 pub const all_id_set_key = "repo-id-set"; 103 pub const name_index_key = "name->repo-id"; 104 105 pub fn validateName(name: []const u8) !void { 106 if (name.len == 0) return error.NameEmpty; 107 if (name.len > name_max_len) return error.NameTooLong; 108 if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return error.InvalidName; 109 110 for (name) |c| { 111 if (!std.ascii.isAlphanumeric(c) and c != '-' and c != '_' and c != '.') 112 return error.InvalidName; 113 } 114 } 115 116 pub fn consume( 117 comptime DB: type, 118 comptime hash_kind: hash.HashKind, 119 haxy_moment: DB.HashMap(.read_write), 120 event_id: *const [evt.event_id_size]u8, 121 record_maybe: ?Record, 122 arena: *std.heap.ArenaAllocator, 123 _: ?[]const u8, 124 ) !void { 125 const repo_key = hash.hashInt(hash_kind, event_id); 126 127 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->repo")); 128 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 129 130 const name_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, name_index_key)); 131 const name_to_repo_id = try DB.SortedMap(.read_write).init(name_to_repo_id_cursor); 132 133 var existing_record_maybe: ?Record = null; 134 const existing_cursor_maybe = try event_id_to_repo.getCursor(repo_key); 135 if (existing_cursor_maybe) |existing_cursor| { 136 const existing_repo = try DB.HashMap(.read_only).init(existing_cursor); 137 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_repo); 138 } 139 140 var record_to_write = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 141 142 if (!record_to_write.removed) { 143 _ = try evt.parseEventId(&record_to_write.event.user_id); 144 try validateName(record_to_write.event.name); 145 } 146 147 if (existing_record_maybe) |existing_record| { 148 // updates preserve the original creation metadata 149 record_to_write.created_order = existing_record.created_order; 150 151 // drop the old active index; active values are re-added below 152 if (!existing_record.removed) { 153 _ = try name_to_repo_id.remove(existing_record.event.name); 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 if (!record_to_write.removed) { 172 try name_to_repo_id.put(record_to_write.event.name, .{ .bytes = event_id }); 173 } 174 } 175 176 // a repo plus its event id (the id is the on-disk repo directory name, so the 177 // caller can locate the working repo under <server>/repos/<hex event id>). 178 pub const RepoWithId = struct { 179 repo: Record, 180 event_id: [evt.event_id_size]u8, 181 }; 182 183 // a repo as one viewer sees it. without a role, the viewer may not see it. 184 pub const RepoWithRole = struct { 185 repo: Record, 186 event_id: [evt.event_id_size]u8, 187 role: ?Role, 188 }; 189 190 // read a repo by its name from its owner's user repo 191 pub fn readByName( 192 comptime DB: type, 193 comptime hash_kind: hash.HashKind, 194 user_moment: DB.HashMap(.read_only), 195 arena: *std.heap.ArenaAllocator, 196 name: []const u8, 197 ) !?RepoWithId { 198 const name_to_repo_id_cursor = try user_moment.getCursor(hash.hashInt(hash_kind, name_index_key)) orelse return null; 199 const name_to_repo_id = try DB.SortedMap(.read_only).init(name_to_repo_id_cursor); 200 const repo_id_cursor = try name_to_repo_id.getCursor(name) orelse return null; 201 var repo_id: [evt.event_id_size]u8 = undefined; 202 _ = try repo_id_cursor.readBytes(&repo_id); 203 const repo = (try readById(DB, hash_kind, user_moment, arena, &repo_id)) orelse return null; 204 return .{ .repo = repo, .event_id = repo_id }; 205 } 206 207 // a deleted repo reads as missing 208 pub fn readById( 209 comptime DB: type, 210 comptime hash_kind: hash.HashKind, 211 haxy_moment: DB.HashMap(.read_only), 212 arena: *std.heap.ArenaAllocator, 213 id: []const u8, 214 ) !?Record { 215 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 216 const records = try DB.HashMap(.read_only).init(records_cursor); 217 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 218 const record = try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 219 return if (record.removed) null else record; 220 } 221