2013-01-03 49 views
0

使用MarkLogic Xquery,我有一個函數(admin:add-collection-to-publication),它調用另一個維護函數(admin:check-collections-exists),它檢查元素的存在,如果它不存在,則創建該特定元素。xquery在另一個函數中調用維護功能

我稱之爲維護功能的方式是使用。這看起來像一個奇怪的方式,要做到這一點,它需要創建一個未使用的變量。我是否應該返回一個序列,其中admin:check-collections-exists是序列中的第一項,那麼後續處理是第二個元素?只是尋找標準的優雅方式來做到這一點。我的功能是:

declare function admin:add-collection-to-publication($pub-name, $collection-name) 
{ 
(:does this publication have a COLLECTIONS element?:) 
let $unnecessary-variable := admin:check-collections-exists($pub-name) 
    (:now go and do what this function does:) 
return "do some other stuff then return" 

}; 

declare function admin:check-collections-exists($pub-name) 
{ 
if(fn:exists($pubs-node/pub:PUBLICATION[pub:NAME/text()=$pub-name]/pub:COLLECTIONS)) 
then 
    "exists" 
else 
    xdmp:node-insert-child($pubs-node/pub:PUBLICATION[pub:NAME/text()=$pub-name],<pub:COLLECTIONS/>) 
}; 
+0

我經常使用'func()[0]'(或'func()[4000000000]'如果前者被優化掉了)來計算一些東西並忽略它的結果 – BeniBela

+0

我在macloglogic文檔中看到xdmp:function,但看起來像你如果你的函數需要一個參數,仍然需要返回。 012quxquery版本「1.0-ml」; 讓$函數:= xdmp:功能(XS:QName的( 「FN:CONCAT」)) 回報 xdmp:申請($功能, 「你好」, 「世界」) =>世界你好 –

回答

0

使用序列是不可靠的。 MarkLogic很可能會嘗試並行評估序列項目,這可能會導致創建在「相同」時間發生,甚至在其他工作之後發生。最好的方法是使用let。讓我們總是在返回之前進行評估。請注意,我們也可以並行評估,但優化器足夠智能以檢測依賴關係。

就我個人而言,我經常使用未使用的變量。例如插入記錄語句,在這種情況下,我有我每次都重複使用一個未使用的變量名:

let $log := xdmp:log($before) 
let $result := do:something($before) 
let $log := xdmp:log($result) 

你也可以用很短的變量名稱,比如$ _。或者你可以考慮實際上是給變量一個明智的名稱,並用它畢竟,即使你知道它永遠不會到達其他

let $exists := 
    if (collection-exists()) then 
     create() 
    else true() 
return 
    if ($exists) then 
     "do stuff" 
    else() (: never reached!! :) 

HTH!

+0

THX的答案! –