# `Alternera.Plugin.JsonIndex`
[🔗](https://github.com/10e-ab/alternera-sdk/blob/v0.9.0/alternera_plugin_sdk/lib/alternera/plugin/json_index.ex#L1)

A sparse index of a JSON text: where its large values start and end, so
a reader can find a value without decoding the text
(`Plugin.JsonDoc`).

One pass over the text, which may come in pieces cut anywhere (a body
read from its spool file), builds it and validates the text with `:json`
in the same pass, keeping nothing of the values. The index records:

  * the root value's start and end, without the whitespace around it;
  * every value of at least `min_entry_bytes` (4,096 by default): an
    object, an array, a string, a number. A smaller value is found by
    scanning it, which costs less than storing it;
  * for each such object or array, its number of children (an object's
    members) and the offset of every `checkpoint_every`-th child (32 by
    default): child 0, 32, 64, … (a member's offset is its key's).

On 10 MB of JSON records the index is a few kilobytes, and building it
costs about as much as a quarter of a `:json` decode. A value under
`min_entry_bytes` holds only values under it, so a reader that reads
that many bytes from a value's start has the whole value.

The index is one binary, shared between processes without a copy:

    <<"JIX1", min_entry::32, every::32, size::64, root_start::64,
      root_end::64, count::32, entries::binary-size(count * 32),
      checkpoints::binary>>

with each entry `<<start::64, stop::64, children::64, at::32,
checkpoints::32>>`, sorted by `start` (`stop` is the offset after the
value), and `at` the position of its first checkpoint among the 64-bit
offsets that follow the entries.

# `entry`

```elixir
@type entry() ::
  {non_neg_integer(), non_neg_integer(), non_neg_integer(), [non_neg_integer()]}
```

An entry as `entries/1` lists it.

# `t`

```elixir
@type t() :: binary()
```

An index (`build/2`, `finish/1`).

# `assemble`

```elixir
@spec assemble(
  {pos_integer(), pos_integer(), non_neg_integer(),
   {non_neg_integer(), non_neg_integer()}, [entry()]}
) :: t()
```

The index `parts/1` took apart (entries sorted by start).

# `build`

```elixir
@spec build(binary(), keyword()) :: {:ok, t()} | {:error, :not_json}
```

The index of a whole text: `new/1`, one `feed/2`, `finish/1`.

# `checkpoint`

```elixir
@spec checkpoint(t(), non_neg_integer(), non_neg_integer()) :: non_neg_integer()
```

The offset of an entry's `n`-th checkpoint (`entry/2`'s `at`).

# `checkpoint_every`

```elixir
@spec checkpoint_every(t()) :: pos_integer()
```

Every how many children a checkpoint is kept.

# `count`

```elixir
@spec count(t()) :: non_neg_integer()
```

The number of entries.

# `entries_of`

```elixir
@spec entries_of(binary(), non_neg_integer(), keyword()) :: [entry()]
```

The entries of the values in `fragment`, text that holds whole values
but need not be one JSON value (`,"key":{...}`, what an edit inserts,
`Plugin.JsonDoc`), each `{start, stop, children, checkpoints}` with
offsets from `at`, sorted by start. The text is not validated: the
caller made it.

# `entry`

```elixir
@spec entry(t(), non_neg_integer()) ::
  {non_neg_integer(), non_neg_integer(), non_neg_integer(), non_neg_integer()}
  | nil
```

The entry of the value that starts at `start`: `{stop, children, at,
checkpoints}`, or nil for a value too small to have one.

# `feed`

```elixir
@spec feed(Alternera.Plugin.JsonIndex.Scanner.t(), binary()) ::
  Alternera.Plugin.JsonIndex.Scanner.t()
```

The next piece of the text.

# `finish`

```elixir
@spec finish(Alternera.Plugin.JsonIndex.Scanner.t()) ::
  {:ok, t()} | {:error, :not_json}
```

After the last piece: `{:ok, index}`, or `{:error, :not_json}` for a
text `:json` does not accept (an empty one included).

# `min_entry_bytes`

```elixir
@spec min_entry_bytes(t()) :: pos_integer()
```

Values under this many bytes have no entry.

# `new`

```elixir
@spec new(keyword()) :: Alternera.Plugin.JsonIndex.Scanner.t()
```

A scanner before the first byte. Options: `:min_entry_bytes` and
`:checkpoint_every` (tests use small ones to exercise the index on small
texts).

# `parts`

```elixir
@spec parts(t()) ::
  {pos_integer(), pos_integer(), non_neg_integer(),
   {non_neg_integer(), non_neg_integer()}, [entry()]}
```

The index's parts: `{min_entry_bytes, checkpoint_every, size, {root_start,
root_stop}, entries}`, the entries sorted by start, each with its
checkpoints' offsets; `assemble/1` builds an index from them.

# `root`

```elixir
@spec root(t()) :: {non_neg_integer(), non_neg_integer()}
```

The root value's start and the offset after it.

# `size`

```elixir
@spec size(t()) :: non_neg_integer()
```

The text's size in bytes.

---

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