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