Function Repository Resource:

CreateSortableUniqueID

Source Notebook

Create a K-Sortable Unique IDentifier (KSUID), inspired by NoSQL JSON document databases

Contributed by: Daniel de Souza Carvalho

ResourceFunction["CreateSortableUniqueID"][]

creates a hexadecimal unique partially sortable ID.

ResourceFunction["CreateSortableUniqueID"][n]

creates a list of n hexadecimal unique partially sortable IDs.

Details

A K-Sortable Unique IDentifier (KSUID) is a partially sortable ID which has two parts: the epoch time stamp comprises the first 8 characters, and the send part comprising the remainder is a random value.
Because the start of the hexadecimal ID is a timestamp, it can be partially sorted.
The KSUID is useful as an ID in NoSQL database documents (JSON) and when working with unstructured data.

Examples

Basic Examples (2) 

Get one unique sortable ID:

In[1]:=
ResourceFunction["CreateSortableUniqueID"][]
Out[1]=

Get a list of unique sortable IDs:

In[2]:=
ResourceFunction["CreateSortableUniqueID"][10]
Out[2]=

Scope (1) 

Note that the beginning of the IDs are close to one another because they are based on a Unix timestamp. The second part is a random value:

In[3]:=
ResourceFunction["CreateSortableUniqueID"][20] // Column
Out[3]=

Applications (2) 

We can check when these mysterious IDs were created:

In[4]:=
SKUIDsample =
  {"0ujsszwN8NRY24YaXiTIE2VWDTS",
   "0ujsswThIGTUYm2K8FjOOfXtY1K",
   "0ujssxh0cECutqzMgbtXSGnjorm",
   "0ujsszgFvbiEr7CDgE3z8MAUPFt"};

Get the first eight digits, convert to decimal and get a DateObject from each Unix timestamp:

In[5]:=
FromUnixTime[FromDigits[StringTake[#, 8], 16]] & /@ SKUIDsample // Column
Out[5]=

Neat Examples (2) 

By breaking the KSUID apart into two hexadecimal values, we can plot multiple sortable IDs in a graph, and view the random and repetitive parts:

In[6]:=
ResourceFunction[
ResourceObject[<|"Name" -> "SequenceGraph", "ShortName" -> "SequenceGraph", "UUID" -> "64291990-d58f-416f-9b91-ae2d603789df", "ResourceType" -> "Function", "Version" -> "1.0.0", "Description" -> "Create a graph from a data sequence", "RepositoryLocation" -> URL[
     "https://www.wolframcloud.com/objects/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$badfb81e881745f79b7f0893d8a6bef0`SequenceGraph", "FunctionLocation" -> CloudObject[
     "https://www.wolframcloud.com/obj/d04f8b5f-853d-47fe-af51-a24df23c3036"]|>, ResourceSystemBase -> Automatic]][
 Flatten[StringPartition[#, 2] & /@ ResourceFunction["CreateSortableUniqueID"][10]]]
Out[6]=

We can view a set of IDs as an image:

In[7]:=
ArrayPlot[
 FromDigits[#, 16] & /@ StringPartition[#, 1] & /@ ResourceFunction["CreateSortableUniqueID"][30], Frame -> None, ColorFunction -> "Rainbow"]
Out[7]=

Publisher

Daniel de Souza Carvalho

Version History

  • 1.0.0 – 15 July 2022

Source Metadata

Related Resources

Author Notes

KSUID generates globally unique IDs close to RFC 4122 UUIDs, with a time component at the beginning of the hexadecimal value, in order to be "roughly" sorted by time of creation.

License Information