RaftEx.Domain.Utils (raft_ex v0.3.1)

View Source

General-purpose utilities used across the RaftEx codebase.

Covers type coercions, UID generation, file helpers, parallel execution, and string/path utilities.

Summary

Functions

Prepend item to list.

Derive a safe string from s by keeping only URL-safe base64 characters, truncated to num characters.

Shuffle a list using Fisher-Yates (via random sort keys).

Generate a random UID with no prefix.

Generate a UID with an optional binary prefix.

Apply fun to each element of elements in parallel, collecting results into {:ok, successes, failures}.

Extract the node from a server id, defaulting to node() for bare atoms.

Extract the local registered name from a {name, node} server id.

Return true iff str contains only URL-safe base64 characters.

Zero-pad num to a 16-character uppercase hex string.

Functions

cons(item, list)

@spec cons(term(), list()) :: list()

Prepend item to list.

derive_safe_string(s, num)

@spec derive_safe_string(binary(), pos_integer()) :: binary()

Derive a safe string from s by keeping only URL-safe base64 characters, truncated to num characters.

lists_shuffle(list)

@spec lists_shuffle(list()) :: list()

Shuffle a list using Fisher-Yates (via random sort keys).

make_uid()

@spec make_uid() :: binary()

Generate a random UID with no prefix.

make_uid(prefix0)

@spec make_uid(binary() | atom() | charlist()) :: binary()

Generate a UID with an optional binary prefix.

The prefix is prepended as-is; a random 12-character alphanumeric suffix is appended.

partition_parallel(fun, elements, timeout \\ 60000)

@spec partition_parallel((term() -> boolean()), list(), timeout()) ::
  {:ok, list(), list()} | {:error, term()}

Apply fun to each element of elements in parallel, collecting results into {:ok, successes, failures}.

fun must return true (success) or false (failure). Fails with {:error, {:partition_parallel_timeout, ...}} if timeout ms elapses.

ra_server_id_node(name)

@spec ra_server_id_node(RaftEx.Domain.Types.server_id() | atom()) :: node()

Extract the node from a server id, defaulting to node() for bare atoms.

ra_server_id_to_local_name(name)

@spec ra_server_id_to_local_name(RaftEx.Domain.Types.server_id() | atom()) :: atom()

Extract the local registered name from a {name, node} server id.

to_atom(a)

@spec to_atom(atom() | binary() | list()) :: atom()

to_binary(b)

@spec to_binary(atom() | binary() | integer() | list()) :: binary()

to_list(a)

@spec to_list(atom() | binary() | integer() | list()) :: charlist()

validate_base64uri(str)

@spec validate_base64uri(binary()) :: boolean()

Return true iff str contains only URL-safe base64 characters.

zpad_extract_num(fn_)

@spec zpad_extract_num(binary()) :: non_neg_integer()

zpad_filename(prefix, ext, num)

@spec zpad_filename(binary(), binary(), non_neg_integer()) :: binary()

zpad_filename_incr(fn_)

@spec zpad_filename_incr(Path.t()) :: Path.t() | nil

zpad_hex(num)

@spec zpad_hex(non_neg_integer()) :: binary()

Zero-pad num to a 16-character uppercase hex string.