╭ 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 // the moment keys `evt.merge` reads and writes for this kind 27 pub const record_map_key = "event-id->issue"; 28 pub const id_set_key = "issue-id-set"; 29 pub const conflicts_key = "conflicted-issue-id->conflict"; 30 pub const id_to_field_to_oid_key = "issue-id->field->oid"; 31 32 // a "tag status" key names a tag's per-status issue set (tags can't contain 33 // spaces, so the pair is unambiguous) 34 pub const TagStatusKey = [tag_max_len + 1 + Status.longest_len]u8; 35 36 pub fn tagStatusKey(buffer: *TagStatusKey, tag: []const u8, status: Status) ![]const u8 { 37 return std.fmt.bufPrint(buffer, "{s} {s}", .{ tag, @tagName(status) }); 38 } 39 40 pub fn tagIterator(tags: []const u8) std.mem.SplitIterator(u8, .scalar) { 41 return std.mem.splitScalar(u8, tags, ' '); 42 } 43 44 // a title is required, and a too-long tag would fail consumption after the 45 // event is already committed 46 pub fn fieldsValid(title: []const u8, tags: []const u8) bool { 47 if (title.len == 0) return false; 48 var tag_iter = tagIterator(tags); 49 while (tag_iter.next()) |tag| { 50 if (tag.len > tag_max_len) return false; 51 } 52 return true; 53 } 54 55 // `created_ts` comes from the commit timestamp, not the event 56 // payload, so it must not appear in the JSON we output 57 pub fn jsonStringify(self: @This(), jw: anytype) !void { 58 try jw.beginObject(); 59 inline for (std.meta.fields(@This())) |field| { 60 if (comptime std.mem.eql(u8, field.name, "created_ts")) continue; 61 try jw.objectField(field.name); 62 try jw.write(@field(self, field.name)); 63 } 64 try jw.endObject(); 65 } 66 67 pub fn consume( 68 comptime DB: type, 69 comptime hash_kind: hash.HashKind, 70 haxy_moment: DB.HashMap(.read_write), 71 event_id: *const [evt.event_id_size]u8, 72 event_maybe: ?@This(), 73 arena: *std.heap.ArenaAllocator, 74 created_ts: u64, 75 // the commit this event came from, recorded against every field it 76 // changes. null from `merge`, which sets oids and conflicts itself. 77 event_oid: ?[]const u8, 78 ) !void { 79 const issue_key = hash.hashInt(hash_kind, event_id); 80 81 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->issue")); 82 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 83 84 // the per-status sets the issues views list, ordered by creation time, 85 // keyed by status name 86 const status_to_issues_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "status->issue-id-set")); 87 const status_to_issues = try DB.SortedMap(.read_write).init(status_to_issues_cursor); 88 89 // the per-tag, per-status sets, keyed "tag,status" 90 const tag_to_issues_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "tag+status->issue-id-set")); 91 const tag_to_issues = try DB.SortedMap(.read_write).init(tag_to_issues_cursor); 92 93 const conflicts_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, conflicts_key)); 94 const conflicts = try DB.SortedMap(.read_write).init(conflicts_cursor); 95 96 const issue_id_to_field_to_oid_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_to_field_to_oid_key)); 97 const issue_id_to_field_to_oid = try DB.HashMap(.read_write).init(issue_id_to_field_to_oid_cursor); 98 99 if (event_maybe) |event| { 100 var event_to_write = event; 101 102 var existing_event_maybe: ?@This() = null; 103 const existing_cursor_maybe = try event_id_to_issue.getCursor(issue_key); 104 if (existing_cursor_maybe) |existing_cursor| { 105 // updates preserve the original creation timestamp 106 const existing_issue = try DB.HashMap(.read_only).init(existing_cursor); 107 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_issue); 108 existing_event_maybe = existing_event; 109 event_to_write.created_ts = existing_event.created_ts; 110 111 // drop the old status's and tags' entries; the current ones are re-added below 112 const order_key = evt.orderKeyDesc(existing_event.created_ts, event_id); 113 const status_set = try statusSet(DB, status_to_issues, existing_event.status); 114 _ = try status_set.remove(&order_key); 115 try removeFromTagSets(DB, tag_to_issues, existing_event.tags, existing_event.status, &order_key); 116 117 // any event settles the conflict, since resolving in the ui may 118 // keep our own values and so change nothing to detect 119 if (event_oid != null) _ = try conflicts.remove(&order_key); 120 } else { 121 // first time we've seen this issue: stamp it with the commit timestamp 122 event_to_write.created_ts = created_ts; 123 } 124 125 try evt.writeOid(@This(), DB, hash_kind, issue_id_to_field_to_oid, issue_key, existing_event_maybe, event, event_oid); 126 127 const issue_cursor = try event_id_to_issue.putCursor(issue_key); 128 const issue = try DB.HashMap(.read_write).init(issue_cursor); 129 try evt.upsert(@This(), DB, hash_kind, issue, event_to_write); 130 131 const order_key = evt.orderKeyDesc(event_to_write.created_ts, event_id); 132 133 // first time we've seen this issue: add it to the set that enumerates 134 // every issue regardless of status 135 if (existing_cursor_maybe == null) { 136 const issue_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key)); 137 const issue_id_set = try DB.SortedSet(.read_write).init(issue_id_set_cursor); 138 try issue_id_set.put(&order_key); 139 } 140 141 const status_set = try statusSet(DB, status_to_issues, event.status); 142 try status_set.put(&order_key); 143 144 var tag_iter = tagIterator(event.tags); 145 while (tag_iter.next()) |tag| { 146 if (tag.len == 0) continue; 147 if (tag.len > tag_max_len) return error.TagTooLong; 148 var key_buffer: TagStatusKey = undefined; 149 const tag_set_cursor = try tag_to_issues.putCursor(try tagStatusKey(&key_buffer, tag, event.status)); 150 const tag_set = try DB.SortedSet(.read_write).init(tag_set_cursor); 151 try tag_set.put(&order_key); 152 } 153 } else { 154 // drop it from the ordered sets using its recorded creation timestamp and status 155 if (try event_id_to_issue.getCursor(issue_key)) |existing_cursor| { 156 const existing_issue = try DB.HashMap(.read_only).init(existing_cursor); 157 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_issue); 158 const order_key = evt.orderKeyDesc(existing_event.created_ts, event_id); 159 160 const status_set = try statusSet(DB, status_to_issues, existing_event.status); 161 _ = try status_set.remove(&order_key); 162 163 try removeFromTagSets(DB, tag_to_issues, existing_event.tags, existing_event.status, &order_key); 164 165 const issue_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key)); 166 const issue_id_set = try DB.SortedSet(.read_write).init(issue_id_set_cursor); 167 _ = try issue_id_set.remove(&order_key); 168 169 _ = try conflicts.remove(&order_key); 170 _ = try issue_id_to_field_to_oid.remove(issue_key); 171 } 172 if (!try event_id_to_issue.remove(issue_key)) return error.EventNotFound; 173 } 174 } 175 176 // `status`'s sorted set within `statuses`, keyed by status name 177 fn statusSet( 178 comptime DB: type, 179 statuses: DB.SortedMap(.read_write), 180 status: Status, 181 ) !DB.SortedSet(.read_write) { 182 const cursor = try statuses.putCursor(@tagName(status)); 183 return DB.SortedSet(.read_write).init(cursor); 184 } 185 186 // what an update replaces on an issue: its status, or its content 187 pub const Update = union(enum) { 188 status: Status, 189 fields: struct { title: []const u8, tags: []const u8, description: []const u8 }, 190 }; 191 192 // re-emit the issue `id_bytes` names with `change` applied, preserving the 193 // rest of its current state. error.NotFound when the id names no issue. 194 // `repo` must be writable. 195 pub fn update( 196 comptime repo_kind: rp.RepoKind, 197 comptime repo_opts: rp.RepoOpts(repo_kind), 198 io: std.Io, 199 allocator: std.mem.Allocator, 200 repo: *rp.Repo(repo_kind, repo_opts), 201 id_bytes: *const [evt.event_id_size]u8, 202 change: Update, 203 ) !void { 204 var arena = std.heap.ArenaAllocator.init(allocator); 205 defer arena.deinit(); 206 207 const issue = (try readById(repo_kind, repo_opts, io, allocator, &arena, repo, id_bytes)) orelse return error.NotFound; 208 209 var updated = issue; 210 switch (change) { 211 .status => |status| { 212 if (issue.status == status) return; 213 updated.status = status; 214 }, 215 .fields => |fields| { 216 updated.title = fields.title; 217 updated.tags = fields.tags; 218 updated.description = fields.description; 219 }, 220 } 221 222 try evt.commitAndConsume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 223 .id = std.fmt.bytesToHex(id_bytes.*, .lower), 224 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 225 .event = .{ .issue = updated }, 226 }}); 227 } 228 229 // the issue `id_bytes` names in the repo's consumed event db (the repo's own 230 // db for xit; the side db next to a git repo), or null 231 fn readById( 232 comptime repo_kind: rp.RepoKind, 233 comptime repo_opts: rp.RepoOpts(repo_kind), 234 io: std.Io, 235 allocator: std.mem.Allocator, 236 arena: *std.heap.ArenaAllocator, 237 repo: *rp.Repo(repo_kind, repo_opts), 238 id_bytes: *const [evt.event_id_size]u8, 239 ) !?@This() { 240 const DB = evt.EventDB(repo_opts.hash); 241 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; 242 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 243 const moment = (if (event_db_maybe) |*event_db| 244 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 245 else if (repo_kind == .git) 246 return null 247 else 248 evt.currentMoment(repo_opts, repo)) catch return null; 249 const event_id_to_issue_cursor = (try moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue"))) orelse return null; 250 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 251 const issue_cursor = (try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, id_bytes))) orelse return null; 252 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 253 return try evt.read(@This(), DB, repo_opts.hash, arena, issue_map); 254 } 255 256 // remove an issue's order key from its tags' `status` sets, pruning entries 257 // whose set becomes empty 258 fn removeFromTagSets( 259 comptime DB: type, 260 tag_to_issues: DB.SortedMap(.read_write), 261 tags: []const u8, 262 status: Status, 263 order_key: []const u8, 264 ) !void { 265 var tag_iter = tagIterator(tags); 266 while (tag_iter.next()) |tag| { 267 if (tag.len == 0) continue; 268 var key_buffer: TagStatusKey = undefined; 269 const key = try tagStatusKey(&key_buffer, tag, status); 270 const tag_set_cursor = try tag_to_issues.putCursor(key); 271 const tag_set = try DB.SortedSet(.read_write).init(tag_set_cursor); 272 _ = try tag_set.remove(order_key); 273 if (0 == try tag_set.count()) _ = try tag_to_issues.remove(key); 274 } 275 } 276