sahilpaudel

sahilpaudel

Generate Timebased UUID in elixir

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.

First Post!

Qqwy

Qqwy

TypeCheck Core Team

Which relational-mapping library are you using, if any?

Most Liked

kokolegorille

kokolegorille

ksuid is sortable uuid

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.

Qqwy

Qqwy

TypeCheck Core Team

A cursory look through Xandra’s documentation and source code shows that Xandra does not (yet?) expose any way to generate timeuuid’s in Elixir (but only encoding/decoding values genereated from within Cassandra).

I did find another library called ‘Cassandra’ which has not seen very much usage and has not been updated in a couple of years, but it does contain an internal (undocumented) module called Cassandra.UUID which seems to generate UUIDs in the desired format.

Looking at that module in a bit more detail, it seems like the actual work is offloaded to elixir_uuid (which is stable and widely used), and that Cassandra’s timeuuids are nothing else than ‘v1’ UUIDs.

So using elixir_uuid is probably the best approach :slight_smile:.

Last Post!

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

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement