buid: universally unique lexicographically sortable identifiers
This package provides an implementation of flake ids that are lexicographically-sortable and highly unlikely to collide in a distributed system.
BUIDs can (and are meant to!) be safely stored inside a PostgreSQL UUID field.
1 Spec
A BUID is made up of a 40 bit time component and an 88 bit randomness component. The time component represents the current number of centiseconds since the UNIX timestamp 1586026830000.
BUIDs are represented as 22 character strings where the first 7 characters represent the 0-padded time component encoded in base 62 and the remaining 15 characters represent the 0-padded randomness component encoded in base 62.
The randomness component must have its most significant bit initially set to 0 and all ids generated within the same centisecond must increase monotonically.
In binary, BUIDs are represented as 16 bytes, encoded in network order.
2 Reference
value
procedure
(make-buid-factory) → (-> (and/c buid/c immutable?))
The generator functions are thread-safe.
procedure
(buid) → (-> (and/c buid/c immutable?))
> (require buid) > (for ([_ (in-range 10)]) (displayln (buid)))
0JgAGgZ8n0XjKCtKOD5wj3
0JgAGgZ8n0XjKCtKOD5wj4
0JgAGgZ8n0XjKCtKOD5wj5
0JgAGgZ8n0XjKCtKOD5wj6
0JgAGgZ8n0XjKCtKOD5wj7
0JgAGgZ8n0XjKCtKOD5wj8
0JgAGgZ8n0XjKCtKOD5wj9
0JgAGgZ8n0XjKCtKOD5wjA
0JgAGgZ8n0XjKCtKOD5wjB
0JgAGgZ8n0XjKCtKOD5wjC
procedure
s : buid/c
procedure
s : buid/c
procedure
s : buid/c
procedure
(buid->bytes u) → buid-bytes/c
u : buid/c
procedure
(bytes->buid bs) → buid/c
bs : buid-bytes/c
procedure
(buid->uuid b) → string?
b : buid/c
Added in version 1.2 of package buid-lib.
procedure
(uuid->buid s) → buid/c
s : string?
Added in version 1.2 of package buid-lib.