2023-07-27 21:00:26 +00:00
|
|
|
# Bottom line up front
|
|
|
|
Julids are globally unique, sortable identifiers, that are backwards-compatible with
|
|
|
|
[ULIDs](https://github.com/ulid/spec). This crate provides a Rust Julid datatype, as well as a
|
|
|
|
loadable extension for SQLite for creating and querying them:
|
2023-07-26 00:51:49 +00:00
|
|
|
|
|
|
|
``` text
|
|
|
|
$ sqlite3
|
|
|
|
SQLite version 3.40.1 2022-12-28 14:03:47
|
|
|
|
Enter ".help" for usage hints.
|
|
|
|
Connected to a transient in-memory database.
|
|
|
|
Use ".open FILENAME" to reopen on a persistent database.
|
|
|
|
sqlite> .load ./libjulid
|
|
|
|
sqlite> select hex(julid_new());
|
2023-07-27 21:00:26 +00:00
|
|
|
018998768ACF000060B31DB175E0C5F9
|
2023-07-26 00:51:49 +00:00
|
|
|
sqlite> select julid_string(julid_new());
|
2023-07-27 21:00:26 +00:00
|
|
|
01H6C7D9CT00009TF3EXXJHX4Y
|
|
|
|
sqlite> select julid_seconds(julid_new());
|
|
|
|
1690480066.208
|
|
|
|
sqlite> select datetime(julid_timestamp(julid_new()), 'auto');
|
|
|
|
2023-07-27 17:47:50
|
2023-07-26 00:51:49 +00:00
|
|
|
sqlite> select julid_counter(julid_new());
|
|
|
|
0
|
|
|
|
```
|
|
|
|
|
2023-07-27 21:25:06 +00:00
|
|
|
Crates.io: https://crates.io/crates/julid-rs
|
|
|
|
docs: https://docs.rs/julid-rs/latest/julid/
|
|
|
|
|
|
|
|
|
2023-07-27 21:00:26 +00:00
|
|
|
## A slightly deeper look
|
2023-07-26 00:51:49 +00:00
|
|
|
|
2023-07-27 21:00:26 +00:00
|
|
|
Julids are ULID-backwards-compatible (that is, all Julids are valid ULIDs, but not all ULIDs are
|
|
|
|
Julids) identifiers with the following properties:
|
2023-07-26 00:51:49 +00:00
|
|
|
|
|
|
|
* they are 128-bits long
|
|
|
|
* they are lexicographically sortable
|
2023-07-27 21:00:26 +00:00
|
|
|
* they encode their creation time as the number of milliseconds since the [UNIX
|
|
|
|
epoch](https://en.wikipedia.org/wiki/Unix_time)
|
2023-07-26 00:51:49 +00:00
|
|
|
* IDs created within the same millisecond will still sort in their order of creation, due to the
|
|
|
|
presence of a 16-bit monotonic counter, placed immediately after the creation time bits
|
|
|
|
|
2023-07-27 21:00:26 +00:00
|
|
|
It's that last thing that makes them distinctive. ULIDs have the following structure, from most to
|
|
|
|
least-significant bit:
|
2023-07-26 00:51:49 +00:00
|
|
|
|
2023-07-26 00:54:20 +00:00
|
|
|
![ULID bit structure](./ulid.svg)
|
2023-07-26 00:51:49 +00:00
|
|
|
|
|
|
|
According to the ULID spec, for ULIDs created in the same millisecond, the least-significant bit
|
2023-07-27 21:00:26 +00:00
|
|
|
should be incremented for each new ID. Since that portion of the ULID is random, that means you may
|
2023-07-26 00:51:49 +00:00
|
|
|
not be able to increment it without spilling into the timestamp portion. Likewise, it's easy to
|
|
|
|
guess a new possibly-valid ULID simply by incrementing an already-known one. And finally, this means
|
|
|
|
that sorting will need to read all the way to the end of the ULID for IDs created in the same
|
|
|
|
millisecond.
|
|
|
|
|
2023-07-27 21:00:26 +00:00
|
|
|
To address these shortcomings, Julids (Joe's ULIDs) have the following structure:
|
2023-07-26 00:51:49 +00:00
|
|
|
|
2023-07-26 00:55:06 +00:00
|
|
|
![Julid bit structure](./julid.svg)
|
2023-07-26 00:51:49 +00:00
|
|
|
|
|
|
|
As with ULIDs, the 48 most-significant bits encode the time of creation. Unlike ULIDs, the next 16
|
2023-07-27 21:00:26 +00:00
|
|
|
most-significant bits are not random, they're a monotonic counter for IDs created within the same
|
|
|
|
millisecond. Since it's only 16 bits, it will saturate after 65,536 IDs intra-millisecond creations,
|
|
|
|
after which, IDs in that same millisecond will not have an intrinsic total order (the random bits
|
|
|
|
will still be different, so you shouldn't have collisions). My PC, which is no slouch, can only
|
|
|
|
generate about 20,000 per millisecond, so hopefully this is not an issue! Because the random bits
|
|
|
|
are always fresh, it's not possible to easily guess a valid Julid if you already know one.
|
2023-07-26 00:51:49 +00:00
|
|
|
|
2023-07-27 21:00:26 +00:00
|
|
|
# SQLite extension
|
2023-07-26 00:51:49 +00:00
|
|
|
|
2023-07-27 21:00:26 +00:00
|
|
|
The extension, when loaded into SQLite, provides the following functions:
|
2023-07-26 00:51:49 +00:00
|
|
|
|
|
|
|
* `julid_new()`: create a new Julid and return it as a `blob`
|
2023-07-27 21:13:42 +00:00
|
|
|
* `julid_seconds(julid)`: get the number seconds (as a 64-bit float) since the UNIX epoch that this
|
|
|
|
julid was created
|
2023-07-26 00:51:49 +00:00
|
|
|
* `julid_counter(julid)`: show the value of this julid's monotonic counter
|
|
|
|
* `julid_sortable(julid)`: return the 64-bit concatenation of the timestamp and counter
|
|
|
|
* `julid_string(julid)`: show the [base-32 Crockford](https://en.wikipedia.org/wiki/Base32)
|
|
|
|
encoding of this julid
|
|
|
|
|
2023-07-27 21:00:26 +00:00
|
|
|
## Building and loading
|
2023-07-26 00:51:49 +00:00
|
|
|
|
2023-07-27 01:39:00 +00:00
|
|
|
If you want to use it as a SQLite extension:
|
|
|
|
|
2023-07-27 21:00:26 +00:00
|
|
|
* clone the [repo](https://gitlab.com/nebkor/julid)
|
|
|
|
* build it with `cargo build --features plugin` (this builds the SQLite extension)
|
2023-07-26 00:51:49 +00:00
|
|
|
* copy the resulting `libjulid.[so|dylib|whatevs]` to some place where you can...
|
|
|
|
* load it into SQLite with `.load /path/to/libjulid` as shown at the top
|
|
|
|
* party
|
|
|
|
|
|
|
|
If you, like me, wish to use Julids as primary keys, just create your table like:
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
create table users (
|
2023-07-27 21:00:26 +00:00
|
|
|
id blob not null primary key default (julid_new()),
|
2023-07-26 00:51:49 +00:00
|
|
|
...
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
and you've got a first-class ticket straight to Julid City, baby!
|
|
|
|
|
2023-07-27 21:00:26 +00:00
|
|
|
# Rust crate
|
2023-07-26 00:51:49 +00:00
|
|
|
|
|
|
|
Of course, you can also use it outside of a database; the `Julid` type is publicly exported, and
|
|
|
|
you can do like such as:
|
|
|
|
|
|
|
|
``` rust
|
|
|
|
use julid::Julid;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let id = Julid::new();
|
|
|
|
dbg!(id.timestamp(), id.counter(), id.sortable(), id.as_string());
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-07-28 14:11:56 +00:00
|
|
|
after adding it to your project's dependencies (eg, `cargo add julid-rs`; note the package name is
|
2023-07-27 21:00:26 +00:00
|
|
|
"julid-rs", but the library name as used in your `use` statements is just "julid"). By default, it
|
|
|
|
will also include trait implementations for using Julids with
|
|
|
|
[SQLx](https://github.com/launchbadge/sqlx), and serializing/deserializing with
|
|
|
|
[Serde](https://serde.rs/), via the `sqlx` and `serde` features, respectively. One final default
|
2023-07-28 14:11:56 +00:00
|
|
|
optional feature, `chrono`, uses the Chrono crate to return the timestamp as a
|
|
|
|
[`DateTime`](https://docs.rs/chrono/latest/chrono/struct.DateTime.html) by adding a `created_at(&self)`
|
|
|
|
method to `Julid`.
|
2023-07-27 21:13:42 +00:00
|
|
|
|
|
|
|
# Thanks
|
|
|
|
|
|
|
|
This crate wouldn't have been possible without a lot of inspiration (and a little shameless
|
|
|
|
stealing) from the [ulid-rs](https://github.com/dylanhart/ulid-rs) crate, as well as the
|
|
|
|
[sqlite-loadable-rs](https://github.com/asg017/sqlite-loadable-rs) crate, which made it *extremely*
|
|
|
|
easy to write the SQLite extension. Thank you, authors of those crates! Feel free to steal from this
|
2023-07-28 14:11:56 +00:00
|
|
|
project!
|