╭ 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 6 const event_id_size: usize = 32; 7 8 pub const EventData = union(enum) { 9 issue: struct { 10 title: []const u8, 11 description: []const u8, 12 tags: []const []const u8, 13 }, 14 }; 15 16 pub const Event = struct { 17 id: [event_id_size * 2]u8, 18 data: EventData, 19 }; 20 21 /// associates an Event with a git object id. 22 /// we save this oid in the database so we can reliably re-consume 23 /// events if their oid changed. this can happen if the branch 24 /// is rebased and force-pushed. 25 pub fn RepoEvent(comptime hash_kind: hash.HashKind) type { 26 return struct { 27 oid: [hash.byteLen(hash_kind)]u8, 28 event: Event, 29 }; 30 } 31 32 pub fn randomId(random: std.Random) [event_id_size]u8 { 33 var id_bytes: [event_id_size]u8 = undefined; 34 random.bytes(&id_bytes); 35 return id_bytes; 36 } 37 38 pub fn consume( 39 comptime DB: type, 40 comptime hash_kind: hash.HashKind, 41 io: std.Io, 42 db: *DB, 43 db_file: std.Io.File, 44 repo_events: []RepoEvent(hash_kind), 45 ) !void { 46 const Ctx = struct { 47 repo_events: []RepoEvent(hash_kind), 48 49 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 50 const moment = try DB.HashMap(.read_write).init(cursor.*); 51 52 // the map with all of haxy's state and materialized views 53 const haxy_cursor = try moment.putCursor(hash.hashInt(hash_kind, "haxy")); 54 const haxy = try DB.HashMap(.read_write).init(haxy_cursor); 55 56 // try reading the last object id that was consumed 57 var last_object_id_maybe: ?[hash.byteLen(hash_kind)]u8 = null; 58 if (try haxy.getCursor(hash.hashInt(hash_kind, "last-object-id"))) |last_object_id_cursor| { 59 var last_object_id_buffer: [hash.byteLen(hash_kind)]u8 = undefined; 60 _ = try last_object_id_cursor.readBytes(&last_object_id_buffer); 61 last_object_id_maybe = last_object_id_buffer; 62 } 63 64 // for each event we want to consume... 65 for (ctx.repo_events) |repo_event| { 66 // map with the views as they appeared when each event was consumed. 67 // we can use this to see (and revert) the views to any previous state. 68 const object_id_to_views_cursor = try haxy.putCursor(hash.hashInt(hash_kind, "object-id->views")); 69 const object_id_to_views = try DB.HashMap(.read_write).init(object_id_to_views_cursor); 70 71 // create a new views map for the current event we are consuming 72 var views_cursor = try object_id_to_views.putCursor(hash.bytesToInt(hash_kind, &repo_event.oid)); 73 74 // if there was a previous event, set the views map to have the same value as it. 75 // this efficiently "clones" the map so we make further modifications based on it. 76 if (last_object_id_maybe) |*last_object_id| { 77 if (try object_id_to_views.getCursor(hash.bytesToInt(hash_kind, last_object_id))) |last_view_cursor| { 78 try views_cursor.write(.{ .slot = last_view_cursor.slot() }); 79 } 80 } 81 82 // consume the event into the views map 83 { 84 const event = repo_event.event; 85 86 // get the id of the current event as bytes 87 var current_event_id: [event_id_size]u8 = undefined; 88 _ = try std.fmt.hexToBytes(&current_event_id, &event.id); 89 90 const views = try DB.HashMap(.read_write).init(views_cursor); 91 92 switch (event.data) { 93 .issue => |data| { 94 const event_id_to_issue_cursor = try views.putCursor(hash.hashInt(hash_kind, "event-id->issue")); 95 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 96 97 const issue_cursor = try event_id_to_issue.putCursor(hash.hashInt(hash_kind, &current_event_id)); 98 const issue = try DB.HashMap(.read_write).init(issue_cursor); 99 100 try upsert(DB, hash_kind, issue, @TypeOf(data), data); 101 }, 102 } 103 } 104 105 // the current object id is now the last one 106 last_object_id_maybe = repo_event.oid; 107 108 // prevent any of the data created above from being mutated by future iterations of this loop 109 try cursor.db.freeze(); 110 } 111 112 if (last_object_id_maybe) |*last_object_id| { 113 try haxy.put(hash.hashInt(hash_kind, "last-object-id"), .{ .bytes = last_object_id }); 114 } 115 } 116 }; 117 118 try db_file.lock(io, .exclusive); 119 defer db_file.unlock(io); 120 121 // create a new transaction in the database that runs the above-defined Ctx function 122 const history = try DB.ArrayList(.read_write).init(db.rootCursor()); 123 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ .repo_events = repo_events }); 124 } 125 126 fn upsert( 127 comptime DB: type, 128 comptime hash_kind: hash.HashKind, 129 map: DB.HashMap(.read_write), 130 comptime Data: type, 131 data: Data, 132 ) !void { 133 switch (@typeInfo(Data)) { 134 .@"struct" => |struct_info| { 135 inline for (struct_info.fields) |field| { 136 try upsertField(DB, hash_kind, map, field.name, field.type, @field(data, field.name)); 137 } 138 }, 139 else => @compileError("upsert expects a struct"), 140 } 141 } 142 143 fn upsertField( 144 comptime DB: type, 145 comptime hash_kind: hash.HashKind, 146 map: DB.HashMap(.read_write), 147 comptime field_name: []const u8, 148 comptime Field: type, 149 value: Field, 150 ) !void { 151 const key = hash.hashInt(hash_kind, field_name); 152 153 switch (@typeInfo(Field)) { 154 .pointer => |pointer_info| { 155 if (pointer_info.size != .slice) { 156 @compileError("upsert only supports slices, not other pointer types"); 157 } 158 159 if (pointer_info.child == u8) { 160 try upsertBytes(DB, hash_kind, map, key, value); 161 } else { 162 try upsertArray(DB, hash_kind, map, key, Field, value); 163 } 164 }, 165 .array => |array_info| { 166 if (array_info.child == u8) { 167 try upsertBytes(DB, hash_kind, map, key, &value); 168 } else { 169 try upsertArray(DB, hash_kind, map, key, Field, value); 170 } 171 }, 172 .int => |int_info| switch (int_info.signedness) { 173 .unsigned => try upsertUint(DB, hash_kind, map, key, value), 174 .signed => try upsertInt(DB, hash_kind, map, key, value), 175 }, 176 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 177 } 178 } 179 180 fn upsertArray( 181 comptime DB: type, 182 comptime hash_kind: hash.HashKind, 183 map: DB.HashMap(.read_write), 184 key: hash.HashInt(hash_kind), 185 comptime Value: type, 186 value: Value, 187 ) !void { 188 var existing_cursor_maybe = try map.getCursor(key); 189 if (existing_cursor_maybe) |*existing_cursor| { 190 if (try valueEqual(DB, hash_kind, Value, value, existing_cursor)) { 191 return; 192 } 193 } 194 195 var value_cursor = try map.putCursor(key); 196 try value_cursor.write(.{ .slot = null }); 197 try upsertValue(DB, hash_kind, Value, value, &value_cursor); 198 } 199 200 fn upsertValue( 201 comptime DB: type, 202 comptime hash_kind: hash.HashKind, 203 comptime Value: type, 204 value: Value, 205 cursor: *DB.Cursor(.read_write), 206 ) !void { 207 switch (@typeInfo(Value)) { 208 .pointer => |pointer_info| { 209 if (pointer_info.size == .slice and pointer_info.child == u8) { 210 try cursor.write(.{ .bytes = value }); 211 } else if (pointer_info.size == .slice) { 212 const list = try DB.ArrayList(.read_write).init(cursor.*); 213 for (value) |item| { 214 var item_cursor = try list.appendCursor(); 215 try upsertValue(DB, hash_kind, pointer_info.child, item, &item_cursor); 216 } 217 } else { 218 @compileError("unsupported upsert value type: " ++ @typeName(Value)); 219 } 220 }, 221 .array => |array_info| { 222 const list = try DB.ArrayList(.read_write).init(cursor.*); 223 for (value) |item| { 224 var item_cursor = try list.appendCursor(); 225 try upsertValue(DB, hash_kind, array_info.child, item, &item_cursor); 226 } 227 }, 228 .int => |int_info| switch (int_info.signedness) { 229 .unsigned => try cursor.write(.{ .uint = value }), 230 .signed => try cursor.write(.{ .int = value }), 231 }, 232 else => @compileError("unsupported upsert value type: " ++ @typeName(Value)), 233 } 234 } 235 236 fn upsertBytes( 237 comptime DB: type, 238 comptime hash_kind: hash.HashKind, 239 map: DB.HashMap(.read_write), 240 key: hash.HashInt(hash_kind), 241 value: []const u8, 242 ) !void { 243 var existing_cursor_maybe = try map.getCursor(key); 244 if (existing_cursor_maybe) |*existing_cursor| { 245 if (try bytesEqual(DB, existing_cursor, value)) { 246 return; 247 } 248 } 249 250 var value_cursor = try map.putCursor(key); 251 var write_buffer: [1024]u8 = undefined; 252 var writer = try value_cursor.writer(&write_buffer); 253 try writer.interface.writeAll(value); 254 try writer.finish(); 255 } 256 257 fn upsertUint( 258 comptime DB: type, 259 comptime hash_kind: hash.HashKind, 260 map: DB.HashMap(.read_write), 261 key: hash.HashInt(hash_kind), 262 value: u64, 263 ) !void { 264 if (try map.getCursor(key)) |value_cursor| { 265 if (try value_cursor.readUint() == value) { 266 return; 267 } 268 } 269 270 try map.put(key, .{ .uint = value }); 271 } 272 273 fn upsertInt( 274 comptime DB: type, 275 comptime hash_kind: hash.HashKind, 276 map: DB.HashMap(.read_write), 277 key: hash.HashInt(hash_kind), 278 value: i64, 279 ) !void { 280 if (try map.getCursor(key)) |value_cursor| { 281 if (try value_cursor.readInt() == value) { 282 return; 283 } 284 } 285 286 try map.put(key, .{ .int = value }); 287 } 288 289 fn valueEqual( 290 comptime DB: type, 291 comptime hash_kind: hash.HashKind, 292 comptime Value: type, 293 value: Value, 294 cursor: *DB.Cursor(.read_only), 295 ) !bool { 296 switch (@typeInfo(Value)) { 297 .pointer => |pointer_info| { 298 if (pointer_info.size == .slice and pointer_info.child == u8) { 299 return try bytesEqual(DB, cursor, value); 300 } else if (pointer_info.size == .slice) { 301 return try arrayEqual(DB, hash_kind, pointer_info.child, value, cursor); 302 } else { 303 @compileError("unsupported upsert value type: " ++ @typeName(Value)); 304 } 305 }, 306 .array => |array_info| return try arrayEqual(DB, hash_kind, array_info.child, value[0..], cursor), 307 .int => |int_info| switch (int_info.signedness) { 308 .unsigned => return (try cursor.readUint()) == value, 309 .signed => return (try cursor.readInt()) == value, 310 }, 311 else => @compileError("unsupported upsert value type: " ++ @typeName(Value)), 312 } 313 } 314 315 fn arrayEqual( 316 comptime DB: type, 317 comptime hash_kind: hash.HashKind, 318 comptime Item: type, 319 values: []const Item, 320 cursor: *DB.Cursor(.read_only), 321 ) !bool { 322 const list = try DB.ArrayList(.read_only).init(cursor.*); 323 if (try list.count() != values.len) { 324 return false; 325 } 326 327 for (values, 0..) |value, i| { 328 var item_cursor = try list.getCursor(@intCast(i)) orelse return false; 329 if (!try valueEqual(DB, hash_kind, Item, value, &item_cursor)) { 330 return false; 331 } 332 } 333 334 return true; 335 } 336 337 fn bytesEqual( 338 comptime DB: type, 339 cursor: *DB.Cursor(.read_only), 340 value: []const u8, 341 ) !bool { 342 var read_buffer: [1024]u8 = undefined; 343 var reader = try cursor.reader(&read_buffer); 344 if (reader.size != value.len) { 345 return false; 346 } 347 348 var chunk_buffer: [1024]u8 = undefined; 349 var offset: usize = 0; 350 while (offset < value.len) { 351 const chunk_len = @min(chunk_buffer.len, value.len - offset); 352 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 353 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 354 return false; 355 } 356 offset += chunk_len; 357 } 358 359 return true; 360 } 361