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

Nosql MarkLogic:在一个事务中提交多个语句,nosql,xquery,marklogic,marklogic-8,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/

我想对一个文档进行版本设置,我们按照下面的方法在单个事务中签出和签入文档

(:--------------------------- 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 ---------------------------:)
(:------------------------------------XQuery启动----------------------------------------------------:)
xquery版本“1.0-ml”;
声明命名空间html=”http://www.w3.org/1999/xhtml";
导入模块命名空间dls=”http://marklogic.com/xdmp/dls“at”/MarkLogic/dls.xqy”;
声明函数local:ManageDocument($docUri)
{
让$query:=fn:concat('
xquery版本“1.0-ml”;
声明命名空间html=”http://www.w3.org/1999/xhtml";
导入模块命名空间dls=”http://marklogic.com/xdmp/dls“at”/MarkLogic/dls.xqy”;
将变量$docUri声明为xs:string external;
dls:documentmanage($docUri,fn:false(),fn:concat(“第一版”,$docUri))'
)
返回xdmp:eval(
$query,
(xs:QName(“docUri”),$docUri),
真的
)
};
let$docUri:=“/searchable/as-2018-1981_standard.pdf.xml”
让$isManaged:=dls:文档被管理($docUri)
让$manageDoc:=if($isManaged)then()else本地:ManageDocument($docUri)
let$chechoutStatus:=dls:文档签出状态($docUri)
让$checkOut:=if($chechoutStatus和$isManaged)然后(fn:error(xs:QName('error'),“已编辑”))否则dls:documentcheckout-update-checkin($docUri,元素{“ROOT”}{“HELLO-WORLD!”,“document checkOut-update-checkin”,fn:true())
返回$manageDoc
(:------------------------------------XQuery结束------------------------------------------:)
但是,它抛出以下异常:

[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
[1.0-ml]XDMP-prevent死锁:XDMP:eval(“
;xquery版本”1.0-
ml“
;声明…”,(fn:QName(“,“docUri”),“/searchable
/as-2018-1981_standard.pdf.xml”),true)——处理来自
使用不同事务隔离的更新可能会死锁
为了克服这个问题,我修改了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开始------------------------------------------:)
xquery版本“1.0-ml”;
声明命名空间html=”http://www.w3.org/1999/xhtml";
导入模块命名空间dls=”http://marklogic.com/xdmp/dls“at”/MarkLogic/dls.xqy”;
声明函数local:ManageDocument($docUri)
{
让$query:=fn:concat('
xquery版本“1.0-ml”;
声明命名空间html=”http://www.w3.org/1999/xhtml";
导入模块命名空间dls=”http://marklogic.com/xdmp/dls“at”/MarkLogic/dls.xqy”;
将变量$docUri声明为xs:string external;
dls:documentmanage($docUri,fn:false(),fn:concat(“第一版”,$docUri))'
)
返回xdmp:eval(
$query,
(xs:QName(“docUri”),$docUri),
真的
)
};
声明函数本地:CheckouotDocument($docUri)
{
让$query:=fn:concat('
xquery版本“1.0-ml”;
声明命名空间html=”http://www.w3.org/1999/xhtml";
导入模块命名空间dls=”http://marklogic.com/xdmp/dls“at”/MarkLogic/dls.xqy”;
将变量$docUri声明为xs:string external;
(:dls:documentcheckout($docUri,fn:true(),“更新文档”,3600):)
dls:documentcheckoutupdatecheckin($docUri,元素{“ROOT”}{“helloworld!”},“documentcheckoutupdatecheckin”,fn:true())
')
返回xdmp:eval(
$query,
(xs:QName(“docUri”),$docUri),
真的
)
};
let$docUri:=“/searchable/as-2018-1981_standard.pdf.xml”
让$isManaged:=dls:文档被管理($docUri)
让$manageDoc:=if($isManaged)then()else本地:ManageDocument($docUri)
let$chechoutStatus:=dls:文档签出状态($docUri)
让$checkOut:=if($chechoutStatus和$isManaged)然后(fn:error(xs:QName('error'),“已编辑”))否则本地:CheckouotDocument($docUri)
返回$manageDoc
(:------------------------------------新的XQuery结束------------------------------------------:)

上面的XQuery按预期工作,但是,如果有人能帮助我以更高效和简化的方式解决我的问题,那就太好了。

您可以通过使用而不是为
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
xquery版本“1.0-ml”;
声明命名空间html=”http://www.w3.org/1999/xhtml";
导入模块命名空间dls=”http://marklogic.com/xdmp/dls“at”/MarkLogic/dls.xqy”;
声明变量$OPTIONS:=
真的
;
声明函数local:ManageDocument($docUri)
{
xdmp:调用函数(函数(){
dls:documentmanage($docUri,fn:false(),“| |$docUri”的第一个版本)
},$OPTIONS)
};
声明函数本地:CheckouotDocument($docUri)
{
xdmp:调用函数(函数(){
(:dls:documentcheckout($docUri,fn:true(),“更新文档”,3600):)
dls:documentcheckoutupdatecheckin($docUri,元素{“ROOT”}{“helloworld!”},“documentcheckoutupdatecheckin”,fn:true())
},$OPTIONS)
};
let$docUri:=“/searchable/as-2018-1981_standard.pdf.xml”
让$isManaged:=dls:文档被管理($docUri)
让$manageDoc:=if($isManaged)then()else本地:ManageDocument($docUri)
let$chechoutStatus:=dls:文档签出状态($docUri)
让$checkOut:=if($chechoutStatus和$isManaged)然后(fn:error(xs:QName('error'),“已编辑”))否则本地:CheckouotDocument($docUri)
返回$manageDoc