cas #
A content-addressable heap with typed digests, for OCaml.
Git, Irmin and the AT Protocol all store immutable blocks under the hash of
their contents, and keep a few mutable names (branches, heads) that point at
those hashes. cas provides that layer once. Cas.Hash gives SHA-1 and
SHA-256 digests distinct types, so a Git object id cannot be used where an
ATProto block key is expected. Cas.Heap stores blocks under their hashes,
binds names to hashes with refs that support compare-and-set, and composes
heaps with combinators. layer top bottom reads from top first and falls
back to bottom, writes to top, and copies into top every block it finds
in bottom, as a cache in front of a store. recording heap behaves as
heap and also keeps every block a find returned; a proof collects the
blocks one computation read this way. It interprets neither
the contents of a block nor any file path: a codec above it decides what a
block means, and a backend below it decides where blocks live.
The cas library is pure. A storage backend implements Cas.Heap.BACKEND,
and Cas.Heap.Make (Backend).v turns a backend handle into a heap. The
companion package cas-eio provides Cas_eio.Fs, a durable SHA-256 heap in a
directory, which refuses a write whose key is not the digest of its block and
checks stored bytes on every read.
The package cas-store keeps answers to requests over such a heap. A crawler
that asks GitHub, ClickHouse, a model or a web page the same question twice
should read the first answer back. cas-store keys a request by the SHA-256 of
its normalised description (a JSON spec, in its RFC 8785 canonical form),
keeps the answer's bytes in a Cas_eio.Fs heap, and binds keys to answers in
an SQLite index (index.db, in WAL mode, on the pure-OCaml sqlite). Each
binding records when the answer was fetched and last confirmed, its
validators (ETag, Last-Modified, a mark such as the newest updatedAt it
covers) and whether it is sealed. The cas-store.key library builds and
compares keys and does no I/O.
Installation #
$ opam install cas cas-eio cas-store
If opam cannot find the packages, they may not yet be released in the public
opam-repository. Add the overlay repository, then install them:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install cas cas-eio cas-store
Usage #
Store a block in an in-memory heap under its SHA-256 digest:
Cas.Heap.read_only ~name heap preserves reads while refusing every mutating
operation with an error that names the store. The cas-eio package provides
Cas_eio.Fs.read_only ~dir () to open a filesystem heap without creating a
missing store or modifying an existing one.
module Memory = Cas.Heap.Mem (struct
type hash = Cas.Hash.sha256
type block = string
let equal = Cas.Hash.equal
let hash = Cas.Hash.hash
end)
let heap = Memory.v ()
let block = "hello"
let key = Cas.Hash.sha256 block
let () = Cas.Heap.put heap key block
Cas.Heap.Content_addressed wraps any heap so that a put whose key differs
from the digest of its block is refused, and a find that reads a block stored
under the wrong key raises Cas.Heap.Integrity_error.
Answers to requests #
A freshness policy decides when a stored answer is still the answer:
Sealed (always: past months, model replies, derivations), Snapshot d
(fetched in snapshot d), Revalidate span (ask again with the stored
validators after span; a not-modified answer keeps the digest), Probe mark
(a cheap probe reported nothing past the stored mark) and Max_age span.
Cas_store.resolve applies it and fetches at most once per key, across the
fibers of a process and the processes sharing a store. Here the fetch counts
its calls; the second resolution in the same snapshot does not call it:
# let key = Cas_store_key.Key.of_spec (Json.Value.of_string_exn {|{"src":"t","q":1}|});;
val key : Cas_store_key.Key.t = <abstr>
# let calls = ref 0;;
val calls : int ref = {contents = 0}
# let fetch _validators =
incr calls;
Cas_store.Body ({|[{"number":1}]|}, Cas_store.Validators.v ~etag:{|"abc"|} ());;
val fetch : 'a -> Cas_store.fetched = <fun>
# let pulls () =
Eio_mock.Backend.run @@ fun () ->
let store = Cas_store.in_memory ~clock:(Eio_mock.Clock.make ()) () in
let policy = Cas_store.Policy.Snapshot "2026-10-08" in
let answer () =
match Cas_store.resolve store ~policy ~fetch key with
| Ok (_, body) -> body
| Error (`Gone status) -> Fmt.str "gone (%d)" status
in
let first = answer () in
let second = answer () in
(first, second);;
val pulls : unit -> string * string = <fun>
# pulls ();;
- : string * string = ("[{\"number\":1}]", "[{\"number\":1}]")
# !calls;;
- : int = 1
A store on disk is opened with Cas_store.v; several processes may open the
same directory. Its heap is Cas_store.objects: Cas_eio.Fs.add stores bytes,
Cas.Heap.find reads them, and a ref set with Cas.Heap.set_ref is a root
that Cas_store.gc keeps. A paged answer is written page by page with
Cas_store.Chain, so a crash resumes after the last committed page, and
sealed when complete. Cas_store.gc removes the objects nothing names once
they are older than a grace period; Cas_store.verify rehashes what the index
and the refs name.
API #
Cas.Hashprovides typed SHA-1 and SHA-256 digests, their raw and hexadecimal forms, comparison, andCas.Hash.anyfor stores that mix both algorithms.Cas.Heapprovides block lookup, insertion and enumeration, named refs with compare-and-set, the combinatorslayer,recordingandof_list, theContent_addressedcheck and the in-memory heapMem.Cas_eio.Fsprovides the filesystem heap.Cas_storeprovides the index from request keys to answers, freshness policies,resolve, page chains, observations and collection.Cas_store_key(cas-store.key) provides the request keys,Key, and the spec builders,Spec.
Related work #
builder-graph'sDerivationkeys a build step by its inputs;Cas_store_key.Spec.derivationmakes such a key one more request key, so step outputs live in the same store.requestshas an RFC 9111 cache in memory;cas-storeis persistent, shared between processes and keyed on a normalised request rather than a URL.cacheis an in-memory TTL cache for values that are cheap to recompute.
Licence #
ISC. See LICENSE.md.