ExKsuid v0.2.1 ExKsuid View Source

Module for KSUID (K-Sortable ID)

KSUID is a identifier which consist 4 byte of timestamp data + 16 byte of random data. usually represented in base62 string, which can be lexicographically sorted for rough time ordering

KSUID in raw form has 20 byte of data, 27 byte in base62 format

Link to this section Summary

Types

Option for generating KSUID timestamp is epoch timestamp in second random_fn is custom random function used, need to return 16 byte of random data

timestamp epoch in second random_bytes random payload carried by ksuid

Functions

Epoch used as timestamp lower limit at KSUID, 1400000000

Generate a new KSUID in Base62 form, there is option to generate ksuid from existing timestamp

Generate a new KSUID in raw form, there is option to generate ksuid from existing timestamp

Parse a base62 Ksuid to it's timestamp and random payload

Parse a raw Ksuid to it's timestamp and random payload

Link to this section Types

Specs

generate_opt() :: [timestamp: integer(), random_fn: (() -> binary())]

Option for generating KSUID timestamp is epoch timestamp in second random_fn is custom random function used, need to return 16 byte of random data

Specs

parse_result() :: %{timestamp: pos_integer(), random_bytes: random_payload()}

timestamp epoch in second random_bytes random payload carried by ksuid

Specs

random_payload() :: <<_::128>>

Specs

raw_ksuid() :: <<_::160>>

Link to this section Functions

Epoch used as timestamp lower limit at KSUID, 1400000000

Specs

generate(generate_opt()) :: ExKsuid.Base62.t()

Generate a new KSUID in Base62 form, there is option to generate ksuid from existing timestamp

Examples

iex> ExKsuid.generate([timestamp: 1507611700, random_fn: fn -> Base.decode16!("9850EEEC191BF4FF26F99315CE43B0C8") end])
"0uk1Hbc9dQ9pxyTqJ93IUrfhdGq"

Specs

generate_raw(generate_opt()) :: raw_ksuid()

Generate a new KSUID in raw form, there is option to generate ksuid from existing timestamp

Examples

iex> ExKsuid.generate_raw([timestamp: 1507611700, random_fn: fn -> Base.decode16!("9850EEEC191BF4FF26F99315CE43B0C8") end])
<<107611700::32>> <> Base.decode16!("9850EEEC191BF4FF26F99315CE43B0C8")

Specs

Parse a base62 Ksuid to it's timestamp and random payload

Examples

iex> ExKsuid.parse("0uk1Hbc9dQ9pxyTqJ93IUrfhdGq")
%{timestamp: 1507611700, random_bytes: Base.decode16!("9850EEEC191BF4FF26F99315CE43B0C8")}

Specs

parse_raw(raw_ksuid()) :: parse_result()

Parse a raw Ksuid to it's timestamp and random payload

Examples

iex> ExKsuid.parse_raw(<<107611700::32>> <> Base.decode16!("9850EEEC191BF4FF26F99315CE43B0C8")) %{timestamp: 1507611700, random_bytes: Base.decode16!("9850EEEC191BF4FF26F99315CE43B0C8")}