View Source Electric.Postgres.Lsn (electric v0.9.5)

Encoding, decoding and helper functions for the pg_lsn type.

Summary

Functions

Compare two Lsns and determine if one is less or greater or both are equal.

Decode a binary representation of the LSN into a struct. Reverses encode_bin/1

Convert the non-negative byte offset into Lsn.

Parse the given string as a pg_lsn value.

Add the given byte offset to the Lsn value.

Convert the Lsn into an equivalent byte offset.

Format Lsn to its text representation in an iolist.

Types

@type int32() :: 0..4_294_967_295
@type t() :: %Electric.Postgres.Lsn{offset: int32(), segment: int32()}

Functions

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

Compare two Lsns and determine if one is less or greater or both are equal.

Examples

iex> compare(from_integer(0), from_integer(1))
:lt
iex> compare(from_string("1/0"), from_string("2/0"))
:lt

iex> compare(from_integer(99), from_integer(98))
:gt
iex> compare(from_string("2/0"), from_string("1/0"))
:gt

iex> compare(from_integer(127_000_256), from_string("0/791DEC0"))
:eq
@spec decode_bin(binary()) :: t()

Decode a binary representation of the LSN into a struct. Reverses encode_bin/1

Examples

iex> decode_bin(encode_bin(%Elixir.Electric.Postgres.Lsn{}))
%Elixir.Electric.Postgres.Lsn{}
@spec encode_bin(t()) :: binary()
@spec from_integer(non_neg_integer()) :: t()

Convert the non-negative byte offset into Lsn.

Examples

iex> from_integer(0)
%Elixir.Electric.Postgres.Lsn{segment: 0, offset: 0}

iex> from_integer(1_000_000)
%Elixir.Electric.Postgres.Lsn{segment: 0, offset: 1_000_000}

iex> from_integer(0xFFFFFFFF)
%Elixir.Electric.Postgres.Lsn{segment: 0, offset: 4294967295}

iex> from_integer(0xFFFFFFFFF)
%Elixir.Electric.Postgres.Lsn{segment: 15, offset: 4294967295}

iex> from_integer(-1)
** (FunctionClauseError) no function clause matching in Electric.Postgres.Lsn.from_integer/1
@spec from_string(String.t()) :: t()

Parse the given string as a pg_lsn value.

Examples

iex> from_string("0/0")
%Elixir.Electric.Postgres.Lsn{segment: 0, offset: 0}

iex> from_string("7F/400")
%Elixir.Electric.Postgres.Lsn{segment: 127, offset: 1024}
@spec increment(t(), integer()) :: t()

Add the given byte offset to the Lsn value.

The result is capped at the bottom to not go below #Lsn<0/0>.

Examples

iex> increment(from_integer(0), 8_000_000)
%Elixir.Electric.Postgres.Lsn{segment: 0, offset: 8_000_000}

iex> increment(from_integer(4_000_000_000), 1_000_000_000)
%Elixir.Electric.Postgres.Lsn{segment: 1, offset: 705_032_704}

iex> to_integer(%Elixir.Electric.Postgres.Lsn{segment: 1, offset: 705_032_704})
5_000_000_000

iex> increment(from_integer(4_000_000_000), 10_000_000_000)
%Elixir.Electric.Postgres.Lsn{segment: 3, offset: 1_115_098_112}

iex> to_integer(%Elixir.Electric.Postgres.Lsn{segment: 3, offset: 1_115_098_112})
14_000_000_000

iex> increment(from_integer(14_000_000_000), -8_000_000_000)
%Elixir.Electric.Postgres.Lsn{segment: 1, offset: 1_705_032_704}

iex> increment(from_integer(100), -99)
%Elixir.Electric.Postgres.Lsn{segment: 0, offset: 1}

iex> increment(from_integer(100), -100)
%Elixir.Electric.Postgres.Lsn{segment: 0, offset: 0}

iex> increment(from_integer(100), -101)
%Elixir.Electric.Postgres.Lsn{segment: 0, offset: 0}
@spec to_integer(t()) :: non_neg_integer()

Convert the Lsn into an equivalent byte offset.

Examples

iex> to_integer(%Elixir.Electric.Postgres.Lsn{segment: 0, offset: 0})
0

iex> to_integer(%Elixir.Electric.Postgres.Lsn{segment: 0, offset: 1_000_000})
1_000_000

iex> to_integer(%Elixir.Electric.Postgres.Lsn{segment: 0, offset: 4294967295})
0xFFFFFFFF

iex> to_integer(%Elixir.Electric.Postgres.Lsn{segment: 15, offset: 4294967295})
0xFFFFFFFFF
@spec to_iolist(t()) :: iolist()

Format Lsn to its text representation in an iolist.

Examples

iex> to_iolist(%Elixir.Electric.Postgres.Lsn{})
["0", ?/, "0"]

iex> to_iolist(%Elixir.Electric.Postgres.Lsn{segment: 127, offset: 1024})
["7F", ?/, "400"]