All Projects → f4b6a3 → ulid-creator

f4b6a3 / ulid-creator

Licence: MIT License
A Java library for generating Universally Unique Lexicographically Sortable Identifiers (ULID)

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to ulid-creator

tsid-creator
A Java library for generating Time Sortable Identifiers (TSID).
Stars: ✭ 16 (-57.89%)
Mutual labels:  id, ulid, unique-id, identifier, id-generator
Butterfly
分布式ID生成器框架:超高性能的发号器框架。通过引入多种新的方案,彻底解决雪花算法的时间回拨等问题,并将雪花算法原生QPS提高最少十几~二十倍
Stars: ✭ 111 (+192.11%)
Mutual labels:  uuid, unique-id, uuid-generator
python-ulid
ULID implementation for Python
Stars: ✭ 177 (+365.79%)
Mutual labels:  uuid, ulid, unique-id
uuid-generator-plugin
An IntelliJ Idea plugin to generate UUID (Universally Unique Identifier), ULID (Universally Unique Lexicographically Sortable Identifier) and CUID (Collision Resistant Unique Identifier)
Stars: ✭ 30 (-21.05%)
Mutual labels:  uuid, ulid, uuid-generator
sno
Compact, sortable and fast unique IDs with embedded metadata.
Stars: ✭ 77 (+102.63%)
Mutual labels:  uuid, unique-id, id-generator
ulid
Universally Unique Lexicographically Sortable Identifier (ULID) in Crystal
Stars: ✭ 28 (-26.32%)
Mutual labels:  uuid, ulid
fuuid
Functional UUIDs for Python.
Stars: ✭ 145 (+281.58%)
Mutual labels:  uuid, unique-id
uuidgen-el
UUID generation implemented in elisp.
Stars: ✭ 44 (+15.79%)
Mutual labels:  uuid, uuid-generator
ulid
Haskell implementation of ULIDs (Unique Lexicographically Sortable Identifiers)
Stars: ✭ 22 (-42.11%)
Mutual labels:  uuid, ulid
react-native-uuid-generator
UUID generator for React Native utilizing native iOS and Android UUID classes
Stars: ✭ 88 (+131.58%)
Mutual labels:  uuid, uuid-generator
rulid.rs
Rust Universally Unique Lexicographically Sortable Identifier
Stars: ✭ 40 (+5.26%)
Mutual labels:  uuid, ulid
friendly-id
Java Friendly Id for UUID
Stars: ✭ 173 (+355.26%)
Mutual labels:  uuid, unique-id
pure-uuid
Pure JavaScript Based Universally Unique Identifiers (UUID)
Stars: ✭ 60 (+57.89%)
Mutual labels:  uuid, identifier
go-ulid
Universally Unique Lexicographically Sortable Identifier (ULID) in Go - Please use https://github.com/oklog/ulid
Stars: ✭ 31 (-18.42%)
Mutual labels:  uuid, ulid
UUIDNext
A fast and modern .NET library to generate UUID/GUID that are either sequential and database friendly (versions 7), name based (versions 5) or random (version 4).
Stars: ✭ 84 (+121.05%)
Mutual labels:  uuid, guid
Ksuid
K-Sortable Globally Unique IDs
Stars: ✭ 3,202 (+8326.32%)
Mutual labels:  uuid, unique-id
id-mask
IDMask is a Java library for masking internal ids (e.g. from your DB) when they need to be published to hide their actual value and to prevent forging. It has support optional randomisation has a wide support for various Java types including long, UUID and BigInteger. This library bases its security on strong cryptographic primitives.
Stars: ✭ 39 (+2.63%)
Mutual labels:  uuid, id
Javascript
Universally Unique Lexicographically Sortable Identifier
Stars: ✭ 1,781 (+4586.84%)
Mutual labels:  uuid, ulid
Uuid
A PHP library for generating universally unique identifiers (UUIDs).
Stars: ✭ 11,475 (+30097.37%)
Mutual labels:  uuid, guid
php-uuid
RFC 4122 compliant UUID generator and parser for PHP.
Stars: ✭ 13 (-65.79%)
Mutual labels:  uuid, uuid-generator

ULID Creator

This is a Java implementation of Universally Unique Lexicographically Sortable Identifier.

In summary:

  • Sorted by generation time;
  • Can be stored as a UUID/GUID;
  • Can be stored as a string of 26 chars;
  • Can be stored as an array of 16 bytes;
  • String format is encoded to Crockford's base32;
  • String format is URL safe, is case insensitive, and has no hyphens.

This library contains a good amount of unit tests. It also has a micro benchmark for you to check if the performance is good enough.

How to Use

Create a ULID:

Ulid ulid = UlidCreator.getUlid();

Create a Monotonic ULID:

Ulid ulid = UlidCreator.getMonotonicUlid();

Maven dependency

Add these lines to your pom.xml.

<!-- https://search.maven.org/artifact/com.github.f4b6a3/ulid-creator -->
<dependency>
  <groupId>com.github.f4b6a3</groupId>
  <artifactId>ulid-creator</artifactId>
  <version>4.1.2</version>
</dependency>

See more options in maven.org.

Modularity

Module and bundle names are the same as the root package name.

  • JPMS module name: com.github.f4b6a3.ulid
  • OSGi symbolic name: com.github.f4b6a3.ulid

ULID

The ULID is a 128 bit long identifier. The first 48 bits represent the number of milliseconds since Unix Epoch, 1970-01-01. The remaining 80 bits are generated by a secure random number generator. Its canonical string representation is 26 characters long.

// Generate a ULID
Ulid ulid = UlidCreator.getUlid();

Sequence of ULIDs:

01EX8Y21KBH49ZZCA7KSKH6X1C
01EX8Y21KBJTFK0JV5J20QPQNR
01EX8Y21KBG2CS1V6WQCTVM7K6
01EX8Y21KB8HPZNBP3PTW7HVEY
01EX8Y21KB3HZV38VAPTPAG1TY
01EX8Y21KB9FTEJHPAGAKYG9Z8
01EX8Y21KBQGKGH2SVPQAYEFFC
01EX8Y21KBY17J9WR9KQR8SE7H
01EX8Y21KCVHYSJGVK4HBXDMR9 < millisecond changed
01EX8Y21KC668W3PEDEAGDHMVG
01EX8Y21KC53D2S5ADQ2EST327
01EX8Y21KCPQ3TENMTY1S7HV56
01EX8Y21KC3755QF9STQEV05EB
01EX8Y21KC5ZSHK908GMDK69WE
01EX8Y21KCSGJS8S1FVS06B3SX
01EX8Y21KC6ZBWQ0JBV337R1CN
         ^ look

|---------|--------------|
    time      random

Monotonic ULID

The Monotonic ULID is variant of ULID. The random component is incremented by 1 whenever the current millisecond is equal to the previous one. Its main advantage is speed.

// Generate a Monotonic ULID
Ulid ulid = UlidCreator.getMonotonicUlid();

Sequence of Monotonic ULIDs:

01EX8Y7M8MDVX3M3EQG69EEMJW
01EX8Y7M8MDVX3M3EQG69EEMJX
01EX8Y7M8MDVX3M3EQG69EEMJY
01EX8Y7M8MDVX3M3EQG69EEMJZ
01EX8Y7M8MDVX3M3EQG69EEMK0
01EX8Y7M8MDVX3M3EQG69EEMK1
01EX8Y7M8MDVX3M3EQG69EEMK2
01EX8Y7M8MDVX3M3EQG69EEMK3
01EX8Y7M8N1G30CYF2PJR23J2J < millisecond changed
01EX8Y7M8N1G30CYF2PJR23J2K
01EX8Y7M8N1G30CYF2PJR23J2M
01EX8Y7M8N1G30CYF2PJR23J2N
01EX8Y7M8N1G30CYF2PJR23J2P
01EX8Y7M8N1G30CYF2PJR23J2Q
01EX8Y7M8N1G30CYF2PJR23J2R
01EX8Y7M8N1G30CYF2PJR23J2S
         ^ look          ^ look

|---------|--------------|
    time      random

More Examples

Create a ULID from a canonical string (26 chars):

Ulid ulid = Ulid.from("0123456789ABCDEFGHJKMNPQRS");

Convert a ULID into a canonical string in lower case:

String string = ulid.toLowerCase(); // 0123456789abcdefghjkmnpqrs

Convert a ULID into a UUID:

UUID uuid = ulid.toUuid(); // 0110c853-1d09-52d8-d73e-1194e95b5f19

Convert a ULID into a RFC-4122 UUID v4:

UUID uuid = ulid.toRfc4122().toUuid(); // 0110c853-1d09-42d8-973e-1194e95b5f19
                                       //               ^ UUID v4

Get the creation instant of a ULID:

Instant instant = ulid.getInstant(); // 2007-02-16T02:13:14.633Z
// static method
Instant instant = Ulid.getInstant("0123456789ABCDEFGHJKMNPQRS"); // 2007-02-16T02:13:14.633Z

A key generator that makes substitution easy if necessary:

package com.example;

import com.github.f4b6a3.ulid.UlidCreator;

public class KeyGenerator {
    public static String next() {
        return UlidCreator.getUlid().toString();
    }
}
String key = KeyGenerator.next();

A UlidFactory with java.util.Random:

// use a `java.util.Random` instance for fast generation
UlidFactory factory = UlidFactory.newInstance(new Random());
Ulid ulid = factory.create();

// use the factory

A UlidFactory with ThreadLocalRandom inside of a Supplier<byte[]>:

// use a random supplier that returns an array of 10 bytes
UlidFactory factory = UlidFactory.newInstance(() -> {
    final byte[] bytes = new byte[Ulid.RANDOM_BYTES];
    ThreadLocalRandom.current().nextBytes(bytes);
    return bytes;
});

// use the factory
Ulid ulid = factory.create();

Benchmark

This section shows benchmarks comparing UlidCreator to java.util.UUID.

--------------------------------------------------------------------------------
THROUGHPUT (operations/msec)            Mode  Cnt      Score     Error   Units
--------------------------------------------------------------------------------
UUID_randomUUID                        thrpt    5   2060,570 ±  37,242  ops/ms
UUID_randomUUID_toString               thrpt    5   1177,386 ±  39,803  ops/ms
-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
UlidCreator_getUlid                    thrpt    5   2740,609 ±  86,350  ops/ms
UlidCreator_getUlid_toString           thrpt    5   2526,284 ±  56,726  ops/ms
-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
UlidCreator_getMonotonicUlid           thrpt    5  19373,150 ± 192,402  ops/ms
UlidCreator_getMonotonicUlid_toString  thrpt    5  13269,201 ± 254,953  ops/ms
--------------------------------------------------------------------------------
Total time: 00:08:01
--------------------------------------------------------------------------------

System: JVM 8, Ubuntu 20.04, CPU i5-3330, 8G RAM.

To execute the benchmark, run ./benchmark/run.sh.

Other identifier generators

Check out the other ID generators from the same family:

License

This library is Open Source software released under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].