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