Skip to content

Commit

Permalink
moved the caching of the collections to the shared library module
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstadler committed Oct 23, 2017
1 parent 294cbec commit fbcb0cf
Showing 1 changed file with 15 additions and 40 deletions.
55 changes: 15 additions & 40 deletions modules/core.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ declare namespace request="http://exist-db.org/xquery/request";
import module namespace config="http://xquery.weber-gesamtausgabe.de/modules/config" at "config.xqm";
import module namespace norm="http://xquery.weber-gesamtausgabe.de/modules/norm" at "norm.xqm";
import module namespace wdt="http://xquery.weber-gesamtausgabe.de/modules/wdt" at "wdt.xqm";
import module namespace cache="http://exist-db.org/xquery/cache" at "java:org.exist.xquery.modules.cache.CacheModule";
import module namespace cache="http://xquery.weber-gesamtausgabe.de/modules/cache" at "xmldb:exist:///db/apps/WeGA-WebApp-lib/xquery/cache.xqm";
import module namespace date="http://xquery.weber-gesamtausgabe.de/modules/date" at "xmldb:exist:///db/apps/WeGA-WebApp-lib/xquery/date.xqm";
import module namespace str="http://xquery.weber-gesamtausgabe.de/modules/str" at "xmldb:exist:///db/apps/WeGA-WebApp-lib/xquery/str.xqm";

Expand Down Expand Up @@ -63,43 +63,27 @@ declare function core:data-collection($collectionName as xs:string) as document-
: @return document-node()*
:)
declare function core:getOrCreateColl($collName as xs:string, $cacheKey as xs:string, $useCache as xs:boolean) as document-node()* {
let $dateTimeOfCache := cache:get($collName, 'lastModDateTime')
let $collCached := cache:get($collName, $cacheKey)
return
if(exists($collCached) and not(config:eXistDbWasUpdatedAfterwards($dateTimeOfCache)) and $useCache) then
typeswitch($collCached)
case xs:string return ()
default return $collCached
else if($collName eq 'diaries' and not($cacheKey = ('indices', 'A002068'))) then () (: Suppress the creation of diary collections for others than Weber :)
let $lease := function($dateTimeOfCache) as xs:boolean {
config:eXistDbWasUpdatedAfterwards($dateTimeOfCache) and $useCache
}
let $onFailure := function($errCode as xs:string, $errDesc as xs:string) {
core:logToFile('error', concat($errCode, ': ', $errDesc))
}
let $callBack := function() {
if($collName eq 'diaries' and not($cacheKey = ('indices', 'A002068'))) then () (: Suppress the creation of diary collections for others than Weber :)
else
let $newColl := core:createColl($collName,$cacheKey)
let $newIndex :=
if($cacheKey eq 'indices') then wdt:lookup($collName, $newColl)('init-sortIndex')()
else ()
let $sortedColl := wdt:lookup($collName, $newColl)('sort')( map { 'personID' := $cacheKey} )
let $setCache :=
(: Do not cache all collections. This will result in too much memory consumption :)
if(count($sortedColl) gt 250) then core:put-cache($collName, $cacheKey, $sortedColl)
else ()
(:let $log := core:logToFile('debug', 'Creating collection' || $collName || ': ' || $cacheKey):)
return $sortedColl
};

(:~
: helper function for core:getOrCreateColl
:
: @author Peter Stadler
: @param $cacheName the name of the cache
: @param $cacheKey the key for the cache
: @param $content the content to cache
: @return item()*
:)
declare %private function core:put-cache($cacheName as xs:string, $cacheKey as xs:string, $content as item()*) as item()* {
let $logMessage := concat('core:put-cache(): created cache (',$cacheKey,') for ', $cacheName, ' (', count($content), ' items)')
let $logToFile := core:logToFile('info', $logMessage)
return (
cache:put($cacheName, 'lastModDateTime', current-dateTime()),
cache:put($cacheName, $cacheKey, $content)
)
}
return
(: Do not cache all collections. This will result in too much memory consumption :)
if($cacheKey = ('indices', 'A002068', 'A130001', 'A130002', 'A000213', 'A130003', 'A000914', 'A020040', 'A130004', 'A130005', 'A130006', 'A001002', 'A002078', 'A020043', 'A002160', 'A002085')) then cache:collection($collName || $cacheKey, $callBack, $lease, $onFailure)
else $callBack()
};

(:~
Expand All @@ -111,15 +95,6 @@ declare %private function core:put-cache($cacheName as xs:string, $cacheKey as x
: @return document-node()*
:)
declare %private function core:createColl($collName as xs:string, $cacheKey as xs:string) as document-node()* {
(:let $func :=
try { function-lookup(xs:QName('wdt:' || $collName), 1) }
catch * { core:logToFile('error', 'core:createColl(): failed to lookup function "' || $collName || '"') }
let $coll := $func(())('init-collection')()
return
if(config:is-person($cacheKey) or config:is-org($cacheKey)) then
$func($coll)('filter-by-person')($cacheKey)
else if($cacheKey eq 'indices') then $coll
else ():)
if($cacheKey eq 'indices') then wdt:lookup($collName, ())('init-collection')()
else if($collName eq 'backlinks') then wdt:backlinks(())('filter-by-person')($cacheKey)
else wdt:lookup($collName, core:getOrCreateColl($collName, 'indices', true()))('filter-by-person')($cacheKey)
Expand Down

0 comments on commit fbcb0cf

Please sign in to comment.