DurableStreams.Offset (Streamkeeper v0.3.0)

View Source

Generates and compares opaque, lexicographically sortable offsets.

Format: 16 hex digits from erlang:unique_integer([:monotonic, :positive]) Example: "0000000000a1b2c3"

Offsets are designed to be:

  • Opaque: Clients should never parse them
  • Lexicographically sortable: String comparison equals temporal order
  • Unique: Guaranteed by erlang:unique_integer
  • Clock-independent: Uses Erlang's monotonic integer, not wall clock

Summary

Functions

Returns true if offset is after reference.

Compares two offsets and returns :lt, :eq, or :gt.

Generates a new unique, lexicographically sortable offset.

Returns the start offset, which represents the beginning of a stream.

Returns true if the given offset is the start offset.

Converts an offset string to its integer representation for use as ETS key.

Returns the zero offset - the earliest possible real offset. Used for SSE when the stream is empty and we need a non-sentinel value.

Types

t()

@type t() :: String.t()

Functions

after?(offset, reference)

@spec after?(t(), t()) :: boolean()

Returns true if offset is after reference.

compare(a, b)

@spec compare(t(), t()) :: :lt | :eq | :gt

Compares two offsets and returns :lt, :eq, or :gt.

The start offset (-1) is always less than any generated offset.

generate()

@spec generate() :: t()

Generates a new unique, lexicographically sortable offset.

Uses erlang:unique_integer([:monotonic, :positive]) for clock-independent monotonic ordering. The integer is formatted as a 16-character zero-padded hexadecimal string for lexicographic sortability.

start()

@spec start() :: t()

Returns the start offset, which represents the beginning of a stream.

start?(offset)

@spec start?(t()) :: boolean()

Returns true if the given offset is the start offset.

to_integer(offset)

@spec to_integer(t()) :: non_neg_integer() | nil

Converts an offset string to its integer representation for use as ETS key.

Returns nil for the start offset (-1) or invalid offsets.

zero()

@spec zero() :: t()

Returns the zero offset - the earliest possible real offset. Used for SSE when the stream is empty and we need a non-sentinel value.