Project Status: Active – The project has reached a stable, usable state and is being actively developed. Signed by Signed commit %Linux build Status Windows build status Coverage Status cran checks CRAN status Minimal R VersionLicense

Universally Unique Lexicographically Sortable Identifiers

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:

  • ts_generate: Generate ULIDs from timestamps
  • ULIDgenerate: Generate ULID
  • unmarshal: Unmarshal a ULID into a data frame with timestamp and random bitstring columns

Installation

install.packages("ulid", repos = "https://cinc.rud.is")
# or
remotes::install_git("https://git.rud.is/hrbrmstr/ulid.git")
# or
remotes::install_git("https://git.sr.ht/~hrbrmstr/ulid")
# or
remotes::install_gitlab("hrbrmstr/ulid")
# or
remotes::install_bitbucket("hrbrmstr/ulid")
# or
remotes::install_github("hrbrmstr/ulid")

NOTE: To use the ‘remotes’ install options you will need to have the {remotes} package installed.

Usage

library(ulid)

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

One

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

Many

(u <- ulid::ULIDgenerate(20))
##  [1] "0001EKRGGP0RR3N22WVE0SW8GY" "0001EKRGGP5J1RFNVKB3JNWW26" "0001EKRGGP5DC5KWHHXRF4RY29" "0001EKRGGPVVJHQTE2JNSAAR88"
##  [5] "0001EKRGGPVPRMB8RTJGQGY277" "0001EKRGGP8ZDMFHKQTWG6CYJH" "0001EKRGGP3Q1A95E348EMD930" "0001EKRGGPQJDETQQA8QS0WNT1"
##  [9] "0001EKRGGPNJQCZ685S101FD3V" "0001EKRGGPNZCEY28M7QXW1XFE" "0001EKRGGPTEN17Z03Y6P0DP9B" "0001EKRGGP8SWWP2PZ4N7W4B2A"
## [13] "0001EKRGGP9AJSWVQAFKE6ZEBS" "0001EKRGGPQ529FGVZ7DQ8AM3X" "0001EKRGGP5WHD5WRCG6PGFQNZ" "0001EKRGGPJJJ8XMTGRRZT1KF1"
## [17] "0001EKRGGP7EGS3WNG6K97Z1Q4" "0001EKRGGPVMV6Y1Y209DVZAN6" "0001EKRGGPHKWM98Y1DGRNAB1K" "0001EKRGGPQ4YYT1K3PP0HFXWA"

Unmarshal

unmarshal(u)
##                     ts              rnd
## 1  2019-07-27 08:22:46 0RR3N22WVE0SW8GY
## 2  2019-07-27 08:22:46 5J1RFNVKB3JNWW26
## 3  2019-07-27 08:22:46 5DC5KWHHXRF4RY29
## 4  2019-07-27 08:22:46 VVJHQTE2JNSAAR88
## 5  2019-07-27 08:22:46 VPRMB8RTJGQGY277
## 6  2019-07-27 08:22:46 8ZDMFHKQTWG6CYJH
## 7  2019-07-27 08:22:46 3Q1A95E348EMD930
## 8  2019-07-27 08:22:46 QJDETQQA8QS0WNT1
## 9  2019-07-27 08:22:46 NJQCZ685S101FD3V
## 10 2019-07-27 08:22:46 NZCEY28M7QXW1XFE
## 11 2019-07-27 08:22:46 TEN17Z03Y6P0DP9B
## 12 2019-07-27 08:22:46 8SWWP2PZ4N7W4B2A
## 13 2019-07-27 08:22:46 9AJSWVQAFKE6ZEBS
## 14 2019-07-27 08:22:46 Q529FGVZ7DQ8AM3X
## 15 2019-07-27 08:22:46 5WHD5WRCG6PGFQNZ
## 16 2019-07-27 08:22:46 JJJ8XMTGRRZT1KF1
## 17 2019-07-27 08:22:46 7EGS3WNG6K97Z1Q4
## 18 2019-07-27 08:22:46 VMV6Y1Y209DVZAN6
## 19 2019-07-27 08:22:46 HKWM98Y1DGRNAB1K
## 20 2019-07-27 08:22:46 Q4YYT1K3PP0HFXWA

Use defined timestamps

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

unmarshal(ut)
##                    ts              rnd
## 1 2017-11-01 15:00:00 QPFPVC9HKEKJ3K4W

ulid Code Metrics

cloc::cloc_pkg_md()
Lang # Files (%) LoC (%) Blank lines (%) # Lines (%)
C/C++ Header 3 0.27 763 0.86 238 0.71 302 0.53
C++ 2 0.18 87 0.10 22 0.07 37 0.06
Rmd 2 0.18 22 0.02 66 0.20 137 0.24
R 4 0.36 15 0.02 7 0.02 94 0.16

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.