Knowledgebase: MarkLogic Server
Generating unique IDs (GUIDs)
28 July 2015 01:30 PM

SUMMARY

This article will show you a way to create a GUID using the XQuery language.

What are GUIDs?

A GUID (Globally Unique IDentifier) is expressed as a string and is comprised of groups of hexadecimal characters, each of which are separated into five groups by hyphens.

The format can be represented as:

XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

Where X denotes a hexidecimal value.

Generating a GUID

Version 7 and above

In version 7 and higher, the function sem:uuid-string is available to help in creating a UUID/GUID.

Version 6
xquery version "1.0-ml";

declare private function local:random-hex($seq as xs:integer*) as xs:string+ {
  for $i in $seq return 
    fn:string-join(for $n in 1 to $i
      return xdmp:integer-to-hex(xdmp:random(15)), "")
};

declare function local:guid() as xs:string {
  fn:string-join(local:random-hex((8,4,4,4,12)),"-")
};
Example use:
for $i in 1 to 250
return
local:guid()
Best Practice Tip

If you need to create thousands of GUIDs as part of your content processing architecture, be careful to create a separate URI to represent each unique identifier. When managing all GUIDs within a single URI, you may end up creating a bottleneck with multiple processes waiting on a write lock on this single URI. This is especially true if your working database enables the 'maintain last modified' or 'maintain directory last modified' options. Be sure to scan the server ErrorLog.txt file for any XDMP-DEADLOCK messages, which are indicative of a processing bottleneck.

(4 vote(s))
Helpful
Not helpful

Comments (0)