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
Functions
The index parts/1 took apart (entries sorted by start).
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
@type entry() :: {non_neg_integer(), non_neg_integer(), non_neg_integer(), [non_neg_integer()]}
An entry as entries/1 lists it.
@type t() :: binary()
Functions
@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).
@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).
@spec checkpoint_every(t()) :: pos_integer()
Every how many children a checkpoint is kept.
@spec count(t()) :: non_neg_integer()
The number of entries.
@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.
@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.
@spec feed(Alternera.Plugin.JsonIndex.Scanner.t(), binary()) :: Alternera.Plugin.JsonIndex.Scanner.t()
The next piece of the text.
@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).
@spec min_entry_bytes(t()) :: pos_integer()
Values under this many bytes have no entry.
@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).
@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.
@spec root(t()) :: {non_neg_integer(), non_neg_integer()}
The root value's start and the offset after it.
@spec size(t()) :: non_neg_integer()
The text's size in bytes.