╭ 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 user = @import("event/user.zig"); 10 11 pub const event_id_size: usize = 32; 12 13 pub const EventKind = enum { 14 user, 15 issue, 16 }; 17 18 pub const EventData = union(EventKind) { 19 user: struct { 20 name: []const u8, 21 email: []const u8, 22 password_hash: []const u8, 23 }, 24 issue: struct { 25 title: []const u8, 26 description: []const u8, 27 tags: []const u8, 28 }, 29 30 pub fn read( 31 comptime DB: type, 32 comptime hash_kind: hash.HashKind, 33 allocator: std.mem.Allocator, 34 map: DB.HashMap(.read_only), 35 kind: EventKind, 36 ) !EventData { 37 return switch (kind) { 38 .user => .{ 39 .user = .{ 40 .name = try readBytes(DB, hash_kind, allocator, map, "name"), 41 .email = try readBytes(DB, hash_kind, allocator, map, "email"), 42 .password_hash = try readBytes(DB, hash_kind, allocator, map, "password_hash"), 43 }, 44 }, 45 .issue => .{ 46 .issue = .{ 47 .title = try readBytes(DB, hash_kind, allocator, map, "title"), 48 .description = try readBytes(DB, hash_kind, allocator, map, "description"), 49 .tags = try readBytes(DB, hash_kind, allocator, map, "tags"), 50 }, 51 }, 52 }; 53 } 54 55 fn readBytes( 56 comptime DB: type, 57 comptime hash_kind: hash.HashKind, 58 allocator: std.mem.Allocator, 59 map: DB.HashMap(.read_only), 60 field_name: []const u8, 61 ) ![]const u8 { 62 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 63 return try cursor.readBytesAlloc(allocator, null); 64 } 65 }; 66 67 pub const Event = struct { 68 id: [event_id_size * 2]u8, 69 data: EventData, 70 }; 71 72 pub fn randomId(random: std.Random) [event_id_size]u8 { 73 var id_bytes: [event_id_size]u8 = undefined; 74 random.bytes(&id_bytes); 75 return id_bytes; 76 } 77 78 pub fn consume( 79 comptime repo_opts: rp.RepoOpts(.xit), 80 io: std.Io, 81 allocator: std.mem.Allocator, 82 repo: *rp.Repo(.xit, repo_opts), 83 ref: rf.Ref, 84 ) !void { 85 const DB = rp.Repo(.xit, repo_opts).DB; 86 const State = rp.Repo(.xit, repo_opts).State; 87 88 const Ctx = struct { 89 core: *rp.Repo(.xit, repo_opts).Core, 90 io: std.Io, 91 allocator: std.mem.Allocator, 92 ref: rf.Ref, 93 94 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 95 var moment = try DB.HashMap(.read_write).init(cursor.*); 96 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 97 try consumeInTransaction(repo_opts, state, ctx.io, ctx.allocator, ctx.ref); 98 } 99 }; 100 101 try repo.core.db_file.lock(io, .exclusive); 102 defer repo.core.db_file.unlock(io); 103 104 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 105 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 106 .core = &repo.core, 107 .io = io, 108 .allocator = allocator, 109 .ref = ref, 110 }); 111 } 112 113 pub fn consumeInTransaction( 114 comptime repo_opts: rp.RepoOpts(.xit), 115 state: rp.Repo(.xit, repo_opts).State(.read_write), 116 io: std.Io, 117 allocator: std.mem.Allocator, 118 ref: rf.Ref, 119 ) !void { 120 const DB = rp.Repo(.xit, repo_opts).DB; 121 122 // the last_object_id represents the object id that was last consumed 123 var last_object_id_maybe: ?[hash.byteLen(repo_opts.hash)]u8 = null; 124 if (try state.extra.moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id"))) |last_object_id_cursor| { 125 var last_object_id_buffer: [hash.byteLen(repo_opts.hash)]u8 = undefined; 126 _ = try last_object_id_cursor.readBytes(&last_object_id_buffer); 127 last_object_id_maybe = last_object_id_buffer; 128 } 129 130 // the list with all of haxy's state, including materialized views. 131 // the reason it is a list is so we can keep every previous haxy 132 // state, making it easy to revert to an older state if the user 133 // rebases some of the past commits. 134 const haxy_cursor = try state.extra.moment.putCursor(hash.hashInt(repo_opts.hash, "haxy")); 135 const haxy = try DB.ArrayList(.read_write).init(haxy_cursor); 136 137 // add a new item to the haxy list created above. 138 // we call the item haxy_moments. it is a map of object id to haxy moment. 139 // in other words, it maps each object id to a hash map containing the 140 // state that the database was in when that object id was consumed. 141 var haxy_moments_cursor = try haxy.appendCursor(); 142 // use the previous haxy_moments as the basis for this one if it exists 143 if (try haxy.getCursor(-2)) |last_haxy_moments_cursor| { 144 try haxy_moments_cursor.write(.{ .slot = last_haxy_moments_cursor.slot() }); 145 } 146 var haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 147 148 var arena = std.heap.ArenaAllocator.init(allocator); 149 defer arena.deinit(); 150 151 // compute a list of events that haven't been consumed yet 152 const RepoEvent = struct { 153 parent_oids: []const [hash.hexLen(repo_opts.hash)]u8, 154 oid: [hash.byteLen(repo_opts.hash)]u8, 155 }; 156 var repo_events: std.ArrayList(RepoEvent) = .empty; 157 defer repo_events.deinit(allocator); 158 { 159 var commit_iter = try obj.ObjectIterator(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, .{ .kind = .commit }); 160 defer commit_iter.deinit(); 161 162 const head_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), io, .{ .ref = ref })) orelse return error.OidNotFound; 163 try commit_iter.include(&head_oid); 164 165 while (try commit_iter.next(arena.allocator())) |commit_object| { 166 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 167 _ = try std.fmt.hexToBytes(&oid, &commit_object.oid); 168 169 // if this object id has already been consumed, skip it 170 if (null != try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid))) { 171 continue; 172 } 173 174 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 175 try repo_events.append(allocator, .{ .parent_oids = parent_oids, .oid = oid }); 176 } 177 } 178 179 // if there are no events to process, look at the oid at HEAD and update 180 // the last_object_id to point to it. this is important in situations 181 // where we do a merge and then force-push to remove the merge. see the 182 // merge test for an example. 183 if (repo_events.items.len == 0) { 184 const head_oid_hex = (try rf.readRecur(.xit, repo_opts, state.readOnly(), io, .{ .ref = ref })) orelse return error.OidNotFound; 185 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 186 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 187 188 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &head_oid)) orelse return error.CursorNotFound; 189 const haxy_moment = try DB.HashMap(.read_only).init(haxy_moment_cursor); 190 191 const moment_index_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 192 const moment_index = try moment_index_cursor.readUint(); 193 194 try haxy.slice(moment_index + 1); 195 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = &head_oid }); 196 return; 197 } 198 199 // if this branch was rebased and force pushed, we need to detect that and 200 // properly revert the haxy state to the last valid state. we detect this 201 // by simply asking if the last event is a descendent of the last event 202 // we consumed. if it isn't, then we know the haxy state needs to be reverted. 203 if (last_object_id_maybe) |*last_object_id| { 204 var is_rebased = false; 205 206 _ = mrg.getDescendent( 207 .xit, 208 repo_opts, 209 state.readOnly(), 210 io, 211 allocator, 212 &std.fmt.bytesToHex(last_object_id, .lower), 213 &std.fmt.bytesToHex(repo_events.items[0].oid, .lower), 214 ) catch |err| switch (err) { 215 error.DescendentNotFound => is_rebased = true, 216 else => |e| return e, 217 }; 218 219 if (is_rebased) { 220 const parent_oids = repo_events.items[repo_events.items.len - 1].parent_oids; 221 switch (parent_oids.len) { 222 0 => { 223 // the branch was rebased all the way to the very beginning. 224 // we have a repo event with no parent, which means it is now 225 // the very first event. all we need to do is set the haxy list 226 // to be empty and make a new haxy_moments map to work with. 227 228 try haxy.slice(0); 229 haxy_moments_cursor = try haxy.appendCursor(); 230 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 231 232 last_object_id_maybe = null; 233 }, 234 1 => { 235 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 236 _ = try std.fmt.hexToBytes(&oid, &parent_oids[0]); 237 238 const old_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 239 const old_moment = try DB.HashMap(.read_only).init(old_moment_cursor); 240 241 const old_moment_index_cursor = try old_moment.getCursor(hash.hashInt(repo_opts.hash, "moment-index")) orelse return error.CursorNotFound; 242 const old_moment_index = try old_moment_index_cursor.readUint(); 243 244 // resize the haxy list so we truncate all the moments that were 245 // created after the parent_oid was consumed 246 try haxy.slice(old_moment_index + 1); 247 248 // make a new haxy moment and set its initial value to the last haxy moment 249 haxy_moments_cursor = try haxy.appendCursor(); 250 const old_haxy_moments_cursor = try haxy.getCursor(old_moment_index) orelse return error.CursorNotFound; 251 try haxy_moments_cursor.write(.{ .slot = old_haxy_moments_cursor.slot() }); 252 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 253 254 last_object_id_maybe = oid; 255 }, 256 else => return error.UnexpectedParentCount, 257 } 258 } 259 } else { 260 if (repo_events.items[repo_events.items.len - 1].parent_oids.len != 0) { 261 // there is no last_object_id, but this event has a parent. 262 // this is an invalid state. if the event has a parent, that 263 // implies that an event has already been processed, but 264 // if that's true then there would be a last_object_id. 265 return error.UnexpectedParent; 266 } 267 } 268 269 // iterate over the events in reverse order 270 // (they are sorted by most recent events first) 271 for (0..repo_events.items.len) |i| { 272 const repo_event = repo_events.items[repo_events.items.len - i - 1]; 273 274 // create a moment for this object id 275 var haxy_moment_cursor = try haxy_moments.putCursor(hash.bytesToInt(repo_opts.hash, &repo_event.oid)); 276 277 // if there was a previous object id, set this haxy moment's initial value to it. 278 // this efficiently "clones" the map so we make further modifications based on it. 279 if (repo_event.parent_oids.len > 0) { 280 var first_parent_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 281 _ = try std.fmt.hexToBytes(&first_parent_oid, &repo_event.parent_oids[0]); 282 283 const first_parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &first_parent_oid)) orelse return error.CursorNotFound; 284 try haxy_moment_cursor.write(.{ .slot = first_parent_haxy_moment_cursor.slot() }); 285 } 286 287 const haxy_moment = try DB.HashMap(.read_write).init(haxy_moment_cursor); 288 289 // merge changes from every parent after the first parent. the common 290 // ancestor is the baseline, so a later parent only contributes values 291 // it changed relative to the merge base. 292 if (repo_event.parent_oids.len > 1) { 293 for (repo_event.parent_oids[1..]) |*parent_oid| { 294 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 295 _ = try std.fmt.hexToBytes(&oid, parent_oid); 296 297 const parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 298 const parent_haxy_moment = try DB.HashMap(.read_only).init(parent_haxy_moment_cursor); 299 300 const baseline_oid_hex = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), io, allocator, &repo_event.parent_oids[0], parent_oid); 301 var baseline_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 302 _ = try std.fmt.hexToBytes(&baseline_oid, &baseline_oid_hex); 303 const baseline_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &baseline_oid)) orelse return error.CursorNotFound; 304 const baseline_haxy_moment = try DB.HashMap(.read_only).init(baseline_haxy_moment_cursor); 305 306 try mergeChangedMapEntries(DB, haxy_moment, parent_haxy_moment, baseline_haxy_moment, true); 307 } 308 } 309 310 // associate this moment with the index it will first appear at in the haxy list. 311 // this will be important later so we can truncate that list if the user ever 312 // rebases starting at this object id. 313 try haxy_moment.put(hash.hashInt(repo_opts.hash, "moment-index"), .{ .uint = try haxy.count() - 1 }); 314 315 // consume the event unless it's a merge commit 316 if (repo_event.parent_oids.len <= 1) { 317 var commit_object = try obj.Object(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, &std.fmt.bytesToHex(repo_event.oid, .lower)); 318 defer commit_object.deinit(); 319 320 // read the message from the commit 321 try commit_object.object_reader.seekTo(commit_object.content.commit.message_position); 322 const message = try commit_object.object_reader.interface.allocRemaining(arena.allocator(), .unlimited); 323 324 const event = try std.json.parseFromSliceLeaky(Event, arena.allocator(), message, .{}); 325 326 // get the id of the current event as bytes 327 var current_event_id: [event_id_size]u8 = undefined; 328 _ = try std.fmt.hexToBytes(&current_event_id, &event.id); 329 330 switch (event.data) { 331 .user => |data| { 332 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->user")); 333 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 334 335 const user_cursor = try event_id_to_user.putCursor(hash.hashInt(repo_opts.hash, &current_event_id)); 336 const user_map = try DB.HashMap(.read_write).init(user_cursor); 337 338 try upsert(DB, repo_opts.hash, user_map, @TypeOf(data), data); 339 }, 340 .issue => |data| { 341 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->issue")); 342 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 343 344 const issue_cursor = try event_id_to_issue.putCursor(hash.hashInt(repo_opts.hash, &current_event_id)); 345 const issue = try DB.HashMap(.read_write).init(issue_cursor); 346 347 try upsert(DB, repo_opts.hash, issue, @TypeOf(data), data); 348 }, 349 } 350 } 351 352 // the current object id is now the last one 353 last_object_id_maybe = repo_event.oid; 354 355 // prevent any of the data created above from being mutated by future iterations of this loop 356 try state.core.db.freeze(); 357 } 358 359 if (last_object_id_maybe) |*last_object_id| { 360 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = last_object_id }); 361 } 362 } 363 364 fn upsert( 365 comptime DB: type, 366 comptime hash_kind: hash.HashKind, 367 map: DB.HashMap(.read_write), 368 comptime Data: type, 369 data: Data, 370 ) !void { 371 switch (@typeInfo(Data)) { 372 .@"struct" => |struct_info| { 373 inline for (struct_info.fields) |field| { 374 try upsertField(DB, hash_kind, map, field.name, field.type, @field(data, field.name)); 375 } 376 }, 377 else => @compileError("upsert expects a struct"), 378 } 379 } 380 381 fn mergeChangedMapEntries( 382 comptime DB: type, 383 target: DB.HashMap(.read_write), 384 parent: DB.HashMap(.read_only), 385 baseline: DB.HashMap(.read_only), 386 comptime is_top_level: bool, 387 ) !void { 388 var parent_iter = try parent.iterator(); 389 while (try parent_iter.next()) |kv_pair_cursor| { 390 const kv_pair = try kv_pair_cursor.readKeyValuePair(); 391 392 const baseline_value_cursor = try baseline.getCursor(kv_pair.hash) orelse { 393 if ((try target.getCursor(kv_pair.hash)) != null) { 394 return error.MergeConflict; 395 } 396 397 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 398 continue; 399 }; 400 401 const tag = kv_pair.value_cursor.slot().tag; 402 403 if (tag != baseline_value_cursor.slot().tag) { 404 return error.UnexpectedTag; 405 } 406 407 if (kv_pair.value_cursor.slot().value == baseline_value_cursor.slot().value) { 408 continue; 409 } 410 411 if (tag == .hash_map) { 412 const target_existing_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 413 if (target_existing_cursor.slot().tag != .hash_map) { 414 return error.MergeConflict; 415 } 416 417 const target_child_cursor = try target.putCursor(kv_pair.hash); 418 const target_child = try DB.HashMap(.read_write).init(target_child_cursor); 419 const parent_child = try DB.HashMap(.read_only).init(kv_pair.value_cursor); 420 const baseline_child = try DB.HashMap(.read_only).init(baseline_value_cursor); 421 try mergeChangedMapEntries(DB, target_child, parent_child, baseline_child, false); 422 } else if (is_top_level) { 423 // the moment-index is a top-level key in the haxy moment 424 // and is not a map. we don't want to do any merge of this. 425 continue; 426 } else { 427 const target_value_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 428 if (target_value_cursor.slot().value != baseline_value_cursor.slot().value) { 429 return error.MergeConflict; 430 } 431 432 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 433 } 434 } 435 } 436 437 fn upsertField( 438 comptime DB: type, 439 comptime hash_kind: hash.HashKind, 440 map: DB.HashMap(.read_write), 441 comptime field_name: []const u8, 442 comptime Field: type, 443 value: Field, 444 ) !void { 445 const key = hash.hashInt(hash_kind, field_name); 446 447 switch (@typeInfo(Field)) { 448 .pointer => |pointer_info| { 449 if (pointer_info.size == .slice and pointer_info.child == u8) { 450 try upsertBytes(DB, hash_kind, map, key, value); 451 } else { 452 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 453 } 454 }, 455 .array => |array_info| { 456 if (array_info.child == u8) { 457 try upsertBytes(DB, hash_kind, map, key, &value); 458 } else { 459 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 460 } 461 }, 462 .int => |int_info| switch (int_info.signedness) { 463 .unsigned => { 464 if (try map.getCursor(key)) |value_cursor| { 465 if (try value_cursor.readUint() == value) { 466 return; 467 } 468 } 469 470 try map.put(key, .{ .uint = value }); 471 }, 472 .signed => { 473 if (try map.getCursor(key)) |value_cursor| { 474 if (try value_cursor.readInt() == value) { 475 return; 476 } 477 } 478 479 try map.put(key, .{ .int = value }); 480 }, 481 }, 482 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 483 } 484 } 485 486 fn upsertBytes( 487 comptime DB: type, 488 comptime hash_kind: hash.HashKind, 489 map: DB.HashMap(.read_write), 490 key: hash.HashInt(hash_kind), 491 value: []const u8, 492 ) !void { 493 var existing_cursor_maybe = try map.getCursor(key); 494 if (existing_cursor_maybe) |*existing_cursor| { 495 if (try bytesEqual(DB, existing_cursor, value)) { 496 return; 497 } 498 } 499 500 var value_cursor = try map.putCursor(key); 501 var write_buffer: [1024]u8 = undefined; 502 var writer = try value_cursor.writer(&write_buffer); 503 try writer.interface.writeAll(value); 504 try writer.finish(); 505 } 506 507 fn bytesEqual( 508 comptime DB: type, 509 cursor: *DB.Cursor(.read_only), 510 value: []const u8, 511 ) !bool { 512 var read_buffer: [1024]u8 = undefined; 513 var reader = try cursor.reader(&read_buffer); 514 if (reader.size != value.len) { 515 return false; 516 } 517 518 var chunk_buffer: [1024]u8 = undefined; 519 var offset: usize = 0; 520 while (offset < value.len) { 521 const chunk_len = @min(chunk_buffer.len, value.len - offset); 522 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 523 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 524 return false; 525 } 526 offset += chunk_len; 527 } 528 529 return true; 530 } 531