# `Juggernaut.Cache.Shard`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/cache/shard.ex#L1)

The result cache on disk (design notes §7.3): one compact JSON file per mutated source
file, under a directory named for the cache's globals.

    cache/results/g-<16 hex of the globals' sha>/<16 hex of the file's sha>.json

A shard holds the entries of the mutants of one source file, with what entries share
written once and referred to by index:

  * `sources` - `[file, content sha, [[function key, hash], ...]]`: each footprint file
    at one content, with the functions any entry recorded in it. An entry's
    `footprint` is `[[source index, [function index, ...]], ...]`;
  * `test_files` - `[file, hash]`: an entry's `tests` is `[index, ...]`;
  * `tests` - `[id, module, name, file, line]`: a result's `tests` (and `killed_by`) is
    `[[index, duration_ms, outcome], ...]`.

Reading a shard gives back exactly the entries written (`encode/3`, `read/2`). The
single-file cache this replaces held each entry's footprint hashes, file shas and
test references in full, once per entry, and was rewritten whole on every run
(157 MB on a large Phoenix app); a shard is rewritten only when its file's entries
change.

# `dir`

```elixir
@spec dir(Path.t(), String.t()) :: Path.t()
```

The directory of the shards written under `globals`, inside the cache `path`.

# `encode`

```elixir
@spec encode(String.t(), String.t(), %{required(String.t()) =&gt; map()}) :: iodata()
```

The JSON of the shard of source `file`'s `entries` (by mutant key) under `globals`.

# `path`

```elixir
@spec path(Path.t(), String.t()) :: Path.t()
```

The shard of source `file` in `dir`.

# `paths`

```elixir
@spec paths(Path.t(), [String.t()] | :all) :: [Path.t()]
```

The shard paths in `dir` of `files`, or every shard there with `:all`.

# `prune`

```elixir
@spec prune(Path.t(), Path.t()) :: :ok
```

Removes, from the cache `path`, every shard directory but `keep`, and the format 1
file (`path` with `.json`): what other globals wrote can never be read again.

# `read`

```elixir
@spec read(Path.t(), String.t()) :: %{required(String.t()) =&gt; map()}
```

The entries of the shard at `path` (by mutant key), or none when it is missing,
unreadable, of another format or written under other globals.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
