Ferricstore.Store.ValueCodec (ferricstore v0.10.1)

Copy Markdown View Source

Shared helpers for parsing, formatting, and encoding values.

Extracted from Shard and StateMachine to eliminate code duplication (performance audit L2 / memory audit L7). Both modules delegate to this module instead of maintaining identical private copies.

Summary

Functions

Adds numeric operands without allowing float conversion or arithmetic exceptions to escape.

Decodes rate limiter state from its fixed-width 24-byte binary format.

Decodes rate limiter state, using fallback_start_ms for malformed values.

Encodes rate limiter state as a fixed 24-byte binary.

Formats a float for Redis INCRBYFLOAT output: compact decimals, strips trailing zeros and unnecessary decimal point.

Converts an integer or float to a finite BEAM float without raising.

Parses a binary as a float. Accepts integer strings ("10") and float strings ("3.14"). Returns {:ok, float} or :error.

Parses a binary as an integer. Returns {:ok, integer} or :error.

Functions

checked_float_add(left, right)

@spec checked_float_add(term(), term()) :: {:ok, float()} | :error | :overflow

Adds numeric operands without allowing float conversion or arithmetic exceptions to escape.

decode_ratelimit(value)

@spec decode_ratelimit(binary()) :: {integer(), integer(), integer()}

Decodes rate limiter state from its fixed-width 24-byte binary format.

decode_ratelimit(arg1, fallback_start_ms)

@spec decode_ratelimit(binary(), integer()) :: {integer(), integer(), integer()}

Decodes rate limiter state, using fallback_start_ms for malformed values.

Raft apply paths pass their stamped apply time here so corrupted or malformed state is repaired deterministically on every replica.

encode_ratelimit(cur, start, prev)

@spec encode_ratelimit(integer(), integer(), integer()) :: binary()

Encodes rate limiter state as a fixed 24-byte binary.

~10x faster than the old string format ("#{cur}:#{start}:#{prev}"). No allocation, no parsing — just bit packing.

format_float(val)

@spec format_float(float()) :: binary()

Formats a float for Redis INCRBYFLOAT output: compact decimals, strips trailing zeros and unnecessary decimal point.

Uses :binary.match/2 instead of String.contains?/2 and removes the dead-code no-op then that was present in the original duplicated versions.

number_to_float(value)

@spec number_to_float(number()) :: {:ok, float()} | :error

Converts an integer or float to a finite BEAM float without raising.

parse_float(str)

@spec parse_float(binary()) :: {:ok, float()} | :error

Parses a binary as a float. Accepts integer strings ("10") and float strings ("3.14"). Returns {:ok, float} or :error.

parse_integer(str)

@spec parse_integer(binary()) :: {:ok, integer()} | :error

Parses a binary as an integer. Returns {:ok, integer} or :error.