Function Repository Resource:

CreateSortableUniqueID

Source Notebook

Create a K-Sortable Unique Identifiers hexadecimal string

Contributed by: Daniel de Souza Carvalho

ResourceFunction["CreateSortableUniqueID"][]

retrieves a hexadecimal unique partial sortable ID string.

ResourceFunction["CreateSortableUniqueID"][n]

retrieves a list of n hexadecimal unique partial sortable ID strings.

Details

A K-Sortable Unique Identifier (KSUID) is a partially sortable type of identifier.
The algorithm to create KSUIDs produces hexadecimal strings that are statistically unlikely to repeat.
A KSUID is comprised of two parts. The first eight characters are from a timestamp and can be sorted lexicographically. The remainder is a random 19 character hexadecimal string.
KSUIDs can be sorted because the timestamp parts are at the beginning. This is only a partial ordering because there is a (very small) possibility that the random parts of two KSUIDs can agree.
This function can be used to provide identifiers to NoSQL database documents (JSON) in working with unstructured data and objects.
The time stamp uses the current time zone.

Examples

Basic Examples (2) 

Get one unique KSUID:

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

Get a list of unique IDs:

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

Scope (1) 

The beginning of a set of IDs are close to one another because they are based on Unix timestamps, while the second parts are pseudorandom hex digits:

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

Applications (2) 

We can check when a set of IDs were created:

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

Take the first eight hexadecimal digits, converting to decimal and converting from Unix time format:

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

Properties and Relations (6) 

It is possible to recover the timestamp from an ID, step by step:

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

Take just the 8 initial characters:

In[7]:=
StringTake[%, 8]
Out[7]=

Convert the hexadecimal to integer:

In[8]:=
FromDigits[%, 16]
Out[8]=

Convert from Unix time format:

In[9]:=
FromUnixTime[%]
Out[9]=

Compare with the value of a current Unix time:

In[10]:=
UnixTime[]
Out[10]=

Validate the timestamp at the beginning of the hash:

In[11]:=
FromDigits[
  StringTake[ResourceFunction["CreateSortableUniqueID"][], 8], 16] == UnixTime[]
Out[11]=

Neat Examples (2) 

Breaking the IDs into 2 hexadecimal pairs, we can plot the IDs as a graph, and view the random and repetitive parts:

In[12]:=
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[12]=

We can view a set of IDs as an image:

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

We can likewise view a sorted set of IDs as an image:

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

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. This algorithm is statistically hard to get repeated values.

License Information