Changeset 802 for MDRepository


Ignore:
Timestamp:
10/18/10 14:59:43 (14 years ago)
Author:
ljo
Message:

cmd-model.xqm - move som more lookups to ft:query().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • MDRepository/trunk/xquery/cmd-model.xqm

    r800 r802  
    5151        $dummy :=
    5252      if (cmd-model:is-doc-available($names[1], $names[2])) then
    53                                 ()
     53        ()
    5454      else
    55                                 let $children :=
    56                                         for $collection-item in $collections
    57                         return
    58                                                         for $collection-doc in cmd-model:get-resource-by-handle($collection-item)
    59                                                                 return cmd-model:recurse-collections($collection-doc, cmd-model:get-md-collection-name($collection-doc), $collection-doc//MdSelfLink, "", $max-depth)
    60                                 (: let $res-count := $children//c[not(c)]/@cnt)
    61                                         actually it should be enough to sum the first level: :)
    62                                         let $res-count := sum($children/@cnt)
    63                                         let $coll-count := sum($children/@cnt_subcolls) + count($children)
    64                                 return cmd-model:store-collection-data(<Collections cnt="{$res-count}" cnt_subcolls="{$coll-count}" cnt_children="{count($children)}" root="{$collections}">{$children}</Collections>, $names[1], $names[2])
     55        let $children :=
     56          for $collection-item in $collections
     57            return
     58            for $collection-doc in cmd-model:get-resource-by-handle($collection-item)
     59              return cmd-model:recurse-collections($collection-doc, cmd-model:get-md-collection-name($collection-doc), $collection-doc//MdSelfLink, "", $max-depth)
     60            let $res-count := sum($children/@cnt)
     61            let $coll-count := sum($children/@cnt_subcolls) + count($children)
     62              return cmd-model:store-collection-data(<Collections cnt="{$res-count}" cnt_subcolls="{$coll-count}" cnt_children="{count($children)}" root="{$collections}">{$children}</Collections>, $names[1], $names[2])
     63
    6564        return
    6665        cmd-model:serialise-as(cmd-model:get-doc($names[1], $names[2]), $format)
     
    177176
    178177(:
    179   Get the resource ONLY by handle (previously: by handle or by path. )
     178  Get the MD resource by handle.
    180179:)
    181180declare function cmd-model:get-resource-by-handle($id as xs:string) as node()* {
    182  
    183   if ($id="" or $id=$cmd-model:collectionRoot ) then
    184     collection($cmd-model:cmdiMirrorPath)//IsPartOf[. = $cmd-model:collectionRoot]/ancestor::CMD
     181  let $collection := collection($cmd-model:cmdiMirrorPath)
     182  return
     183    if ($id eq "" or $id eq $cmd-model:collectionRoot) then
     184    $collection//IsPartOf[. = $cmd-model:collectionRoot]/ancestor::CMD
    185185  else
    186     collection($cmd-model:cmdiMirrorPath)//MdSelfLink[. = xdb:decode($id)]/ancestor::CMD
    187       (: let $key := xdb:decode(($doc-name, $id)[1])
    188     return ft:query(//MdSelfLink, <term>{$key}</term>)[matches(., concat("^", $key, "$"))]/ancestor::CMD :)
    189 };
    190 
    191 (:
    192   Recurse for collections
    193 :)
    194 declare function cmd-model:recurse-collections($collection as node()+, $name as xs:string, $handle as xs:string, $proxy-id as xs:string, $depth as xs:integer) as item()* {
    195     (:let $children := if ($depth eq 0) then () else $collection//ResourceProxy[ResourceType = "Metadata"]
    196     let $children_colls := $children $collection//ResourceProxy[ResourceType = "Metadata"] :)
    197     let $children :=  if ($depth eq 0) then () else cmd-model:get-children-colls($collection)
     186    util:eval(concat("$collection/ft:query(descendant::MdSelfLink, <term>", xdb:decode($id), "</term>)/ancestor::CMD"))
     187 (: $collection/descendant::MdSelfLink[. = xdb:decode($id)]/ancestor::CMD :)
     188
     189};
     190
     191(:
     192  Recurse down in collections.
     193:)
     194declare function cmd-model:recurse-collections($collection as node(), $name as xs:string, $handle as xs:string, $proxy-id as xs:string, $depth as xs:integer) as item()* {
     195  let $children :=  if ($depth eq 0) then () else cmd-model:get-children-colls($collection)
     196  let $dummy := util:log('debug', fn:concat(cmd-model:get-md-collection-name($collection), " ", $collection//MdSelfLink, " ", xs:string($depth), " CHILDREN = ", string-join(for $child in $children return $child//MdSelfLink, "#")))
    198197    return
    199198      if (fn:exists($children)) then
    200                         let $child-results :=
    201                             for $child in $children
    202                                                 (:            let $child-doc := if (empty($child/unresolvable-uri)) then
    203                                                 cmd-model:get-resource-by-handle($child/ResourceRef) else (), :)
    204                                         let $child-name := cmd-model:get-md-collection-name($child)
    205                                         let $proxyid := $collection//ResourceProxy[ResourceRef = $child//MdSelfLink]/@id
    206                                 return
    207                                         if (empty($child)) then ()
    208                                         else
    209                                                 cmd-model:recurse-collections($child, $child-name, $child//Header/MdSelfLink, $proxyid, $depth - 1)
    210                         return
    211                           <c n="{$name}" handle="{$handle}" proxy-id="{$proxy-id}" cnt="{sum($child-results/@cnt)}" cnt_subcolls="{if ($handle eq '') then '-1' else cmd-model:get-collection-count($handle)}" cnt_children="{count($child-results)}" >{$child-results}</c>
     199        let $child-results :=
     200          for $child in $children
     201            (: let $child-doc := if (empty($child/unresolvable-uri)) then
     202                cmd-model:get-resource-by-handle($child/ResourceRef) else (), :)
     203            let $child-name := cmd-model:get-md-collection-name($child)
     204            let $proxyid := $collection//ResourceProxy[ResourceRef = $child//MdSelfLink]/@id
     205            return
     206              if (empty($child)) then ()
     207            else
     208              cmd-model:recurse-collections($child, $child-name, $child//Header/MdSelfLink, $proxyid, $depth - 1)
     209
     210          return
     211          <c n="{$name}" handle="{$handle}" proxy-id="{$proxy-id}" cnt="{sum($child-results/@cnt)}" cnt_subcolls="{if ($handle eq '') then '-1' else cmd-model:get-collection-count($handle)}" cnt_children="{count($child-results)}" >{$child-results}</c>
    212212      else
    213213        <c n="{$name}" handle="{$handle}" proxy-id="{$proxy-id}" cnt_subcolls="{if ($handle eq '') then '-1' else cmd-model:get-collection-count($handle)}" cnt="{if ($handle eq '') then '-1' else cmd-model:get-resource-count($handle)}"></c>
     
    216216
    217217(:
    218         Get the next level collection-records (only ResourceType='Metadata')
    219         rely just on the ResourceProxy of the parent (param),   
    220 :)
    221 declare function cmd-model:get-children-colls($collection as node()+) as node()* {
    222 (:      let $proxies := $collection//ResourceProxy[ResourceType='Metadata']/ResourceRef :)
    223         let $handle := $collection//MdSelfLink
    224   return collection($cmd-model:cmdiMirrorPath)//IsPartOf[@level=1][matches(.,$handle)]/ancestor::CMD[descendant::ResourceType[. = "Metadata"]]
    225 (: this turned out to be very inefficient!!:
    226         return collection($cmd-model:cmdiMirrorPath)//CMD[descendant::MdSelfLink = $proxies/.][descendant::ResourceType = "Metadata"]   
    227 :)
    228   };
     218  Get the next level collection-records (ResourceType='Metadata')
     219  rely on the ResourceProxy of the parent (param)
     220:)
     221declare function cmd-model:get-children-colls($collection as node()) as node()* {
     222  let $handle := $collection//MdSelfLink/text(),
     223    $cmdi-collection := collection($cmd-model:cmdiMirrorPath)
     224  return util:eval(concat("$cmdi-collection/ft:query(descendant::IsPartOf, <term>", $handle, "</term>)/ancestor::CMD[descendant::ResourceType[. = 'Metadata']]"))
     225    (: collection($cmd-model:cmdiMirrorPath)/descendant::IsPartOf[. eq $handle]/ancestor::CMD[descendant::ResourceType[. = "Metadata"]] :)
     226};
    229227
    230228(: count ALL (independent of maxDepth) resource-records (ie actually ResourceType=Resource, but
    231229                        there are records without ResourceProxy[ResourceType=Resource] - so care for that (not(exists((ResourceType))))
    232                         Still we dont want collections here!   
    233230:)
    234231declare function cmd-model:get-resource-count($handle as xs:string) as xs:string {
    235         (: xs:string(count(collection($cmd-model:cmdiMirrorPath)//IsPartOf[. eq $handle]/ancestor::CMD[descendant::ResourceType[. = "Resource"]]))  :) 
    236232        xs:string(count(collection($cmd-model:cmdiMirrorPath)//IsPartOf[. eq $handle]/ancestor::CMD[descendant::ResourceType[. = "Resource"] or not(exists(descendant::ResourceType)) ]))
    237233};
     
    245241
    246242(:
    247 try to derive a name from the collection-record (more-or-less agnostic about the actual schema
     243 Try to derive a name from the collection-record (more-or-less agnostic about the actual schema.
    248244:)
    249245declare function cmd-model:get-md-collection-name($collection-doc as node()) as xs:string {
    250 ($collection-doc//Corpus/Name, $collection-doc//Session/Name, $collection-doc//Collection/GeneralInfo/Name, $collection-doc//Collection/GeneralInfo/Title,
    251   $collection-doc//Name,   $collection-doc//name,   $collection-doc//Title,   $collection-doc//title,    "UNKNOWN")[1]
     246($collection-doc//Corpus/Name, $collection-doc//Session/Name, $collection-doc//Collection/GeneralInfo/Name, $collection-doc//Collection/GeneralInfo/Title, $collection-doc//Name, $collection-doc//name, $collection-doc//Title, $collection-doc//title, "UNKNOWN")[1]
    252247};
    253248
     
    380375
    381376(:
    382   Skapa elementnamn för dokumentet av typ.
     377  Get element name for document type.
    383378:)
    384379declare function cmd-model:get-doc-type-element-name($type-name as xs:string) as xs:string {
Note: See TracChangeset for help on using the changeset viewer.