╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issue.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
59 return true; 60 } 61 62 pub fn consume( 63 comptime DB: type, 64 comptime hash_kind: hash.HashKind, 65 haxy_moment: DB.HashMap(.read_write), 66 event_id: *const [evt.event_id_size]u8, 67 record_maybe: ?Record, 68 arena: *std.heap.ArenaAllocator, 69 // the commit this event came from, recorded against every field it 70 // changes. null from `merge`, which sets oids and conflicts itself. 71 event_oid: ?[]const u8, 72 ) !void { 73 const issue_key = hash.hashInt(hash_kind, event_id); 74 75 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->issue")); 76 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 77 78 // the per-status sets the issues views list, ordered by creation time, 79 // keyed by status name 80 const status_to_issues_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "status->issue-id-set")); 81 const status_to_issues = try DB.SortedMap(.read_write).init(status_to_issues_cursor); 82 83 // the per-tag, per-status sets, keyed "tag,status" 84 const tag_to_issues_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "tag+status->issue-id-set")); 85 const tag_to_issues = try DB.SortedMap(.read_write).init(tag_to_issues_cursor); 86 87 const conflicts_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, conflicts_key)); 88 const conflicts = try DB.SortedMap(.read_write).init(conflicts_cursor); 89 90 const issue_id_to_field_to_oid_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_to_field_to_oid_key)); 91 const issue_id_to_field_to_oid = try DB.HashMap(.read_write).init(issue_id_to_field_to_oid_cursor); 92 93 if (record_maybe) |record| { 94 var record_to_write = record; 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 // updates preserve the original creation timestamp and author 100 const existing_issue = try DB.HashMap(.read_only).init(existing_cursor); 101 const existing_record = try evt.read(Record, DB, hash_kind, arena, existing_issue); 102 existing_record_maybe = existing_record; 103 record_to_write.created_ts = existing_record.created_ts; 104 record_to_write.author_email = existing_record.author_email; 105 106 // drop the old status's and tags' entries; the current ones are re-added below 107 const order_key = evt.orderKeyDesc(existing_record.created_ts, event_id); 108 const status_set = try statusSet(DB, status_to_issues, existing_record.event.status); 109 _ = try status_set.remove(&order_key); 110 try removeFromTagSets(DB, tag_to_issues, existing_record.event.tags, existing_record.event.status, &order_key); 111 112 // any event settles the conflict, since resolving in the ui may 113 // keep our own values and so change nothing to detect 114 if (event_oid != null) _ = try conflicts.remove(&order_key); 115 } 116 117 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); 118 119 const issue_cursor = try event_id_to_issue.putCursor(issue_key); 120 const issue = try DB.HashMap(.read_write).init(issue_cursor); 121 try evt.upsert(Record, DB, hash_kind, issue, record_to_write); 122 123 const order_key = evt.orderKeyDesc(record_to_write.created_ts, event_id); 124 125 // first time we've seen this issue: add it to the set that enumerates 126 // every issue regardless of status 127 if (existing_cursor_maybe == null) { 128 const issue_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key)); 129 const issue_id_set = try DB.SortedSet(.read_write).init(issue_id_set_cursor); 130 try issue_id_set.put(&order_key); 131 } 132 133 const status_set = try statusSet(DB, status_to_issues, record.event.status); 134 try status_set.put(&order_key); 135 136 var tag_iter = tagIterator(record.event.tags); 137 while (tag_iter.next()) |tag| { 138 // an over-long tag goes unindexed rather than failing the consume, 139 // which would wedge the branch it arrived on 140 if (tag.len == 0 or tag.len > tag_max_len) continue; 141 var key_buffer: TagStatusKey = undefined; 142 const tag_set_cursor = try tag_to_issues.putCursor(try tagStatusKey(&key_buffer, tag, record.event.status)); 143 const tag_set = try DB.SortedSet(.read_write).init(tag_set_cursor); 144 try tag_set.put(&order_key); 145 } 146 } else { 147 // drop it from the ordered sets using its recorded creation timestamp and status 148 if (try event_id_to_issue.getCursor(issue_key)) |existing_cursor| { 149 const existing_issue = try DB.HashMap(.read_only).init(existing_cursor); 150 const existing_record = try evt.read(Record, DB, hash_kind, arena, existing_issue); 151 const order_key = evt.orderKeyDesc(existing_record.created_ts, event_id); 152 153 const status_set = try statusSet(DB, status_to_issues, existing_record.event.status); 154 _ = try status_set.remove(&order_key); 155 156 try removeFromTagSets(DB, tag_to_issues, existing_record.event.tags, existing_record.event.status, &order_key); 157 158 const issue_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key)); 159 const issue_id_set = try DB.SortedSet(.read_write).init(issue_id_set_cursor); 160 _ = try issue_id_set.remove(&order_key); 161 162 _ = try conflicts.remove(&order_key); 163 _ = try issue_id_to_field_to_oid.remove(issue_key); 164 } 165 if (!try event_id_to_issue.remove(issue_key)) return error.EventNotFound; 166 } 167 } 168 169 // `status`'s sorted set within `statuses`, keyed by status name 170 fn statusSet( 171 comptime DB: type, 172 statuses: DB.SortedMap(.read_write), 173 status: Status, 174 ) !DB.SortedSet(.read_write) { 175 const cursor = try statuses.putCursor(@tagName(status)); 176 return DB.SortedSet(.read_write).init(cursor); 177 } 178 179 // what an update replaces on an issue: its status, or its content 180 pub const Update = union(enum) { 181 status: Status, 182 fields: struct { title: []const u8, tags: []const u8, description: []const u8 }, 183 }; 184 185 // re-emit the issue `id_bytes` names with `change` applied, preserving the 186 // rest of its current state. error.NotFound when the id names no issue. 187 // `repo` must be writable. 188 pub fn update( 189 comptime repo_kind: rp.RepoKind, 190 comptime repo_opts: rp.RepoOpts(repo_kind), 191 io: std.Io, 192 allocator: std.mem.Allocator, 193 repo: *rp.Repo(repo_kind, repo_opts), 194 id_bytes: *const [evt.event_id_size]u8, 195 change: Update, 196 author_email: []const u8, 197 ) !void { 198 var arena = std.heap.ArenaAllocator.init(allocator); 199 defer arena.deinit(); 200 201 const issue = (try readById(repo_kind, repo_opts, io, allocator, &arena, repo, id_bytes)) orelse return error.NotFound; 202 203 var updated = issue.event; 204 switch (change) { 205 .status => |status| { 206 if (updated.status == status) return; 207 updated.status = status; 208 }, 209 .fields => |fields| { 210 updated.title = fields.title; 211 updated.tags = fields.tags; 212 updated.description = fields.description; 213 }, 214 } 215 216 try evt.consume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 217 .id = std.fmt.bytesToHex(id_bytes.*, .lower), 218 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 219 .author_email = author_email, 220 .event = .{ .issue = updated }, 221 }}); 222 } 223 224 // the issue `id_bytes` names in the repo's consumed event db (the repo's own 225 // db for xit; the side db next to a git repo), or null 226 fn readById( 227 comptime repo_kind: rp.RepoKind, 228 comptime repo_opts: rp.RepoOpts(repo_kind), 229 io: std.Io, 230 allocator: std.mem.Allocator, 231 arena: *std.heap.ArenaAllocator, 232 repo: *rp.Repo(repo_kind, repo_opts), 233 id_bytes: *const [evt.event_id_size]u8, 234 ) !?Record { 235 const DB = evt.EventDB(repo_opts.hash); 236 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; 237 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 238 const moment = (if (event_db_maybe) |*event_db| 239 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 240 else if (repo_kind == .git) 241 return null 242 else 243 evt.currentMoment(repo_opts, repo)) catch return null; 244 const event_id_to_issue_cursor = (try moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue"))) orelse return null; 245 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 246 const issue_cursor = (try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, id_bytes))) orelse return null; 247 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 248 return try evt.read(Record, DB, repo_opts.hash, arena, issue_map); 249 } 250 251 // remove an issue's order key from its tags' `status` sets, pruning entries 252 // whose set becomes empty 253 fn removeFromTagSets( 254 comptime DB: type, 255 tag_to_issues: DB.SortedMap(.read_write), 256 tags: []const u8, 257 status: Status, 258 order_key: []const u8, 259 ) !void { 260 var tag_iter = tagIterator(tags); 261 while (tag_iter.next()) |tag| { 262 // skipped the same way consume skipped it, so it was never indexed 263 if (tag.len == 0 or tag.len > tag_max_len) continue; 264 var key_buffer: TagStatusKey = undefined; 265 const key = try tagStatusKey(&key_buffer, tag, status); 266 const tag_set_cursor = try tag_to_issues.putCursor(key); 267 const tag_set = try DB.SortedSet(.read_write).init(tag_set_cursor); 268 _ = try tag_set.remove(order_key); 269 if (0 == try tag_set.count()) _ = try tag_to_issues.remove(key); 270 } 271 } 272