Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore terminology and model resolver caches #337

Merged
merged 5 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@

import java.lang.reflect.InvocationTargetException;
import java.sql.Driver;
import java.time.Duration;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import org.apache.commons.dbcp2.BasicDataSource;
import org.cqframework.cql.cql2elm.model.Model;
import org.cqframework.cql.elm.execution.Library;
import org.cqframework.cql.elm.execution.VersionedIdentifier;
import org.hl7.fhir.instance.model.api.IIdType;
import org.opencds.cqf.cds.providers.ProviderConfiguration;
import org.opencds.cqf.cql.engine.fhir.searchparam.SearchParameterResolver;
import org.opencds.cqf.cql.engine.runtime.Code;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
Expand All @@ -14,8 +26,12 @@

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.api.config.DaoConfig;
import ca.uhn.fhir.jpa.cache.IResourceChangeEvent;
import ca.uhn.fhir.jpa.cache.IResourceChangeListener;
import ca.uhn.fhir.jpa.cache.IResourceChangeListenerRegistry;
import ca.uhn.fhir.jpa.model.config.PartitionSettings;
import ca.uhn.fhir.jpa.model.entity.ModelConfig;
import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
import ca.uhn.fhir.rest.server.interceptor.LoggingInterceptor;
import ca.uhn.fhir.rest.server.interceptor.ResponseHighlighterInterceptor;

Expand Down Expand Up @@ -143,4 +159,43 @@ public ProviderConfiguration providerConfiguration() {
public SearchParameterResolver searchParameterResolver(FhirContext fhirContext) {
return new SearchParameterResolver(fhirContext);
}

@Bean
public IResourceChangeListener valueSetChangeListener(IResourceChangeListenerRegistry resourceChangeListenerRegistry, Map<String, Iterable<Code>> terminologyCache) {
IResourceChangeListener listener = new IResourceChangeListener(){

@Override
public void handleInit(Collection<IIdType> theResourceIds) {
// Intentionally empty
}

// TODO: Selectively clear by url. Requires a lookup on the resource
@Override
public void handleChange(IResourceChangeEvent theResourceChangeEvent) {
terminologyCache.clear();
}

};

resourceChangeListenerRegistry.registerResourceResourceChangeListener("ValueSet", SearchParameterMap.newSynchronous(), listener, 1000);

return listener;
}

@Bean
public Map<String, Iterable<Code>> terminologyCache() {
Cache<String, Iterable<Code>> cache = Caffeine.newBuilder().maximumSize(100).expireAfterAccess(Duration.ofMinutes(60)).build();
return cache.asMap();
}


@Bean(name="globalModelCache")
Map<VersionedIdentifier, Model> globalModelCache() {
return new ConcurrentHashMap<VersionedIdentifier, Model>();
}

@Bean(name="globalLibraryCache")
Map<org.cqframework.cql.elm.execution.VersionedIdentifier, Library> globalLibraryCache() {
return new ConcurrentHashMap<org.cqframework.cql.elm.execution.VersionedIdentifier, Library>();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.opencds.cqf.common.providers;

import java.util.Map;
import org.opencds.cqf.cql.engine.runtime.Code;
import org.opencds.cqf.cql.engine.terminology.CodeSystemInfo;
import org.opencds.cqf.cql.engine.terminology.TerminologyProvider;
import org.opencds.cqf.cql.engine.terminology.ValueSetInfo;

public class CacheAwareTerminologyProvider implements TerminologyProvider {

private TerminologyProvider innerTerminologyProvider;

private Map<String, Iterable<Code>> globalCodeCacheByUrl;

public CacheAwareTerminologyProvider(Map<String, Iterable<Code>> globalCodeCacheByUrl, TerminologyProvider innerTerminologyProvider) {
this.globalCodeCacheByUrl = globalCodeCacheByUrl;
this.innerTerminologyProvider = innerTerminologyProvider;
}

@Override
public boolean in(Code code, ValueSetInfo valueSet) {
Iterable<Code> codes = this.expand(valueSet);
if (codes == null) {
return false;
}

for (Code c : codes) {
if (c.equivalent(code)) {
return true;
}
}

return false;
}

@Override
public Iterable<Code> expand(ValueSetInfo valueSet) {
if (this.globalCodeCacheByUrl.containsKey(valueSet.getId())) {
return this.globalCodeCacheByUrl.get(valueSet.getId());
}

Iterable<Code> codes = this.innerTerminologyProvider.expand(valueSet);

if (codes != null) {
this.globalCodeCacheByUrl.put(valueSet.getId(), codes);
}

return codes;
}

@Override
public Code lookup(Code code, CodeSystemInfo codeSystem) {
return this.innerTerminologyProvider.lookup(code, codeSystem);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import ca.uhn.fhir.cql.common.provider.LibraryResolutionProvider;

public class InMemoryLibraryResourceProvider<LibraryType> implements LibraryResolutionProvider<LibraryType> {

private Map<String, LibraryType> libraries = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@

import org.cqframework.cql.cql2elm.FhirLibrarySourceProvider;
import org.hl7.elm.r1.VersionedIdentifier;
import org.opencds.cqf.cql.evaluator.cql2elm.content.LibraryContentType;

import ca.uhn.fhir.cql.common.provider.LibraryResolutionProvider;

/**
* Created by Christopher on 1/12/2017.
*/
public class LibrarySourceProvider<LibraryType, AttachmentType>
implements org.cqframework.cql.cql2elm.LibrarySourceProvider {
public class LibraryContentProvider<LibraryType, AttachmentType>
implements org.opencds.cqf.cql.evaluator.cql2elm.content.LibraryContentProvider {

private FhirLibrarySourceProvider innerProvider;
private LibraryResolutionProvider<LibraryType> provider;
private Function<LibraryType, Iterable<AttachmentType>> getAttachments;
private Function<AttachmentType, String> getContentType;
private Function<AttachmentType, byte[]> getContent;

public LibrarySourceProvider(LibraryResolutionProvider<LibraryType> provider,
public LibraryContentProvider(LibraryResolutionProvider<LibraryType> provider,
Function<LibraryType, Iterable<AttachmentType>> getAttachments,
Function<AttachmentType, String> getContentType, Function<AttachmentType, byte[]> getContent) {

Expand All @@ -46,4 +49,14 @@ public InputStream getLibrarySource(VersionedIdentifier versionedIdentifier) {

return this.innerProvider.getLibrarySource(versionedIdentifier);
}

@Override
public InputStream getLibraryContent(VersionedIdentifier libraryIdentifier, LibraryContentType libraryContentType) {

if (libraryContentType == LibraryContentType.CQL) {
return this.getLibrarySource(libraryIdentifier);
}

return null;
}
}

This file was deleted.

Loading