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