Universally Unique Lexicographically Sortable Identifier

Description

(grifted from https://github.com/ulid/spec)

UUID can be suboptimal for many uses-cases because:

  • It isn’t the most character efficient way of encoding 128 bits of randomness
  • UUID v1/v2 is impractical in many environments, as it requires access to a unique, stable MAC address
  • UUID v3/v5 requires a unique seed and produces randomly distributed IDs, which can cause fragmentation in many data structures
  • UUID v4 provides no other information than randomness which can cause fragmentation in many data structures

Instead, herein is proposed ULID:

ulid() // 01ARZ3NDEKTSV4RRFFQ69G5FAV
  • 128-bit compatibility with UUID
  • 1.21e+24 unique ULIDs per millisecond
  • Lexicographically sortable!
  • Canonically encoded as a 26 character string, as opposed to the 36 character UUID
  • Uses Crockford’s base32 for better efficiency and readability (5 bits per character)
  • Case insensitive
  • No special characters (URL safe)
  • Monotonic sort order (correctly detects and handles the same millisecond)
 01AN4Z07BY      79KA1307SR9X4MV3

|----------|    |----------------|
 Timestamp          Randomness
   48bits             80bits

Components

Timestamp - 48 bit integer - UNIX-time in milliseconds - Won’t run out of space till the year 10889 AD.

Randomness - 80 bits - Cryptographically secure source of randomness, if possible

Sorting

The left-most character must be sorted first, and the right-most character sorted last (lexical order). The default ASCII character set must be used. Within the same millisecond, sort order is not guaranteed.

What’s Inside The Tin

The following functions are implemented:

  • ULIDgenerate / generate / ulid_generate: Generate a time-based ULID
  • ts_generate: Generate ULID from timestamps
  • unmarshal: Unmarshal a ULID into a data frame with timestamp and random bitstring columns

Installation

devtools::install_github("hrbrmstr/ulid")

Usage

library(ulid)

# current verison
packageVersion("ulid")
## [1] '0.1.0'

One

ulid::ULIDgenerate()
## [1] "0001E7E2NTQ177NJ2TDVAA9MF5"

Many

(u <- ulid::ULIDgenerate(20))
##  [1] "0001E7E2NT3MWZ98DWE51NCHWC" "0001E7E2NTQ8P156XE27XSPV3N" "0001E7E2NTAPXZ07GPBZSVMCPT" "0001E7E2NTPS819AZHT4EKCA35"
##  [5] "0001E7E2NTRFHS5NBZ8BKQSQJN" "0001E7E2NTY4T13RTJ4X1MDRKP" "0001E7E2NT46CWCDJ6Q44P7VSJ" "0001E7E2NTS6SHCQ4AJTFQ44VY"
##  [9] "0001E7E2NTS03B5PF90R12TK75" "0001E7E2NTMDQ7YTX5RMJTWAGN" "0001E7E2NTW3SWKXTHAJ7F1HX1" "0001E7E2NTDY8AE8VBJ4MPVFVP"
## [13] "0001E7E2NTADJEBMYFHEDF8EY4" "0001E7E2NT1CEBKN8M1GNA7ZFJ" "0001E7E2NT8NQ87V4HXXZVPQ9Z" "0001E7E2NT6ART8646EJJ22TPZ"
## [17] "0001E7E2NTWSTZS8AB2APQDDDB" "0001E7E2NTDBX1QVF8NKF6JT9X" "0001E7E2NTN2EEWKDNEV15N61D" "0001E7E2NT7DZT6E11XX5WYSG1"

Unmarshal

##                     ts              rnd
## 1  2019-02-27 17:10:02 3MWZ98DWE51NCHWC
## 2  2019-02-27 17:10:02 Q8P156XE27XSPV3N
## 3  2019-02-27 17:10:02 APXZ07GPBZSVMCPT
## 4  2019-02-27 17:10:02 PS819AZHT4EKCA35
## 5  2019-02-27 17:10:02 RFHS5NBZ8BKQSQJN
## 6  2019-02-27 17:10:02 Y4T13RTJ4X1MDRKP
## 7  2019-02-27 17:10:02 46CWCDJ6Q44P7VSJ
## 8  2019-02-27 17:10:02 S6SHCQ4AJTFQ44VY
## 9  2019-02-27 17:10:02 S03B5PF90R12TK75
## 10 2019-02-27 17:10:02 MDQ7YTX5RMJTWAGN
## 11 2019-02-27 17:10:02 W3SWKXTHAJ7F1HX1
## 12 2019-02-27 17:10:02 DY8AE8VBJ4MPVFVP
## 13 2019-02-27 17:10:02 ADJEBMYFHEDF8EY4
## 14 2019-02-27 17:10:02 1CEBKN8M1GNA7ZFJ
## 15 2019-02-27 17:10:02 8NQ87V4HXXZVPQ9Z
## 16 2019-02-27 17:10:02 6ART8646EJJ22TPZ
## 17 2019-02-27 17:10:02 WSTZS8AB2APQDDDB
## 18 2019-02-27 17:10:02 DBX1QVF8NKF6JT9X
## 19 2019-02-27 17:10:02 N2EEWKDNEV15N61D
## 20 2019-02-27 17:10:02 7DZT6E11XX5WYSG1

Use defined timestamps

(ut <- ts_generate(as.POSIXct("2017-11-01 15:00:00", origin="1970-01-01")))
## [1] "0001CZM6DGZJBVZSJ5KS8AXPZS"
##                    ts              rnd
## 1 2017-11-01 15:00:00 ZJBVZSJ5KS8AXPZS

Package Code Metrics

cloc::cloc_pkg_md()
Lang # Files (%) LoC (%) Blank lines (%) # Lines (%)
C/C++ Header 3 0.27 755 0.87 231 0.78 264 0.57
C++ 2 0.18 81 0.09 22 0.07 37 0.08
R 5 0.45 17 0.02 7 0.02 95 0.20
Rmd 1 0.09 10 0.01 37 0.12 69 0.15