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