A JSON text read through its index (Plugin.JsonIndex) instead of
decoded whole: a body in memory, or a body spooled to a file
(Plugin.Body), which is then read by position and never loaded.
open/2 scans and validates the text once. fetch/2 then finds the
value at a path, walking the text from the root (an object's members
in order, an array's elements from the nearest checkpoint, a large
value jumped over through its entry) and decodes only that value, as
JsonPath.decode/1 would: the result is the same as JsonPath.fetch/2
on what JsonPath.decode(text) returns, the first of a repeated key
included.
A file is read through one 64 KiB block at a time, in the process that
calls fetch/2 or locate/2: a raw file descriptor serves only the
process that opened it, so each call opens the file and closes it
before it returns (a call inside another reuses the one open). A file
that cannot be read raises File.Error, as Plugin.Body.read/1 does.
An edit (put/3, remove/2, rename/3, merge/2) of a document over
a file does not read the file into memory: the document's source
becomes a piece table over it ({:pieces, file, chunks, size}), the
file's ranges the edit kept and the bytes it inserted, in chunks of at
most 128 pieces each, and its index is the edited text's. Reads and
further edits go through the pieces; an edit rebuilds only the chunks
it touches and moves the starts of those after them. Such
a document lives only within a step: the host writes its text to a file
of its own when the step ends (stream/1), and puts the document on
that file (written/2). body/1 is what a view holds for it.
Summary
Types
A piece table's chunks in order, each its start in the text and its pieces, their starts relative to the chunk's.
What a scan found for a body: its document, or that it is not JSON
(not_json/1), kept so that a later read of the same body does not
scan it again to find that out.
Functions
What a view holds for the document's body: its text when in memory, the file when it is one, and the document itself for an edited file (a piece table), which the host writes out when the step ends.
The text's bytes from start, length of them.
The number of elements of the array, or members of the object, that
starts at start, as its index entry records them; nil for a value
too small to have an entry (under JsonIndex's min_entry_bytes),
whose children a reader counts by walking it.
The {start, length} of element i (from 0) of the array that starts
at start, found from the nearest checkpoint, or :error past its
last element.
The value at segments (JsonPath.segments/1), decoded: {:ok, value},
or :error when a key or an index along the path is not there, as
JsonPath.fetch/2 answers.
fun over the elements of the array that starts at start, in order,
each as its {start, length}: fun.(span, acc). Nothing is read but
the separators; an element is jumped over.
fun over the members of the object that starts at start, in order,
each as its key (decoded) and its value's {start, length}:
fun.(key, span, acc). A repeated key comes once per occurrence. Only
the keys are read; a value is jumped over.
doc over text, the bytes of the file it was built over read into
memory, so that it reads them there instead of from the file. Answers
doc unchanged when text is not that file's size.
The bytes the document's index takes.
What the value that starts at start (a value's offset, as locate/2
gives it) is: :object, :array or :scalar.
Where the value at segments is in the text: {:ok, {start, length}},
or :error as fetch/2.
Merges patch into the root object, as Transform merges: a key whose
value is an object on both sides is merged into, recursively; any other
is set (put/3). The root must be an object.
The verdict that body (a binary or a Plugin.Body handle) is not
JSON, kept in place of a document (verdict/0): reads?/2,
in_memory/2 and on_handle/2 take it as they take a document.
Notes a document built in this process for the body kind
(:request_body or :response_body), for the host to take after the
step (take_built/0) and keep with the body, so that later steps read
through it instead of building it again. The latest for a kind wins. A
scan that found the body is not JSON notes its verdict (not_json/1)
instead, so that no later step scans it again.
Whether the root value is an object.
doc over the handle its text was read from: of pairs, {text, handle} with handle a body handle and text its bytes, the one whose
text doc was built over. Answers doc unchanged when none is.
The document of a text or a body: {:ok, doc}, {:error, :not_json}
for a text :json does not accept. A file is read once, a block at a
time. Options as JsonIndex.new/1.
Whether term is an edited file's document, which only lives within a step (body/1).
Sets the value at segments to value, as JsonPath.put/3 sets it in
the decoded text: a key that is there is replaced where it stands (the
first of a repeated one), a new one is appended to its object, a missing
or null key on the way gets an object, and a scalar in the way, an array
index that is not there or the root ([]) is an error with
JsonPath.put/3's reason. Only the value's bytes change: the rest of the
text keeps its own, whitespace and number spellings included, and the
new value is written compact. A document over a file is edited over it
(the moduledoc): the file is read a block at a time, never whole.
put/3 of JSON text the caller made and vouches for (raw, iodata),
written as it is, unchecked: a value encoded once by its maker, such as
an array built element by element, need not be decoded to be put.
Runs fun (no arguments) with the document's file open, so that the
reads inside it (kind/2, fold_members/4, fold_elements/4,
bytes/3) share one descriptor instead of each opening the file.
Whether doc indexes exactly body (a binary or a Plugin.Body
handle): the same file, or the same bytes in memory. A document is used
for a body only when this holds, so one left over from a body a step
has since replaced is never read for the new one. Equal binaries that
are not the same one are compared byte for byte, which is a small cost
beside the scan it saves.
fun over the elements of the array at segments in order, each as its
text (fun.(bytes, acc)), one at a time: a document over a file reads
them from it a block at a time, never the array whole. {:ok, acc},
:error where nothing is at segments, {:error, :not_array} where the
value there is not an array.
Removes the value at segments: every member of its object with the
last key, or the one array element the last index names. A path that is
not there, or the root, leaves the document as it is.
Moves the value at from to to. Within one object the key is renamed
where it stands, and a member already named as the new key is removed;
anywhere else the value is removed (remove/2) and its bytes set at to
(put/3's rules). {:error, :not_found} when from is not there, and
{:error, "no field"} for the root as either path.
The size of the document's text in bytes.
The document's text as a stream of binaries: the text itself in memory,
or the file's ranges (read in 64 KiB blocks, the file opened by the
process that runs the stream) and the inserted bytes, in order. A file
that cannot be read, or is shorter than its ranges, raises File.Error.
The documents noted in this process (note_built/2), forgotten here.
The document's text, once it is in memory (an {:inline, text} source,
or in_memory/2 of a file's).
The document over the file its edited text was written to (stream/1):
handle, a body handle of the text's size, becomes its source, and the
index, already the text's, stays. A handle of another size is not that
text: {:error, :size}, logged, and the document must not be kept.
Types
@type chunks() :: tuple()
A piece table's chunks in order, each its start in the text and its pieces, their starts relative to the chunk's.
@type file() :: {:file, Path.t(), non_neg_integer()}
@type piece() :: {non_neg_integer(), :base, non_neg_integer(), pos_integer()} | {non_neg_integer(), :bytes, binary()}
@type source() :: {:inline, binary()} | file() | {:pieces, file(), chunks(), non_neg_integer()}
@type t() :: %Alternera.Plugin.JsonDoc{ index: Alternera.Plugin.JsonIndex.t(), source: source() }
What a scan found for a body: its document, or that it is not JSON
(not_json/1), kept so that a later read of the same body does not
scan it again to find that out.
Functions
What a view holds for the document's body: its text when in memory, the file when it is one, and the document itself for an edited file (a piece table), which the host writes out when the step ends.
@spec bytes(t(), non_neg_integer(), non_neg_integer()) :: binary()
The text's bytes from start, length of them.
@spec children(t(), non_neg_integer()) :: non_neg_integer() | nil
The number of elements of the array, or members of the object, that
starts at start, as its index entry records them; nil for a value
too small to have an entry (under JsonIndex's min_entry_bytes),
whose children a reader counts by walking it.
@spec element(t(), non_neg_integer(), non_neg_integer()) :: {:ok, {non_neg_integer(), non_neg_integer()}} | :error
The {start, length} of element i (from 0) of the array that starts
at start, found from the nearest checkpoint, or :error past its
last element.
@spec fetch(t(), Alternera.Plugin.JsonPath.segments()) :: {:ok, term()} | :error
The value at segments (JsonPath.segments/1), decoded: {:ok, value},
or :error when a key or an index along the path is not there, as
JsonPath.fetch/2 answers.
@spec fold_elements(t(), non_neg_integer(), acc, (span, acc -> acc)) :: acc when acc: term(), span: {non_neg_integer(), non_neg_integer()}
fun over the elements of the array that starts at start, in order,
each as its {start, length}: fun.(span, acc). Nothing is read but
the separators; an element is jumped over.
@spec fold_members(t(), non_neg_integer(), acc, (binary(), span, acc -> acc)) :: acc when acc: term(), span: {non_neg_integer(), non_neg_integer()}
fun over the members of the object that starts at start, in order,
each as its key (decoded) and its value's {start, length}:
fun.(key, span, acc). A repeated key comes once per occurrence. Only
the keys are read; a value is jumped over.
doc over text, the bytes of the file it was built over read into
memory, so that it reads them there instead of from the file. Answers
doc unchanged when text is not that file's size.
@spec index_bytes(t()) :: non_neg_integer()
The bytes the document's index takes.
@spec kind(t(), non_neg_integer()) :: :object | :array | :scalar
What the value that starts at start (a value's offset, as locate/2
gives it) is: :object, :array or :scalar.
@spec locate(t(), Alternera.Plugin.JsonPath.segments()) :: {:ok, {non_neg_integer(), non_neg_integer()}} | :error
Where the value at segments is in the text: {:ok, {start, length}},
or :error as fetch/2.
@spec merge(t(), Jason.OrderedObject.t()) :: {:ok, t()} | {:error, String.t()}
Merges patch into the root object, as Transform merges: a key whose
value is an object on both sides is merged into, recursively; any other
is set (put/3). The root must be an object.
The verdict that body (a binary or a Plugin.Body handle) is not
JSON, kept in place of a document (verdict/0): reads?/2,
in_memory/2 and on_handle/2 take it as they take a document.
Notes a document built in this process for the body kind
(:request_body or :response_body), for the host to take after the
step (take_built/0) and keep with the body, so that later steps read
through it instead of building it again. The latest for a kind wins. A
scan that found the body is not JSON notes its verdict (not_json/1)
instead, so that no later step scans it again.
Whether the root value is an object.
doc over the handle its text was read from: of pairs, {text, handle} with handle a body handle and text its bytes, the one whose
text doc was built over. Answers doc unchanged when none is.
The document of a text or a body: {:ok, doc}, {:error, :not_json}
for a text :json does not accept. A file is read once, a block at a
time. Options as JsonIndex.new/1.
Whether term is an edited file's document, which only lives within a step (body/1).
@spec put(t(), Alternera.Plugin.JsonPath.segments(), term()) :: {:ok, t()} | {:error, String.t()}
Sets the value at segments to value, as JsonPath.put/3 sets it in
the decoded text: a key that is there is replaced where it stands (the
first of a repeated one), a new one is appended to its object, a missing
or null key on the way gets an object, and a scalar in the way, an array
index that is not there or the root ([]) is an error with
JsonPath.put/3's reason. Only the value's bytes change: the rest of the
text keeps its own, whitespace and number spellings included, and the
new value is written compact. A document over a file is edited over it
(the moduledoc): the file is read a block at a time, never whole.
@spec put_raw(t(), Alternera.Plugin.JsonPath.segments(), iodata()) :: {:ok, t()} | {:error, String.t()}
put/3 of JSON text the caller made and vouches for (raw, iodata),
written as it is, unchecked: a value encoded once by its maker, such as
an array built element by element, need not be decoded to be put.
Runs fun (no arguments) with the document's file open, so that the
reads inside it (kind/2, fold_members/4, fold_elements/4,
bytes/3) share one descriptor instead of each opening the file.
Whether doc indexes exactly body (a binary or a Plugin.Body
handle): the same file, or the same bytes in memory. A document is used
for a body only when this holds, so one left over from a body a step
has since replaced is never read for the new one. Equal binaries that
are not the same one are compared byte for byte, which is a small cost
beside the scan it saves.
@spec reduce_array(t(), Alternera.Plugin.JsonPath.segments(), acc, (binary(), acc -> acc)) :: {:ok, acc} | :error | {:error, :not_array} when acc: term()
fun over the elements of the array at segments in order, each as its
text (fun.(bytes, acc)), one at a time: a document over a file reads
them from it a block at a time, never the array whole. {:ok, acc},
:error where nothing is at segments, {:error, :not_array} where the
value there is not an array.
@spec remove(t(), Alternera.Plugin.JsonPath.segments()) :: {:ok, t()}
Removes the value at segments: every member of its object with the
last key, or the one array element the last index names. A path that is
not there, or the root, leaves the document as it is.
@spec rename( t(), Alternera.Plugin.JsonPath.segments(), Alternera.Plugin.JsonPath.segments() ) :: {:ok, t()} | {:error, :not_found | String.t()}
Moves the value at from to to. Within one object the key is renamed
where it stands, and a member already named as the new key is removed;
anywhere else the value is removed (remove/2) and its bytes set at to
(put/3's rules). {:error, :not_found} when from is not there, and
{:error, "no field"} for the root as either path.
@spec size(t()) :: non_neg_integer()
The size of the document's text in bytes.
@spec stream(t()) :: Enumerable.t()
The document's text as a stream of binaries: the text itself in memory,
or the file's ranges (read in 64 KiB blocks, the file opened by the
process that runs the stream) and the inserted bytes, in order. A file
that cannot be read, or is shorter than its ranges, raises File.Error.
The documents noted in this process (note_built/2), forgotten here.
The document's text, once it is in memory (an {:inline, text} source,
or in_memory/2 of a file's).
The document's text in memory: text/1, or stream/1 read whole.
The document over the file its edited text was written to (stream/1):
handle, a body handle of the text's size, becomes its source, and the
index, already the text's, stays. A handle of another size is not that
text: {:error, :size}, logged, and the document must not be kept.