【问题标题】:MarkLogic: Committing multiple statements within a single transactionMarkLogic:在单个事务中提交多个语句
【发布时间】:2018-08-18 01:34:02
【问题描述】:

我想对文档进行版本化,我们按照以下方法在单个事务中签出和签入文档。

(:--------------------------- 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 上面的工作按预期工作,但是,如果有人能帮助我以更有效和简化的方式解决我的目的,那就太好了。

【问题讨论】:

    标签: nosql xquery marklogic marklogic-8


    【解决方案1】:

    您可以通过使用xdmp:invoke-function() 而不是为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
    

    【讨论】:

      猜你喜欢
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      相关资源
      最近更新 更多