ULID (Universally Unique Lexicographically Sortable Identifier) for Dart

Overview

ulid4d

pub package codecov

Universally Unique Lexicographically Sortable Identifier (ULID) implementation for Dart, with binary implementation and monotonicity support.

Background

UUID can be suboptimal for many use-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:

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

Usage

  • Generate ULID String:
var ulid = ULID.randomULID();
  • Generate ULID instance:
var ulid = ULID.nextULID();
var ulidString = ulid.toString();
  • Generate ULID using ULIDFactory
var factory = ULIDFactory();
var ulid = factory.nextULID();
var ulidString = ulid.toString();

You can use ULIDFactory(Random) to use a different Random instance.

  • Generating ULID from/to bytes
// generate a ULID instance
var ulid = ULID.nextULID();

// generate byte array (Uint8List) of ULID instance
var data = ulid.toBytes();

// generate a ULID from given byte array using 'fromBytes'
var ulidFromBytes = ULID.fromBytes(data);
  • Generate ULID from/to ULID string
// generate a ULID string
var ulidString = ULID.randomULID();

// generate a ULID from given String using 'fromString'
var ulidFromString = ULID.fromString(ulidString);

// generate a ULID String from ULID instance
var ulidStringFromULID = ulid.toString();

Monotonicity

  • Generate monotonic ULID
// generate ULID instance using a monotonic factory
var ulid = ULID.nextMonotonicULID(ulid);

// using a monotonic factory, generate a ULID instance or null in case
// of overflow
var ulidStrict = ULID.nextMonotonicULIDStrict(ulidMono);
  • Generate ULID using ULIDMonocity factory
// generate ULID using ULID monotonic factory
var factory = ULIDMonotonic();
var ulid = factory.nextULID(ulid);
var ulidStrict = factory.nextULIDStrict(ulid);

Specification

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

Components

Timestamp

  • 48 bits
  • UNIX-time in milliseconds
  • Won't run out of space till the year 10889 AD

Entropy

  • 80 bits
  • User defined entropy source.

Encoding

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

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

0                   1                   2                   3
 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
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                      32_bit_uint_time_high                    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|     16_bit_uint_time_low      |       16_bit_uint_random      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       32_bit_uint_random                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       32_bit_uint_random                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

String Representation

 01AN4Z07BY      79KA1307SR9X4MV3
|----------|    |----------------|
 Timestamp           Entropy
  10 chars           16 chars
   48bits             80bits
   base32             base32

Prior Art

License

ulid4d is an open-sourced software licensed under the BSD 3-clause license.

You might also like...

A Dart client for the NATS messaging system. Design to use with Dart and Flutter.

Dart-NATS A Dart client for the NATS messaging system. Design to use with Dart and flutter. Flutter Web Support by WebSocket client.connect(Uri.parse(

Nov 18, 2022

GitHub Action that uses the Dart Package Analyzer to compute the Pub score of Dart/Flutter packages

GitHub Action that uses the Dart Package Analyzer to compute the Pub score of Dart/Flutter packages

Dart/Flutter package analyzer This action uses the pana (Package ANAlysis) package to compute the score that your Dart or Flutter package will have on

Dec 29, 2022

Dart-com-Shelf - Web server básico feito com dart e shelf, configurações de rotas e conexão com mysql.

A server app built using Shelf, configured to enable running with Docker. This sample code handles HTTP GET requests to / and /echo/message Running

Jan 3, 2022

Rock-Paper-Scissor-Game-Using-Dart - This is a repository of Rock Paper Scissor Game which I developed while learning Dart.

Rock-Paper-Scissor-Game-Using-Dart This is a repository of Rock Paper Scissor Game which I developed while learning Dart. The main.dart file consist o

Jan 4, 2022

Flutter Navigation - all types of navigation in flutter run main.tabBar.dart to see tabBar, and run main.dart to see the otheres

pop_up_bar A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if th

Jan 1, 2022

Spider - A small dart library to generate Assets dart code from assets folder.

Spider - A small dart library to generate Assets dart code from assets folder.

Spider A small dart library to generate Assets dart code from assets folder. It generates dart class with static const variables in it which can be us

Nov 8, 2022

Tello-Dart - A testing ground for the Tello drone's socket API in dart

This packages provides a Dart interface to Ryze Tello drones. Getting started Ma

Jul 27, 2022

🎬 A movie catalog app for both Android & IOS ~ Flutter.io project in Dart | Dart, Bloc, Movies

Movie Catalog App 🎬 Browse through movies from the YIFY api Getting Started For help getting started with Flutter, view our online documentation. Tod

Nov 21, 2022

Socketio dart server and client - Full Socket.io implementation using Dart Lang

Socketio dart server and client - Full Socket.io implementation using Dart Lang

Getting Started Step 1: Run dart_server.dart Step 2: Android Emulator has proble

Jan 23, 2022
Comments
  • refact: split declaration and implementation

    refact: split declaration and implementation

    | Q | A | ----------------- | ---------- | Bug fix? | no | New feature? | no | BC breaks? | no | Related Issue | n/a

    opened by Aallam 0
  • fix: js platform behavior

    fix: js platform behavior

    | Q | A | ----------------- | ---------- | Bug fix? | yes | New feature? | no | BC breaks? | yes | Related Issue | n/a

    On JS platform, numbers (and by consequence bitwise operations) behave differently. In this PR with those issues by using fixed size numbers from fixnum.

    More details about numbers in dart: documentation

    opened by Aallam 0
Releases(2.0.0)
Owner
Mouaad Aallam
Software Engineer @Algolia working on Android, Kotlin, Java & Scala. I like learning, and diving into challenges.
Mouaad Aallam
Behruz Hurramov 0 Dec 29, 2021
Arna Framework - A unique set of widgets for building applications with Flutter.

Arna Arna Framework - A unique set of widgets for building applications with Flutter. This Framework is in active development. Any contribution, idea,

Mahan 86 Dec 11, 2022
AhoyHacks Hackathon aims to bring together developers from around the globe to build something unique on the weekend

Pirate Island Are you a budding pirate & want to go on a journey to hunt treasures? Well, what are you waiting for! Register yourself on the Pirate Is

Amartya Yadav 1 May 15, 2022
In this project, we will design a travel app UI with a parallax effect for a unique scroll experience. You will learn how to create your own parallax effect without using external libraries.

Travel App UI In this part, we will design a travel app UI with a parallax effect for a unique scroll experience. You will learn how to create your ow

DebugErrorX 5 Dec 5, 2022
Mysql.dart - MySQL client for Dart written in Dart

Native MySQL client written in Dart for Dart See example directory for examples

null 48 Dec 29, 2022
Docker images for the Dart programming language (https://dart.dev)

dart-docker This is the Git repo of the Docker "Official Images" for the Dart programming language. See the Docker Hub page for a full description on

Dart 49 Dec 14, 2022
A most easily usable Duolingo API wrapper in Dart. Duolingo4D is an open-sourced Dart library.

A most easily usable Duolingo API wrapper in Dart! 1. About Duolingo4D Duolingo4D is an open-sourced Dart library. With Duolingo4D, you can easily int

Kato Shinya 18 Oct 17, 2022
dna, dart native access. A lightweight dart to native super channel plugin

dna, dart native access. A lightweight dart to native super channel plugin, You can use it to invoke any native code directly in contextual and chained dart code.

Assuner 14 Jul 11, 2022
Dart package responsible to provide the basic resources to Lambda Functions with Clean Dart

AWS Lambda Core This package is responsible to provide the basic resources to all services; Usage pubspec.yaml dependencies: aws_lambda_core: <laste

David Araujo 1 Dec 2, 2021