cdc #
Content-defined chunking (FastCDC) in pure OCaml.
Splitting a file into fixed-size blocks makes deduplication fragile: insert
one byte near the start and every later block shifts, so every later hash
changes. Content-defined chunking decides each boundary from the bytes around
it instead of from its offset. After a small insertion or deletion only the
chunks around the edit change, and every later chunk keeps its boundary and
its hash. Deduplicating storage and delta synchronisation (rsync, restic,
casync) are built on this property.
The algorithm is FastCDC (Wen Xia et al., USENIX ATC 2016): a gear
rolling hash with normalised chunking. The fingerprint fp is updated for each
byte b as fp = (fp << 1) + Gear[b], where Gear is a fixed table of 256
pseudo-random 62-bit values; the shift pushes each byte's contribution out of
the 62 low bits the masks read after 62 more bytes, so the fingerprint depends
only on a sliding window of the input. A boundary falls after the byte at which
fp ANDed with a mask of its high bits is zero. With an expected chunk size of
2^k bytes, the mask has k + 2 bits before that size (a boundary with
probability 2^-(k+2) per byte) and k - 2 bits after it (2^-(k-2)), the
harder and easier tests of normalised chunking, which pull chunk sizes towards
2^k. No boundary falls in the first 2^k / 4 bytes of a chunk, and a chunk
ends at 8 x 2^k bytes whatever the fingerprint. With the default
avg_size = 8192 (k = 13), chunks are 2 KiB to 64 KiB, and the per-byte
boundary probability is 2^-15 before 8 KiB and 2^-11 after it.
Cdc.cutreturns the chunks of a string.Cdc.foldchunks aBytesrw.Bytes.Reader.tas a stream, in memory bounded
by the maximum chunk size.Cdc.Deltasends a blob over a one-way link: it splits the blob into chunks
named by a hash the caller supplies, selects the chunks the receiver does not
hold, and reassembles the blob on the other side. Over a one-way link the
receiver cannot answer, soDelta.noveltakes ahavepredicate from the
caller, typically built from the chunk digests the receiver listed in its
last report. The sender ships the novel chunks and the recipe (the ordered
list of digests), andDelta.reassemblerebuilds the blob from the recipe,
the receiver's store and the shipped chunks.
The gear table is a constant of this implementation, so boundaries are stable
across runs and versions. No standard fixes them across implementations.
Installation #
$ opam install cdc
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 cdc
Usage #
# let p = Cdc.params ~min_size:64 ~avg_size:256 ~max_size:1024 ();;
val p : Cdc.params = <abstr>
# let gen () = String.init 20000 (fun i -> Char.chr ((i * 31 + (i / 7)) land 0xff));;
val gen : unit -> string = <fun>
# let data = gen () in
let chunks = Cdc.cut p data in
List.for_all
(fun (c : Cdc.chunk) -> c.len >= 1 && c.len <= 1024)
chunks
- : bool = true
# let data = gen () in
let chunks = Cdc.cut p data in
String.concat ""
(List.map (fun (c : Cdc.chunk) -> String.sub data c.off c.len) chunks)
= data
- : bool = true
Chunk a stream without holding all of it in memory:
# let data = gen () in
let r = Bytesrw.Bytes.Reader.of_string ~slice_length:4096 data in
Cdc.fold p (fun n ~off:_ chunk -> n + String.length chunk) 0 r
= String.length data
- : bool = true
Licence #
ISC. See LICENSE.md.