╭ Issue.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const xit = @import("xit"); 4 const rp = xit.repo; 5 const hash = xit.hash; 6 7 title: []const u8, 8 description: []const u8, 9 tags: []const u8, // space-separated 10 status: Status = .open, 11 created_ts: u64 = 0, // the commit timestamp of the event that first created this issue 12 13 pub const Status = enum { 14 open, 15 closed, 16 17 const longest_len = blk: { 18 var len: usize = 0; 19 for (@typeInfo(Status).@"enum".fields) |field| len = @max(len, field.name.len); 20 break :blk len; 21 }; 22 }; 23 24 pub const tag_max_len = 64; 25 26 // a "tag status" key names a tag's per-status issue set (tags can't contain 27 // spaces, so the pair is unambiguous) 28 pub const TagStatusKey = [tag_max_len + 1 + Status.longest_len]u8; 29 30 pub fn tagStatusKey(buffer: *TagStatusKey, tag: []const u8, status: Status) ![]const u8 { 31 return std.fmt.bufPrint(buffer, "{s} {s}", .{ tag, @tagName(status) }); 32 } 33 34 pub fn tagIterator(tags: []const u8) std.mem.SplitIterator(u8, .scalar) { 35 return std.mem.splitScalar(u8, tags, ' '); 36 } 37 38 // a title is required, and a too-long tag would fail consumption after the 39 // event is already committed 40 pub fn fieldsValid(title: []const u8, tags: []const u8) bool { 41 if (title.len == 0) return false; 42 var tag_iter = tagIterator(tags); 43 while (tag_iter.next()) |tag| { 44 if (tag.len > tag_max_len) return false; 45 } 46 return true; 47 } 48 49 // `created_ts` comes from the commit timestamp, not the event 50 // payload, so it must not appear in the JSON we output 51 pub fn jsonStringify(self: @This(), jw: anytype) !void { 52 try jw.beginObject(); 53 inline for (std.meta.fields(@This())) |field| { 54 if (comptime std.mem.eql(u8, field.name, "created_ts")) continue; 55 try jw.objectField(field.name); 56 try jw.write(@field(self, field.name)); 57 } 58 try jw.endObject(); 59 } 60 61 pub fn consume( 62 comptime DB: type, 63 comptime hash_kind: hash.HashKind, 64 haxy_moment: DB.HashMap(.read_write), 65 event_id: *const [evt.event_id_size]u8, 66 event_maybe: ?@This(), 67 arena: *std.heap.ArenaAllocator, 68 created_ts: u64, 69 ) !void { 70 const issue_key = hash.hashInt(hash_kind, event_id); 71 72 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->issue")); 73 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 74 75 // the per-status sets the issues views list, ordered by creation time, 76 // keyed by status name 77 const status_to_issues_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "status->issue-id-set")); 78 const status_to_issues = try DB.SortedMap(.read_write).init(status_to_issues_cursor); 79 80 // the per-tag, per-status sets, keyed "tag,status" 81 const tag_to_issues_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "tag+status->issue-id-set")); 82 const tag_to_issues = try DB.SortedMap(.read_write).init(tag_to_issues_cursor); 83 84 if (event_maybe) |event| { 85 var event_to_write = event; 86 87 const existing_cursor_maybe = try event_id_to_issue.getCursor(issue_key); 88 if (existing_cursor_maybe) |existing_cursor| { 89 // updates preserve the original creation timestamp 90 const existing_issue = try DB.HashMap(.read_only).init(existing_cursor); 91 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_issue); 92 event_to_write.created_ts = existing_event.created_ts; 93 94 // drop the old status's and tags' entries; the current ones are re-added below 95 const order_key = evt.orderKeyDesc(existing_event.created_ts, event_id); 96 const status_set = try statusSet(DB, status_to_issues, existing_event.status); 97 _ = try status_set.remove(&order_key); 98 try removeFromTagSets(DB, tag_to_issues, existing_event.tags, existing_event.status, &order_key); 99 } else { 100 // first time we've seen this issue: stamp it with the commit timestamp 101 event_to_write.created_ts = created_ts; 102 } 103 104 const issue_cursor = try event_id_to_issue.putCursor(issue_key); 105 const issue = try DB.HashMap(.read_write).init(issue_cursor); 106 try evt.upsert(@This(), DB, hash_kind, issue, event_to_write); 107 108 const order_key = evt.orderKeyDesc(event_to_write.created_ts, event_id); 109 110 const status_set = try statusSet(DB, status_to_issues, event.status); 111 try status_set.put(&order_key); 112 113 var tag_iter = tagIterator(event.tags); 114 while (tag_iter.next()) |tag| { 115 if (tag.len == 0) continue; 116 if (tag.len > tag_max_len) return error.TagTooLong; 117 var key_buffer: TagStatusKey = undefined; 118 const tag_set_cursor = try tag_to_issues.putCursor(try tagStatusKey(&key_buffer, tag, event.status)); 119 const tag_set = try DB.SortedSet(.read_write).init(tag_set_cursor); 120 try tag_set.put(&order_key); 121 } 122 } else { 123 // drop it from the ordered sets using its recorded creation timestamp and status 124 if (try event_id_to_issue.getCursor(issue_key)) |existing_cursor| { 125 const existing_issue = try DB.HashMap(.read_only).init(existing_cursor); 126 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_issue); 127 const order_key = evt.orderKeyDesc(existing_event.created_ts, event_id); 128 129 const status_set = try statusSet(DB, status_to_issues, existing_event.status); 130 _ = try status_set.remove(&order_key); 131 132 try removeFromTagSets(DB, tag_to_issues, existing_event.tags, existing_event.status, &order_key); 133 } 134 if (!try event_id_to_issue.remove(issue_key)) return error.EventNotFound; 135 } 136 } 137 138 // `status`'s sorted set within `statuses`, keyed by status name 139 fn statusSet( 140 comptime DB: type, 141 statuses: DB.SortedMap(.read_write), 142 status: Status, 143 ) !DB.SortedSet(.read_write) { 144 const cursor = try statuses.putCursor(@tagName(status)); 145 return DB.SortedSet(.read_write).init(cursor); 146 } 147 148 // what an update replaces on an issue: its status, or its content 149 pub const Update = union(enum) { 150 status: Status, 151 fields: struct { title: []const u8, tags: []const u8, description: []const u8 }, 152 }; 153 154 // re-emit the issue `id_bytes` names with `change` applied, preserving the 155 // rest of its current state. error.NotFound when the id names no issue. 156 // `repo` must be writable. 157 pub fn update( 158 comptime repo_kind: rp.RepoKind, 159 comptime repo_opts: rp.RepoOpts(repo_kind), 160 io: std.Io, 161 allocator: std.mem.Allocator, 162 repo: *rp.Repo(repo_kind, repo_opts), 163 id_bytes: *const [evt.event_id_size]u8, 164 change: Update, 165 ) !void { 166 var arena = std.heap.ArenaAllocator.init(allocator); 167 defer arena.deinit(); 168 169 const issue = (try readById(repo_kind, repo_opts, io, allocator, &arena, repo, id_bytes)) orelse return error.NotFound; 170 171 var updated = issue; 172 switch (change) { 173 .status => |status| { 174 if (issue.status == status) return; 175 updated.status = status; 176 }, 177 .fields => |fields| { 178 updated.title = fields.title; 179 updated.tags = fields.tags; 180 updated.description = fields.description; 181 }, 182 } 183 184 try evt.commitAndConsume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 185 .id = std.fmt.bytesToHex(id_bytes.*, .lower), 186 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 187 .event = .{ .issue = updated }, 188 }}); 189 } 190 191 // the issue `id_bytes` names in the repo's consumed event db (the repo's own 192 // db for xit; the side db next to a git repo), or null 193 fn readById( 194 comptime repo_kind: rp.RepoKind, 195 comptime repo_opts: rp.RepoOpts(repo_kind), 196 io: std.Io, 197 allocator: std.mem.Allocator, 198 arena: *std.heap.ArenaAllocator, 199 repo: *rp.Repo(repo_kind, repo_opts), 200 id_bytes: *const [evt.event_id_size]u8, 201 ) !?@This() { 202 const DB = evt.EventDB(repo_opts.hash); 203 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 204 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 205 const moment = (if (event_db_maybe) |*event_db| 206 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 207 else if (repo_kind == .git) 208 return null 209 else 210 evt.currentMoment(repo_opts, repo)) catch return null; 211 const event_id_to_issue_cursor = (try moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue"))) orelse return null; 212 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 213 const issue_cursor = (try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, id_bytes))) orelse return null; 214 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 215 return try evt.read(@This(), DB, repo_opts.hash, arena, issue_map); 216 } 217 218 // remove an issue's order key from its tags' `status` sets, pruning entries 219 // whose set becomes empty 220 fn removeFromTagSets( 221 comptime DB: type, 222 tag_to_issues: DB.SortedMap(.read_write), 223 tags: []const u8, 224 status: Status, 225 order_key: []const u8, 226 ) !void { 227 var tag_iter = tagIterator(tags); 228 while (tag_iter.next()) |tag| { 229 if (tag.len == 0) continue; 230 var key_buffer: TagStatusKey = undefined; 231 const key = try tagStatusKey(&key_buffer, tag, status); 232 const tag_set_cursor = try tag_to_issues.putCursor(key); 233 const tag_set = try DB.SortedSet(.read_write).init(tag_set_cursor); 234 _ = try tag_set.remove(order_key); 235 if (0 == try tag_set.count()) _ = try tag_to_issues.remove(key); 236 } 237 } 238