MarkLogic - Sequential processing of XQuery statements

时间:2019-01-09 22:33:32

标签: marklogic marklogic-9 marklogic-dhf

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();

2 个答案:

答案 0 :(得分:1)

语句按该顺序执行,但是所有内容都在同一快照中提交给数据库。

答案 1 :(得分:1)

fn:current-dateTime()deterministic,并且将始终在同一笔交易中返回一致的答案。

  

[定义]如果显式和隐式参数相同,则保证在单个执行范围内重复调用可产生相同结果的函数称为确定性。

您可以使用xdmp:elapsed-time(),它将返回自开始处理查询以来经过的时间xs:dayTimeDuration,并将其添加到fn:currentDateTime()值中:

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