╭ 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 var arena = std.heap.ArenaAllocator.init(allocator); 134 defer arena.deinit(); 135 136 // compute a list of events that haven't been consumed yet 137 const RepoEvent = struct { 138 parent_oids: []const [hash.hexLen(repo_opts.hash)]u8, 139 oid: [hash.byteLen(repo_opts.hash)]u8, 140 }; 141 var repo_events: std.ArrayList(RepoEvent) = .empty; 142 defer repo_events.deinit(allocator); 143 { 144 var commit_iter = try obj.ObjectIterator(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, .{ .kind = .commit }); 145 defer commit_iter.deinit(); 146 147 const head_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), io, .{ .ref = ref })) orelse return error.OidNotFound; 148 try commit_iter.include(&head_oid); 149 150 while (try commit_iter.next(arena.allocator())) |commit_object| { 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 try repo_events.append(allocator, .{ .parent_oids = parent_oids, .oid = oid }); 161 } 162 } 163 164 // if there are no events to process, look at the oid at HEAD and update 165 // the last_object_id to point to it. this is important in situations 166 // where we do a merge and then force-push to remove the merge. see the 167 // merge test for an example. 168 if (repo_events.items.len == 0) { 169 const head_oid_hex = (try rf.readRecur(.xit, repo_opts, state.readOnly(), io, .{ .ref = ref })) orelse return error.OidNotFound; 170 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 171 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 172 173 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &head_oid)) orelse return error.CursorNotFound; 174 const haxy_moment = try DB.HashMap(.read_only).init(haxy_moment_cursor); 175 176 const moment_index_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 177 const moment_index = try moment_index_cursor.readUint(); 178 179 try haxy.slice(moment_index + 1); 180 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = &head_oid }); 181 return; 182 } 183 184 // if this branch was rebased and force pushed, we need to detect that and 185 // properly revert the haxy state to the last valid state. we detect this 186 // by simply asking if the last event is a descendent of the last event 187 // we consumed. if it isn't, then we know the haxy state needs to be reverted. 188 if (last_object_id_maybe) |*last_object_id| { 189 var is_rebased = false; 190 191 _ = mrg.getDescendent( 192 .xit, 193 repo_opts, 194 state.readOnly(), 195 io, 196 allocator, 197 &std.fmt.bytesToHex(last_object_id, .lower), 198 &std.fmt.bytesToHex(repo_events.items[0].oid, .lower), 199 ) catch |err| switch (err) { 200 error.DescendentNotFound => is_rebased = true, 201 else => |e| return e, 202 }; 203 204 if (is_rebased) { 205 const parent_oids = repo_events.items[repo_events.items.len - 1].parent_oids; 206 switch (parent_oids.len) { 207 0 => { 208 // the branch was rebased all the way to the very beginning. 209 // we have a repo event with no parent, which means it is now 210 // the very first event. all we need to do is set the haxy list 211 // to be empty and make a new haxy_moments map to work with. 212 213 try haxy.slice(0); 214 haxy_moments_cursor = try haxy.appendCursor(); 215 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 216 217 last_object_id_maybe = null; 218 }, 219 1 => { 220 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 221 _ = try std.fmt.hexToBytes(&oid, &parent_oids[0]); 222 223 const old_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 224 const old_moment = try DB.HashMap(.read_only).init(old_moment_cursor); 225 226 const old_moment_index_cursor = try old_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 227 const old_moment_index = try old_moment_index_cursor.readUint(); 228 229 // resize the haxy list so we truncate all the moments that were 230 // created after the parent_oid was consumed 231 try haxy.slice(old_moment_index + 1); 232 233 // make a new haxy moment and set its initial value to the last haxy moment 234 haxy_moments_cursor = try haxy.appendCursor(); 235 const old_haxy_moments_cursor = try haxy.getCursor(old_moment_index) orelse return error.CursorNotFound; 236 try haxy_moments_cursor.write(.{ .slot = old_haxy_moments_cursor.slot() }); 237 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 238 239 last_object_id_maybe = oid; 240 }, 241 else => return error.UnexpectedParentCount, 242 } 243 } 244 } else { 245 if (repo_events.items[repo_events.items.len - 1].parent_oids.len != 0) { 246 // there is no last_object_id, but this event has a parent. 247 // this is an invalid state. if the event has a parent, that 248 // implies that an event has already been processed, but 249 // if that's true then there would be a last_object_id. 250 return error.UnexpectedParent; 251 } 252 } 253 254 // iterate over the events in reverse order 255 // (they are sorted by most recent events first) 256 for (0..repo_events.items.len) |i| { 257 const repo_event = repo_events.items[repo_events.items.len - i - 1]; 258 259 // create a moment for this object id 260 var haxy_moment_cursor = try haxy_moments.putCursor(hash.bytesToInt(repo_opts.hash, &repo_event.oid)); 261 262 // if there was a previous object id, set this haxy moment's initial value to it. 263 // this efficiently "clones" the map so we make further modifications based on it. 264 if (repo_event.parent_oids.len > 0) { 265 var first_parent_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 266 _ = try std.fmt.hexToBytes(&first_parent_oid, &repo_event.parent_oids[0]); 267 268 const first_parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &first_parent_oid)) orelse return error.CursorNotFound; 269 try haxy_moment_cursor.write(.{ .slot = first_parent_haxy_moment_cursor.slot() }); 270 } 271 272 const haxy_moment = try DB.HashMap(.read_write).init(haxy_moment_cursor); 273 274 // merge changes from every parent after the first parent. the common 275 // ancestor is the baseline, so a later parent only contributes values 276 // it changed relative to the merge base. 277 if (repo_event.parent_oids.len > 1) { 278 for (repo_event.parent_oids[1..]) |*parent_oid| { 279 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 280 _ = try std.fmt.hexToBytes(&oid, parent_oid); 281 282 const parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 283 const parent_haxy_moment = try DB.HashMap(.read_only).init(parent_haxy_moment_cursor); 284 285 const baseline_oid_hex = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), io, allocator, &repo_event.parent_oids[0], parent_oid); 286 var baseline_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 287 _ = try std.fmt.hexToBytes(&baseline_oid, &baseline_oid_hex); 288 const baseline_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &baseline_oid)) orelse return error.CursorNotFound; 289 const baseline_haxy_moment = try DB.HashMap(.read_only).init(baseline_haxy_moment_cursor); 290 291 try mergeChangedMapEntries(DB, haxy_moment, parent_haxy_moment, baseline_haxy_moment, true); 292 } 293 } 294 295 // associate this moment with the index it will first appear at in the haxy list. 296 // this will be important later so we can truncate that list if the user ever 297 // rebases starting at this object id. 298 try haxy_moment.put(hash.hashInt(repo_opts.hash, "moment-index"), .{ .uint = try haxy.count() - 1 }); 299 300 // consume the event unless it's a merge commit 301 if (repo_event.parent_oids.len <= 1) { 302 var commit_object = try obj.Object(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, &std.fmt.bytesToHex(repo_event.oid, .lower)); 303 defer commit_object.deinit(); 304 305 // read the message from the commit 306 try commit_object.object_reader.seekTo(commit_object.content.commit.message_position); 307 const message = try commit_object.object_reader.interface.allocRemaining(arena.allocator(), .unlimited); 308 309 const event = try std.json.parseFromSliceLeaky(Event, arena.allocator(), message, .{}); 310 311 // get the id of the current event as bytes 312 var current_event_id: [event_id_size]u8 = undefined; 313 _ = try std.fmt.hexToBytes(&current_event_id, &event.id); 314 315 switch (event.data) { 316 .issue => |data| { 317 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->issue")); 318 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 319 320 const issue_cursor = try event_id_to_issue.putCursor(hash.hashInt(repo_opts.hash, &current_event_id)); 321 const issue = try DB.HashMap(.read_write).init(issue_cursor); 322 323 try upsert(DB, repo_opts.hash, issue, @TypeOf(data), data); 324 }, 325 } 326 } 327 328 // the current object id is now the last one 329 last_object_id_maybe = repo_event.oid; 330 331 // prevent any of the data created above from being mutated by future iterations of this loop 332 try state.core.db.freeze(); 333 } 334 335 if (last_object_id_maybe) |*last_object_id| { 336 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = last_object_id }); 337 } 338 } 339 340 fn upsert( 341 comptime DB: type, 342 comptime hash_kind: hash.HashKind, 343 map: DB.HashMap(.read_write), 344 comptime Data: type, 345 data: Data, 346 ) !void { 347 switch (@typeInfo(Data)) { 348 .@"struct" => |struct_info| { 349 inline for (struct_info.fields) |field| { 350 try upsertField(DB, hash_kind, map, field.name, field.type, @field(data, field.name)); 351 } 352 }, 353 else => @compileError("upsert expects a struct"), 354 } 355 } 356 357 fn mergeChangedMapEntries( 358 comptime DB: type, 359 target: DB.HashMap(.read_write), 360 parent: DB.HashMap(.read_only), 361 baseline: DB.HashMap(.read_only), 362 comptime is_top_level: bool, 363 ) !void { 364 var parent_iter = try parent.iterator(); 365 while (try parent_iter.next()) |kv_pair_cursor| { 366 const kv_pair = try kv_pair_cursor.readKeyValuePair(); 367 368 const baseline_value_cursor = try baseline.getCursor(kv_pair.hash) orelse { 369 if ((try target.getCursor(kv_pair.hash)) != null) { 370 return error.MergeConflict; 371 } 372 373 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 374 continue; 375 }; 376 377 const tag = kv_pair.value_cursor.slot().tag; 378 379 if (tag != baseline_value_cursor.slot().tag) { 380 return error.UnexpectedTag; 381 } 382 383 if (kv_pair.value_cursor.slot().value == baseline_value_cursor.slot().value) { 384 continue; 385 } 386 387 if (tag == .hash_map) { 388 const target_existing_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 389 if (target_existing_cursor.slot().tag != .hash_map) { 390 return error.MergeConflict; 391 } 392 393 const target_child_cursor = try target.putCursor(kv_pair.hash); 394 const target_child = try DB.HashMap(.read_write).init(target_child_cursor); 395 const parent_child = try DB.HashMap(.read_only).init(kv_pair.value_cursor); 396 const baseline_child = try DB.HashMap(.read_only).init(baseline_value_cursor); 397 try mergeChangedMapEntries(DB, target_child, parent_child, baseline_child, false); 398 } else if (is_top_level) { 399 // the moment-index is a top-level key in the haxy moment 400 // and is not a map. we don't want to do any merge of this. 401 continue; 402 } else { 403 const target_value_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 404 if (target_value_cursor.slot().value != baseline_value_cursor.slot().value) { 405 return error.MergeConflict; 406 } 407 408 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 409 } 410 } 411 } 412 413 fn upsertField( 414 comptime DB: type, 415 comptime hash_kind: hash.HashKind, 416 map: DB.HashMap(.read_write), 417 comptime field_name: []const u8, 418 comptime Field: type, 419 value: Field, 420 ) !void { 421 const key = hash.hashInt(hash_kind, field_name); 422 423 switch (@typeInfo(Field)) { 424 .pointer => |pointer_info| { 425 if (pointer_info.size == .slice and pointer_info.child == u8) { 426 try upsertBytes(DB, hash_kind, map, key, value); 427 } else { 428 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 429 } 430 }, 431 .array => |array_info| { 432 if (array_info.child == u8) { 433 try upsertBytes(DB, hash_kind, map, key, &value); 434 } else { 435 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 436 } 437 }, 438 .int => |int_info| switch (int_info.signedness) { 439 .unsigned => { 440 if (try map.getCursor(key)) |value_cursor| { 441 if (try value_cursor.readUint() == value) { 442 return; 443 } 444 } 445 446 try map.put(key, .{ .uint = value }); 447 }, 448 .signed => { 449 if (try map.getCursor(key)) |value_cursor| { 450 if (try value_cursor.readInt() == value) { 451 return; 452 } 453 } 454 455 try map.put(key, .{ .int = value }); 456 }, 457 }, 458 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 459 } 460 } 461 462 fn upsertBytes( 463 comptime DB: type, 464 comptime hash_kind: hash.HashKind, 465 map: DB.HashMap(.read_write), 466 key: hash.HashInt(hash_kind), 467 value: []const u8, 468 ) !void { 469 var existing_cursor_maybe = try map.getCursor(key); 470 if (existing_cursor_maybe) |*existing_cursor| { 471 if (try bytesEqual(DB, existing_cursor, value)) { 472 return; 473 } 474 } 475 476 var value_cursor = try map.putCursor(key); 477 var write_buffer: [1024]u8 = undefined; 478 var writer = try value_cursor.writer(&write_buffer); 479 try writer.interface.writeAll(value); 480 try writer.finish(); 481 } 482 483 fn bytesEqual( 484 comptime DB: type, 485 cursor: *DB.Cursor(.read_only), 486 value: []const u8, 487 ) !bool { 488 var read_buffer: [1024]u8 = undefined; 489 var reader = try cursor.reader(&read_buffer); 490 if (reader.size != value.len) { 491 return false; 492 } 493 494 var chunk_buffer: [1024]u8 = undefined; 495 var offset: usize = 0; 496 while (offset < value.len) { 497 const chunk_len = @min(chunk_buffer.len, value.len - offset); 498 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 499 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 500 return false; 501 } 502 offset += chunk_len; 503 } 504 505 return true; 506 } 507