Skip to content

neliva/Neliva.UidProvider

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Neliva.UidProvider

This repository provides functionality for generating unique across space and time identifiers. The specification and the reference implementation are released into the public domain. See the UNLICENSE file.

main dotnet 6.0 Nuget (with prereleases)

Overview

The UidProvider generates variable size IDs ranging from 16 bytes to 32 bytes. An ID consists of:

  • A 6-byte timestamp, representing the ID's creation, measured in milliseconds since the Unix epoch.
  • A 6-byte random value generated once per provider instance.
  • A 4-byte incrementing counter, initialized to a random value per provider instance.
  • A random value if the ID length is greater than 16 bytes.

For the timestamp and counter values, the most significant bytes appear first in the byte sequence (big-endian). The IDs are lexicographically sortable when encoded in hex or base32hex format.

The byte format of the ID is the following:

+-------------+--------+-----------+----------+
|  Timestamp  |  Node  |  Counter  |  Random  |
+-------------+--------+-----------+----------+
|  6          |  6     |  4        |  0 - 16  |
+-------------+--------+-----------+----------+ 

Usage

// using Neliva;

var data = new byte[16]; // min ID size
var provider = new UidProvider(/* node span, timestamp callback, RNG callback */);

provider.Fill(data);

// Using the default provider

Span<byte> dataSpan = stackalloc byte[32]; // max ID size

UidProvider.Default.Fill(dataSpan);

About

Generate unique across space and time identifiers.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages