sahilpaudel

sahilpaudel

Hey there,

I am using Cassandra and it takes id as of type timebased uuid so I was curious if there is a way or library in Elixir that can generate Timebased UUID for me.

Thanks.

Showing Posts 16 to 7

cjbottaro

cjbottaro

I just ported the Cassandra::Uuid::Generator code from the official Cassandra Ruby gem:
https://github.com/datastax/ruby-driver/blob/master/lib/cassandra/uuid/generator.rb#L157

defmodule TimeUuid do
  use Bitwise, only_operators: true

  @gregorian_offset 122192928000000000

  def new(date_time, jitter \\ nil) do
    jitter = jitter || :rand.uniform(65536)
    clock_id = :rand.uniform(65536)
    node_id = generate_node_id()
    usecs = DateTime.to_unix(date_time, :microsecond) + jitter

    t = @gregorian_offset + usecs * 10
    time_hi  = t &&& 0x0fff000000000000
    time_mid = t &&& 0x0000ffff00000000
    time_low = t &&& 0x00000000ffffffff
    version = 1
    clock_id = clock_id &&& 0x3fff
    node_id = node_id &&& 0xffffffffffff
    variant = 0x8000

    n = (time_low <<< 96) ||| (time_mid <<< 48) ||| (time_hi <<< 16)
    n = n ||| (version <<< 76)
    n = n ||| ((clock_id ||| variant) <<< 48)
    n = n ||| node_id

    <<n::integer-size(128)>>
    |> UUID.binary_to_string!()
  end

  def generate_node_id do
    :math.pow(2, 47)
    |> trunc()
    |> :rand.uniform()
    |> Bitwise.bor(0x010000000000)
  end

end
cjbottaro

cjbottaro

Can you explain how to do this? I just get…

** (ArgumentError) Invalid argument; Expected: <<clock_seq::14>>, <<node::48>>
    (uuid 1.1.8) lib/uuid.ex:250: UUID.uuid1/3

Thanks.

hauleth

hauleth

By format I do not mean hexadecimal representation, but binary representation. Just to make it clear. String 46529a99-5727-4655-b1e9-58ebb8d90c6f is NOT UUID.

jc00ke

jc00ke

Exactly. That was the most annoying part for me: having to convert from one format to another. I didn’t really need ULID anyway, but it was nice to get some experience with it.

hauleth

hauleth

My problem with ULID is that it is only length-compatible with UUID, not format-compatible, so it cannot be added to the UUID specs as a new version to the IETF RFC. That is why I prefer UUIDv6 proposal, which could be handled in PostgreSQL and any other implementation with almost no changes to the code.

jc00ke

jc00ke

I’ll throw ecto_ulid out there too. ULID is a pretty nice ID type (and while I realize this post is about IDs in Cassandra) but its lack of support in Postgres turned into a PITA. I was storing it as a :binary_id since it’s a 128bit integer but querying by the human-readable format was impossible; I had to convert it manually and that got old.

Anyway, here’s a good write-up of the differences between ULID & KSUID. Both are likely great options for your use case.

hauleth

hauleth

For lexicographically sortable UUIDs I prefer UUIDv6.

mpope

mpope

KSUID is great, and monotonically increasing. Couldn’t push for it more.

kokolegorille

kokolegorille

ksuid is sortable uuid

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews