╭ search_commit.zig ────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("event.zig"); 3 const srch = @import("search.zig"); 4 const xit = @import("xit"); 5 const rp = xit.repo; 6 const hash = xit.hash; 7 const rf = xit.ref; 8 const obj = xit.object; 9 const mrg = xit.merge; 10 11 // the repo moment key holding one commit-message index per ref tip. it sits 12 // outside the haxy moment because pushes change it and events never do. each 13 // entry is keyed by its commit oid's hash: its value is the index as of that 14 // commit, and its key slot holds the raw commit oid. 15 pub const index_key = "haxy/commit-oid->word->time+oid"; 16 17 // a commit a version covers. 18 pub fn Commit(comptime hash_kind: hash.HashKind) type { 19 return struct { 20 oid: [hash.hexLen(hash_kind)]u8, 21 timestamp: u64, 22 }; 23 } 24 25 // `commit_oid`'s index, or null when it has none. a version is correct for its 26 // commit by construction, so there is nothing to check for staleness. 27 pub fn lookup( 28 comptime repo_opts: rp.RepoOpts(.xit), 29 moment: rp.Repo(.xit, repo_opts).DB.HashMap(.read_only), 30 commit_oid: *const [hash.hexLen(repo_opts.hash)]u8, 31 ) !?rp.Repo(.xit, repo_opts).DB.SortedMap(.read_only) { 32 const DB = rp.Repo(.xit, repo_opts).DB; 33 const index_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, index_key)) orelse return null; 34 const index = try DB.HashMap(.read_only).init(index_cursor); 35 const cursor = try index.getCursor(try hash.hexToInt(repo_opts.hash, commit_oid)) orelse return null; 36 return try DB.SortedMap(.read_only).init(cursor); 37 } 38 39 // a commit's doc key: its committer timestamp counting down, so a posting set 40 // iterates newest first and a new commit inserts at the front, then its raw 41 // oid, which makes the key unique. 42 pub fn docKey( 43 comptime hash_kind: hash.HashKind, 44 timestamp: u64, 45 oid: *const [hash.hexLen(hash_kind)]u8, 46 ) ![docKeyLen(hash_kind)]u8 { 47 var key: [docKeyLen(hash_kind)]u8 = undefined; 48 std.mem.writeInt(u64, key[0..@sizeOf(u64)], std.math.maxInt(u64) - timestamp, .big); 49 _ = try std.fmt.hexToBytes(key[@sizeOf(u64)..], oid); 50 return key; 51 } 52 53 // the commit a doc key names. 54 pub fn keyOid(comptime hash_kind: hash.HashKind, key: []const u8) ![hash.hexLen(hash_kind)]u8 { 55 if (key.len != docKeyLen(hash_kind)) return error.InvalidCommitIndex; 56 var oid: [hash.byteLen(hash_kind)]u8 = undefined; 57 @memcpy(&oid, key[@sizeOf(u64)..]); 58 return std.fmt.bytesToHex(oid, .lower); 59 } 60 61 fn docKeyLen(comptime hash_kind: hash.HashKind) usize { 62 return @sizeOf(u64) + hash.byteLen(hash_kind); 63 } 64 65 // the action a commit index refresh records when it runs on its own 66 pub const undo_action = "haxy/commit-index"; 67 68 // reconcile the index versions with the repo's ref tips, in a transaction of 69 // its own 70 pub fn refresh( 71 comptime repo_opts: rp.RepoOpts(.xit), 72 io: std.Io, 73 allocator: std.mem.Allocator, 74 repo: *rp.Repo(.xit, repo_opts), 75 updates: ?[]const xit.net_server_receive_pack.AppliedRefUpdate, 76 ) !void { 77 const Repo = rp.Repo(.xit, repo_opts); 78 const DB = Repo.DB; 79 const Save = struct { 80 core: *Repo.Core, 81 io: std.Io, 82 allocator: std.mem.Allocator, 83 updates: ?[]const xit.net_server_receive_pack.AppliedRefUpdate, 84 85 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 86 var moment = try DB.HashMap(.read_write).init(cursor.*); 87 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 88 if (!try refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.updates)) return error.CancelTransaction; 89 90 // record the user action for undo 91 try xit.undo.write(repo_opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ .custom = .{ .action_kind = undo_action } }); 92 } 93 }; 94 95 // held across the reads and the writes, so a concurrent refresh can't 96 // change the entries these decisions were made from. 97 try repo.core.db_file.lock(io, .exclusive); 98 defer repo.core.db_file.unlock(io); 99 100 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 101 history.appendContext(.{ .slot = try history.getSlot(-1) }, Save{ .core = &repo.core, .io = io, .allocator = allocator, .updates = updates }) catch |err| switch (err) { 102 error.CancelTransaction => {}, 103 else => return err, 104 }; 105 } 106 107 // reconcile them inside the caller's transaction, so the index lands with the 108 // push that invalidated it. `updates` is that push, whose old oids are the 109 // cheapest bases to derive the new tips from. reports whether anything was 110 // written, so a caller that owns an otherwise empty transaction can cancel it 111 pub fn refreshInTransaction( 112 comptime repo_opts: rp.RepoOpts(.xit), 113 state: rp.Repo(.xit, repo_opts).State(.read_write), 114 moment: *rp.Repo(.xit, repo_opts).DB.HashMap(.read_write), 115 io: std.Io, 116 allocator: std.mem.Allocator, 117 updates: ?[]const xit.net_server_receive_pack.AppliedRefUpdate, 118 ) !bool { 119 const Repo = rp.Repo(.xit, repo_opts); 120 const DB = Repo.DB; 121 const Oid = [hash.hexLen(repo_opts.hash)]u8; 122 const Tip = Commit(repo_opts.hash); 123 124 // one version to build: the commit it covers, and the commit whose version 125 // it derives from (null = walk the whole history). 126 const Work = struct { 127 oid: Oid, 128 timestamp: u64, 129 base: ?Oid, 130 }; 131 132 var arena = std.heap.ArenaAllocator.init(allocator); 133 defer arena.deinit(); 134 const aa = arena.allocator(); 135 136 var work: std.ArrayList(Work) = .empty; 137 var gone: std.ArrayList(Oid) = .empty; 138 var default_tip: ?Oid = null; 139 // the versions a new tip can derive from 140 var versions: std.ArrayList(Tip) = .empty; 141 142 { 143 const read_state = state.readOnly(); 144 145 // the commits that already have a version 146 var existing: std.AutoArrayHashMapUnmanaged(Oid, void) = .empty; 147 if (try moment.getCursor(hash.hashInt(repo_opts.hash, index_key))) |index_cursor| { 148 const index = try DB.HashMap(.read_only).init(index_cursor); 149 var iter = try index.iterator(); 150 while (try iter.next()) |cursor| { 151 const pair = try cursor.readKeyValuePair(); 152 var oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 153 if ((try pair.key_cursor.readBytes(&oid_bytes)).len != oid_bytes.len) return error.InvalidCommitIndex; 154 try existing.put(aa, std.fmt.bytesToHex(oid_bytes, .lower), {}); 155 } 156 } 157 158 // what every head except the events branch and every tag points at 159 var tips: std.ArrayList(Oid) = .empty; 160 { 161 var branches = try rf.RefIterator(.xit, repo_opts).init(read_state, io, allocator, .head, .beginning); 162 defer branches.deinit(); 163 while (try branches.next()) |ref| { 164 if (std.mem.eql(u8, ref.name, evt.events_ref.name)) continue; 165 try tips.append(aa, (try rf.readRecur(.xit, repo_opts, read_state, io, .{ .ref = .{ .kind = .head, .name = ref.name } })) orelse continue); 166 } 167 } 168 { 169 var tags = try rf.RefIterator(.xit, repo_opts).init(read_state, io, allocator, .tag, .beginning); 170 defer tags.deinit(); 171 while (try tags.next()) |ref| { 172 try tips.append(aa, (try rf.readRecur(.xit, repo_opts, read_state, io, .{ .ref = .{ .kind = .tag, .name = ref.name } })) orelse continue); 173 } 174 } 175 176 // the commits a version is wanted for, annotated tags peeled. refs 177 // sharing a commit share its version. 178 var wanted: std.AutoArrayHashMapUnmanaged(Oid, void) = .empty; 179 var missing: std.AutoArrayHashMapUnmanaged(Oid, Work) = .empty; 180 for (tips.items) |tip| { 181 // a tip that already has a version needs no reading 182 if (existing.contains(tip)) { 183 try wanted.put(aa, tip, {}); 184 continue; 185 } 186 var commit = obj.Object(.xit, repo_opts).initCommit(read_state, io, allocator, &tip) catch continue; 187 defer commit.deinit(); 188 try wanted.put(aa, commit.oid, {}); 189 if (existing.contains(commit.oid)) continue; 190 const entry = try missing.getOrPut(aa, commit.oid); 191 if (!entry.found_existing) entry.value_ptr.* = .{ 192 .oid = commit.oid, 193 .timestamp = commit.content.commit.metadata.timestamp, 194 .base = null, 195 }; 196 if (entry.value_ptr.base == null) entry.value_ptr.base = pushedFrom(repo_opts, updates, &tip, existing); 197 } 198 199 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 200 if (rf.readHead(.xit, repo_opts, read_state, io, &head_buffer) catch null) |head| switch (head) { 201 .ref => |ref| default_tip = try rf.readRecur(.xit, repo_opts, read_state, io, .{ .ref = .{ .kind = .head, .name = ref.name } }), 202 .oid => {}, 203 }; 204 205 // an entry whose commit is gone can't be walked from, so it isn't a 206 // candidate. 207 const unbased = for (missing.values()) |item| { 208 if (item.base == null) break true; 209 } else false; 210 if (unbased) for (existing.keys()) |oid| { 211 var commit = obj.Object(.xit, repo_opts).initCommit(read_state, io, allocator, &oid) catch continue; 212 defer commit.deinit(); 213 try versions.append(aa, .{ .oid = oid, .timestamp = commit.content.commit.metadata.timestamp }); 214 }; 215 216 try work.appendSlice(aa, missing.values()); 217 // oldest first, so a later tip derives from an earlier one 218 std.mem.sort(Work, work.items, {}, struct { 219 fn lessThan(_: void, a: Work, b: Work) bool { 220 return a.timestamp < b.timestamp; 221 } 222 }.lessThan); 223 224 for (existing.keys()) |oid| { 225 if (!wanted.contains(oid)) try gone.append(aa, oid); 226 } 227 } 228 229 if (work.items.len == 0 and gone.items.len == 0) return false; 230 231 { 232 const index_hash = hash.hashInt(repo_opts.hash, index_key); 233 234 var message: std.ArrayList(u8) = .empty; 235 defer message.deinit(allocator); 236 237 for (work.items) |item| { 238 const index = try DB.HashMap(.read_write).init(try moment.putCursor(index_hash)); 239 const base = item.base orelse nearestVersion(repo_opts.hash, versions.items, default_tip, item.timestamp); 240 241 var oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 242 _ = try std.fmt.hexToBytes(&oid_bytes, &item.oid); 243 const oid_int = hash.bytesToInt(repo_opts.hash, &oid_bytes); 244 try index.putKey(oid_int, .{ .bytes = &oid_bytes }); 245 var version_cursor = try index.putCursor(oid_int); 246 if (base) |base_oid| { 247 const base_cursor = (try index.getCursor(try hash.hexToInt(repo_opts.hash, &base_oid))) orelse return error.InvalidCommitIndex; 248 try version_cursor.write(.{ .slot = base_cursor.slot() }); 249 } 250 const version = try DB.SortedMap(.read_write).init(version_cursor); 251 252 var diff_arena = std.heap.ArenaAllocator.init(allocator); 253 defer diff_arena.deinit(); 254 const diff = try commitDiff(repo_opts, state.readOnly(), io, allocator, diff_arena.allocator(), if (base) |*base_oid| base_oid else null, &item.oid); 255 for (diff.removed) |commit| { 256 const key = try readPosting(repo_opts, state.readOnly(), io, allocator, &message, commit); 257 try srch.remove(DB, version, allocator, &key, message.items); 258 } 259 for (diff.added) |commit| { 260 const key = try readPosting(repo_opts, state.readOnly(), io, allocator, &message, commit); 261 try srch.add(DB, version, allocator, &key, message.items); 262 } 263 264 try versions.append(aa, .{ .oid = item.oid, .timestamp = item.timestamp }); 265 // the next version copies this one's slot, so everything 266 // written so far has to be shared rather than mutated 267 try state.core.db.freeze(); 268 } 269 270 // removing last keeps a tip that is going away available as a base 271 const index = try DB.HashMap(.read_write).init(try moment.putCursor(index_hash)); 272 for (gone.items) |oid| _ = try index.remove(try hash.hexToInt(repo_opts.hash, &oid)); 273 } 274 return true; 275 } 276 277 // the version a tip committed at `timestamp` derives from when no push names 278 // one: the default branch's unless it is newer than the tip, else the nearest 279 // older one, else the oldest. a guess by dates that only the cost rides on. 280 fn nearestVersion( 281 comptime hash_kind: hash.HashKind, 282 versions: []const Commit(hash_kind), 283 default_tip: ?[hash.hexLen(hash_kind)]u8, 284 timestamp: u64, 285 ) ?[hash.hexLen(hash_kind)]u8 { 286 var older: ?Commit(hash_kind) = null; 287 var oldest: ?Commit(hash_kind) = null; 288 for (versions) |version| { 289 if (version.timestamp <= timestamp) { 290 if (default_tip) |oid| if (std.mem.eql(u8, &oid, &version.oid)) return oid; 291 const nearer = if (older) |found| version.timestamp > found.timestamp else true; 292 if (nearer) older = version; 293 } 294 const earlier = if (oldest) |found| version.timestamp < found.timestamp else true; 295 if (earlier) oldest = version; 296 } 297 return if (older orelse oldest) |version| version.oid else null; 298 } 299 300 // the commit this push moved a ref to `tip` from, when it has a version to 301 // derive from. 302 fn pushedFrom( 303 comptime repo_opts: rp.RepoOpts(.xit), 304 updates: ?[]const xit.net_server_receive_pack.AppliedRefUpdate, 305 tip: *const [hash.hexLen(repo_opts.hash)]u8, 306 existing: std.AutoArrayHashMapUnmanaged([hash.hexLen(repo_opts.hash)]u8, void), 307 ) ?[hash.hexLen(repo_opts.hash)]u8 { 308 for (updates orelse &.{}) |update| { 309 if (!std.mem.eql(u8, update.new_oid, tip)) continue; 310 if (update.old_oid.len != hash.hexLen(repo_opts.hash)) continue; 311 var oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 312 @memcpy(&oid, update.old_oid); 313 if (existing.contains(oid)) return oid; 314 } 315 return null; 316 } 317 318 // read `commit`'s message into `message` and return the doc key it indexes 319 // under. 320 fn readPosting( 321 comptime repo_opts: rp.RepoOpts(.xit), 322 state: rp.Repo(.xit, repo_opts).State(.read_only), 323 io: std.Io, 324 allocator: std.mem.Allocator, 325 message: *std.ArrayList(u8), 326 commit: Commit(repo_opts.hash), 327 ) ![docKeyLen(repo_opts.hash)]u8 { 328 message.clearRetainingCapacity(); 329 var object = try obj.Object(.xit, repo_opts).init(state, io, allocator, &commit.oid); 330 defer object.deinit(); 331 object.readMessage(allocator, message, .limited(srch.max_indexed_bytes)) catch |err| switch (err) { 332 // only the indexed prefix matters 333 error.StreamTooLong => {}, 334 else => |e| return e, 335 }; 336 return docKey(repo_opts.hash, commit.timestamp, &commit.oid); 337 } 338 339 // what `tip` reaches that `base_tip` doesn't, and the other way round, built 340 // into `aa`. with no base the whole history is added. 341 fn commitDiff( 342 comptime repo_opts: rp.RepoOpts(.xit), 343 state: rp.Repo(.xit, repo_opts).State(.read_only), 344 io: std.Io, 345 allocator: std.mem.Allocator, 346 aa: std.mem.Allocator, 347 base_tip: ?*const [hash.hexLen(repo_opts.hash)]u8, 348 tip: *const [hash.hexLen(repo_opts.hash)]u8, 349 ) !struct { removed: []const Commit(repo_opts.hash), added: []const Commit(repo_opts.hash) } { 350 var removed: std.ArrayList(Commit(repo_opts.hash)) = .empty; 351 var added: std.ArrayList(Commit(repo_opts.hash)) = .empty; 352 353 if (base_tip) |base| { 354 const Ancestry = mrg.Ancestry(.xit, repo_opts); 355 var ancestry = try Ancestry.init(state, io, allocator, base, tip); 356 defer ancestry.deinit(); 357 try ancestry.finish(); 358 359 // timestamps only choose the walk's order, so a commit the base reached 360 // can be passed before the tip turns out to reach it too. that can only 361 // make a removal wrong, never an addition. a fast-forward removes 362 // nothing, which the base tip picking up the tip's flag proves. anything 363 // else is settled by walking until nothing is left to carry down. 364 const base_node = ancestry.nodes.get(ancestry.tips[0]) orelse unreachable; 365 const fast_forward = base_node.flags & Ancestry.two != 0; 366 if (!fast_forward) { 367 var candidates = false; 368 var flagged = ancestry.nodes.valueIterator(); 369 while (flagged.next()) |node| { 370 if (node.flags & Ancestry.both == Ancestry.one) candidates = true; 371 } 372 if (candidates) while (try ancestry.step()) {}; 373 } 374 375 var iter = ancestry.nodes.iterator(); 376 while (iter.next()) |entry| { 377 // tag objects are peeled through, not walked 378 if (entry.value_ptr.tag_target != null) continue; 379 const commit = Commit(repo_opts.hash){ .oid = entry.key_ptr.*, .timestamp = entry.value_ptr.timestamp }; 380 switch (entry.value_ptr.flags & Ancestry.both) { 381 Ancestry.one => if (!fast_forward) try removed.append(aa, commit), 382 Ancestry.two => try added.append(aa, commit), 383 else => {}, 384 } 385 } 386 } else { 387 var iter = try obj.ObjectIterator(.xit, repo_opts).init(state, io, allocator, .{ .kind = .commit }); 388 defer iter.deinit(); 389 try iter.include(tip); 390 while (try iter.next(allocator)) |object| { 391 defer object.deinit(); 392 try added.append(aa, .{ .oid = object.oid, .timestamp = object.content.commit.metadata.timestamp }); 393 } 394 } 395 396 return .{ .removed = removed.items, .added = added.items }; 397 } 398