H.Storage behaviour (H v0.3.0)

Copy Markdown View Source

Pluggable persistence boundary for a single H session.

Every backend (H.Storage.Memory, H.Storage.File, and later H.Storage.SQLite) implements atomic multi-batch commits with per-batch seq revisions and prefix-refusal semantics: if batch k fails a domain check, batches 0..(k - 1) remain durably stored and refused reports {k, reason}; any storage I/O error returns {:error, reason} without advancing committed state.

Summary

Types

check()

@type check() ::
  {:unique_request_id, request_id :: String.t(), fingerprint :: String.t()}
  | {:conversation_exists, conversation_id :: id()}

commit_result()

@type commit_result() :: %{
  stored: non_neg_integer(),
  seqs: [seq()],
  refused: nil | {non_neg_integer(), term()}
}

doc_address()

@type doc_address() ::
  {:session, kind :: String.t()}
  | {:conversation, conversation_id :: id(), kind :: String.t()}
  | {:task, task_id :: id(), kind :: String.t()}

entry_query()

@type entry_query() :: %{
  :conversation_id => id(),
  optional(:cutoff) => nil | id(),
  optional(:order) => :asc | :desc,
  optional(:cursor) => nil | id(),
  optional(:limit) => pos_integer()
}

id()

@type id() :: pos_integer()

seq()

@type seq() :: non_neg_integer()

t()

@type t() :: {module(), state :: term()}

write()

@type write() ::
  {:check, check()}
  | {:put_session_meta, map()}
  | {:put_conversation, map()}
  | {:append_entry, map()}
  | {:put_submission, map()}
  | {:put_task, map()}
  | {:put_doc, doc_address(), map()}
  | {:retire_doc, doc_address()}

Callbacks

close(state)

@callback close(state :: term()) :: :ok

commit(state, batches)

@callback commit(state :: term(), batches :: [[write()]]) ::
  {:ok, commit_result(), state :: term()} | {:error, term()}

get_doc(state, doc_address, opts)

@callback get_doc(state :: term(), doc_address(), opts :: keyword()) ::
  {:ok, nil | map(), state :: term()} | {:error, term()}

get_entry(state, entry_id)

@callback get_entry(state :: term(), entry_id :: id()) ::
  {:ok, nil | map(), state :: term()} | {:error, term()}

get_submission(state, request_id)

@callback get_submission(state :: term(), request_id :: String.t()) ::
  {:ok, nil | map(), state :: term()} | {:error, term()}

list_entries(state)

@callback list_entries(state :: term()) ::
  {:ok, [map()], state :: term()} | {:error, term()}

mint_id(state)

@callback mint_id(state :: term()) :: {:ok, id(), state :: term()} | {:error, term()}

open(keyword)

@callback open(keyword()) :: {:ok, state :: term()} | {:error, term()}

read_bootstrap(state)

@callback read_bootstrap(state :: term()) ::
  {:ok, map(), state :: term()} | {:error, term()}

scan_entries(state, entry_query)

@callback scan_entries(state :: term(), entry_query()) ::
  {:ok, %{items: [map()], next_cursor: nil | id()}, state :: term()}
  | {:error, term()}

Functions

close(arg)

@spec close(t()) :: :ok

commit(arg, batches)

@spec commit(t(), [[write()]]) :: {:ok, commit_result(), t()} | {:error, term()}

get_doc(arg, address, opts \\ [])

@spec get_doc(t(), doc_address(), keyword()) ::
  {:ok, nil | map(), t()} | {:error, term()}

get_entry(arg, entry_id)

@spec get_entry(t(), id()) :: {:ok, nil | map(), t()} | {:error, term()}

get_submission(arg, request_id)

@spec get_submission(t(), String.t()) :: {:ok, nil | map(), t()} | {:error, term()}

list_entries(arg)

@spec list_entries(t()) :: {:ok, [map()], t()} | {:error, term()}

Lists all persisted session entries across conversations in ascending ID order.

mint_id(arg)

@spec mint_id(t()) :: {:ok, id(), t()} | {:error, term()}

open(mod, opts \\ [])

@spec open(module(), keyword()) :: {:ok, t()} | {:error, term()}

read_bootstrap(arg)

@spec read_bootstrap(t()) :: {:ok, map(), t()} | {:error, term()}

scan_entries(arg, query)

@spec scan_entries(t(), entry_query()) ::
  {:ok, %{items: [map()], next_cursor: nil | id()}, t()} | {:error, term()}