【发布时间】: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)
我正在使用<result>{fn:true()}</results> 选项,以便我的下一条语句等待所有生成任务完成,但它没有发生。
有人可以帮我解决这个要求吗?
注意:需要插入 1000 个文档,然后在同一个函数调用中读取它们
【问题讨论】:
-
这样的问题总是让我想知道为什么要这样做。退后一步,问问自己是否真的需要在同一个电话中完成所有这些操作可能是明智之举。在一次调用中进行多次更新不会扩展,并且尝试读取结果也不会使其更简单..