Juggernaut.Cache.Footprint (Juggernaut v0.4.0)

Copy Markdown View Source

Function hashes, and the footprint a cached result rests on (design notes §7.3).

A function's hash is the SHA-256 of its module's context (every module-body expression that is not a function clause, a nested module or a documentation attribute: use, import, alias, attributes, defstruct, ...) and all its clauses in source order, as Sourceror parses them with every node's metadata stripped. So a comment, a blank line or a layout change keeps the hash; an edit in the function, or in what its module gives it (an attribute it reads, an alias it calls through), changes it.

A function the source does not write (__struct__/0, a default-argument arity, a schemata copy) resolves to its module's context hash, and a module the file does not define (or a file that does not parse) to the hash of the whole file: never a hash that could stay the same while the code behind it changed.

A footprint, as recorded in a cache entry, is %{file => %{"sha" => content sha, "functions" => %{key => hash}}}, keys as key/3 makes them. It stands while every file's content is unchanged, or, where the content changed, every recorded function hashes the same.

Summary

Types

Each file of the footprints looked at: its content sha and, if needed, its hashes.

Functions

Whether recorded names every file of functions.

The current state of every file in footprints (recorded footprints): its content sha, and its hashes only when its content differs from a sha some footprint recorded. nil for a file that cannot be read.

The hashes of every function and module of source.

The footprint key of a function: "Elixir.Mod|fun/arity".

The footprint of functions ({file, module, fun, arity}, files relative to root) as the files under root are now. A file that cannot be read is left out, which no later footprint check can confirm, so callers record no entry for it (complete?/2).

The footprint of functions from scans (scan/2 of their files). As in record/2, a file scans holds no hashes for (unreadable, or not scanned) is left out.

The hash of key in a file's hashes: the function's own, else its module's context hash, else "file:" and the file's content hash.

The hashes of each of files (relative to root) as it is now, nil for a file that cannot be read. Each file is parsed once, in parallel, so a run that records many footprints over the same files (recorded/2) parses a file once, not once per footprint.

Whether a recorded footprint stands against current (see the module doc).

Types

current()

@type current() :: %{
  required(String.t()) => %{sha: String.t(), hashes: hashes() | nil} | nil
}

Each file of the footprints looked at: its content sha and, if needed, its hashes.

function_ref()

@type function_ref() :: {file :: String.t(), module(), atom(), arity()}

hashes()

@type hashes() :: %{
  sha: String.t(),
  functions: %{required(String.t()) => String.t()},
  modules: %{required(String.t()) => String.t()}
}

recorded()

@type recorded() :: %{required(String.t()) => %{required(String.t()) => term()}}

Functions

complete?(recorded, functions)

@spec complete?(recorded(), [function_ref()]) :: boolean()

Whether recorded names every file of functions.

current(root, footprints)

@spec current(Path.t(), [recorded()]) :: current()

The current state of every file in footprints (recorded footprints): its content sha, and its hashes only when its content differs from a sha some footprint recorded. nil for a file that cannot be read.

file(source)

@spec file(String.t()) :: hashes()

The hashes of every function and module of source.

key(module, fun, arity)

@spec key(module() | String.t(), atom(), arity()) :: String.t()

The footprint key of a function: "Elixir.Mod|fun/arity".

record(root, functions)

@spec record(Path.t(), [function_ref()]) :: recorded()

The footprint of functions ({file, module, fun, arity}, files relative to root) as the files under root are now. A file that cannot be read is left out, which no later footprint check can confirm, so callers record no entry for it (complete?/2).

recorded(scans, functions)

@spec recorded(%{required(String.t()) => hashes() | nil}, [function_ref()]) ::
  recorded()

The footprint of functions from scans (scan/2 of their files). As in record/2, a file scans holds no hashes for (unreadable, or not scanned) is left out.

resolve(hashes, key)

@spec resolve(hashes(), String.t()) :: String.t()

The hash of key in a file's hashes: the function's own, else its module's context hash, else "file:" and the file's content hash.

scan(root, files)

@spec scan(Path.t(), [String.t()]) :: %{required(String.t()) => hashes() | nil}

The hashes of each of files (relative to root) as it is now, nil for a file that cannot be read. Each file is parsed once, in parallel, so a run that records many footprints over the same files (recorded/2) parses a file once, not once per footprint.

stands?(recorded, current)

@spec stands?(recorded(), current()) :: boolean()

Whether a recorded footprint stands against current (see the module doc).