╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Assignment.zig ───────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
113 } 114 115 // put or drop an issue's order key in `email`'s assignee set, pruning an emptied set 116 pub fn indexIssue( 117 comptime DB: type, 118 comptime hash_kind: hash.HashKind, 119 haxy_moment: DB.HashMap(.read_write), 120 email: []const u8, 121 order_key: []const u8, 122 active: bool, 123 ) !void { 124 const by_assignee = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, assignee_to_issue_id_set_key))); 125 const email_key = hash.hashInt(hash_kind, email); 126 if (active) { 127 const issues = try DB.SortedSet(.read_write).init(try by_assignee.putCursor(email_key)); 128 try issues.put(order_key); 129 return; 130 } 131 if (null == try by_assignee.getCursor(email_key)) return; 132 const issues = try DB.SortedSet(.read_write).init(try by_assignee.putCursor(email_key)); 133 _ = try issues.remove(order_key); 134 if (0 == try issues.count()) _ = try by_assignee.remove(email_key); 135 } 136 137 // assign `email` to a thread and return the event id. `repo` must be writable. 138 pub fn create( 139 host: evt.Host, 140 comptime repo_kind: rp.RepoKind, 141 comptime repo_opts: rp.RepoOpts(repo_kind), 142 io: std.Io, 143 allocator: std.mem.Allocator, 144 repo: *rp.Repo(repo_kind, repo_opts), 145 thread_kind: evt.EventKind, 146 thread_id: *const [evt.event_id_size * 2]u8, 147 email: []const u8, 148 author: evt.CommitAuthor, 149 ) ![evt.event_id_size * 2]u8 { 150 if (!emailValid(email)) return error.InvalidFields; 151 152 const thread_id_bytes = evt.parseEventId(thread_id) catch return error.NotFound; 153 if (!try evt.threadHolds(repo_kind, repo_opts, io, allocator, repo, thread_kind, &thread_id_bytes, null)) return error.NotFound; 154 155 const id_bytes = idOf(&thread_id_bytes, email); 156 { 157 var arena = std.heap.ArenaAllocator.init(allocator); 158 defer arena.deinit(); 159 if (try evt.readFromRepo(Self, repo_kind, repo_opts, io, allocator, &arena, repo, &id_bytes)) |existing| { 160 if (!existing.removed) return error.AlreadyAssigned; 161 } 162 } 163 164 const event_id = std.fmt.bytesToHex(id_bytes, .lower); 165 try evt.consume(host, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{.{ 166 .id = event_id, 167 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 168 .author = author, 169 .event = .{ .assign = .{ .thread_id = thread_id.*, .email = email } }, 170 }}); 171 return event_id; 172 } 173 174 // read an assignment by event id, or null if the id isn't a known assignment 175 pub fn readById( 176 comptime DB: type, 177 comptime hash_kind: hash.HashKind, 178 haxy_moment: DB.HashMap(.read_only), 179 arena: *std.heap.ArenaAllocator, 180 assignment_id: []const u8, 181 ) !?Record { 182 return try evt.readRecordSubset(Self, Record, DB, hash_kind, haxy_moment, arena, assignment_id); 183 } 184 185 // the emails assigned to a thread, oldest first 186 pub fn load( 187 comptime hash_kind: hash.HashKind, 188 arena: *std.heap.ArenaAllocator, 189 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 190 thread_id: []const u8, 191 ) ![]const []const u8 { 192 const DB = evt.EventDB(hash_kind); 193 var emails: std.ArrayList([]const u8) = .empty; 194 const index_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, thread_id_to_assignment_id_set_key)) orelse return emails.items; 195 const index = try DB.HashMap(.read_only).init(index_cursor); 196 const set_cursor = try index.getCursor(hash.hashInt(hash_kind, thread_id)) orelse return emails.items; 197 const set = try DB.SortedSet(.read_only).init(set_cursor); 198 199 var iter = try set.iteratorFromIndex(0); 200 while (try iter.next()) |kv_cursor| { 201 const id_bytes = try evt.readOrderKeyId(DB, kv_cursor); 202 const record = (try evt.readRecordSubset(Self, struct { event: struct { email: []const u8 } }, DB, hash_kind, haxy_moment, arena, &id_bytes)) orelse continue; 203 try emails.append(arena.allocator(), record.event.email); 204 } 205 return emails.items; 206 } 207