项目作者: lucasschejtman

项目描述 :
Universally Unique Lexicographically Sortable Identifier
高级语言: F#
项目地址: git://github.com/lucasschejtman/FSharp.Ulid.git
创建时间: 2017-08-31T01:41:44Z
项目社区:https://github.com/lucasschejtman/FSharp.Ulid

开源协议:Apache License 2.0

下载


Universally Unique Lexicographically Sortable Identifier

F# .NET Core port of alizain/ulid

Background

A GUID/UUID can be suboptimal for many use-cases because:

  • It isn’t the most character efficient way of encoding 128 bits
  • 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

A ULID however:

  • Is compatible with UUID/GUID’s
  • 1.21e+24 unique ULIDs per millisecond (1,208,925,819,614,629,174,706,176 to be exact)
  • 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)

Usage

  1. open Ulid
  2. // Without specified time
  3. let ulid = Ulid.New
  4. // With specified time
  5. let ulid = Ulid.FromTimestamp(1469918176385L)

Specification

Below is the current specification of ULID as implemented in this repository.

Components

Timestamp

  • 48 bits
  • UNIX-time in milliseconds

Entropy

  • 80 bits

Encoding

Crockford’s Base32 is used as shown.
This alphabet excludes the letters I, L, O, and U to avoid confusion and abuse.

  1. 0123456789ABCDEFGHJKMNPQRSTVWXYZ

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).

  1. 0 1 2 3
  2. 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
  3. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  4. | 32_bit_uint_time_high |
  5. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  6. | 16_bit_uint_time_low | 16_bit_uint_random |
  7. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  8. | 32_bit_uint_random |
  9. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  10. | 32_bit_uint_random |
  11. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

String Representation

  1. 01AN4Z07BY 79KA1307SR9X4MV3
  2. |----------| |----------------|
  3. Timestamp Entropy
  4. 10 chars 16 chars
  5. 48bits 80bits
  6. base32 base32

Test

  1. cd test && dotnet run

Prior Art