Home

Awesome

ulid

Universally Unique Lexicographically Sortable Identifier implementation for Elixir

<h1 align="center"> <br> <br> <img width="360" src="logo.png" alt="ulid"> <br> <br> <br> </h1>

Gem Downloads GitHub License

Universally Unique Lexicographically Sortable Identifier

UUID can be suboptimal for many uses-cases because:

Instead, herein is proposed ULID:

Installation

If available in Hex, the package can be installed as:

  1. Add ulid to your list of dependencies in mix.exs:
```elixir
def deps do
  [{:ulid, "~> 0.1.0"}]
end
```

2. Ensure ulid is started before your application:

```elixir
def application do
  [applications: [:ulid]]
end
```

Usage

Ulid.generate # 01ARZ3NDEKTSV4RRFFQ69G5FAV

Specification

Below is the current specification of ULID as implemented in this repository. Note: the binary format has not been implemented.

 01AN4Z07BY      79KA1307SR9X4MV3

|----------|    |----------------|
 Timestamp          Randomness
  10 chars           16 chars
   48bits             80bits
   base32             base32

Components

Timestamp

Randomness

Sorting

The left-most character must be sorted first, and the right-most character sorted last. The default ASCII order is used for sorting.

Binary Layout and Byte Order

The components are encoded as 16 octets. Each component is encoded with the Most Significant Byte first (network byte order).

0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                      32_bit_uint_time_high                    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|     16_bit_uint_time_low      |       16_bit_uint_random      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       32_bit_uint_random                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       32_bit_uint_random                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

String Representation

ttttttttttrrrrrrrrrrrrrrrr

where
t is Timestamp
r is Randomness

Test Suite

mix test

Credits and references: