╭ 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 176 // if this branch was rebased and force pushed, we need to detect that and 177 // properly revert the haxy state to the last valid state. we detect this 178 // by simply asking if the last event is a descendent of the last event 179 // we consumed. if it isn't, then we know the haxy state needs to be reverted. 180 if (last_object_id_maybe) |*last_object_id| { 181 _ = mrg.getDescendent( 182 .xit, 183 repo_opts, 184 state.readOnly(), 185 io, 186 allocator, 187 &std.fmt.bytesToHex(last_object_id, .lower), 188 &std.fmt.bytesToHex(repo_events.items[0].oid, .lower), 189 ) catch |err| switch (err) { 190 error.DescendentNotFound => { 191 if (repo_events.items[repo_events.items.len - 1].parent_oid) |*parent_oid| { 192 const old_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, parent_oid)) orelse return error.CursorNotFound; 193 const old_moment = try DB.HashMap(.read_only).init(old_moment_cursor); 194 195 const old_moment_index_cursor = try old_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 196 const old_moment_index = try old_moment_index_cursor.readUint(); 197 198 // resize the haxy list so we truncate all the moments that were 199 // created after the parent_oid was consumed 200 try haxy.slice(old_moment_index + 1); 201 202 // make a new haxy moment and set its initial value to the last haxy moment 203 haxy_moments_cursor = try haxy.appendCursor(); 204 const old_haxy_moments_cursor = try haxy.getCursor(old_moment_index) orelse return error.CursorNotFound; 205 try haxy_moments_cursor.write(.{ .slot = old_haxy_moments_cursor.slot() }); 206 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 207 208 last_object_id_maybe = parent_oid.*; 209 } else { 210 // the branch was rebased all the way to the very beginning. 211 // we have a repo event with no parent, which means it is now 212 // the very first event. all we need to do is set the haxy list 213 // to be empty and make a new haxy_moments map to work with. 214 215 try haxy.slice(0); 216 haxy_moments_cursor = try haxy.appendCursor(); 217 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 218 219 last_object_id_maybe = null; 220 } 221 }, 222 else => |e| return e, 223 }; 224 } else { 225 if (repo_events.items[repo_events.items.len - 1].parent_oid) |_| { 226 // there is no last_object_id, but this event has a parent. 227 // this is an invalid state. if the event has a parent, that 228 // implies that an event has already been processed, but 229 // if that's true then there would be a last_object_id. 230 return error.UnexpectedParent; 231 } 232 } 233 234 // iterate over the events in reverse order 235 // (they are sorted by most recent events first) 236 for (0..repo_events.items.len) |i| { 237 const repo_event = repo_events.items[repo_events.items.len - i - 1]; 238 239 // create a moment for this object id 240 var haxy_moment_cursor = try haxy_moments.putCursor(hash.bytesToInt(repo_opts.hash, &repo_event.oid)); 241 242 // if there was a previous object id, make this haxy moment's initial value to it. 243 // this efficiently "clones" the map so we make further modifications based on it. 244 if (last_object_id_maybe) |*last_object_id| { 245 if (try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, last_object_id))) |last_haxy_moment_cursor| { 246 try haxy_moment_cursor.write(.{ .slot = last_haxy_moment_cursor.slot() }); 247 } 248 } 249 250 const haxy_moment = try DB.HashMap(.read_write).init(haxy_moment_cursor); 251 252 // associate this moment with the index it will first appear at in the haxy list. 253 // this will be important later so we can truncate that list if the user ever 254 // rebases starting at this object id. 255 try haxy_moment.put(hash.hashInt(repo_opts.hash, "moment-index"), .{ .uint = try haxy.count() - 1 }); 256 257 // consume the event unless it's a merge commit 258 if (!repo_event.is_merge) { 259 var commit_object = try obj.Object(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, &std.fmt.bytesToHex(repo_event.oid, .lower)); 260 defer commit_object.deinit(); 261 262 var arena = std.heap.ArenaAllocator.init(allocator); 263 defer arena.deinit(); 264 265 // read the message from the commit 266 try commit_object.object_reader.seekTo(commit_object.content.commit.message_position); 267 const message = try commit_object.object_reader.interface.allocRemaining(arena.allocator(), .unlimited); 268 269 const event = try std.json.parseFromSliceLeaky(Event, arena.allocator(), message, .{}); 270 271 // get the id of the current event as bytes 272 var current_event_id: [event_id_size]u8 = undefined; 273 _ = try std.fmt.hexToBytes(&current_event_id, &event.id); 274 275 switch (event.data) { 276 .issue => |data| { 277 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->issue")); 278 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 279 280 const issue_cursor = try event_id_to_issue.putCursor(hash.hashInt(repo_opts.hash, &current_event_id)); 281 const issue = try DB.HashMap(.read_write).init(issue_cursor); 282 283 try upsert(DB, repo_opts.hash, issue, @TypeOf(data), data); 284 }, 285 } 286 } 287 288 // the current object id is now the last one 289 last_object_id_maybe = repo_event.oid; 290 291 // prevent any of the data created above from being mutated by future iterations of this loop 292 try state.core.db.freeze(); 293 } 294 295 if (last_object_id_maybe) |*last_object_id| { 296 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = last_object_id }); 297 } 298 } 299 300 fn upsert( 301 comptime DB: type, 302 comptime hash_kind: hash.HashKind, 303 map: DB.HashMap(.read_write), 304 comptime Data: type, 305 data: Data, 306 ) !void { 307 switch (@typeInfo(Data)) { 308 .@"struct" => |struct_info| { 309 inline for (struct_info.fields) |field| { 310 try upsertField(DB, hash_kind, map, field.name, field.type, @field(data, field.name)); 311 } 312 }, 313 else => @compileError("upsert expects a struct"), 314 } 315 } 316 317 fn upsertField( 318 comptime DB: type, 319 comptime hash_kind: hash.HashKind, 320 map: DB.HashMap(.read_write), 321 comptime field_name: []const u8, 322 comptime Field: type, 323 value: Field, 324 ) !void { 325 const key = hash.hashInt(hash_kind, field_name); 326 327 switch (@typeInfo(Field)) { 328 .pointer => |pointer_info| { 329 if (pointer_info.size == .slice and pointer_info.child == u8) { 330 try upsertBytes(DB, hash_kind, map, key, value); 331 } else { 332 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 333 } 334 }, 335 .array => |array_info| { 336 if (array_info.child == u8) { 337 try upsertBytes(DB, hash_kind, map, key, &value); 338 } else { 339 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 340 } 341 }, 342 .int => |int_info| switch (int_info.signedness) { 343 .unsigned => { 344 if (try map.getCursor(key)) |value_cursor| { 345 if (try value_cursor.readUint() == value) { 346 return; 347 } 348 } 349 350 try map.put(key, .{ .uint = value }); 351 }, 352 .signed => { 353 if (try map.getCursor(key)) |value_cursor| { 354 if (try value_cursor.readInt() == value) { 355 return; 356 } 357 } 358 359 try map.put(key, .{ .int = value }); 360 }, 361 }, 362 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 363 } 364 } 365 366 fn upsertBytes( 367 comptime DB: type, 368 comptime hash_kind: hash.HashKind, 369 map: DB.HashMap(.read_write), 370 key: hash.HashInt(hash_kind), 371 value: []const u8, 372 ) !void { 373 var existing_cursor_maybe = try map.getCursor(key); 374 if (existing_cursor_maybe) |*existing_cursor| { 375 if (try bytesEqual(DB, existing_cursor, value)) { 376 return; 377 } 378 } 379 380 var value_cursor = try map.putCursor(key); 381 var write_buffer: [1024]u8 = undefined; 382 var writer = try value_cursor.writer(&write_buffer); 383 try writer.interface.writeAll(value); 384 try writer.finish(); 385 } 386 387 fn bytesEqual( 388 comptime DB: type, 389 cursor: *DB.Cursor(.read_only), 390 value: []const u8, 391 ) !bool { 392 var read_buffer: [1024]u8 = undefined; 393 var reader = try cursor.reader(&read_buffer); 394 if (reader.size != value.len) { 395 return false; 396 } 397 398 var chunk_buffer: [1024]u8 = undefined; 399 var offset: usize = 0; 400 while (offset < value.len) { 401 const chunk_len = @min(chunk_buffer.len, value.len - offset); 402 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 403 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 404 return false; 405 } 406 offset += chunk_len; 407 } 408 409 return true; 410 } 411