Alternera.Plugin.JsonIndex (alternera_plugin_sdk v0.9.0)

Copy Markdown View Source

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.

Summary

Types

An entry as entries/1 lists it.

t()

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

Functions

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

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

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

Every how many children a checkpoint is kept.

The number of entries.

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.

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

The next piece of the text.

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

Values under this many bytes have no entry.

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

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.

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

The text's size in bytes.

Types

entry()

An entry as entries/1 lists it.

t()

@type t() :: binary()

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

Functions

assemble(arg)

@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(text, opts \\ [])

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

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

checkpoint(index, at, n)

@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(arg)

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

Every how many children a checkpoint is kept.

count(arg)

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

The number of entries.

entries_of(fragment, at, opts \\ [])

@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(index, start)

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

feed(st, piece)

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

The next piece of the text.

finish(st)

@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(arg)

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

Values under this many bytes have no entry.

new(opts \\ [])

@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(index)

@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(arg)

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

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

size(arg)

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

The text's size in bytes.