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