Content-addressable store with pluggable Git, ATProto and OCI backends
README.md

Irmin #

Content-addressable store with pluggable Git, ATProto and OCI backends.

Overview #

Git keeps every object under the hash of its bytes, and a commit or a tree refers to other objects by their hashes, so the objects form a directed acyclic graph (DAG). Many other formats work the same way. An ATProto repository (the signed tree of records each user of the AT Protocol keeps; Bluesky runs on that protocol) is one. An OCI container image, whose files and metadata are blobs named by their SHA-256, is another, and IPFS names every file by its hash too. Each format comes with its own library and its own tools, although the operations are much the same everywhere: hash a block, store it, follow links, diff, merge, sync.

Irmin implements those operations once. You write a schema that describes the blocks, and Irmin gives you typed cursors to walk the DAG, fetching each block the first time you step into it. The schema also says how each leaf merges (as a counter, a set, a text or last-write-wins), and Irmin combines those rules to merge whole trees.

Irmin calls the block store a heap: the blocks, found by hash, plus named references such as branches. A computation can record the blocks it reads into a small heap, which then serves as a Merkle proof: another party replays the computation on that heap and checks the result. Two stores synchronise by anti-entropy gossip, by Merkle descent or by Bloom-slice transfer (Journault and Gazagnaire, 2014); Concepts describes all three. The irm command inspects, edits and serves a store from the terminal.

Code that goes through cursors never names its backend. Moving it from Git to ATProto means opening a different heap.

This version of Irmin is rebuilt around a typed cursor over a content-addressed DAG, with codecs that define block structure and merge behaviour in the schema rather than in each backend. It uses Eio for concurrent I/O and requires OCaml 5.2 or later. It installs as nox-irmin, with a CLI called irm, so it does not collide with the upstream irmin and irmin-cli opam packages in a shared switch. The toplevel module is still Irmin, so one binary cannot link both libraries.

Installation #

Install with opam:

$ opam install nox-irmin

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-irmin

Packages #

One source tree, three opam packages:

Package Contents
nox-irmin The library: core (Schema/Gc/Lease/Sync/Worktree) plus dotted sublibraries; heaps and hashes come from cas
irm The porcelain: the irm binary (init/get/set/.../serve/gc), its HTTP host (irm.host), admin (irm.admin) and UI (irm.ui)
nox-irmin-sql SQL over an Irmin tree via ocaml-catalog, with the git-sql shell

The nox-irmin sublibraries come in two groups, and a binary links only the ones it names:

  • Heaps (block storage): nox-irmin.git, nox-irmin.car, nox-irmin.parquet, nox-irmin.atproto
  • Codecs (block interpretation): nox-irmin.json, nox-irmin.cbor, nox-irmin.yaml, nox-irmin.toml, nox-irmin.text, nox-irmin.tar, nox-irmin.gzip, nox-irmin.oci, and nox-irmin.mime (dispatch across all of them)

Quick Start #

Navigate a Git repository through a cursor:

open Irmin_git

let walk_tree () =
  Eio_main.run @@ fun env ->
  let fs = Eio.Stdenv.fs env in
  Eio.Switch.run @@ fun sw ->
  let heap = open_ ~sw ~fs ~path:(Fpath.v ".") in
  match head heap ~branch:"main" with
  | None -> print_endline "empty repository"
  | Some h ->
    let c = at heap tree h in
    list c
    |> List.iter (fun (name, kind) ->
         let tag = match kind with `Node -> "/" | `Leaf -> "" in
         Fmt.pr "  %s%s@." name tag)

Merge behaviour belongs to the schema. A typed merge function is lifted into a strategy on blocks, and the tree-level merge composes the strategies of the leaves:

let counter_merge =
  Irmin.Merge.v
    ~decode:int_of_string
    ~encode:string_of_int
    Irmin.Merge.counter

let counter_leaf =
  leaf ~name:"counter" ~merge:counter_merge ()
(* 5 + 3 = 8, not a conflict *)

let text_leaf =
  leaf ~name:"text"
    ~merge:(Irmin.Merge.v ~decode:Fun.id ~encode:Fun.id Irmin.Merge.lww) ()

let merged_tree =
  fix (fun self ->
    directory
      [ "*.count" => counter_leaf;
        "*.txt"   => text_leaf;
        "*"       => self ])

The same kind of store, from the command line with irm:

$ irm init -r mystore
$ irm set -r mystore config/db.json '{"host":"localhost"}'
$ irm get -r mystore config/db.json
{"host":"localhost"}
$ irm tree -r mystore
config/
  db.json

Concepts #

Heap. The block store. put stores a block under its hash and find returns it; named refs such as branches and HEAD are the only part that changes in place. A heap never looks inside a block.

Codec. What a block means at one level of the DAG. A codec has a MIME-style name, a decoder, an encoder and possibly a merge function. It also says, for each child name, which codec reads that child; fix lets a codec refer to itself, for recursive structures such as directories. A child is either linked, a block of its own that every parent naming its hash shares, or inlined in its parent's bytes. The schema chooses, and a cursor walks both in the same way.

Cursor. A position in the DAG, typed by the value found there. step moves to a child and fetches the child's block only when it is needed. Writes stay in the cursor until flush puts them in the heap.

Proof. produce runs a function on a full heap and records every block it reads into a subheap, together with the root hash before (before) and after (after) the run. verify runs the same function on that subheap alone, starting from before, and returns its result only if it ends on a root whose hash is after. The verifier trusts one value, the before hash, and checks it against a root it already holds. verify does not rehash the blocks it reads, so a proof that comes from someone else is loaded through Cas.Heap.Content_addressed.v, whose find raises Integrity_error on a block whose bytes do not hash to its key. Every block is fetched by the hash its parent gives, so a forged block cannot be reached from the trusted root.

Sync. Blocks move between two live stores in one of three ways. Sync.exchange (anti-entropy gossip) looks at the head each store has for every branch and then, branch by branch, pulls, pushes or reports a divergence. Sync.merkle_diff (Merkle descent) starts from a remote root and only goes into children whose hash is missing locally; it returns the missing blocks, children first. With Sync.Slice_sync (Bloom-slice transfer) the client cuts its history into slices of a fixed size and sends a Bloom filter of the hashes in each. The server walks back from its heads until it hits a hash in a filter, and sends the blocks it walked over: the transfer follows the difference between the stores, however long their history.

irm import and irm export move archives and sit apart from sync: they carry a self-contained CAR snapshot, checked by hash, with no refs, no merge and no network. irm clone runs init, then import, then sets HEAD.

Heaps and codecs #

A heap is a Cas.Heap.t: content-addressed block storage with find, put, mem and named refs. Four sublibraries provide one.

Module Block format Refs
Irmin_git SHA-1 Git trees, blobs and commits Read and write
Irmin_atproto DAG-CBOR (SHA-256 CIDs) Read and write
Irmin_car CARv1 archive segment None: an archive stores blocks only
Irmin_parquet Parquet archive segment, queryable with SQL None: an archive stores blocks only

CBOR is a binary encoding of JSON-like values (RFC 8949), and DAG-CBOR is its deterministic subset in which a link to another block is a tagged CID. A CID (content identifier) is a self-describing hash: a version, a code for the block's encoding and a multihash (the hash function's code, the digest length and the digest). A CAR file is a flat archive of such blocks, each prefixed by its length and its CID.

A codec provides the dec and enc functions that interpret a block and manages no storage. Irmin_json, Irmin_cbor, Irmin_yaml and Irmin_toml split documents into named and indexed children, Irmin_oci reads OCI manifests and configurations, Irmin_tar turns a tar archive into a SHA-256 Merkle tree, and Irmin_text and Irmin_gzip cover text and compressed blocks. Irmin_mime bundles all of them into dispatch rules keyed by media type.

Running a repository #

irm serve hosts a store over HTTP (a tree browser, uploads, optional GitHub OAuth), runs the live garbage collector at an interval, and binds a Unix control socket for the repository. The socket makes the server the daemon that owns the repository. The collector's lease protocol (Lease) gives each block a cell, one atomic integer holding the generation in which a writer last touched it. A writer freshens the cell when it writes or reuses the block, and the collector condemns and then deletes only blocks whose lease is stale; compare-and-set on the cell makes the two decisions exclusive. The cells live in the memory of one process, and a collector in another process could not see a writer's fresh lease and could delete a block that writer is about to link, so a repository has exactly one collector. irm gc respects this on its own: it collects in its own process when no daemon is running, and inside the daemon when one is. A one-shot irm gc on a repository that other processes are writing is safe only when those writers go through the daemon; on a repository nobody is writing it is always safe.

A PDS store (irm init --backend pds) holds one AT Protocol repository: irm set, get, list and del read and write its records as JSON under collection/rkey keys, and irm serve answers the XRPC reads com.atproto.repo.getRecord and com.atproto.repo.listRecords for the DID the store holds.

References #

Licence #

ISC. See LICENSE.md.