╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issue.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
10 description: []const u8, 11 labels: []const u8, // space-separated 12 status: Status = .open, 13 14 // what the db stores: the event's data plus the commit-derived fields 15 pub const Record = struct { 16 event: Self, 17 removed: bool = false, 18 author_email: ?[]const u8 = null, 19 created_order: u64 = 0, 20 updated_order: u64 = 0, 21 }; 22 23 const Self = @This(); 24 25 pub const Status = enum { 26 open, 27 closed, 28 29 const longest_len = blk: { 30 var len: usize = 0; 31 for (@typeInfo(Status).@"enum".fields) |field| len = @max(len, field.name.len); 32 break :blk len; 33 }; 34 }; 35 36 pub const label_max_len = 64; 37 38 // the moment keys `evt.merge` reads and writes for this kind 39 pub const merge_policy: evt.MergePolicy = .field_conflicts; 40 pub const record_map_key = "event-id->issue"; 41 pub const all_id_set_key = "issue-id-set"; 42 pub const conflicts_key = "conflicted-issue-id->conflict"; 43 pub const id_to_field_to_oid_key = "issue-id->field->oid"; 44 pub const status_to_id_set_key = "status->issue-id-set"; 45 pub const label_status_to_id_set_key = "label+status->issue-id-set"; 46 47 // a "label status" key names a label's per-status issue set (labels can't contain 48 // spaces, so the pair is unambiguous) 49 pub const LabelStatusKey = [label_max_len + 1 + Status.longest_len]u8; 50 51 pub fn labelStatusKey(buffer: *LabelStatusKey, label: []const u8, status: Status) ![]const u8 { 52 return std.fmt.bufPrint(buffer, "{s} {s}", .{ label, @tagName(status) }); 53 } 54 55 pub fn labelIterator(labels: []const u8) std.mem.SplitIterator(u8, .scalar) { 56 return std.mem.splitScalar(u8, labels, ' '); 57 } 58 59 // a title is required, and a too-long label would go unindexed once consumed 60 pub fn fieldsValid(title: []const u8, labels: []const u8) bool { 61 if (!evt.titleValid(title)) return false; 62 var label_iter = labelIterator(labels); 63 while (label_iter.next()) |label| { 64 if (label.len > label_max_len) return false; 65 } 66 return true; 67 } 68 69 pub fn consume( 70 comptime DB: type, 71 comptime hash_kind: hash.HashKind, 72 haxy_moment: DB.HashMap(.read_write), 73 event_id: *const [evt.event_id_size]u8, 74 record_maybe: ?Record, 75 arena: *std.heap.ArenaAllocator, 76 // the commit this event came from, recorded against every field it 77 // changes. null from `merge`, which sets oids and conflicts itself. 78 event_oid: ?[]const u8, 79 ) !void { 80 const issue_key = hash.hashInt(hash_kind, event_id); 81 82 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, record_map_key)); 83 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 84 85 // the per-status sets the issues views list, ordered by creation, 86 // keyed by status name 87 const status_to_issues_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, status_to_id_set_key)); 88 const status_to_issues = try DB.SortedMap(.read_write).init(status_to_issues_cursor); 89 90 // the per-label, per-status sets, keyed "label,status" 91 const label_to_issues_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, label_status_to_id_set_key)); 92 const label_to_issues = try DB.SortedMap(.read_write).init(label_to_issues_cursor); 93 94 const conflicts_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, conflicts_key)); 95 const conflicts = try DB.SortedMap(.read_write).init(conflicts_cursor); 96 97 const issue_id_to_field_to_oid_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_to_field_to_oid_key)); 98 const issue_id_to_field_to_oid = try DB.HashMap(.read_write).init(issue_id_to_field_to_oid_cursor); 99 100 var existing_record_maybe: ?Record = null; 101 const existing_cursor_maybe = try event_id_to_issue.getCursor(issue_key); 102 if (existing_cursor_maybe) |existing_cursor| { 103 const existing_issue = try DB.HashMap(.read_only).init(existing_cursor); 104 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_issue); 105 } 106 107 var record_to_write = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 108 109 if (existing_record_maybe) |existing_record| { 110 // updates preserve the original creation order and author 111 record_to_write.created_order = existing_record.created_order; 112 record_to_write.author_email = existing_record.author_email; 113 114 // drop the old status's and labels' entries; active values are re-added below 115 const order_key = evt.orderKeyDesc(existing_record.created_order, event_id); 116 if (!existing_record.removed) { 117 const status_set = try statusSet(DB, status_to_issues, existing_record.event.status); 118 _ = try status_set.remove(&order_key); 119 try removeFromLabelSets(DB, label_to_issues, existing_record.event.labels, existing_record.event.status, &order_key); 120 } 121 122 // any event settles the conflict, since resolving in the ui may 123 // keep our own values and so change nothing to detect 124 if (event_oid != null or record_to_write.removed) _ = try conflicts.remove(&order_key); 125 } 126 127 try evt.writeOid(Self, DB, hash_kind, issue_id_to_field_to_oid, issue_key, if (existing_record_maybe) |existing| existing.event else null, record_to_write.event, event_oid); 128 129 const issue_cursor = try event_id_to_issue.putCursor(issue_key); 130 const issue = try DB.HashMap(.read_write).init(issue_cursor); 131 try evt.upsert(Record, DB, hash_kind, issue, record_to_write); 132 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .issue, existing_record_maybe, record_to_write); 133 134 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 135 136 // the id set retains removed records so merges can carry removals 137 if (existing_cursor_maybe == null) { 138 const issue_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 139 const issue_id_set = try DB.SortedSet(.read_write).init(issue_id_set_cursor); 140 try issue_id_set.put(&order_key); 141 } 142 143 // the search index is keyed by the same order key as the sets above 144 try srch_thrd.update(DB, hash_kind, .issue, haxy_moment, arena.child_allocator, &order_key, existing_record_maybe, record_to_write); 145 146 if (!record_to_write.removed) { 147 const status_set = try statusSet(DB, status_to_issues, record_to_write.event.status); 148 try status_set.put(&order_key); 149 150 var label_iter = labelIterator(record_to_write.event.labels); 151 while (label_iter.next()) |label| { 152 // an over-long label goes unindexed rather than failing the consume, 153 // which would wedge the branch it arrived on 154 if (label.len == 0 or label.len > label_max_len) continue; 155 var key_buffer: LabelStatusKey = undefined; 156 const label_set_cursor = try label_to_issues.putCursor(try labelStatusKey(&key_buffer, label, record_to_write.event.status)); 157 const label_set = try DB.SortedSet(.read_write).init(label_set_cursor); 158 try label_set.put(&order_key); 159 } 160 } 161 } 162 163 // `status`'s sorted set within `statuses`, keyed by status name 164 fn statusSet( 165 comptime DB: type, 166 statuses: DB.SortedMap(.read_write), 167 status: Status, 168 ) !DB.SortedSet(.read_write) { 169 const cursor = try statuses.putCursor(@tagName(status)); 170 return DB.SortedSet(.read_write).init(cursor); 171 } 172 173 // the resolve form's inputs: null fields keep the live values, `hunks` holds 174 // the description conflict hunks' resolutions in chunk order (absent 175 // trailing ones keep our side) 176 pub const Resolve = struct { 177 title: ?[]const u8 = null, 178 labels: ?[]const u8 = null, 179 hunks: []const []const u8 = &.{}, 180 }; 181 182 // what an update replaces on an issue: its status, its content, or — for a 183 // conflicted issue — the resolution of its conflict 184 pub const Update = union(enum) { 185 status: Status, 186 fields: struct { title: []const u8, labels: []const u8, description: []const u8 }, 187 resolve: Resolve, 188 }; 189 190 // re-emit the issue `id_bytes` names with `change` applied, preserving the 191 // rest of its current state. error.NotFound when the id names no issue, 192 // error.InvalidFields when a resolve composes invalid fields. `repo` must be 193 // writable. 194 pub fn update( 195 host: evt.Host, 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 author: evt.CommitAuthor, 204 ) !void { 205 var arena = std.heap.ArenaAllocator.init(allocator); 206 defer arena.deinit(); 207 208 const issue = (try readById(repo_kind, repo_opts, io, allocator, &arena, repo, id_bytes)) orelse return error.NotFound; 209 if (issue.removed) return error.NotFound; 210 211 var updated = issue.event; 212 switch (change) { 213 .status => |status| { 214 if (updated.status == status) return; 215 updated.status = status; 216 }, 217 .fields => |fields| { 218 updated.title = fields.title; 219 updated.labels = fields.labels; 220 updated.description = fields.description; 221 }, 222 .resolve => |resolve| { 223 updated = try resolveFields(repo_kind, repo_opts, io, allocator, &arena, repo, id_bytes, issue, resolve); 224 if (!fieldsValid(updated.title, updated.labels)) return error.InvalidFields; 225 }, 226 } 227 228 try evt.consume(host, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 229 .id = std.fmt.bytesToHex(id_bytes.*, .lower), 230 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 231 .author = author, 232 .event = .{ .issue = updated }, 233 }}); 234 } 235 236 // the live fields with `resolve` applied: submitted title/labels stand in for the 237 // live ones, and a conflicted description is reassembled from the hunk 238 // resolutions against the conflict entry. the chunking is deterministic: the 239 // entry holds immutable slots and any event since would have removed it, so 240 // the hunks match what the resolve form was built from. a settled or 241 // unreadable entry keeps the live description. 242 fn resolveFields( 243 comptime repo_kind: rp.RepoKind, 244 comptime repo_opts: rp.RepoOpts(repo_kind), 245 io: std.Io, 246 allocator: std.mem.Allocator, 247 arena: *std.heap.ArenaAllocator, 248 repo: *rp.Repo(repo_kind, repo_opts), 249 id_bytes: *const [evt.event_id_size]u8, 250 live: Record, 251 resolve: Resolve, 252 ) !Self { 253 const DB = evt.EventDB(repo_opts.hash); 254 const aa = arena.allocator(); 255 256 var updated = live.event; 257 if (resolve.title) |title| updated.title = title; 258 if (resolve.labels) |labels| updated.labels = labels; 259 260 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; 261 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 262 const moment = (if (event_db_maybe) |*event_db| 263 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 264 else if (repo_kind == .git) 265 return updated 266 else 267 evt.currentMoment(repo_opts, repo)) catch return updated; 268 269 const conflicts_cursor = (try moment.getCursor(hash.hashInt(repo_opts.hash, conflicts_key))) orelse return updated; 270 const conflicts = try DB.SortedMap(.read_only).init(conflicts_cursor); 271 const order_key = evt.orderKeyDesc(live.created_order, id_bytes); 272 const conflict_cursor = (try conflicts.getCursor(&order_key)) orelse return updated; 273 const entry = try DB.HashMap(.read_only).init(conflict_cursor); 274 275 const fields_cursor = (try entry.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key))) orelse return updated; 276 const conflicted_fields = try fields_cursor.readBytesAlloc(aa, null); 277 if (std.mem.indexOf(u8, conflicted_fields, "description") == null) return updated; 278 279 const their_cursor = (try entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key))) orelse return updated; 280 const theirs = try evt.read(Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(their_cursor)); 281 282 // absent when both sides created the issue independently 283 var base_description: []const u8 = ""; 284 if (try entry.getCursor(hash.hashInt(repo_opts.hash, evt.base_record_key))) |base_cursor| { 285 const base = try evt.read(Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(base_cursor)); 286 base_description = base.event.description; 287 } 288 289 const chunks = try diff3.chunks(allocator, arena, base_description, live.event.description, theirs.event.description); 290 var resolutions: std.ArrayList([]const u8) = .empty; 291 var hunk_index: usize = 0; 292 for (chunks) |chunk| { 293 if (chunk != .conflict) continue; 294 try resolutions.append(aa, if (hunk_index < resolve.hunks.len) resolve.hunks[hunk_index] else (chunk.conflict.ours orelse "")); 295 hunk_index += 1; 296 } 297 updated.description = try diff3.assemble(aa, chunks, resolutions.items); 298 return updated; 299 } 300 301 // the issue `id_bytes` names in the repo's consumed event db (the repo's own 302 // db for xit; the side db next to a git repo), or null 303 fn readById( 304 comptime repo_kind: rp.RepoKind, 305 comptime repo_opts: rp.RepoOpts(repo_kind), 306 io: std.Io, 307 allocator: std.mem.Allocator, 308 arena: *std.heap.ArenaAllocator, 309 repo: *rp.Repo(repo_kind, repo_opts), 310 id_bytes: *const [evt.event_id_size]u8, 311 ) !?Record { 312 const DB = evt.EventDB(repo_opts.hash); 313 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; 314 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 315 const moment = (if (event_db_maybe) |*event_db| 316 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 317 else if (repo_kind == .git) 318 return null 319 else 320 evt.currentMoment(repo_opts, repo)) catch return null; 321 const event_id_to_issue_cursor = (try moment.getCursor(hash.hashInt(repo_opts.hash, record_map_key))) orelse return null; 322 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 323 const issue_cursor = (try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, id_bytes))) orelse return null; 324 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 325 return try evt.read(Record, DB, repo_opts.hash, arena, issue_map); 326 } 327 328 // remove an issue's order key from its labels' `status` sets, pruning entries 329 // whose set becomes empty 330 fn removeFromLabelSets( 331 comptime DB: type, 332 label_to_issues: DB.SortedMap(.read_write), 333 labels: []const u8, 334 status: Status, 335 order_key: []const u8, 336 ) !void { 337 var label_iter = labelIterator(labels); 338 while (label_iter.next()) |label| { 339 // skipped the same way consume skipped it, so it was never indexed 340 if (label.len == 0 or label.len > label_max_len) continue; 341 var key_buffer: LabelStatusKey = undefined; 342 const key = try labelStatusKey(&key_buffer, label, status); 343 const label_set_cursor = try label_to_issues.putCursor(key); 344 const label_set = try DB.SortedSet(.read_write).init(label_set_cursor); 345 _ = try label_set.remove(order_key); 346 if (0 == try label_set.count()) _ = try label_to_issues.remove(key); 347 } 348 } 349