MarkLogic:在单个事务中提交多个语句
MarkLogic: Committing multiple statements within a single transaction
我想对文档进行版本控制,为此我们遵循以下方法在单个事务中签出和签入文档。
(:--------------------------- 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(" xquery version "1.0-
ml"; 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 工作正常,但是,如果有人能帮助我以更高效和简化的方式解决我的目的,那就太好了。
您可以通过使用 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
我想对文档进行版本控制,为此我们遵循以下方法在单个事务中签出和签入文档。
(:--------------------------- 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(" xquery version "1.0-
ml"; 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 工作正常,但是,如果有人能帮助我以更高效和简化的方式解决我的目的,那就太好了。
您可以通过使用 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