Alternera.Plugin.JsonDoc (alternera_plugin_sdk v0.9.0)

Copy Markdown View Source

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.

t()

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'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.

Types

chunks()

@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.

file()

@type file() :: {:file, Path.t(), non_neg_integer()}

piece()

@type piece() ::
  {non_neg_integer(), :base, non_neg_integer(), pos_integer()}
  | {non_neg_integer(), :bytes, binary()}

source()

@type source() ::
  {:inline, binary()} | file() | {:pieces, file(), chunks(), non_neg_integer()}

t()

@type t() :: %Alternera.Plugin.JsonDoc{
  index: Alternera.Plugin.JsonIndex.t(),
  source: source()
}

verdict()

@type verdict() :: t() | {:not_json, 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

body(doc)

@spec body(t()) :: binary() | file() | t()

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.

bytes(doc, start, length)

@spec bytes(t(), non_neg_integer(), non_neg_integer()) :: binary()

The text's bytes from start, length of them.

children(json_doc, start)

@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.

element(doc, start, i)

@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.

fetch(doc, segments)

@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.

fold_elements(doc, start, acc, fun)

@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.

fold_members(doc, start, acc, fun)

@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.

in_memory(doc, text)

@spec in_memory(verdict(), binary()) :: verdict()

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.

index_bytes(json_doc)

@spec index_bytes(t()) :: non_neg_integer()

The bytes the document's index takes.

kind(doc, start)

@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.

locate(doc, segments)

@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.

merge(doc, ordered_object)

@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.

not_json(body)

@spec not_json(binary() | source()) :: {:not_json, source()}

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.

note_built(kind, doc)

@spec note_built(atom(), verdict()) :: :ok

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.

object?(doc)

@spec object?(t()) :: boolean()

Whether the root value is an object.

on_handle(doc, pairs)

@spec on_handle(verdict(), [{binary(), source()}]) :: verdict()

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.

open(source, opts \\ [])

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

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.

pieces?(arg1)

@spec pieces?(term()) :: boolean()

Whether term is an edited file's document, which only lives within a step (body/1).

put(doc, segments, value)

@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.

put_raw(doc, segments, raw)

@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.

reading(doc, fun)

@spec reading(t(), (-> result)) :: result when result: term()

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.

reads?(doc, body)

@spec reads?(verdict(), binary() | source() | t() | nil) :: boolean()

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.

reduce_array(doc, segments, acc, fun)

@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.

remove(doc, segments)

@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.

rename(doc, from, to)

@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.

size(json_doc)

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

The size of the document's text in bytes.

stream(json_doc)

@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.

take_built()

@spec take_built() :: %{optional(atom()) => verdict()}

The documents noted in this process (note_built/2), forgotten here.

text(json_doc)

@spec text(t()) :: binary()

The document's text, once it is in memory (an {:inline, text} source, or in_memory/2 of a file's).

to_binary(doc)

@spec to_binary(t()) :: binary()

The document's text in memory: text/1, or stream/1 read whole.

written(doc, handle)

@spec written(t(), file()) :: {:ok, t()} | {:error, :size}

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.