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
99
100
101
102
103
104
105
106
107
108
109
110
# walk a repo
One request pulls an entire repo — every record, plus the tree that indexes them — as a CAR file you walk locally.
```zig
const std = @import("std");
const zat = @import("zat");
/// a running tally of records per collection, built as we walk. keys stay in
/// first-seen order, which — since the MST yields them sorted — is by collection.
const Tally = struct {
allocator: std.mem.Allocator,
counts: std.StringArrayHashMapUnmanaged(u64) = .empty,
fn onEntry(ctx: *anyopaque, entry: zat.mst.WalkEntry) anyerror!void {
const self: *Tally = @ptrCast(@alignCast(ctx));
// MST keys are "collection/rkey"; the collection is everything before the slash.
const slash = std.mem.indexOfScalar(u8, entry.key, '/') orelse return;
const gop = try self.counts.getOrPut(self.allocator, entry.key[0..slash]);
if (!gop.found_existing) gop.value_ptr.* = 0;
gop.value_ptr.* += 1;
}
};
pub fn main(init: std.process.Init) !void {
const io = init.io;
const allocator = init.gpa;
const did = "did:plc:ewvi7nxzyoun6zhxrhs64oiz"; // atproto.com
// find the account's PDS (see the "resolve an identity" recipe for this chain)
var did_resolver = zat.DidResolver.init(io, allocator);
defer did_resolver.deinit();
var doc = try did_resolver.resolve(zat.Did.parse(did).?);
defer doc.deinit();
const endpoint = doc.pdsEndpoint() orelse return error.NoPdsEndpoint;
// one GET pulls the entire repo as a CAR file
const url = try std.fmt.allocPrint(
allocator,
"{s}/xrpc/com.atproto.sync.getRepo?did={s}",
.{ endpoint, did },
);
defer allocator.free(url);
var transport = zat.HttpTransport.initWithUserAgent(io, allocator, "myapp/1.0 (+https://example.com)");
defer transport.deinit();
var res = try transport.fetch(.{ .url = url });
defer res.deinit(allocator);
// decoding the CAR + walking the MST is transient — arena it, free at once
var arena_state = std.heap.ArenaAllocator.init(allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
// decode the signed commit, then load the MST it points at from the CAR blocks
const loaded = try zat.loadCommitFromCAR(arena, res.body);
const root = loaded.commit.data_cid; // MST root CID
var mst = try zat.mst.Mst.loadFromBlocks(arena, loaded.repo_car, root);
// walk every record the MST indexes, tallying by collection
var tally = Tally{ .allocator = arena };
try mst.walk(.{ .ctx = &tally, .entryFn = Tally.onEntry });
var total: u64 = 0;
var it = tally.counts.iterator();
while (it.next()) |kv| {
std.debug.print("{d:>6} {s}\n", .{ kv.value_ptr.*, kv.key_ptr.* });
total += kv.value_ptr.*;
}
const n = tally.counts.count();
std.debug.print("{d:>6} records across {d} collections\n", .{ total, n });
}
```
```
1 actor.rpg.stats
1 app.bsky.actor.profile
2 app.bsky.feed.generator
693 app.bsky.feed.like
220 app.bsky.feed.post
550 app.bsky.feed.repost
21 app.bsky.graph.follow
29 app.bsky.graph.listitem
26 equipment.rpg.give
35 place.stream.chat.message
17 site.standard.document
...
1636 records across 25 collections
```
This is `examples/05_walk_a_repo.zig` verbatim — copy it and `zig build example-walk-a-repo`.
`com.atproto.sync.getRepo` returns the whole repo as a single CAR: all its DAG-CBOR blocks and the MST that orders them. `loadCommitFromCAR` reads the signed commit at the root, `Mst.loadFromBlocks` rebuilds the tree from the CAR's blocks, and walking it yields every record keyed by `collection/rkey`. That is one HTTP request for a repo of any size; paginating `listRecords` per collection is a round trip per page.
`loadCommitFromCAR` already hands you the signed commit, so this walk stops one step short of a proof: verify that commit's signature with the repo's signing key (the "resolve an identity" recipe pulls it), then confirm the tree hashes back to `commit.data_cid`. That is the difference between downloading a repo and proving one.
The same walk sits under a range of tools — backing up or exporting an account, migrating it to another PDS, mirroring the network, verifying a repo block by block against its signed commit, or indexing records for search.
**See also:** the [repository](https://atproto.com/specs/repository) and [sync](https://atproto.com/specs/sync) specs; the [`getRepo`](https://docs.bsky.app/docs/api/com-atproto-sync-get-repo) lexicon. In the wild: [ken](https://ken.waow.tech) and [pensieve](https://pensieve.n8-3e9.workers.dev) walk a repo to index it for semantic search; Bailey Townsend's [how to download your atproto repo](https://notes.atproto.at/a/3mi3p36ni2l23-how-to-download-your-atproto-repo) is the backup angle; [microcosm's Hubble](https://updates.microcosm.blue/3mhj675s55c2a) mirrors every repo in the network.
<details>
<summary>wiring zat into your own <code>build.zig</code></summary>
```zig
const zat = b.dependency("zat", .{}).module("zat");
exe.root_module.addImport("zat", zat);
```
after `zig fetch --save https://tangled.org/zat.dev/zat/archive/main`.
</details>