MarkLogic 7 spawn-function

时间:2015-07-28 07:57:07

标签: xquery marklogic

我有一个REST端点,它需要处理一长串代码。因为这可能会触发超时,所以我尝试使用spawn-function并在后台执行魔术。但看起来spawn-function正在从我的REST端点保持200 OK响应,所以它并没有真正产生。

我已添加日志行以检查它的链接位置。调试日志中会弹出所有日志行。

使用少量数据,这很好用。如果设置较大(60k代码)则会失败。

更改代码以生成$ text中每个项目的函数后,生成60k,我收到此错误:

2015-07-28 10:20:02.326 Debug: Forest::insert: STRLF3-content-001-1 XDMP-INMMFULL: In-memory storage full; list: table=5%, wordsused=3%, wordsfree=95%, overhead=1%; tree: table=8%, wordsused=3%, wordsfree=97%, overhead=0%

插入数据:

{   
    ProjectID: 102124,
    Text: "2311\n2253\n2312\n6626\n2253\n1234"
}

调用spawn进程:

(: ======================================================================= :) 
(: ! Load Transactions into seperate XML files                             :)
(: ======================================================================= :) 

declare 
%roxy:params("")
function strlf:post(
    $context as map:map,
    $params  as map:map,
    $input   as document-node()*
) as document-node()?
{
    map:put($context, "output-types", "application/json"),
    xdmp:set-response-code(200, "OK"),
    document { 

                  (: Get project ID :)
                  let $_ := xdmp:log('TransTest - stap1', 'debug')
                  let $project  := json:transform-from-json($input)/ns:ProjectID

                  let $_ := xdmp:log('TransTest - stap2', 'debug')
                  let $codes    := json:transform-from-json($input)/ns:Text

                  (: Clean current project :)
                  let $_ := xdmp:log('TransTest - stap3', 'debug')
                  let $uridir   := fn:concat('/app/transactie/', $project/text(), '/', '*')

                  let $_ := xdmp:log('TransTest - stap4', 'debug')
                  let $kill     := xdmp:document-delete(cts:uri-match($uridir))

                  (: Spawn the trannies :)
                  let $_ := xdmp:log('TransTest - stap5', 'debug')

                  (: return 'ja' :)
                  let $_ := xdmp:spawn-function(strlf:spawner($project, $codes, $uridir),
                    <options xmlns="xdmp:eval">
                      <transaction-mode>update-auto-commit</transaction-mode>
                    </options>)

                  return 'done'

    }
};

功能strlf:spawner:

declare private function strlf:spawner(
  $project,
  $codes,
  $uridir
)
{ 
  (: Tokenize on lines :)
  let $text     := fn:tokenize($codes, fn:codepoints-to-string(10))

  let $loop     :=
    for $regel in $text

      let $tokregel := fn:tokenize($regel, ",")           

      let $intvalue := 
       if (fn:contains($regel, ","))
       then fn:substring-after($regel, "€")
       else 1 

      let $code :=
       if (fn:contains($regel, ","))
       then $tokregel[1]
       else $regel 

      (: Build map of maps, p4 should be postcode :)
      let $map      := map:map()
      let $_        := map:put($map, 'code', $code)
      let $_        := map:put($map, 'p4', fn:substring($code[1], 1, 4))
      let $_        := map:put($map, 'value', $intvalue)
      let $_        := map:put($map, 'projectid', $project/text())

      (: Create unverified random doc id :)
      let $docid    := fn:string(xdmp:random(1000000000000))

      (: Build URI :)
      let $uridoc   := fn:concat('/app/transactie/', $project/text(), '/', $docid, '.xml')

    (: Save transaction document and skip header :)
    return 
    (if (map:get($map, 'code') != 'CODE')
    then xdmp:document-insert 
            (
            $uridoc,
              <transaction xmlns='http://www.dikw.nl/transactions' projectid='{map:get($map, 'projectid')}' code='{map:get($map, 'code')}' p4='{map:get($map, 'p4')}'>
                <value>{map:get($map, 'value')}</value>
              </transaction>
            )
    else ()) 

  (: Empty return :)
  return $loop        
};

1 个答案:

答案 0 :(得分:1)

正确,你有strlf:spawner($project, $codes, $uridir)作为xdmp:spawn-function的第一个参数,导致它被执行,结果传递到xdmp:spawn-function。由于spawner函数返回一个空序列,因此spawn-function不会抛出任何错误。

修复非常简单,将您的spawner调用包装在匿名函数中:

let $_ := xdmp:spawn-function(function () { strlf:spawner($project, $codes, $uridir) },
                <options xmlns="xdmp:eval">
                  <transaction-mode>update-auto-commit</transaction-mode>
                </options>)

HTH!