[MarkLogic Dev General] unique ids

Michael Blakeley michael.blakeley at marklogic.com
Tue Oct 14 11:44:23 PDT 2008


Paul,

Yes, you can increment a sequence document by more than 1, to mimic 
sequence reservations. But to get any performance benefit, won't you 
also have to maintain a list of reserved, but unused, sequence numbers? 
Perhaps you'd do so in your application layer, where you'll also have to 
serialize access to it?

It's easy to find RDBMS DBAs who have performance problems with 
sequences. Even with reservations and caching, sequences are a point of 
serialization. With modern applications I prefer to avoid potential 
serialization points - hence my preference for random numbers.

On your question (1): no, you don't need an explicit lock, but not 
because of FLWOR syntax. Each XQuery update statement is a transaction, 
regardless of how you arrange the syntax. You never need explicit locks 
within a single transaction: the locking API is used only when you want 
to update a document over multiple points in time (eg, webdav, CMS, etc).

I agree with your point in (2) that a node-level update is cleaner (no 
difference in performance). I'd use xdmp:node-replace().

-- Mike

Paul M wrote:
> (:~
>  : Gets the next unique id. Reserves as many id as needed.
>  : i.e. current id=3 next-id(5) will return 4 and will set current id=8.
>  : so 4,5,6,7,8 are now unique for you to use
>  : calling next-id(2) will now return 9 and will set current id=10.
>  : so 9, 10 are now unique for you to use
>  : Sequences not used are lost.
>  :
>  : @param   $reserve the number of unique id's required 
>  :) 
> define function next-id($reserve as xs:unsignedLong) as xs:unsignedLong
> {
>     let $last-unique-id := xs:long(fn:doc("/my-id.xml")//id)
>     let $new-unique-id := $last-unique-id + $reserve
>     let $id := <id>{$new-unique-id}</id>
>     let $dumb := xdmp:document-insert("/seg-id.xml", $id,
>         (xdmp:permission("my-access", "update"),
>                 xdmp:permission("my-access", "insert"),
>                 xdmp:permission("my-access","read")))
>     return ($last-unique-id + 1)
> }
> 
> 
> 1. I do not need an explicit  lock because flowr stmt uses document-insert? The read and the insert are one flowr stmt?
> 
> 2.
> Any preferences document-insert vs node-insert? (ps. I prefer
> explicitly setting the permissions. I presume, with node-insert, this
> would not be required?)
> 
> 3. Reasonable solution in trying to mimic Oracle functionality?
> 
> 
> 
>       
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> General mailing list
> General at developer.marklogic.com
> http://xqzone.com/mailman/listinfo/general



More information about the General mailing list