1

我必须在 MarkLogic 服务器中复制整个项目文件夹,而不是手动执行,我决定使用递归函数来执行此操作,但这正在成为我曾经有过的最糟糕的想法。我在交易和语法方面遇到问题,但作为新手,我找不到真正的解决方法。这是我的代码,谢谢你的帮助!

import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy";

declare option xdmp:set-transaction-mode "update";

declare function local:recursive-copy($filesystem as xs:string, $uri as xs:string)
{
  for $e in xdmp:filesystem-directory($filesystem)/dir:entry
  return 
    if($e/dir:type/text() = "file")
        then dls:document-insert-and-manage($e/dir:filename, fn:false(), $e/dir:pathname)
    else
      (
          xdmp:directory-create(concat(concat($uri, data($e/dir:filename)), "/")),
          local:recursive-copy($e/dir:pathname, $uri)
      )

};

let $filesystemfolder := 'C:\Users\WB523152\Downloads\expath-ml-console-0.4.0\src'
let $uri := "/expath_console/"

return local:recursive-copy($filesystemfolder, $uri)
4

1 回答 1

3

MLCP 会很好用。但是,这是我的版本:

declare option xdmp:set-transaction-mode "update";

declare variable $prefix-replace := ('C:/', '/expath_console/');

declare function local:recursive-copy($filesystem as xs:string){
   for $e in xdmp:filesystem-directory($filesystem)/dir:entry
    return 
      if($e/dir:type/text() = "file")
         then 
           let $source := $e/dir:pathname/text()
           let $dest := fn:replace($source, $prefix-replace[1], $prefix-replace[2]) 
           let $_ := xdmp:document-insert($source,
              <options xmlns="xdmp:document-load">
                <uri>{$dest}</uri>
              </options>)
           return <record>
                     <from>{$source}</from>
                     <to>{$dest}</to>
                  </record>
         else
           local:recursive-copy($e/dir:pathname)

};

let $filesystemfolder := 'C:\Temp'

return <results>{local:recursive-copy($filesystemfolder)}</results> 

请注意以下事项:

  • 我将示例更改为 C:\Temp 目录
  • 输出只是 XML,因为按照惯例,我会尝试这样做,以防我想分析结果。这实际上是我发现与冲突更新相关的错误的方式。
  • 我选择在 URI 上定义一个简单的前缀替换
  • 我在您的描述中看到不需要 DLS
  • 我认为在您的用例中不需要显式创建目录
  • 您获得冲突更新的原因是您仅使用文件名作为 URI。在整个目录结构中,这些名称不是唯一的 - 因此对相同 URI 的双重插入的更新冲突。
  • 这不是可靠的代码:
    • 您必须确保 URI 有效。并非所有文件系统路径/名称都适用于 URI,因此您需要对此进行测试并在需要时转义字符。
    • 大型文件系统会超时,因此分批生成可能很有用。
      • 举个例子,我可能会像在我的 XML 中一样收集文档列表,然后通过为每 100 个文档生成一个新任务来处理该列表。这可以通过 xdmp:spawn-function 上的简单循环或使用诸如@mblakele的 taskbot 之 类的库来完成
于 2017-08-29T23:39:03.400 回答