Bedrock.Internal.Atomics (bedrock v0.5.2)

View Source

Centralized atomic operations for variable-length little-endian binary values.

This module implements atomic operations that mirror FoundationDB's semantics, operating on variable-length binary values interpreted as little-endian integers. All operations handle missing values (empty binaries) according to their specific semantics.

Value Interpretation

  • Empty binary (<<>>) represents missing/unset values
  • All other binaries are interpreted as little-endian unsigned integers
  • Operations automatically handle size differences through padding/truncation

Operation Semantics

  • Addition: Little-endian carry propagation, missing = 0
  • Bitwise operations: Byte-by-byte operations with appropriate defaults
  • Min/Max: Little-endian integer comparison with V2 semantics
  • Byte min/max: Lexicographic comparison
  • Append: Concatenation with size limit
  • Compare and clear: Clear if values match

Summary

Functions

Addition with little-endian carry propagation

Append if fits within size limit

Apply an atomic operation to an existing value with an operand.

Bitwise AND - V2 version (missing existing = return operand)

Bitwise OR operation

Bitwise XOR operation

Byte-wise maximum (lexicographic comparison)

Byte-wise minimum (lexicographic comparison)

Compare and clear - clear if values match

Maximum operation (little-endian integer comparison)

Minimum operation - V2 version (missing existing = return operand)

Functions

add(ex, op)

Addition with little-endian carry propagation

append_if_fits(ex, op)

@spec append_if_fits(Bedrock.value(), Bedrock.value()) :: Bedrock.value()

Append if fits within size limit

apply_operation(atom, existing_value, operand)

@spec apply_operation(atom(), binary(), binary()) :: binary() | nil

Apply an atomic operation to an existing value with an operand.

Returns the result of the operation, or nil for compare_and_clear when cleared.

Examples

iex> Atomics.apply_operation(:add, <<5>>, <<3>>)
<<8>>

iex> Atomics.apply_operation(:min, <<10>>, <<5>>)
<<5>>

iex> Atomics.apply_operation(:compare_and_clear, <<5>>, <<5>>)
nil

bit_and(ex, op)

@spec bit_and(Bedrock.value(), Bedrock.value()) :: Bedrock.value()

Bitwise AND - V2 version (missing existing = return operand)

bit_or(ex, op)

@spec bit_or(Bedrock.value(), Bedrock.value()) :: Bedrock.value()

Bitwise OR operation

bit_xor(ex, op)

@spec bit_xor(Bedrock.value(), Bedrock.value()) :: Bedrock.value()

Bitwise XOR operation

byte_max(ex, op)

@spec byte_max(Bedrock.value(), Bedrock.value()) :: Bedrock.value()

Byte-wise maximum (lexicographic comparison)

byte_min(ex, op)

@spec byte_min(Bedrock.value(), Bedrock.value()) :: Bedrock.value()

Byte-wise minimum (lexicographic comparison)

compare_and_clear(ex, op)

@spec compare_and_clear(Bedrock.value(), Bedrock.value()) :: Bedrock.value()

Compare and clear - clear if values match

max(ex, op)

Maximum operation (little-endian integer comparison)

min(ex, op)

Minimum operation - V2 version (missing existing = return operand)