在什么时候加载变量?

时间:2019-02-01 03:55:55

标签: xquery exist-db

我们正在重构一个非常大的系统,并查看许多我们编写的xQuery,并想知道是否使用并包括一个声明了许多全局变量的xQuery会无济于事。

但是问题是给建筑师的...这些是按参考加载还是仅在使用时加载?

意思是说我有一些xQuery ... _global.xq像这样:

extension NSTextView {

    func leftIndex(of index: Int) -> Int {

        guard
            let layoutManager = self.layoutManager,
            let textContainer = self.textContainer
            else { assertionFailure(); return index }

        let glyphIndex = layoutManager.glyphIndexForCharacter(at: index)
        let rect = layoutManager.boundingRect(forGlyphRange: NSRange(glyphIndex..<(glyphIndex+1)), in: textContainer)
            .offset(by: self.textContainerOrigin)

        let point = NSPoint(x: rect.minX - 1, y: rect.midY)

        return self.characterIndexForInsertion(at: point)
    }
}

然后我在xQuery中引用它:

module namespace g="global/variables";
declare variable $g:col.build := '/db/foo/data/Build';
declare variable $g:doc.langmap := doc(concat($g:col.build,'/','langmap.xml'));
declare variable $g:doc.easymap := doc(concat($g:col.build,'/','easymap.xml'));
declare variable $g:doc.foomap := doc(concat($g:col.build,'/','foomap.xml'));

然后,我在xQuery中仅使用import module namespace g='global/variables' at '_global.xq'; 。即使不使用它们,其他两个($g:doc.langmap$g:doc.easymap)也会被评估并加载到内存中吗?

$g:doc.foomap是填充在$g:doc.langmap上还是仅在我在查询中实际使用它时填充?就像我编写的xQuery从未引用import而是导入该模块一样,它是否仍在内存中创建并填充?

我想知道是因为在$g:doc.langmap中是否还有许多其他声明变量要在许多其他xQueries中使用。当然,我在每个参考文献中仅使用了一些参考文献。那么问题就很简单了……_global.xq命令是否导致它们全部在导入时进行评估,还是仅在使用它们时才具有值?

我怀疑这将是一个简短的答案。

1 个答案:

答案 0 :(得分:3)

好的,我相信我已经通过一些简单的测试就知道了答案。

我创建了一个Xquery,运行时间约为87秒。

O(n^2)

如果我将其更改为此:

xquery version "3.0";
declare variable $test := collection('/db/foo/data')//*[@docnum='GS01'];
let $foo := 'bar'
return
$test

运行时间不到一秒钟。这使我相信,除非使用$ test可以回答我的问题,否则$ test实际上不会填充数据。如果我做错了,请进来。