╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
196 const user_map = try userMap(DB, hash_kind, haxy_moment, &user_id) orelse return null; 197 const record = try evt.read(struct { event: Public }, DB, hash_kind, arena, user_map); 198 return record.event; 199 } 200 201 // a user's id via the name->user-id index 202 pub fn readIdByName( 203 comptime DB: type, 204 comptime hash_kind: hash.HashKind, 205 haxy_moment: DB.HashMap(.read_only), 206 name: []const u8, 207 ) !?[evt.event_id_size]u8 { 208 const index_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, name_index_key))) orelse return null; 209 const index = try DB.SortedMap(.read_only).init(index_cursor); 210 const user_id_cursor = (try index.getCursor(name)) orelse return null; 211 var user_id: [evt.event_id_size]u8 = undefined; 212 _ = try user_id_cursor.readBytes(&user_id); 213 return user_id; 214 } 215 216 // a user's record map via the event-id->user index 217 fn userMap( 218 comptime DB: type, 219 comptime hash_kind: hash.HashKind, 220 haxy_moment: DB.HashMap(.read_only), 221 user_id: []const u8, 222 ) !?DB.HashMap(.read_only) { 223 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 224 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 225 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 226 return try DB.HashMap(.read_only).init(user_cursor); 227 } 228 229 // read a user by name from the admin event store, or null if the admin repo or 230 // the user doesn't exist. field byte slices are allocated in `arena`. 231 pub fn readByName( 232 io: std.Io, 233 allocator: std.mem.Allocator, 234 admin_repo_path: []const u8, 235 arena: *std.heap.ArenaAllocator, 236 name: []const u8, 237 ) !?Record { 238 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 239 error.RepoNotFound => return null, 240 else => |e| return e, 241 }; 242 defer repo.deinit(io, allocator); 243 244 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 245 246 const user_id = try readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, name) orelse return null; 247 248 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 249 } 250