【问题标题】:Inserting a document and reading it in same transaction in MarkLogic在 MarkLogic 的同一事务中插入文档并读取它
【发布时间】:2019-01-07 17:23:23
【问题描述】:

下面是我用于其中一项功能的代码 sn-p

declare function local:matchCounts($Id as xs:string, $status as xs:string) as xs:int {
  xdmp:estimate(cts:search(/count, cts:and-query((
    cts:element-attribute-value-query(xs:QName("count"), xs:QName("Id"), $Id, "exact"),
    cts:element-attribute-value-query(xs:QName("child"), xs:QName("MatchStatus"), $status, "exact")
  )), "unfiltered"))
};

declare function local:saveCountsMatchC($Id as xs:string) {
  let $evenCount := local:matchCounts($Id, "even")
  let $oddCount := local:matchCounts($Id, "odd")
  return ($evenCount, $oddCount)
};

declare function local:matchingProcess($Id as xs:string) {
let $total-records := 1000
let $batch-size := 50
let $pagination := 0
let $bs := 
   for $records in 1 to fn:ceiling($total-records  div $batch-size )
   let $start := fn:sum($pagination + 1)
   let $end := fn:sum($batch-size + $pagination)
   let $_ := xdmp:set($pagination, $end)
   return
    xdmp:spawn-function
    (
    function() {
     for $each at $pos in ($start to $end)
     let $id := sem:uuid-string()
     let $xml := if(($pos mod 2) eq 0) then <count Id='{$Id}'><child MatchStatus='even'></child></count> 
                 else <count Id='{$Id}'><child MatchStatus='odd'></child></count>
     return xdmp:document-insert(concat("/", $id, ".xml"), $xml)
    },
    <options xmlns="xdmp:eval"><result>{fn:true()}</result><commit>auto</commit><update>true</update></options>
    )
let $_ := $bs
return local:saveCountsMatchC($Id)
};

local:matchingProcess("1")

这里的要求是使用 50 的批次大小迭代 1000 个文档,所以基本上我使用 spawn 函数创建 20 个大小为 50 的批次,在我的数据库中插入 1000 个文档。 插入这些文档后,我需要在同一事务中阅读这些文档。这里 500 个文档的 MatchStatus='odd' 和 500 个文档的 MatchStatus='even' 查询应返回 (500,500) 作为输出;相反,它返回 (0,0)

我正在使用&lt;result&gt;{fn:true()}&lt;/results&gt; 选项,以便我的下一条语句等待所有生成任务完成,但它没有发生。

有人可以帮我解决这个要求吗?

注意:需要插入 1000 个文档,然后在同一个函数调用中读取它们

【问题讨论】:

  • 这样的问题总是让我想知道为什么要这样做。退后一步,问问自己是否真的需要在同一个电话中完成所有这些操作可能是明智之举。在一次调用中进行多次更新不会扩展,并且尝试读取结果也不会使其更简单..

标签: xquery marklogic


【解决方案1】:

执行生成的代码本身不会执行更新,因此将以所谓的查询模式运行。在查询模式下,只有代码开始之前的更新可见。

您可以尝试在更新模式下运行 (declare option xdmp:transaction-mode "update";),但通常只生成或评估更新的计数/读取会更容易。例如。将xdmp:estimate 包装在xdmp:spawn-function 中,结果为true

HTH!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多