I want to version a document, for which we are followed below approach to check-out and check-in the document in 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 ---------------------------:)
However, it is throwing below exception:
[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
To overcome this I modified the XQuery to solve our purpose:
(:--------------------------- 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 ---------------------------:)
Above XQuery is working as expected, however, it would be great if anyone can help me in solving my purpose in more efficient and simplified manner.
You could simplify your code a bit by using xdmp:invoke-function()
instead of constructing strings for xdmp:eval()
, and avoid repeating the options by declaring a variable:
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