walk a repo
One request pulls an entire repo — every record, plus the tree that indexes them — as a CAR file you walk locally.
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 and sync specs; the getRepo lexicon. In the wild: ken and pensieve walk a repo to index it for semantic search; Bailey Townsend's how to download your atproto repo is the backup angle; microcosm's Hubble mirrors every repo in the network.
wiring zat into your own <code>build.zig</code>
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.