╭ 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 first parent 275 // is the baseline, so a later parent only contributes values it changed. 276 if (repo_event.parent_oids.len > 1) { 277 var first_parent_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 278 _ = try std.fmt.hexToBytes(&first_parent_oid, &repo_event.parent_oids[0]); 279 const first_parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &first_parent_oid)) orelse return error.CursorNotFound; 280 const first_parent_haxy_moment = try DB.HashMap(.read_only).init(first_parent_haxy_moment_cursor); 281 282 for (repo_event.parent_oids[1..]) |*parent_oid| { 283 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 284 _ = try std.fmt.hexToBytes(&oid, parent_oid); 285 286 const parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 287 const parent_haxy_moment = try DB.HashMap(.read_only).init(parent_haxy_moment_cursor); 288 289 try mergeChangedMapEntries(DB, haxy_moment, parent_haxy_moment, first_parent_haxy_moment, true); 290 } 291 } 292 293 // associate this moment with the index it will first appear at in the haxy list. 294 // this will be important later so we can truncate that list if the user ever 295 // rebases starting at this object id. 296 try haxy_moment.put(hash.hashInt(repo_opts.hash, "moment-index"), .{ .uint = try haxy.count() - 1 }); 297 298 // consume the event unless it's a merge commit 299 if (repo_event.parent_oids.len <= 1) { 300 var commit_object = try obj.Object(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, &std.fmt.bytesToHex(repo_event.oid, .lower)); 301 defer commit_object.deinit(); 302 303 // read the message from the commit 304 try commit_object.object_reader.seekTo(commit_object.content.commit.message_position); 305 const message = try commit_object.object_reader.interface.allocRemaining(arena.allocator(), .unlimited); 306 307 const event = try std.json.parseFromSliceLeaky(Event, arena.allocator(), message, .{}); 308 309 // get the id of the current event as bytes 310 var current_event_id: [event_id_size]u8 = undefined; 311 _ = try std.fmt.hexToBytes(&current_event_id, &event.id); 312 313 switch (event.data) { 314 .issue => |data| { 315 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->issue")); 316 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 317 318 const issue_cursor = try event_id_to_issue.putCursor(hash.hashInt(repo_opts.hash, &current_event_id)); 319 const issue = try DB.HashMap(.read_write).init(issue_cursor); 320 321 try upsert(DB, repo_opts.hash, issue, @TypeOf(data), data); 322 }, 323 } 324 } 325 326 // the current object id is now the last one 327 last_object_id_maybe = repo_event.oid; 328 329 // prevent any of the data created above from being mutated by future iterations of this loop 330 try state.core.db.freeze(); 331 } 332 333 if (last_object_id_maybe) |*last_object_id| { 334 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = last_object_id }); 335 } 336 } 337 338 fn upsert( 339 comptime DB: type, 340 comptime hash_kind: hash.HashKind, 341 map: DB.HashMap(.read_write), 342 comptime Data: type, 343 data: Data, 344 ) !void { 345 switch (@typeInfo(Data)) { 346 .@"struct" => |struct_info| { 347 inline for (struct_info.fields) |field| { 348 try upsertField(DB, hash_kind, map, field.name, field.type, @field(data, field.name)); 349 } 350 }, 351 else => @compileError("upsert expects a struct"), 352 } 353 } 354 355 fn mergeChangedMapEntries( 356 comptime DB: type, 357 target: DB.HashMap(.read_write), 358 parent: DB.HashMap(.read_only), 359 baseline: DB.HashMap(.read_only), 360 comptime is_top_level: bool, 361 ) !void { 362 var parent_iter = try parent.iterator(); 363 while (try parent_iter.next()) |kv_pair_cursor| { 364 const kv_pair = try kv_pair_cursor.readKeyValuePair(); 365 366 const baseline_value_cursor = try baseline.getCursor(kv_pair.hash) orelse { 367 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 368 continue; 369 }; 370 371 if (baseline_value_cursor.slot().value == kv_pair.value_cursor.slot().value) { 372 continue; 373 } 374 375 if (kv_pair.value_cursor.slot().tag != baseline_value_cursor.slot().tag) { 376 return error.UnexpectedTag; 377 } 378 379 const tag = kv_pair.value_cursor.slot().tag; 380 381 if (tag == .hash_map) { 382 const target_child_cursor = try target.putCursor(kv_pair.hash); 383 const target_child = try DB.HashMap(.read_write).init(target_child_cursor); 384 const parent_child = try DB.HashMap(.read_only).init(kv_pair.value_cursor); 385 const baseline_child = try DB.HashMap(.read_only).init(baseline_value_cursor); 386 try mergeChangedMapEntries(DB, target_child, parent_child, baseline_child, false); 387 } else if (is_top_level) { 388 continue; 389 } else { 390 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 391 } 392 } 393 } 394 395 fn upsertField( 396 comptime DB: type, 397 comptime hash_kind: hash.HashKind, 398 map: DB.HashMap(.read_write), 399 comptime field_name: []const u8, 400 comptime Field: type, 401 value: Field, 402 ) !void { 403 const key = hash.hashInt(hash_kind, field_name); 404 405 switch (@typeInfo(Field)) { 406 .pointer => |pointer_info| { 407 if (pointer_info.size == .slice and pointer_info.child == u8) { 408 try upsertBytes(DB, hash_kind, map, key, value); 409 } else { 410 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 411 } 412 }, 413 .array => |array_info| { 414 if (array_info.child == u8) { 415 try upsertBytes(DB, hash_kind, map, key, &value); 416 } else { 417 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 418 } 419 }, 420 .int => |int_info| switch (int_info.signedness) { 421 .unsigned => { 422 if (try map.getCursor(key)) |value_cursor| { 423 if (try value_cursor.readUint() == value) { 424 return; 425 } 426 } 427 428 try map.put(key, .{ .uint = value }); 429 }, 430 .signed => { 431 if (try map.getCursor(key)) |value_cursor| { 432 if (try value_cursor.readInt() == value) { 433 return; 434 } 435 } 436 437 try map.put(key, .{ .int = value }); 438 }, 439 }, 440 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 441 } 442 } 443 444 fn upsertBytes( 445 comptime DB: type, 446 comptime hash_kind: hash.HashKind, 447 map: DB.HashMap(.read_write), 448 key: hash.HashInt(hash_kind), 449 value: []const u8, 450 ) !void { 451 var existing_cursor_maybe = try map.getCursor(key); 452 if (existing_cursor_maybe) |*existing_cursor| { 453 if (try bytesEqual(DB, existing_cursor, value)) { 454 return; 455 } 456 } 457 458 var value_cursor = try map.putCursor(key); 459 var write_buffer: [1024]u8 = undefined; 460 var writer = try value_cursor.writer(&write_buffer); 461 try writer.interface.writeAll(value); 462 try writer.finish(); 463 } 464 465 fn bytesEqual( 466 comptime DB: type, 467 cursor: *DB.Cursor(.read_only), 468 value: []const u8, 469 ) !bool { 470 var read_buffer: [1024]u8 = undefined; 471 var reader = try cursor.reader(&read_buffer); 472 if (reader.size != value.len) { 473 return false; 474 } 475 476 var chunk_buffer: [1024]u8 = undefined; 477 var offset: usize = 0; 478 while (offset < value.len) { 479 const chunk_len = @min(chunk_buffer.len, value.len - offset); 480 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 481 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 482 return false; 483 } 484 offset += chunk_len; 485 } 486 487 return true; 488 } 489