riptide v0.4.6 Riptide.UUID View Source

This module allows you to generate sortable Time UUIDs that can be used to control sorting order in Riptide stores. They have a time component that defaults to creation time as well as a random component.

Usage

Take the following data:

{
  "todo:info": {
    "0S1eOQ0kd0wnuUSWbDAu" => %{
      "text" => "My first todo!"
    }
  }
}

This entry uses an ascending UUID. This means if we create a new entry with an ascending UUID it will be inserted below the existing entry:

{
  "todo:info": {
    "0S1eOQ0kd0wnuUSWbDAu" => %{
      "text" => "My first todo!"
    },
    "0S1eP9y9x61XmeVoHzVM" => %{
      "text" => "My second todo!"
    }
  }
}

Now let's try it with descending UUIDs:

{
  "todo:info": {
    "zXyLafLmMB6Uac8NTOvA" => %{
      "text" => "My first todo!"
    }
  }
}

Adding another todo will insert it above the existing entry:

{
  "todo:info": {
    "zXyLaUMUDcB4zaVNCLM2" => %{
      "text" => "My second todo!"
    },
    "zXyLafLmMB6Uac8NTOvA" => %{
      "text" => "My first todo!"
    }
  }
}

The type of UUID that should be used depends on how your application is planning on using the data. For example, if your application typically loads the 10 newest created todos it makes sense to use descending UUIDs with the following query:

Riptide.query_path ["todo:info"], %{limit: 10}

Explore the functions in this module to see the additional options there are for creating UUIDs.

Link to this section Summary

Functions

Generate a UUID that can be sorted lexicographically from oldest to newest. The time component is set to the current time.

Like ascending/1 with the ability to specify time component.

For a given ascending UUID, extract the time component in milliseconds.

Like ascending/1 with the ability to specify time component.

Generate a UUID that can be sorted lexicographically from newest to oldest. The time component is set to the current time.

Like descending/0 with the ability to specify time component in milliseconds.

For a given descending UUID, extract the time component in milliseconds.

Like descending/1 with the random part zero filled. Useful for specifying ranges of UUIDs in queries.

Link to this section Functions

Generate a UUID that can be sorted lexicographically from oldest to newest. The time component is set to the current time.

Like ascending/1 with the ability to specify time component.

Link to this function

ascending_decode_time(key)

View Source

For a given ascending UUID, extract the time component in milliseconds.

Like ascending/1 with the ability to specify time component.

Generate a UUID that can be sorted lexicographically from newest to oldest. The time component is set to the current time.

Like descending/0 with the ability to specify time component in milliseconds.

Link to this function

descending_decode_time(key)

View Source

For a given descending UUID, extract the time component in milliseconds.

Link to this function

descending_uniform(time)

View Source

Like descending/1 with the random part zero filled. Useful for specifying ranges of UUIDs in queries.