RaftEx.Lib (raft_ex v0.1.0)

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.

Ensure the parent directory of path exists.

Return true iff path exists (any type).

Return true iff path is an existing directory.

Return true iff path is an existing regular file.

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

Create dir and all parent directories; treat :eexist as success.

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.

Recursively delete dir. Raises on failure.

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.

ensure_dir(path)

@spec ensure_dir(Path.t()) :: :ok | {:error, term()}

Ensure the parent directory of path exists.

is_any_file(path)

@spec is_any_file(Path.t()) :: boolean()

Return true iff path exists (any type).

is_dir(path)

@spec is_dir(Path.t()) :: boolean()

Return true iff path is an existing directory.

is_file(path)

@spec is_file(Path.t()) :: boolean()

Return true iff path is an existing regular file.

lists_shuffle(list)

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

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

make_dir(dir)

@spec make_dir(Path.t()) :: :ok | {:error, term()}

Create dir and all parent directories; treat :eexist as success.

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.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.Types.server_id() | atom()) :: atom()

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

recursive_delete(dir)

@spec recursive_delete(Path.t()) :: :ok

Recursively delete dir. Raises on failure.

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.

write_file(name, io_data, sync \\ true)

@spec write_file(Path.t(), iodata(), boolean()) :: :ok | {:error, term()}

Write io_data to name.

When sync is true (default), the file is fsynced after writing to ensure data reaches durable storage.

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.