[XQZone General] Guaranteed unique?

Ron Hitchens ron.hitchens at marklogic.com
Tue Nov 22 16:40:20 PST 2005


    Here is a function to do what Ian describes, lifted
from the xfaqtor code available on xqzone:


(: Return a monotonically increasing integer (xs:integer) value.
    Store the value in the root node of doc ($doc-uri).
    You could enhance this file to store IDs for multiple uses.
    Logic is to fetch the current id, add one if it exists or assign the
    value to one if it doesn't yet exist, then replace or insert the new
    value before returning it.
:)
define function next-id ($doc-uri) as xs:integer
{
         let $id := doc($doc-uri)/id
         let $next-val :=
                 if ($id castable as xs:integer) then xs:integer  
($id) + 1
                 else 1
         let $next-node := <id>{$next-val}</id>
         let $insert :=
                 if ($id) then xdmp:node-replace ($id, $next-node)
                 else xdmp:document-insert ($doc-uri, $next-node)

         return $next-val
}


On Nov 22, 2005, at 4:15 PM, Ian Small wrote:

> Andy -
>
> Just create a document that contains the serial number in  
> question.  Write some simple XQuery to retrieve the id from the  
> document, increment it, and update the document (using xdmp:node- 
> replace(), etc.).
>
> Because your XQuery module is performing an "update", it will block  
> all other requests trying to update that same document, thereby  
> guaranteeing unique IDs.
>
> Only "queries" (ie. requests which are statically analyzed to be  
> "read-only" requests) run lock-free per your description.  Updates  
> (ie. requests which are statically analyzed to potentially change  
> the state of the database) lock documents and consequently block  
> each other as appropriate.
>
> ian
>
> -----Original Message-----
> From: general-bounces at xqzone.marklogic.com [mailto:general- 
> bounces at xqzone.marklogic.com] On Behalf Of Andy Townsend
> Sent: Tuesday, November 22, 2005 1:43 PM
> To: general at xqzone.marklogic.com
> Subject: [XQZone General] Guaranteed unique?
>
>
>
>
> Anyone know a nice way to generate guaranteed unique sequential IDs  
> with XQuery in Mark Logic?
>
> So I can for example create the values for:  id="1" , id="2", etc...
> and be sure that two users cannot ever pick up the same number.   
> Just having a document in the database with the current number  
> doesn't seem enough to me because I believe there would be a race  
> condition where two users could start at about the same time and so  
> see the same initial 'state' of the database which I presume the  
> XQuery would insist they both saw throughout their queries.
>
> I thought that document-locks might get me there (though in  
> probably contravention of the standard) but have not had any joy  
> with those as yet
>
> In Java it would be easy - just a serialised block - equally easy  
> in C++, but what about XQuery?
>
> Someone must have done this already!   :-)
>
> Thanks
>
> Andy
>
> ######################################################################
> The information contained in this e-mail and any subsequent  
> correspondence is private and confidential and intended solely for  
> the named recipient(s).  If you are not a named recipient, you must  
> not copy, distribute, or disseminate the information, open any  
> attachment, or take any action in reliance on it.  If you have  
> received the e-mail in error, please notify the sender and delete  
> the e-mail.
>
> Any views or opinions expressed in this e-mail are those of the  
> individual sender, unless otherwise stated.  Although this e-mail  
> has been scanned for viruses you should rely on your own virus  
> check, as the sender accepts no liability for any damage arising  
> out of any bug or virus infection.
> ######################################################################
> _______________________________________________
> General mailing list
> General at xqzone.marklogic.com
> http://xqzone.com/mailman/listinfo/general
> _______________________________________________
> General mailing list
> General at xqzone.marklogic.com
> http://xqzone.com/mailman/listinfo/general

---
Ron Hitchens {ron.hitchens at marklogic.com}  650-655-2351





More information about the General mailing list