MarkLogic:在单个事务中提交多个语句

时间:2018-03-09 13:02:54

标签: nosql xquery marklogic marklogic-8

我想对一个文档进行版本化,我们将在下面的方法中检查签出并在单个事务中签入文档。

(:--------------------------- XQuery Starts ---------------------------:)
xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy";

declare function local:ManageDocument($docUri)
{
    let $query := fn:concat('
      xquery version "1.0-ml";
      declare namespace html = "http://www.w3.org/1999/xhtml";
      import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy";
      declare variable $docUri as xs:string external;
      dls:document-manage($docUri,fn:false(),fn:concat("First Version of ", $docUri))'
    )

    return xdmp:eval(
      $query,
      (xs:QName("docUri"), $docUri),
      <options xmlns="xdmp:eval">
        <prevent-deadlocks>true</prevent-deadlocks>
      </options>
    )
};

let $docUri := "/searchable/as-2018-1981_standard.pdf.xml"
let $isManaged  := dls:document-is-managed($docUri)
let $manageDoc := if($isManaged) then() else local:ManageDocument($docUri)

let $chechoutStatus := dls:document-checkout-status($docUri)
let $checkOut   := if($chechoutStatus and $isManaged) then (fn:error(xs:QName('Error'), "Already Editing")) else dls:document-checkout-update-checkin($docUri, element {"ROOT"} {"HELLO WORLD!"}, "document-checkout-update-checkin", fn:true())

return $manageDoc
(:--------------------------- XQuery Ends ---------------------------:)

然而,它抛出以下异常:

[1.0-ml] XDMP-PREVENTDEADLOCKS: xdmp:eval("&#10; xquery version &quot;1.0-
ml&quot;;&#10; declare ...", (fn:QName("","docUri"), "/searchable
/as-2018-1981_standard.pdf.xml"), <options xmlns="xdmp:eval"><prevent-
deadlocks>true</prevent-deadlocks></options>) -- Processing an update from an
update with different-transaction isolation could deadlock

为了解决这个问题,我修改了XQuery来解决我们的目的:

(:--------------------------- New XQuery Starts ---------------------------:)
xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy";

declare function local:ManageDocument($docUri)
{
    let $query := fn:concat('
      xquery version "1.0-ml";
      declare namespace html = "http://www.w3.org/1999/xhtml";
      import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy";
      declare variable $docUri as xs:string external;
      dls:document-manage($docUri,fn:false(),fn:concat("First Version of ", $docUri))'
    )

    return xdmp:eval(
      $query,
      (xs:QName("docUri"), $docUri),
      <options xmlns="xdmp:eval">
        <prevent-deadlocks>true</prevent-deadlocks>
      </options>
    )
};

declare function local:CheckouotDocument($docUri)
{
  let $query := fn:concat('
    xquery version "1.0-ml";
    declare namespace html = "http://www.w3.org/1999/xhtml";
    import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy";
    declare variable $docUri as xs:string external;
    (: dls:document-checkout($docUri, fn:true(), "updating doc", 3600) :)
    dls:document-checkout-update-checkin($docUri, element {"ROOT"} {"HELLO WORLD!"}, "document-checkout-update-checkin", fn:true())
   ')

   return xdmp:eval(
      $query,
      (xs:QName("docUri"), $docUri),
      <options xmlns="xdmp:eval">
        <prevent-deadlocks>true</prevent-deadlocks>
      </options>
    )
};

let $docUri := "/searchable/as-2018-1981_standard.pdf.xml"
let $isManaged  := dls:document-is-managed($docUri)
let $manageDoc := if($isManaged) then() else local:ManageDocument($docUri)

let $chechoutStatus := dls:document-checkout-status($docUri)
let $checkOut   := if($chechoutStatus and $isManaged) then (fn:error(xs:QName('Error'), "Already Editing")) else local:CheckouotDocument($docUri)

return $manageDoc
(:--------------------------- New XQuery Ends ---------------------------:)

上面的XQuery正如预期的那样工作,但是,如果有人能帮助我以更有效和简化的方式解决我的目标,那就太棒了。

1 个答案:

答案 0 :(得分:3)

您可以使用documentation而不是为xdmp:eval()构造字符串来简化代码,并避免通过声明变量来重复选项:

xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy";

declare variable $OPTIONS := 
  <options xmlns="xdmp:eval">
    <prevent-deadlocks>true</prevent-deadlocks>
  </options>;

declare function local:ManageDocument($docUri)
{
  xdmp:invoke-function(function() {
    dls:document-manage($docUri, fn:false(), "First Version of "||$docUri)
  }, $OPTIONS)
};

declare function local:CheckouotDocument($docUri)
{
  xdmp:invoke-function(function() {
    (: dls:document-checkout($docUri, fn:true(), "updating doc", 3600) :)
    dls:document-checkout-update-checkin($docUri, element {"ROOT"} {"HELLO WORLD!"}, "document-checkout-update-checkin", fn:true())
  }, $OPTIONS)
};

let $docUri := "/searchable/as-2018-1981_standard.pdf.xml"
let $isManaged  := dls:document-is-managed($docUri)
let $manageDoc := if ($isManaged) then() else local:ManageDocument($docUri)

let $chechoutStatus := dls:document-checkout-status($docUri)
let $checkOut   := if ($chechoutStatus and $isManaged) then (fn:error(xs:QName('Error'), "Already Editing")) else local:CheckouotDocument($docUri)

return $manageDoc