Note: this code will return only one unique value per transaction. current-datetime() returns the same value throughout the transaction as MarkLogic Server is based on point-in-time querying..
Kind regards, Geert ________________________________ From: [email protected] [mailto:[email protected]] On Behalf Of Keith L. Breinholt Sent: maandag 29 maart 2010 16:45 To: General Mark Logic Developer Discussion Subject: RE: [MarkLogic Dev General] Unique key construction Here is a UUID version 3 implementation that we use for URLs that is unique to the name, host and time created: declare variable $g_defaultNamespace as xs:string := xdmp:host-name(xdmp:host()); declare variable $g_uuidVersion as xs:unsignedLong := 3; declare variable $g_uuidReserved as xs:unsignedLong := 8; (: Calculate the UUID and set the UUID property on the file. The layout of a UUID is as follows. 0 1 2 3 0 1 2 3 4 5 6 7 8 9 a b c d e f 0 1 2 3 4 5 6 7 8 9 a b c d e f +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | time_low | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | time_mid | time_hi |version| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |clk_seq_hi |res| clk_seq_low | node (0-1) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | node (2-5) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 0 1 2 3 4 5 6 7 8 9 a b c d e f 0 1 2 3 4 5 6 7 8 9 a b c d e f This implements version 3 of the UUID specification. The timestamp is a 60-bit value. The clock sequence is a 14 bit value. The node is a 48-bit name value. :) declare function meta:uuid( $uri as xs:string ) as xs:string { meta:uuid( $uri, $g_defaultNamespace ) }; declare function meta:uuid( $uri as xs:string, $namespace as xs:string ) as xs:string { (: calculate md5 with a dateTime for our random values :) let $hash := xdmp:md5( concat( $uri, xs:string(current-dateTime()), $namespace ) ) return concat ( substring( $hash, 1, 15 ), (: set version bits :) xdmp:integer-to-hex($g_uuidVersion), (: set reserved bits :) substring( $hash, 17, 1 ), xdmp:integer-to-hex((xdmp:hex-to-integer(substring($hash, 18, 1)) idiv 4) + $g_uuidReserved), substring( $hash, 19, 14 ) ) }; Keith L. Breinholt [email protected] -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Geert Josten Sent: Monday, March 29, 2010 2:35 AM To: General Mark Logic Developer Discussion Subject: RE: [MarkLogic Dev General] Unique key construction Hi Deepak, Look in the Modules, not the Admin folder. Here is a typical case taken from triggers.xqy: define function get-unique-trigger-id() as xs:unsignedLong { let $col := triggers-collection(), $attempt := xdmp:random(), $doc-uri := fn:concat(triggers-uri(), xs:string($attempt)), $docs := for $d in fn:collection($col) return fn:base-uri($d) return if ($doc-uri = $docs) then get-unique-trigger-id() else $attempt } You can make it slightly more robust by counting the attempts and throwing an error if you need to many attempts. The number of unique numbers generated by xdmp:random is large though, so that is only necessary when you have good reason to believe that you could approach that limit.. Kind regards, Geert > drs. G.P.H. (Geert) Josten Consultant Daidalos BV Hoekeindsehof 1-4 2665 JZ Bleiswijk T +31 (0)10 850 1200 F +31 (0)10 850 1199 mailto:[email protected] http://www.daidalos.nl/ KvK 27164984 P Please consider the environment before printing this mail. De informatie - verzonden in of met dit e-mailbericht - is afkomstig van Daidalos BV en is uitsluitend bestemd voor de geadresseerde. Indien u dit bericht onbedoeld hebt ontvangen, verzoeken wij u het te verwijderen. Aan dit bericht kunnen geen rechten worden ontleend. > From: [email protected] > [mailto:[email protected]] On Behalf Of > deepak mohan > Sent: zondag 28 maart 2010 17:44 > To: [email protected] > Subject: [MarkLogic Dev General] Unique key construction > > Hi All, > > Please tell me how ML constructs the unique ID while creating > any ML entities(App Servers, DB, forest etc.). I need to > generate a unique ID. I tried dig into the ML Admin modules > APi, I couldnot find the algo. Are they using random() and > check for existence? > > Thanks, > Deepak Mohanakrishnan. > > > ________________________________ > > Your Mail works best with the New Yahoo Optimized IE8. Get it > NOW! > <http://in.rd.yahoo.com/tagline_ie8_new/*http://downloads.yaho > o.com/in/internetexplorer/> . > _______________________________________________ General mailing list [email protected] http://xqzone.com/mailman/listinfo/general NOTICE: This email message is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
_______________________________________________ General mailing list [email protected] http://xqzone.com/mailman/listinfo/general
