# `Alternera.PluginWasmFramework.UploadedModules`
[🔗](https://github.com/10e-ab/alternera-wasm/blob/v0.9.1/alternera_plugin_wasm_framework/lib/alternera/plugin_wasm_framework/uploaded_modules.ex#L1)

The node's compiled uploaded modules: modules a user uploaded into a
step, as opposed to the shipped languages' (`Modules`, kept for good in
`:persistent_term`). Uploads come and go, so they live in an ETS table
bounded by the size of their files (`:uploaded_cache_bytes` in the
framework's application env, 256 MiB by default), the least recently
used going first when a new one does not fit. A module evicted is
compiled again from storage on its next use.

Compiling is CPU-heavy and has no time limit of its own, so uploaded
modules compile **one at a time per node**, uploads and cache misses
alike, in this server. A caller that finds no entry reads the file
itself (in its own process, which may hold a database connection) and
asks the server to compile it, waiting at most `compile_wait_ms/0`;
past that the call fails with `:not_ready` while the compile finishes
and the entry is kept for the next call.

An entry is keyed by digest only. Who may run it is not this module's
business: a step checks that its tenant stored the file first
(`Language`), on every run.

Each entry keeps the module's declared parameters (`Uses`), read from
the bytes it was compiled from, so what a step enforces is bound to
the digest that runs.

# `entry`

```elixir
@type entry() :: %{
  module: Alternera.PluginWasmFramework.Modules.t(),
  uses: Alternera.PluginWasmFramework.Uses.t(),
  size: non_neg_integer()
}
```

A compiled upload: the module as `Modules` answers it, its declared
parameters and the size of its file.

# `cache_bytes`

```elixir
@spec cache_bytes() :: pos_integer()
```

The cache's size budget, in bytes of module files.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `clear`

```elixir
@spec clear() :: :ok
```

Drops every entry (a test's clean slate).

# `compile`

```elixir
@spec compile(binary(), map(), module()) ::
  {:ok, entry()} | {:error, String.t()} | :not_ready
```

Compiles `wasm` on this node, one compile at a time, and keeps it:
the entry, or why it does not compile. Waits at most `info.wait_ms`,
capped at `compile_wait_ms/0` (`:not_ready` then); a step passes its
time left, so a wait never outlasts the step.

# `compile_wait_ms`

```elixir
@spec compile_wait_ms() :: pos_integer()
```

How long a step waits for its module to compile before it fails.

# `fetch`

```elixir
@spec fetch(String.t(), (-&gt; {:ok, binary()} | {:error, String.t()}), map(), module()) ::
  {:ok, entry()} | {:error, String.t()} | :not_ready
```

The entry for `digest`, compiled from what `read` answers when it is
not on this node. `read` runs in the calling process, only on a miss.
`info` goes to the compile's log line and telemetry (`tenant_id`),
and its `wait_ms` bounds the wait (`compile/3`).

# `lookup`

```elixir
@spec lookup(String.t(), module()) :: {:ok, entry()} | :miss
```

The entry for `digest` when it is compiled on this node for `runtime`,
marked used. An entry another runtime compiled (the runtime was
switched, as a load profile does) is a miss: the compile that follows
replaces it.

# `own`

```elixir
@spec own(String.t(), String.t()) :: :ok
```

Notes that `tenant_id` stored `digest`.

# `owned?`

```elixir
@spec owned?(String.t() | nil, String.t()) :: boolean()
```

Whether this node has seen that `tenant_id` stored `digest`
(`own/2`): a step checks it on every run, since the compiled module
is shared by digest. A node forgets nothing it learned, also when
`Alternera.PluginFileCleanup` removes the stored file.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
