╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const xit = @import("xit"); 3 const rp = xit.repo; 4 const hash = xit.hash; 5 const mrg = xit.merge; 6 const obj = xit.object; 7 const rf = xit.ref; 8 9 pub const user = @import("event/user.zig"); 10 11 pub const event_id_size: usize = 32; 12 13 pub const EventKind = enum { 14 user, 15 repo, 16 issue, 17 }; 18 19 pub const EventData = union(EventKind) { 20 user: ?struct { 21 name: []const u8, 22 email: []const u8, 23 password_hash: []const u8, 24 }, 25 repo: ?struct { 26 user_id: []const u8, 27 name: []const u8, 28 enable_issue: bool, 29 }, 30 issue: ?struct { 31 title: []const u8, 32 description: []const u8, 33 tags: []const u8, 34 }, 35 36 pub fn read( 37 comptime DB: type, 38 comptime hash_kind: hash.HashKind, 39 allocator: std.mem.Allocator, 40 map: DB.HashMap(.read_only), 41 kind: EventKind, 42 ) !EventData { 43 return switch (kind) { 44 .user => .{ 45 .user = .{ 46 .name = try readBytes(DB, hash_kind, allocator, map, "name"), 47 .email = try readBytes(DB, hash_kind, allocator, map, "email"), 48 .password_hash = try readBytes(DB, hash_kind, allocator, map, "password_hash"), 49 }, 50 }, 51 .repo => .{ 52 .repo = .{ 53 .user_id = try readBytes(DB, hash_kind, allocator, map, "user_id"), 54 .name = try readBytes(DB, hash_kind, allocator, map, "name"), 55 .enable_issue = try readBool(DB, hash_kind, map, "enable_issue"), 56 }, 57 }, 58 .issue => .{ 59 .issue = .{ 60 .title = try readBytes(DB, hash_kind, allocator, map, "title"), 61 .description = try readBytes(DB, hash_kind, allocator, map, "description"), 62 .tags = try readBytes(DB, hash_kind, allocator, map, "tags"), 63 }, 64 }, 65 }; 66 } 67 68 fn readBytes( 69 comptime DB: type, 70 comptime hash_kind: hash.HashKind, 71 allocator: std.mem.Allocator, 72 map: DB.HashMap(.read_only), 73 field_name: []const u8, 74 ) ![]const u8 { 75 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 76 return try cursor.readBytesAlloc(allocator, null); 77 } 78 79 fn readBool( 80 comptime DB: type, 81 comptime hash_kind: hash.HashKind, 82 map: DB.HashMap(.read_only), 83 field_name: []const u8, 84 ) !bool { 85 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 86 var buffer: [5]u8 = undefined; 87 const bytes = try cursor.readBytesObject(&buffer); 88 if (bytes.format_tag == null or !std.mem.eql(u8, &bytes.format_tag.?, "bl")) return error.InvalidFormatTag; 89 if (std.mem.eql(u8, bytes.value, "true")) return true; 90 if (std.mem.eql(u8, bytes.value, "false")) return false; 91 return error.InvalidBool; 92 } 93 }; 94 95 pub const Event = struct { 96 id: [event_id_size * 2]u8, 97 data: EventData, 98 }; 99 100 pub fn randomId(random: std.Random) [event_id_size]u8 { 101 var id_bytes: [event_id_size]u8 = undefined; 102 random.bytes(&id_bytes); 103 return id_bytes; 104 } 105 106 pub fn consume( 107 comptime repo_opts: rp.RepoOpts(.xit), 108 io: std.Io, 109 allocator: std.mem.Allocator, 110 repo: *rp.Repo(.xit, repo_opts), 111 ref: rf.Ref, 112 ) !void { 113 const DB = rp.Repo(.xit, repo_opts).DB; 114 const State = rp.Repo(.xit, repo_opts).State; 115 116 const Ctx = struct { 117 core: *rp.Repo(.xit, repo_opts).Core, 118 io: std.Io, 119 allocator: std.mem.Allocator, 120 ref: rf.Ref, 121 122 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 123 var moment = try DB.HashMap(.read_write).init(cursor.*); 124 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 125 try consumeInTransaction(repo_opts, state, ctx.io, ctx.allocator, ctx.ref); 126 } 127 }; 128 129 try repo.core.db_file.lock(io, .exclusive); 130 defer repo.core.db_file.unlock(io); 131 132 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 133 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 134 .core = &repo.core, 135 .io = io, 136 .allocator = allocator, 137 .ref = ref, 138 }); 139 } 140 141 pub fn consumeInTransaction( 142 comptime repo_opts: rp.RepoOpts(.xit), 143 state: rp.Repo(.xit, repo_opts).State(.read_write), 144 io: std.Io, 145 allocator: std.mem.Allocator, 146 ref: rf.Ref, 147 ) !void { 148 const DB = rp.Repo(.xit, repo_opts).DB; 149 150 // the last_object_id represents the object id that was last consumed 151 var last_object_id_maybe: ?[hash.byteLen(repo_opts.hash)]u8 = null; 152 if (try state.extra.moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id"))) |last_object_id_cursor| { 153 var last_object_id_buffer: [hash.byteLen(repo_opts.hash)]u8 = undefined; 154 _ = try last_object_id_cursor.readBytes(&last_object_id_buffer); 155 last_object_id_maybe = last_object_id_buffer; 156 } 157 158 // the list with all of haxy's state, including materialized views. 159 // the reason it is a list is so we can keep every previous haxy 160 // state, making it easy to revert to an older state if the user 161 // rebases some of the past commits. 162 const haxy_cursor = try state.extra.moment.putCursor(hash.hashInt(repo_opts.hash, "haxy")); 163 const haxy = try DB.ArrayList(.read_write).init(haxy_cursor); 164 165 // add a new item to the haxy list created above. 166 // we call the item haxy_moments. it is a map of object id to haxy moment. 167 // in other words, it maps each object id to a hash map containing the 168 // state that the database was in when that object id was consumed. 169 var haxy_moments_cursor = try haxy.appendCursor(); 170 // use the previous haxy_moments as the basis for this one if it exists 171 if (try haxy.getCursor(-2)) |last_haxy_moments_cursor| { 172 try haxy_moments_cursor.write(.{ .slot = last_haxy_moments_cursor.slot() }); 173 } 174 var haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 175 176 var arena = std.heap.ArenaAllocator.init(allocator); 177 defer arena.deinit(); 178 179 // compute a list of events that haven't been consumed yet 180 const RepoEvent = struct { 181 parent_oids: []const [hash.hexLen(repo_opts.hash)]u8, 182 oid: [hash.byteLen(repo_opts.hash)]u8, 183 }; 184 var repo_events: std.ArrayList(RepoEvent) = .empty; 185 defer repo_events.deinit(allocator); 186 { 187 var commit_iter = try obj.ObjectIterator(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, .{ .kind = .commit }); 188 defer commit_iter.deinit(); 189 190 const head_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), io, .{ .ref = ref })) orelse return error.OidNotFound; 191 try commit_iter.include(&head_oid); 192 193 while (try commit_iter.next(arena.allocator())) |commit_object| { 194 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 195 _ = try std.fmt.hexToBytes(&oid, &commit_object.oid); 196 197 // if this object id has already been consumed, skip it 198 if (null != try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid))) { 199 continue; 200 } 201 202 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 203 try repo_events.append(allocator, .{ .parent_oids = parent_oids, .oid = oid }); 204 } 205 } 206 207 // if there are no events to process, look at the oid at HEAD and update 208 // the last_object_id to point to it. this is important in situations 209 // where we do a merge and then force-push to remove the merge. see the 210 // merge test for an example. 211 if (repo_events.items.len == 0) { 212 const head_oid_hex = (try rf.readRecur(.xit, repo_opts, state.readOnly(), io, .{ .ref = ref })) orelse return error.OidNotFound; 213 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 214 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 215 216 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &head_oid)) orelse return error.CursorNotFound; 217 const haxy_moment = try DB.HashMap(.read_only).init(haxy_moment_cursor); 218 219 const moment_index_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 220 const moment_index = try moment_index_cursor.readUint(); 221 222 try haxy.slice(moment_index + 1); 223 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = &head_oid }); 224 return; 225 } 226 227 // if this branch was rebased and force pushed, we need to detect that and 228 // properly revert the haxy state to the last valid state. we detect this 229 // by simply asking if the last event is a descendent of the last event 230 // we consumed. if it isn't, then we know the haxy state needs to be reverted. 231 if (last_object_id_maybe) |*last_object_id| { 232 var is_rebased = false; 233 234 _ = mrg.getDescendent( 235 .xit, 236 repo_opts, 237 state.readOnly(), 238 io, 239 allocator, 240 &std.fmt.bytesToHex(last_object_id, .lower), 241 &std.fmt.bytesToHex(repo_events.items[0].oid, .lower), 242 ) catch |err| switch (err) { 243 error.DescendentNotFound => is_rebased = true, 244 else => |e| return e, 245 }; 246 247 if (is_rebased) { 248 const parent_oids = repo_events.items[repo_events.items.len - 1].parent_oids; 249 switch (parent_oids.len) { 250 0 => { 251 // the branch was rebased all the way to the very beginning. 252 // we have a repo event with no parent, which means it is now 253 // the very first event. all we need to do is set the haxy list 254 // to be empty and make a new haxy_moments map to work with. 255 256 try haxy.slice(0); 257 haxy_moments_cursor = try haxy.appendCursor(); 258 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 259 260 last_object_id_maybe = null; 261 }, 262 1 => { 263 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 264 _ = try std.fmt.hexToBytes(&oid, &parent_oids[0]); 265 266 const old_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 267 const old_moment = try DB.HashMap(.read_only).init(old_moment_cursor); 268 269 const old_moment_index_cursor = try old_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 270 const old_moment_index = try old_moment_index_cursor.readUint(); 271 272 // resize the haxy list so we truncate all the moments that were 273 // created after the parent_oid was consumed 274 try haxy.slice(old_moment_index + 1); 275 276 // make a new haxy moment and set its initial value to the last haxy moment 277 haxy_moments_cursor = try haxy.appendCursor(); 278 const old_haxy_moments_cursor = try haxy.getCursor(old_moment_index) orelse return error.CursorNotFound; 279 try haxy_moments_cursor.write(.{ .slot = old_haxy_moments_cursor.slot() }); 280 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 281 282 last_object_id_maybe = oid; 283 }, 284 else => return error.UnexpectedParentCount, 285 } 286 } 287 } else { 288 if (repo_events.items[repo_events.items.len - 1].parent_oids.len != 0) { 289 // there is no last_object_id, but this event has a parent. 290 // this is an invalid state. if the event has a parent, that 291 // implies that an event has already been processed, but 292 // if that's true then there would be a last_object_id. 293 return error.UnexpectedParent; 294 } 295 } 296 297 // iterate over the events in reverse order 298 // (they are sorted by most recent events first) 299 for (0..repo_events.items.len) |i| { 300 const repo_event = repo_events.items[repo_events.items.len - i - 1]; 301 302 // create a moment for this object id 303 var haxy_moment_cursor = try haxy_moments.putCursor(hash.bytesToInt(repo_opts.hash, &repo_event.oid)); 304 305 // if there was a previous object id, set this haxy moment's initial value to it. 306 // this efficiently "clones" the map so we make further modifications based on it. 307 if (repo_event.parent_oids.len > 0) { 308 var first_parent_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 309 _ = try std.fmt.hexToBytes(&first_parent_oid, &repo_event.parent_oids[0]); 310 311 const first_parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &first_parent_oid)) orelse return error.CursorNotFound; 312 try haxy_moment_cursor.write(.{ .slot = first_parent_haxy_moment_cursor.slot() }); 313 } 314 315 const haxy_moment = try DB.HashMap(.read_write).init(haxy_moment_cursor); 316 317 // merge changes from every parent after the first parent. the common 318 // ancestor is the baseline, so a later parent only contributes values 319 // it changed relative to the merge base. 320 if (repo_event.parent_oids.len > 1) { 321 for (repo_event.parent_oids[1..]) |*parent_oid| { 322 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 323 _ = try std.fmt.hexToBytes(&oid, parent_oid); 324 325 const parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 326 const parent_haxy_moment = try DB.HashMap(.read_only).init(parent_haxy_moment_cursor); 327 328 const baseline_oid_hex = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), io, allocator, &repo_event.parent_oids[0], parent_oid); 329 var baseline_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 330 _ = try std.fmt.hexToBytes(&baseline_oid, &baseline_oid_hex); 331 const baseline_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &baseline_oid)) orelse return error.CursorNotFound; 332 const baseline_haxy_moment = try DB.HashMap(.read_only).init(baseline_haxy_moment_cursor); 333 334 try mergeChangedMapEntries(DB, haxy_moment, parent_haxy_moment, baseline_haxy_moment, true); 335 } 336 } 337 338 // associate this moment with the index it will first appear at in the haxy list. 339 // this will be important later so we can truncate that list if the user ever 340 // rebases starting at this object id. 341 try haxy_moment.put(hash.hashInt(repo_opts.hash, "moment-index"), .{ .uint = try haxy.count() - 1 }); 342 343 // consume the event unless it's a merge commit 344 if (repo_event.parent_oids.len <= 1) { 345 var commit_object = try obj.Object(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, &std.fmt.bytesToHex(repo_event.oid, .lower)); 346 defer commit_object.deinit(); 347 348 // read the message from the commit 349 try commit_object.object_reader.seekTo(commit_object.content.commit.message_position); 350 const message = try commit_object.object_reader.interface.allocRemaining(arena.allocator(), .unlimited); 351 352 const event = try std.json.parseFromSliceLeaky(Event, arena.allocator(), message, .{}); 353 354 // get the id of the current event as bytes 355 var current_event_id: [event_id_size]u8 = undefined; 356 _ = try std.fmt.hexToBytes(&current_event_id, &event.id); 357 358 switch (event.data) { 359 .user => |data_maybe| { 360 const user_key = hash.hashInt(repo_opts.hash, &current_event_id); 361 362 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->user")); 363 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 364 365 if (data_maybe) |data| { 366 const user_cursor = try event_id_to_user.putCursor(user_key); 367 const user_map = try DB.HashMap(.read_write).init(user_cursor); 368 try upsert(DB, repo_opts.hash, user_map, @TypeOf(data), data); 369 } else { 370 if (!try event_id_to_user.remove(user_key)) return error.EventNotFound; 371 372 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "user-id->repos")); 373 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 374 _ = try user_id_to_repos.remove(user_key); 375 } 376 }, 377 .repo => |data_maybe| { 378 const repo_key = hash.hashInt(repo_opts.hash, &current_event_id); 379 380 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->repo")); 381 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 382 383 if (data_maybe) |data| { 384 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 385 const repo_map = try DB.HashMap(.read_write).init(repo_cursor); 386 try upsert(DB, repo_opts.hash, repo_map, @TypeOf(data), data); 387 388 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "user-id->repos")); 389 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 390 391 const user_repos_cursor = try user_id_to_repos.putCursor(hash.hashInt(repo_opts.hash, data.user_id)); 392 const user_repos = try DB.CountedHashSet(.read_write).init(user_repos_cursor); 393 try user_repos.put(repo_key, .{ .bytes = &current_event_id }); 394 } else { 395 if (try event_id_to_repo.getCursor(repo_key)) |existing_repo_cursor| { 396 const existing_repo_map = try DB.HashMap(.read_only).init(existing_repo_cursor); 397 const existing_repo = try EventData.read(DB, repo_opts.hash, arena.allocator(), existing_repo_map, .repo); 398 const data = existing_repo.repo orelse return error.InvalidEventData; 399 400 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "user-id->repos")); 401 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 402 403 const user_key = hash.hashInt(repo_opts.hash, data.user_id); 404 const user_repos_cursor = try user_id_to_repos.putCursor(user_key); 405 const user_repos = try DB.CountedHashSet(.read_write).init(user_repos_cursor); 406 _ = try user_repos.remove(repo_key); 407 } 408 if (!try event_id_to_repo.remove(repo_key)) return error.EventNotFound; 409 } 410 }, 411 .issue => |data_maybe| { 412 const issue_key = hash.hashInt(repo_opts.hash, &current_event_id); 413 414 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->issue")); 415 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 416 417 if (data_maybe) |data| { 418 const issue_cursor = try event_id_to_issue.putCursor(issue_key); 419 const issue = try DB.HashMap(.read_write).init(issue_cursor); 420 try upsert(DB, repo_opts.hash, issue, @TypeOf(data), data); 421 } else { 422 if (!try event_id_to_issue.remove(issue_key)) return error.EventNotFound; 423 } 424 }, 425 } 426 } 427 428 // the current object id is now the last one 429 last_object_id_maybe = repo_event.oid; 430 431 // prevent any of the data created above from being mutated by future iterations of this loop 432 try state.core.db.freeze(); 433 } 434 435 if (last_object_id_maybe) |*last_object_id| { 436 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = last_object_id }); 437 } 438 } 439 440 fn upsert( 441 comptime DB: type, 442 comptime hash_kind: hash.HashKind, 443 map: DB.HashMap(.read_write), 444 comptime Data: type, 445 data: Data, 446 ) !void { 447 switch (@typeInfo(Data)) { 448 .@"struct" => |struct_info| { 449 inline for (struct_info.fields) |field| { 450 try upsertField(DB, hash_kind, map, field.name, field.type, @field(data, field.name)); 451 } 452 }, 453 else => @compileError("upsert expects a struct"), 454 } 455 } 456 457 fn mergeChangedMapEntries( 458 comptime DB: type, 459 target: DB.HashMap(.read_write), 460 parent: DB.HashMap(.read_only), 461 baseline: DB.HashMap(.read_only), 462 comptime is_top_level: bool, 463 ) !void { 464 var parent_iter = try parent.iterator(); 465 while (try parent_iter.next()) |kv_pair_cursor| { 466 const kv_pair = try kv_pair_cursor.readKeyValuePair(); 467 468 const baseline_value_cursor = try baseline.getCursor(kv_pair.hash) orelse { 469 if ((try target.getCursor(kv_pair.hash)) != null) { 470 return error.MergeConflict; 471 } 472 473 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 474 continue; 475 }; 476 477 const tag = kv_pair.value_cursor.slot().tag; 478 479 if (tag != baseline_value_cursor.slot().tag) { 480 return error.UnexpectedTag; 481 } 482 483 if (kv_pair.value_cursor.slot().value == baseline_value_cursor.slot().value) { 484 continue; 485 } 486 487 if (tag == .hash_map) { 488 const target_existing_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 489 if (target_existing_cursor.slot().tag != .hash_map) { 490 return error.MergeConflict; 491 } 492 493 const target_child_cursor = try target.putCursor(kv_pair.hash); 494 const target_child = try DB.HashMap(.read_write).init(target_child_cursor); 495 const parent_child = try DB.HashMap(.read_only).init(kv_pair.value_cursor); 496 const baseline_child = try DB.HashMap(.read_only).init(baseline_value_cursor); 497 try mergeChangedMapEntries(DB, target_child, parent_child, baseline_child, false); 498 } else if (is_top_level) { 499 // the moment-index is a top-level key in the haxy moment 500 // and is not a map. we don't want to do any merge of this. 501 continue; 502 } else { 503 const target_value_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 504 if (target_value_cursor.slot().value != baseline_value_cursor.slot().value) { 505 return error.MergeConflict; 506 } 507 508 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 509 } 510 } 511 } 512 513 fn upsertField( 514 comptime DB: type, 515 comptime hash_kind: hash.HashKind, 516 map: DB.HashMap(.read_write), 517 comptime field_name: []const u8, 518 comptime Field: type, 519 value: Field, 520 ) !void { 521 const key = hash.hashInt(hash_kind, field_name); 522 523 switch (@typeInfo(Field)) { 524 .pointer => |pointer_info| { 525 if (pointer_info.size == .slice and pointer_info.child == u8) { 526 try upsertBytes(DB, hash_kind, map, key, value); 527 } else { 528 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 529 } 530 }, 531 .array => |array_info| { 532 if (array_info.child == u8) { 533 try upsertBytes(DB, hash_kind, map, key, &value); 534 } else { 535 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 536 } 537 }, 538 .int => |int_info| switch (int_info.signedness) { 539 .unsigned => { 540 if (try map.getCursor(key)) |value_cursor| { 541 if (try value_cursor.readUint() == value) { 542 return; 543 } 544 } 545 546 try map.put(key, .{ .uint = value }); 547 }, 548 .signed => { 549 if (try map.getCursor(key)) |value_cursor| { 550 if (try value_cursor.readInt() == value) { 551 return; 552 } 553 } 554 555 try map.put(key, .{ .int = value }); 556 }, 557 }, 558 .bool => { 559 const bytes = if (value) "true" else "false"; 560 if (try map.getCursor(key)) |value_cursor| { 561 var buffer: [5]u8 = undefined; 562 const existing = try value_cursor.readBytesObject(&buffer); 563 if (existing.format_tag != null and 564 std.mem.eql(u8, &existing.format_tag.?, "bl") and 565 std.mem.eql(u8, existing.value, bytes)) 566 { 567 return; 568 } 569 } 570 571 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 572 }, 573 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 574 } 575 } 576 577 fn upsertBytes( 578 comptime DB: type, 579 comptime hash_kind: hash.HashKind, 580 map: DB.HashMap(.read_write), 581 key: hash.HashInt(hash_kind), 582 value: []const u8, 583 ) !void { 584 var existing_cursor_maybe = try map.getCursor(key); 585 if (existing_cursor_maybe) |*existing_cursor| { 586 if (try bytesEqual(DB, existing_cursor, value)) { 587 return; 588 } 589 } 590 591 var value_cursor = try map.putCursor(key); 592 var write_buffer: [1024]u8 = undefined; 593 var writer = try value_cursor.writer(&write_buffer); 594 try writer.interface.writeAll(value); 595 try writer.finish(); 596 } 597 598 fn bytesEqual( 599 comptime DB: type, 600 cursor: *DB.Cursor(.read_only), 601 value: []const u8, 602 ) !bool { 603 var read_buffer: [1024]u8 = undefined; 604 var reader = try cursor.reader(&read_buffer); 605 if (reader.size != value.len) { 606 return false; 607 } 608 609 var chunk_buffer: [1024]u8 = undefined; 610 var offset: usize = 0; 611 while (offset < value.len) { 612 const chunk_len = @min(chunk_buffer.len, value.len - offset); 613 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 614 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 615 return false; 616 } 617 offset += chunk_len; 618 } 619 620 return true; 621 } 622