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