Ferricstore.Store.ValueCodec (ferricstore v0.3.7)

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

Decodes rate limiter state from a 24-byte binary or legacy string format.

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.

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

decode_ratelimit(value)

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

Decodes rate limiter state from a 24-byte binary or legacy string format.

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.

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.