1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const std = @import("std");
const zat = @import("zat");
const index = @import("internal/collection_index/index.zig");
const Frame = struct { did: []const u8, ops: zat.cbor.Value };
const Pair = struct { collection: []const u8, did: []const u8 };
fn less(_: void, a: Pair, b: Pair) bool {
const collection = std.mem.order(u8, a.collection, b.collection);
return if (collection == .eq) std.mem.lessThan(u8, a.did, b.did) else collection == .lt;
}
pub fn main(init: std.process.Init) !void {
const args = try init.minimal.args.toSlice(init.arena.allocator());
if (args.len != 3) return error.ExpectedScratchAndCorpus;
const io = init.io;
var arena = std.heap.ArenaAllocator.init(init.gpa);
defer arena.deinit();
const allocator = arena.allocator();
const bytes = try std.Io.Dir.cwd().readFileAlloc(io, args[2], allocator, .limited(32 << 20));
if (bytes.len < 4) return error.ShortCorpus;
const count = std.mem.readInt(u32, bytes[0..4], .big);
var at: usize = 4;
var frames: std.ArrayList(Frame) = .empty;
var seen: std.StringHashMap(void) = .init(allocator);
var pairs: std.ArrayList(Pair) = .empty;
for (0..count) |_| {
if (bytes.len - at < 4) return error.ShortCorpus;
const len = std.mem.readInt(u32, bytes[at..][0..4], .big);
at += 4;
if (len > bytes.len - at) return error.ShortCorpus;
const raw = bytes[at..][0..len];
at += len;
const head = try zat.cbor.decode(allocator, raw);
if (head.value.getInt("op") != 1) continue;
if (!std.mem.eql(u8, head.value.getString("t") orelse "", "#commit")) continue;
const body = try zat.cbor.decodeAll(allocator, raw[head.consumed..]);
const did = body.getString("repo") orelse return error.MissingDid;
const ops = body.get("ops") orelse return error.MissingOps;
try frames.append(allocator, .{ .did = did, .ops = ops });
const items = switch (ops) {
.array => |items| items,
else => return error.InvalidOps,
};
for (items) |op| {
const action = op.getString("action") orelse continue;
if (!std.mem.eql(u8, action, "create") and !std.mem.eql(u8, action, "update")) continue;
const path = op.getString("path") orelse continue;
const slash = std.mem.indexOfScalar(u8, path, '/') orelse continue;
if (slash == 0) continue;
const collection = path[0..slash];
const key = try std.mem.concat(allocator, u8, &.{ collection, "\x00", did });
const entry = try seen.getOrPut(key);
if (!entry.found_existing) try pairs.append(allocator, .{ .collection = collection, .did = did });
}
}
if (at != bytes.len) return error.TrailingCorpus;
std.mem.sort(Pair, pairs.items, {}, less);
var ci = try index.CollectionIndex.open(init.gpa, io, args[1]);
defer ci.deinit();
var out = std.Io.File.stdout().writer(io, &.{});
try out.interface.writeAll("pass,frames,unique_pairs,elapsed_ns\n");
for (0..4) |pass| {
const start = std.Io.Clock.awake.now(io);
for (frames.items) |frame| try ci.trackCommitOps(frame.did, frame.ops);
const elapsed = start.durationTo(std.Io.Clock.awake.now(io)).toNanoseconds();
var pos: usize = 0;
while (pos < pairs.items.len) {
const collection = pairs.items[pos].collection;
var cursor: ?[]const u8 = null;
var output: [4096]u8 = undefined;
var offsets: [100]index.Span = undefined;
var cursor_copy: [512]u8 = undefined;
while (true) {
const page = try ci.listReposByCollection(collection, offsets.len, cursor, &output, &offsets);
for (0..page.count) |i| {
if (pos == pairs.items.len) return error.ExtraPair;
const expected = pairs.items[pos];
if (!std.mem.eql(u8, expected.collection, collection) or
!std.mem.eql(u8, expected.did, page.getDid(i))) return error.WrongPair;
if (!ci.hasCollections(expected.did)) return error.MissingReverseIndex;
pos += 1;
}
const last = page.last_did orelse break;
if (last.len > cursor_copy.len) return error.LongDid;
@memcpy(cursor_copy[0..last.len], last);
cursor = cursor_copy[0..last.len];
}
}
const collections = try ci.listKnownCollections(allocator);
var expected_count: usize = 0;
for (pairs.items, 0..) |pair, i| {
if (i == 0 or !std.mem.eql(u8, pairs.items[i - 1].collection, pair.collection)) expected_count += 1;
}
if (collections.len != expected_count) return error.WrongCollectionCount;
try out.interface.print("{d},{d},{d},{d}\n", .{ pass, frames.items.len, pairs.items.len, elapsed });
}
}