dgen_mod_state_codec (DGen v0.3.0)

Copy Markdown View Source

Encoder/decoder for dgen_server mod_state.

Stores structured Erlang terms across FDB key-value pairs using a recursive encoding scheme. Three encoding types are supported:

  • term (fallback) - term_to_binary, chunked into 100KB values.
  • assigns map - map with all atom keys; each entry stored at a separate FDB key using atom_to_binary(Key) in the path.
  • component list - list of maps where every item has an atom id key with a binary value; each item stored separately, ordered by a fractional index embedded in the FDB key.

The encoding is applied recursively. For example, an assigns map whose value is a component list will nest both encodings in the key path.

Key structure

term:  {BaseKey, <<"t">>, ChunkIndex}
map:   {BaseKey, <<"m">>}                       (marker)
       {BaseKey, <<"m">>, KeyBin, ...}           (recursive)
list:  {BaseKey, <<"l">>}                        (marker, holds Id => FracIndex map)
       {BaseKey, <<"l">>, FracIndex, Id, ...}    (recursive)

Summary

Functions

Clears all state keys under BaseKey.

Clears the term-encoded keys under BaseKey.

Reads mod_state from the backend at BaseKey.

Reads a term-encoded value at BaseKey.

Diff-based partial write. Compares OldModState and NewModState and only writes changed keys. Falls back to a full rewrite when the encoding type changes or both values are plain terms.

Returns the packed key for chunk 0 of a term-encoded payload at BaseKey.

Types

base_key()

-type base_key() :: dgen_backend:tuple_key().

mod_state()

-type mod_state() :: term().

option()

-type option() :: {versioned, boolean()}.

tenant()

-type tenant() :: dgen_backend:tenant().

Functions

clear(Tenant, BaseKey)

-spec clear(tenant(), base_key()) -> ok.

Clears all state keys under BaseKey.

clear_term(Tenant, BaseKey)

-spec clear_term(tenant(), base_key()) -> ok.

Clears the term-encoded keys under BaseKey.

Removes all {BaseKey, <<"t">>, N} chunk keys. Used by the call/reply protocol to clear chunked reply payloads.

get(Tenant, BaseKey)

-spec get(tenant(), base_key()) -> {ok, mod_state()} | {error, not_found}.

Reads mod_state from the backend at BaseKey.

get_version/2

-spec get_version(tenant(), base_key()) -> {ok, dgen_backend:versionstamp()} | {error, not_found}.

read_term(Tenant, BaseKey)

-spec read_term(tenant(), base_key()) -> {ok, term()} | {error, not_found}.

Reads a term-encoded value at BaseKey.

Unlike get/2, this only reads the term encoding ({BaseKey, <<"t">>, N} keys) and does not attempt to decode map or list encodings. Used by the call/reply protocol to read chunked reply payloads.

set(Tenant, BaseKey, OldModState, ModState)

-spec set(tenant(), base_key(), mod_state(), mod_state()) -> ok.

set/5

-spec set(tenant(), base_key(), mod_state(), mod_state(), [option()]) -> ok.

Diff-based partial write. Compares OldModState and NewModState and only writes changed keys. Falls back to a full rewrite when the encoding type changes or both values are plain terms.

When OldModState is undefined, existing data is cleared first.

term_first_key(Dir, BaseKey)

-spec term_first_key(dgen_backend:dir(), base_key()) -> dgen_backend:key().

Returns the packed key for chunk 0 of a term-encoded payload at BaseKey.

This is the key that is written first by write_term/3 and is suitable for placing a watch on a term-encoded value.

write_term/3

-spec write_term(tenant(), base_key(), term()) -> ok.