╭ 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 7 pub const event_id_size: usize = 32; 8 9 pub const EventKind = enum { 10 issue, 11 }; 12 13 pub const EventData = union(EventKind) { 14 issue: struct { 15 title: []const u8, 16 description: []const u8, 17 tags: []const u8, 18 }, 19 20 pub fn read( 21 comptime DB: type, 22 comptime hash_kind: hash.HashKind, 23 allocator: std.mem.Allocator, 24 map: DB.HashMap(.read_only), 25 kind: EventKind, 26 ) !EventData { 27 return switch (kind) { 28 .issue => .{ 29 .issue = .{ 30 .title = try readBytes(DB, hash_kind, allocator, map, "title"), 31 .description = try readBytes(DB, hash_kind, allocator, map, "description"), 32 .tags = try readBytes(DB, hash_kind, allocator, map, "tags"), 33 }, 34 }, 35 }; 36 } 37 38 fn readBytes( 39 comptime DB: type, 40 comptime hash_kind: hash.HashKind, 41 allocator: std.mem.Allocator, 42 map: DB.HashMap(.read_only), 43 field_name: []const u8, 44 ) ![]const u8 { 45 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 46 return try cursor.readBytesAlloc(allocator, null); 47 } 48 }; 49 50 pub const Event = struct { 51 id: [event_id_size * 2]u8, 52 data: EventData, 53 }; 54 55 /// associates an Event with a git object id. 56 /// we save this oid in the database so we can reliably re-consume 57 /// events if their oid changed. this can happen if the branch 58 /// is rebased and force-pushed. 59 pub fn RepoEvent(comptime hash_kind: hash.HashKind) type { 60 return struct { 61 parent_oid: ?[hash.byteLen(hash_kind)]u8, 62 oid: [hash.byteLen(hash_kind)]u8, 63 event: ?Event, // if null, this is a merge commit 64 }; 65 } 66 67 pub fn randomId(random: std.Random) [event_id_size]u8 { 68 var id_bytes: [event_id_size]u8 = undefined; 69 random.bytes(&id_bytes); 70 return id_bytes; 71 } 72 73 pub fn consume( 74 comptime repo_opts: rp.RepoOpts(.xit), 75 io: std.Io, 76 allocator: std.mem.Allocator, 77 repo: *rp.Repo(.xit, repo_opts), 78 repo_events: []RepoEvent(repo_opts.hash), 79 ) !void { 80 const DB = rp.Repo(.xit, repo_opts).DB; 81 const State = rp.Repo(.xit, repo_opts).State; 82 83 const Ctx = struct { 84 core: *rp.Repo(.xit, repo_opts).Core, 85 io: std.Io, 86 allocator: std.mem.Allocator, 87 repo_events: []RepoEvent(repo_opts.hash), 88 89 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 90 var moment = try DB.HashMap(.read_write).init(cursor.*); 91 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 92 try consumeInTransaction(repo_opts, state, ctx.io, ctx.allocator, ctx.repo_events); 93 } 94 }; 95 96 try repo.core.db_file.lock(io, .exclusive); 97 defer repo.core.db_file.unlock(io); 98 99 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 100 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 101 .core = &repo.core, 102 .io = io, 103 .allocator = allocator, 104 .repo_events = repo_events, 105 }); 106 } 107 108 pub fn consumeInTransaction( 109 comptime repo_opts: rp.RepoOpts(.xit), 110 state: rp.Repo(.xit, repo_opts).State(.read_write), 111 io: std.Io, 112 allocator: std.mem.Allocator, 113 repo_events: []RepoEvent(repo_opts.hash), 114 ) !void { 115 const DB = rp.Repo(.xit, repo_opts).DB; 116 117 // the last_object_id represents the object id that was last consumed 118 var last_object_id_maybe: ?[hash.byteLen(repo_opts.hash)]u8 = null; 119 if (try state.extra.moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id"))) |last_object_id_cursor| { 120 var last_object_id_buffer: [hash.byteLen(repo_opts.hash)]u8 = undefined; 121 _ = try last_object_id_cursor.readBytes(&last_object_id_buffer); 122 last_object_id_maybe = last_object_id_buffer; 123 } 124 125 // the list with all of haxy's state, including materialized views. 126 // the reason it is a list is so we can keep every previous haxy 127 // state, making it easy to revert to an older state if the user 128 // rebases some of the past commits. 129 const haxy_cursor = try state.extra.moment.putCursor(hash.hashInt(repo_opts.hash, "haxy")); 130 const haxy = try DB.ArrayList(.read_write).init(haxy_cursor); 131 132 // add a new item to the haxy list created above. 133 // we call the item haxy_moments. it is a map of object id to haxy moment. 134 // in other words, it maps each object id to a hash map containing the 135 // state that the database was in when that object id was consumed. 136 var haxy_moments_cursor = try haxy.appendCursor(); 137 // use the previous haxy_moments as the basis for this one if it exists 138 if (try haxy.getCursor(-2)) |last_haxy_moments_cursor| { 139 try haxy_moments_cursor.write(.{ .slot = last_haxy_moments_cursor.slot() }); 140 } 141 var haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 142 143 // if this branch was rebased and force pushed, we need to detect that and 144 // properly revert the haxy state to the last valid state. we detect this 145 // by simply asking if the last event is a descendent of the last event 146 // we consumed. if it isn't, then we know the haxy state needs to be reverted. 147 // from that point on, the rest of the events should be descendents so we no 148 // longer need to even check. this boolean lets us know if that has happened. 149 var checked_for_rebase = false; 150 151 // iterate over the events in reverse order 152 // (they are sorted by most recent events first) 153 for (0..repo_events.len) |i| { 154 const repo_event = repo_events[repo_events.len - i - 1]; 155 156 // if this object id has already been consumed, skip it 157 if (null != try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &repo_event.oid))) { 158 continue; 159 } 160 161 if (!checked_for_rebase) { 162 checked_for_rebase = true; 163 164 if (last_object_id_maybe) |*last_object_id| { 165 // if the last event isn't a descendent of the last event that 166 // we consumed, the branch was rebased so we need to revert the 167 // haxy state back to where it was when this event's parent was 168 // consumed. from that point on, we can consume the rest of the 169 // events on top of that state. 170 _ = mrg.getDescendent( 171 .xit, 172 repo_opts, 173 state.readOnly(), 174 io, 175 allocator, 176 &std.fmt.bytesToHex(last_object_id, .lower), 177 &std.fmt.bytesToHex(repo_events[0].oid, .lower), 178 ) catch |err| switch (err) { 179 error.DescendentNotFound => { 180 if (repo_event.parent_oid) |*parent_oid| { 181 const old_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, parent_oid)) orelse return error.CursorNotFound; 182 const old_moment = try DB.HashMap(.read_only).init(old_moment_cursor); 183 184 const old_moment_index_cursor = try old_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 185 const old_moment_index = try old_moment_index_cursor.readUint(); 186 187 // resize the haxy list so we truncate all the moments that were 188 // created after the parent_oid was consumed 189 try haxy.slice(old_moment_index + 1); 190 191 // make a new haxy moment and set its initial value to the last haxy moment 192 haxy_moments_cursor = try haxy.appendCursor(); 193 const old_haxy_moments_cursor = try haxy.getCursor(old_moment_index) orelse return error.CursorNotFound; 194 try haxy_moments_cursor.write(.{ .slot = old_haxy_moments_cursor.slot() }); 195 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 196 197 last_object_id_maybe = parent_oid.*; 198 } else { 199 // the branch was rebased all the way to the very beginning. 200 // we have a repo event with no parent, which means it is now 201 // the very first event. all we need to do is set the haxy list 202 // to be empty and make a new haxy_moments map to work with. 203 204 try haxy.slice(0); 205 haxy_moments_cursor = try haxy.appendCursor(); 206 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 207 208 last_object_id_maybe = null; 209 } 210 }, 211 else => |e| return e, 212 }; 213 } else { 214 if (repo_event.parent_oid) |_| { 215 // there is no last_object_id, but this event has a parent. 216 // this is an invalid state. if the event has a parent, that 217 // implies that an event has already been processed, but 218 // if that's true then there would be a last_object_id. 219 return error.UnexpectedParent; 220 } 221 } 222 } 223 224 // create a moment for this object id 225 var haxy_moment_cursor = try haxy_moments.putCursor(hash.bytesToInt(repo_opts.hash, &repo_event.oid)); 226 227 // if there was a previous object id, make this haxy moment's initial value to it. 228 // this efficiently "clones" the map so we make further modifications based on it. 229 if (last_object_id_maybe) |*last_object_id| { 230 if (try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, last_object_id))) |last_haxy_moment_cursor| { 231 try haxy_moment_cursor.write(.{ .slot = last_haxy_moment_cursor.slot() }); 232 } 233 } 234 235 const haxy_moment = try DB.HashMap(.read_write).init(haxy_moment_cursor); 236 237 // associate this moment with the index it will first appear at in the haxy list. 238 // this will be important later so we can truncate that list if the user ever 239 // rebases starting at this object id. 240 try haxy_moment.put(hash.hashInt(repo_opts.hash, "moment-index"), .{ .uint = try haxy.count() - 1 }); 241 242 // consume the event into the views map. 243 // if the event is null, it was just a merge commit 244 // so we don't need to update any views. 245 if (repo_event.event) |event| { 246 // get the id of the current event as bytes 247 var current_event_id: [event_id_size]u8 = undefined; 248 _ = try std.fmt.hexToBytes(&current_event_id, &event.id); 249 250 switch (event.data) { 251 .issue => |data| { 252 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->issue")); 253 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 254 255 const issue_cursor = try event_id_to_issue.putCursor(hash.hashInt(repo_opts.hash, &current_event_id)); 256 const issue = try DB.HashMap(.read_write).init(issue_cursor); 257 258 try upsert(DB, repo_opts.hash, issue, @TypeOf(data), data); 259 }, 260 } 261 } 262 263 // the current object id is now the last one 264 last_object_id_maybe = repo_event.oid; 265 266 // prevent any of the data created above from being mutated by future iterations of this loop 267 try state.core.db.freeze(); 268 } 269 270 if (last_object_id_maybe) |*last_object_id| { 271 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = last_object_id }); 272 } 273 } 274 275 fn upsert( 276 comptime DB: type, 277 comptime hash_kind: hash.HashKind, 278 map: DB.HashMap(.read_write), 279 comptime Data: type, 280 data: Data, 281 ) !void { 282 switch (@typeInfo(Data)) { 283 .@"struct" => |struct_info| { 284 inline for (struct_info.fields) |field| { 285 try upsertField(DB, hash_kind, map, field.name, field.type, @field(data, field.name)); 286 } 287 }, 288 else => @compileError("upsert expects a struct"), 289 } 290 } 291 292 fn upsertField( 293 comptime DB: type, 294 comptime hash_kind: hash.HashKind, 295 map: DB.HashMap(.read_write), 296 comptime field_name: []const u8, 297 comptime Field: type, 298 value: Field, 299 ) !void { 300 const key = hash.hashInt(hash_kind, field_name); 301 302 switch (@typeInfo(Field)) { 303 .pointer => |pointer_info| { 304 if (pointer_info.size == .slice and pointer_info.child == u8) { 305 try upsertBytes(DB, hash_kind, map, key, value); 306 } else { 307 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 308 } 309 }, 310 .array => |array_info| { 311 if (array_info.child == u8) { 312 try upsertBytes(DB, hash_kind, map, key, &value); 313 } else { 314 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 315 } 316 }, 317 .int => |int_info| switch (int_info.signedness) { 318 .unsigned => { 319 if (try map.getCursor(key)) |value_cursor| { 320 if (try value_cursor.readUint() == value) { 321 return; 322 } 323 } 324 325 try map.put(key, .{ .uint = value }); 326 }, 327 .signed => { 328 if (try map.getCursor(key)) |value_cursor| { 329 if (try value_cursor.readInt() == value) { 330 return; 331 } 332 } 333 334 try map.put(key, .{ .int = value }); 335 }, 336 }, 337 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 338 } 339 } 340 341 fn upsertBytes( 342 comptime DB: type, 343 comptime hash_kind: hash.HashKind, 344 map: DB.HashMap(.read_write), 345 key: hash.HashInt(hash_kind), 346 value: []const u8, 347 ) !void { 348 var existing_cursor_maybe = try map.getCursor(key); 349 if (existing_cursor_maybe) |*existing_cursor| { 350 if (try bytesEqual(DB, existing_cursor, value)) { 351 return; 352 } 353 } 354 355 var value_cursor = try map.putCursor(key); 356 var write_buffer: [1024]u8 = undefined; 357 var writer = try value_cursor.writer(&write_buffer); 358 try writer.interface.writeAll(value); 359 try writer.finish(); 360 } 361 362 fn bytesEqual( 363 comptime DB: type, 364 cursor: *DB.Cursor(.read_only), 365 value: []const u8, 366 ) !bool { 367 var read_buffer: [1024]u8 = undefined; 368 var reader = try cursor.reader(&read_buffer); 369 if (reader.size != value.len) { 370 return false; 371 } 372 373 var chunk_buffer: [1024]u8 = undefined; 374 var offset: usize = 0; 375 while (offset < value.len) { 376 const chunk_len = @min(chunk_buffer.len, value.len - offset); 377 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 378 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 379 return false; 380 } 381 offset += chunk_len; 382 } 383 384 return true; 385 } 386