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