I tried to do it in this way, here is query I've run:
declare namespace lang = "http://s3r.ru/ns/code-language";
declare namespace t = "none";
declare variable $dbName := "test2";
declare function t:getIdByName($arg as xs:string)
{
let $x := db:attribute($dbName, $arg, 'name')/parent::object[ancestor::module]
return if( empty($x/@id) ) then "-1" else $x/@id
};
for $x in collection($dbName)/module//flow/argument/atom[@lang:tp = "function_call_name"]/@name
let $id := t:getIdByName($x)
return $id
The collection 'test2' contains 100 documents (it is very small collection). The query executed 2017156.38 ms (about 30 minutes). This is too slow :(
> declare function t:getIdByName($arg)Your query will be optimized if $dbName is globally declared (which I
> {
> let $x := collection($dbName)/module//object[@name = $arg]
> return if( empty($x/@id) ) then "-1" else $x/@id
> };
guess it is anyway), and if $arg is specified as string:
declare variable $dbName := '...';
declare function local:getIdByName($arg as xs:string) {
let $x := collection($dbName)/module//object[@name = $arg]You may as well use db:attributes() to explicitly access the index (if
return if( empty($x/@id) ) then "-1" else $x/@id
};
available):
declare variable $dbName := '...';
declare function local:getIdByName($arg) {
let $x := db:attribute($dbName, $arg,
'name')/parent::object[ancestor::module]
return if( empty($x/@id) ) then "-1" else $x/@idHope this helps,
};
Christian