╭ 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 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 and pointer_info.child == u8) { 156 try upsertBytes(DB, hash_kind, map, key, value); 157 } else { 158 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 159 } 160 }, 161 .array => |array_info| { 162 if (array_info.child == u8) { 163 try upsertBytes(DB, hash_kind, map, key, &value); 164 } else { 165 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 166 } 167 }, 168 .int => |int_info| switch (int_info.signedness) { 169 .unsigned => try upsertUint(DB, hash_kind, map, key, value), 170 .signed => try upsertInt(DB, hash_kind, map, key, value), 171 }, 172 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 173 } 174 } 175 176 fn upsertBytes( 177 comptime DB: type, 178 comptime hash_kind: hash.HashKind, 179 map: DB.HashMap(.read_write), 180 key: hash.HashInt(hash_kind), 181 value: []const u8, 182 ) !void { 183 var existing_cursor_maybe = try map.getCursor(key); 184 if (existing_cursor_maybe) |*existing_cursor| { 185 if (try bytesEqual(DB, existing_cursor, value)) { 186 return; 187 } 188 } 189 190 var value_cursor = try map.putCursor(key); 191 var write_buffer: [1024]u8 = undefined; 192 var writer = try value_cursor.writer(&write_buffer); 193 try writer.interface.writeAll(value); 194 try writer.finish(); 195 } 196 197 fn upsertUint( 198 comptime DB: type, 199 comptime hash_kind: hash.HashKind, 200 map: DB.HashMap(.read_write), 201 key: hash.HashInt(hash_kind), 202 value: u64, 203 ) !void { 204 if (try map.getCursor(key)) |value_cursor| { 205 if (try value_cursor.readUint() == value) { 206 return; 207 } 208 } 209 210 try map.put(key, .{ .uint = value }); 211 } 212 213 fn upsertInt( 214 comptime DB: type, 215 comptime hash_kind: hash.HashKind, 216 map: DB.HashMap(.read_write), 217 key: hash.HashInt(hash_kind), 218 value: i64, 219 ) !void { 220 if (try map.getCursor(key)) |value_cursor| { 221 if (try value_cursor.readInt() == value) { 222 return; 223 } 224 } 225 226 try map.put(key, .{ .int = value }); 227 } 228 229 fn bytesEqual( 230 comptime DB: type, 231 cursor: *DB.Cursor(.read_only), 232 value: []const u8, 233 ) !bool { 234 var read_buffer: [1024]u8 = undefined; 235 var reader = try cursor.reader(&read_buffer); 236 if (reader.size != value.len) { 237 return false; 238 } 239 240 var chunk_buffer: [1024]u8 = undefined; 241 var offset: usize = 0; 242 while (offset < value.len) { 243 const chunk_len = @min(chunk_buffer.len, value.len - offset); 244 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 245 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 246 return false; 247 } 248 offset += chunk_len; 249 } 250 251 return true; 252 } 253