╭ 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 20 pub const Event = struct { 21 id: [event_id_size * 2]u8, 22 data: EventData, 23 }; 24 25 /// associates an Event with a git object id. 26 /// we save this oid in the database so we can reliably re-consume 27 /// events if their oid changed. this can happen if the branch 28 /// is rebased and force-pushed. 29 pub fn RepoEvent(comptime hash_kind: hash.HashKind) type { 30 return struct { 31 oid: [hash.byteLen(hash_kind)]u8, 32 event: Event, 33 }; 34 } 35 36 pub fn randomId(random: std.Random) [event_id_size]u8 { 37 var id_bytes: [event_id_size]u8 = undefined; 38 random.bytes(&id_bytes); 39 return id_bytes; 40 } 41 42 pub fn consume( 43 comptime DB: type, 44 comptime hash_kind: hash.HashKind, 45 io: std.Io, 46 db: *DB, 47 db_file: std.Io.File, 48 repo_events: []RepoEvent(hash_kind), 49 ) !void { 50 const Ctx = struct { 51 repo_events: []RepoEvent(hash_kind), 52 53 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 54 const moment = try DB.HashMap(.read_write).init(cursor.*); 55 56 // the map with all of haxy's state and materialized views 57 const haxy_cursor = try moment.putCursor(hash.hashInt(hash_kind, "haxy")); 58 const haxy = try DB.HashMap(.read_write).init(haxy_cursor); 59 60 // try reading the last object id that was consumed 61 var last_object_id_maybe: ?[hash.byteLen(hash_kind)]u8 = null; 62 if (try haxy.getCursor(hash.hashInt(hash_kind, "last-object-id"))) |last_object_id_cursor| { 63 var last_object_id_buffer: [hash.byteLen(hash_kind)]u8 = undefined; 64 _ = try last_object_id_cursor.readBytes(&last_object_id_buffer); 65 last_object_id_maybe = last_object_id_buffer; 66 } 67 68 // for each event we want to consume... 69 for (ctx.repo_events) |repo_event| { 70 // map with the views as they appeared when each event was consumed. 71 // we can use this to see (and revert) the views to any previous state. 72 const object_id_to_views_cursor = try haxy.putCursor(hash.hashInt(hash_kind, "object-id->views")); 73 const object_id_to_views = try DB.HashMap(.read_write).init(object_id_to_views_cursor); 74 75 // create a new views map for the current event we are consuming 76 var views_cursor = try object_id_to_views.putCursor(hash.bytesToInt(hash_kind, &repo_event.oid)); 77 78 // if there was a previous event, set the views map to have the same value as it. 79 // this efficiently "clones" the map so we make further modifications based on it. 80 if (last_object_id_maybe) |*last_object_id| { 81 if (try object_id_to_views.getCursor(hash.bytesToInt(hash_kind, last_object_id))) |last_view_cursor| { 82 try views_cursor.write(.{ .slot = last_view_cursor.slot() }); 83 } 84 } 85 86 // consume the event into the views map 87 { 88 const event = repo_event.event; 89 90 // get the id of the current event as bytes 91 var current_event_id: [event_id_size]u8 = undefined; 92 _ = try std.fmt.hexToBytes(&current_event_id, &event.id); 93 94 const views = try DB.HashMap(.read_write).init(views_cursor); 95 96 switch (event.data) { 97 .issue => |data| { 98 const event_id_to_issue_cursor = try views.putCursor(hash.hashInt(hash_kind, "event-id->issue")); 99 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 100 101 const issue_cursor = try event_id_to_issue.putCursor(hash.hashInt(hash_kind, &current_event_id)); 102 const issue = try DB.HashMap(.read_write).init(issue_cursor); 103 104 try upsert(DB, hash_kind, issue, @TypeOf(data), data); 105 }, 106 } 107 } 108 109 // the current object id is now the last one 110 last_object_id_maybe = repo_event.oid; 111 112 // prevent any of the data created above from being mutated by future iterations of this loop 113 try cursor.db.freeze(); 114 } 115 116 if (last_object_id_maybe) |*last_object_id| { 117 try haxy.put(hash.hashInt(hash_kind, "last-object-id"), .{ .bytes = last_object_id }); 118 } 119 } 120 }; 121 122 try db_file.lock(io, .exclusive); 123 defer db_file.unlock(io); 124 125 // create a new transaction in the database that runs the above-defined Ctx function 126 const history = try DB.ArrayList(.read_write).init(db.rootCursor()); 127 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ .repo_events = repo_events }); 128 } 129 130 pub fn read( 131 comptime DB: type, 132 comptime hash_kind: hash.HashKind, 133 allocator: std.mem.Allocator, 134 map: DB.HashMap(.read_only), 135 kind: EventKind, 136 ) !EventData { 137 return switch (kind) { 138 .issue => .{ 139 .issue = .{ 140 .title = try readBytes(DB, hash_kind, allocator, map, "title"), 141 .description = try readBytes(DB, hash_kind, allocator, map, "description"), 142 .tags = try readBytes(DB, hash_kind, allocator, map, "tags"), 143 }, 144 }, 145 }; 146 } 147 148 fn readBytes( 149 comptime DB: type, 150 comptime hash_kind: hash.HashKind, 151 allocator: std.mem.Allocator, 152 map: DB.HashMap(.read_only), 153 field_name: []const u8, 154 ) ![]const u8 { 155 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 156 return try cursor.readBytesAlloc(allocator, null); 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