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
|
//! test-same (build.zig): `same_sessions A B` exits 0 when two sessions
//! databases hold the same values apart from the wall clock's (a session's
//! `started` and `ended`), and otherwise names the first that differs.
const std = @import("std");
const Sessions = @import("Sessions");
const DB = Sessions.Disk;
const Cursor = DB.Cursor(.read_only);
pub fn main(init: std.process.Init) !void {
const io = init.io;
const gpa = init.arena.allocator();
var args = init.minimal.args.iterate();
_ = args.skip();
var dbs: [2]DB = undefined;
var buffers: [2]std.Io.Writer.Allocating = undefined;
for (&dbs, &buffers) |*db, *buffer| {
const file = try std.Io.Dir.cwd().openFile(io, args.next() orelse return error.Usage, .{});
buffer.* = .init(gpa);
db.* = try DB.init(io, .{ .file = file, .buffer = buffer, .fsync = false });
}
if (!try same(gpa, dbs[0].rootCursor().readOnly(), dbs[1].rootCursor().readOnly(), "root")) std.process.exit(1);
}
fn same(gpa: std.mem.Allocator, a: Cursor, b: Cursor, at: []const u8) anyerror!bool {
const tag = (try a.slot()).tag;
if (tag != (try b.slot()).tag) return differ(at, "kinds");
switch (tag) {
.uint => if (try a.readUint() != try b.readUint()) return differ(at, "numbers"),
.bytes, .short_bytes => {
if (!std.mem.eql(u8, try a.readBytesAlloc(gpa, null), try b.readBytesAlloc(gpa, null))) return differ(at, "bytes");
},
.array_list => {
if (try a.count() != try b.count()) return differ(at, "lengths");
var ia = try a.iterator();
var ib = try b.iterator();
var i: usize = 0;
while (try ia.next()) |ca| : (i += 1) {
if (!try same(gpa, ca, (try ib.next()).?, try std.fmt.allocPrint(gpa, "{s}[{d}]", .{ at, i }))) return false;
}
},
.hash_map => {
const mb = try DB.HashMap(.read_only).init(b);
var n: usize = 0;
var ia = try a.iterator();
while (try ia.next()) |kv_cursor| {
const kv = try kv_cursor.readKeyValuePair();
n += 1;
const name = if (std.enums.fromInt(Sessions.K, kv.hash)) |k| @tagName(k) else "?";
const there = try std.fmt.allocPrint(gpa, "{s}.{s}", .{ at, name });
const cb = try mb.getCursor(kv.hash) orelse return differ(there, "presence");
if (std.mem.eql(u8, name, "started") or std.mem.eql(u8, name, "ended")) continue;
if (!try same(gpa, kv.value_cursor, cb, there)) return false;
}
var ib = try b.iterator();
while (try ib.next()) |_| n -%= 1;
if (n != 0) return differ(at, "fields");
},
else => return differ(at, "kinds (unknown)"),
}
return true;
}
fn differ(at: []const u8, what: []const u8) bool {
std.debug.print("same_sessions: the databases' {s} differ at {s}\n", .{ what, at });
return false;
}
|