Search code examples
marklogicmarklogic-9marklogic-dhf

MarkLogic - Sequential processing of XQuery statements


I am running below code and expecting insert-after function to execute after insert-before function (at least with a 2000 millisecond gap) as my understanding is that XQuery would execute statements in sequence. But, after running the code, I see that both documents (/content/testbefore.xml and /content/testafter.xml) created by the two functions have the exact timestamp value matching to the milliseconds.

How could I have the statements execute sequentially?

xquery version "1.0-ml";

declare function local:insert-before()
  {
  let $contents :=
    <book>
      <bookTitle>All About George</bookTitle>
      <tstmp>{fn:current-dateTime()}</tstmp>
      <chapter1>
        <chapterTitle>Curious George</chapterTitle>
      </chapter1>
   </book>
  return xdmp:document-insert("/content/testbefore.xml", $contents)
};

declare function local:insert-after()
{
let $contents :=
  <after>
    <bookTitle>All About George</bookTitle>
    <tstmp>{fn:current-dateTime()}</tstmp>
    <chapter1>
      <chapterTitle>Curious George</chapterTitle>
    </chapter1>
 </after>
 return xdmp:document-insert("/content/testafter.xml", $contents)
};

local:insert-before(),
xdmp:commit(),
xdmp:sleep(2000),
local:insert-after();

Solution

  • fn:current-dateTime() is deterministic, and will always return a consistent answer within the same transaction.

    [Definition] A function that is guaranteed to produce ·identical· results from repeated calls within a single ·execution scope· if the explicit and implicit arguments are identical is referred to as deterministic.

    You could use xdmp:elapsed-time(), which will return an xs:dayTimeDuration of the elapsed time since the start of processing of the query, and add that to the fn:currentDateTime() value:

    <tstmp>{fn:current-dateTime() + xdmp:elapsed-time()}</tstmp>