Add delayed load resources to all resources, and don't load resources from core examples package over other resources

This commit is contained in:
Grahame Grieve 2021-12-07 18:03:22 +11:00
parent 1225bfbaee
commit ab2212d9fc
4 changed files with 62 additions and 24 deletions

View File

@ -52,6 +52,7 @@ import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.exceptions.NoTerminologyServiceException; import org.hl7.fhir.exceptions.NoTerminologyServiceException;
import org.hl7.fhir.exceptions.TerminologyServiceException; import org.hl7.fhir.exceptions.TerminologyServiceException;
import org.hl7.fhir.r5.conformance.ProfileUtilities; import org.hl7.fhir.r5.conformance.ProfileUtilities;
import org.hl7.fhir.r5.context.BaseWorkerContext.ResourceProxy;
import org.hl7.fhir.r5.context.CanonicalResourceManager.CanonicalResourceProxy; import org.hl7.fhir.r5.context.CanonicalResourceManager.CanonicalResourceProxy;
import org.hl7.fhir.r5.context.IWorkerContext.PackageVersion; import org.hl7.fhir.r5.context.IWorkerContext.PackageVersion;
import org.hl7.fhir.r5.context.IWorkerContext.ILoggingService.LogCategory; import org.hl7.fhir.r5.context.IWorkerContext.ILoggingService.LogCategory;
@ -129,6 +130,35 @@ import ca.uhn.fhir.model.valueset.BundleEntrySearchModeEnum;
public abstract class BaseWorkerContext extends I18nBase implements IWorkerContext{ public abstract class BaseWorkerContext extends I18nBase implements IWorkerContext{
public class ResourceProxy {
private Resource resource;
private CanonicalResourceProxy proxy;
public ResourceProxy(Resource resource) {
super();
this.resource = resource;
}
public ResourceProxy(CanonicalResourceProxy proxy) {
super();
this.proxy = proxy;
}
public Resource getResource() {
return resource != null ? resource : proxy.getResource();
}
public String getUrl() {
if (resource == null) {
return proxy.getUrl();
} else if (resource instanceof CanonicalResource) {
return ((CanonicalResource) resource).getUrl();
} else {
return null;
}
}
}
public class MetadataResourceVersionComparator<T extends CanonicalResource> implements Comparator<T> { public class MetadataResourceVersionComparator<T extends CanonicalResource> implements Comparator<T> {
private List<T> list; private List<T> list;
@ -165,7 +195,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
private boolean isTxCaching; private boolean isTxCaching;
private Set<String> cached = new HashSet<>(); private Set<String> cached = new HashSet<>();
private Map<String, Map<String, Resource>> allResourcesById = new HashMap<String, Map<String, Resource>>(); private Map<String, Map<String, ResourceProxy>> allResourcesById = new HashMap<String, Map<String, ResourceProxy>>();
// all maps are to the full URI // all maps are to the full URI
private CanonicalResourceManager<CodeSystem> codeSystems = new CanonicalResourceManager<CodeSystem>(false); private CanonicalResourceManager<CodeSystem> codeSystems = new CanonicalResourceManager<CodeSystem>(false);
private Set<String> supportedCodeSystems = new HashSet<String>(); private Set<String> supportedCodeSystems = new HashSet<String>();
@ -276,6 +306,12 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
public void registerResourceFromPackage(CanonicalResourceProxy r, PackageVersion packageInfo) throws FHIRException { public void registerResourceFromPackage(CanonicalResourceProxy r, PackageVersion packageInfo) throws FHIRException {
synchronized (lock) { synchronized (lock) {
Map<String, ResourceProxy> map = allResourcesById.get(r.getType());
if (map == null) {
map = new HashMap<String, ResourceProxy>();
allResourcesById.put(r.getType(), map);
}
map.put(r.getId(), new ResourceProxy(r));
String url = r.getUrl(); String url = r.getUrl();
if (!allowLoadingDuplicates && hasResource(r.getType(), url)) { if (!allowLoadingDuplicates && hasResource(r.getType(), url)) {
@ -338,12 +374,14 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
public void cacheResourceFromPackage(Resource r, PackageVersion packageInfo) throws FHIRException { public void cacheResourceFromPackage(Resource r, PackageVersion packageInfo) throws FHIRException {
synchronized (lock) { synchronized (lock) {
Map<String, Resource> map = allResourcesById.get(r.fhirType()); Map<String, ResourceProxy> map = allResourcesById.get(r.fhirType());
if (map == null) { if (map == null) {
map = new HashMap<String, Resource>(); map = new HashMap<String, ResourceProxy>();
allResourcesById.put(r.fhirType(), map); allResourcesById.put(r.fhirType(), map);
} }
map.put(r.getId(), r); if ((packageInfo == null || !packageInfo.isExamplesPackage()) || !map.containsKey(r.getId())) {
map.put(r.getId(), new ResourceProxy(r));
}
if (r instanceof CodeSystem || r instanceof NamingSystem) { if (r instanceof CodeSystem || r instanceof NamingSystem) {
oidCache.clear(); oidCache.clear();
@ -1300,13 +1338,10 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
if (questionnaires.has(uri)) { if (questionnaires.has(uri)) {
return (T) questionnaires.get(uri, version); return (T) questionnaires.get(uri, version);
} }
for (Map<String, Resource> rt : allResourcesById.values()) { for (Map<String, ResourceProxy> rt : allResourcesById.values()) {
for (Resource r : rt.values()) { for (ResourceProxy r : rt.values()) {
if (r instanceof CanonicalResource) { if (uri.equals(r.getUrl())) {
CanonicalResource mr = (CanonicalResource) r; return (T) r.getResource();
if (uri.equals(mr.getUrl())) {
return (T) mr;
}
} }
} }
} }
@ -1489,13 +1524,10 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
if (questionnaires.has(uri)) { if (questionnaires.has(uri)) {
return (T) questionnaires.get(uri, version); return (T) questionnaires.get(uri, version);
} }
for (Map<String, Resource> rt : allResourcesById.values()) { for (Map<String, ResourceProxy> rt : allResourcesById.values()) {
for (Resource r : rt.values()) { for (ResourceProxy r : rt.values()) {
if (r instanceof CanonicalResource) { if (uri.equals(r.getUrl())) {
CanonicalResource mr = (CanonicalResource) r; return (T) r.getResource();
if (uri.equals(mr.getUrl())) {
return (T) mr;
}
} }
} }
} }
@ -1569,7 +1601,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
String[] parts = uri.split("\\/"); String[] parts = uri.split("\\/");
if (!Utilities.noString(type) && parts.length == 1) { if (!Utilities.noString(type) && parts.length == 1) {
if (allResourcesById.containsKey(type)) { if (allResourcesById.containsKey(type)) {
return allResourcesById.get(type).get(parts[0]); return allResourcesById.get(type).get(parts[0]).getResource();
} else { } else {
return null; return null;
} }
@ -1580,7 +1612,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
throw new Error(formatMessage(I18nConstants.RESOURCE_TYPE_MISMATCH_FOR___, type, uri)); throw new Error(formatMessage(I18nConstants.RESOURCE_TYPE_MISMATCH_FOR___, type, uri));
} }
} }
return allResourcesById.get(parts[parts.length-2]).get(parts[parts.length-1]); return allResourcesById.get(parts[parts.length-2]).get(parts[parts.length-1]).getResource();
} else { } else {
throw new Error(formatMessage(I18nConstants.UNABLE_TO_PROCESS_REQUEST_FOR_RESOURCE_FOR___, type, uri)); throw new Error(formatMessage(I18nConstants.UNABLE_TO_PROCESS_REQUEST_FOR_RESOURCE_FOR___, type, uri));
} }
@ -1692,13 +1724,13 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
public void dropResource(String fhirType, String id) { public void dropResource(String fhirType, String id) {
synchronized (lock) { synchronized (lock) {
Map<String, Resource> map = allResourcesById.get(fhirType); Map<String, ResourceProxy> map = allResourcesById.get(fhirType);
if (map == null) { if (map == null) {
map = new HashMap<String, Resource>(); map = new HashMap<String, ResourceProxy>();
allResourcesById.put(fhirType, map); allResourcesById.put(fhirType, map);
} }
if (map.containsKey(id)) { if (map.containsKey(id)) {
map.remove(id); map.remove(id); // this is a challenge because we might have more than one resource with this id (different versions)
} }
if (fhirType.equals("StructureDefinition")) { if (fhirType.equals("StructureDefinition")) {

View File

@ -236,6 +236,9 @@ public class CanonicalResourceManager<T extends CanonicalResource> {
} }
} }
CachedCanonicalResource<T> existing = cr.hasVersion() ? map.get(cr.getUrl()+"|"+cr.getVersion()) : map.get(cr.getUrl()+"|#0"); CachedCanonicalResource<T> existing = cr.hasVersion() ? map.get(cr.getUrl()+"|"+cr.getVersion()) : map.get(cr.getUrl()+"|#0");
if (existing != null && (cr.getPackageInfo() != null && cr.getPackageInfo().isExamplesPackage())) {
return;
}
if (existing != null) { if (existing != null) {
list.remove(existing); list.remove(existing);
} }

View File

@ -167,6 +167,9 @@ public interface IWorkerContext {
public String getVersion() { public String getVersion() {
return version; return version;
} }
public boolean isExamplesPackage() {
return !(id.startsWith("hl7.fhir.") && id.endsWith(".example"));
}
} }
public class PackageDetails extends PackageVersion { public class PackageDetails extends PackageVersion {

View File

@ -19,7 +19,7 @@
<properties> <properties>
<hapi_fhir_version>5.1.0</hapi_fhir_version> <hapi_fhir_version>5.1.0</hapi_fhir_version>
<validator_test_case_version>1.1.79</validator_test_case_version> <validator_test_case_version>1.1.80-SNAPSHOT</validator_test_case_version>
<junit_jupiter_version>5.7.1</junit_jupiter_version> <junit_jupiter_version>5.7.1</junit_jupiter_version>
<junit_platform_launcher_version>1.7.1</junit_platform_launcher_version> <junit_platform_launcher_version>1.7.1</junit_platform_launcher_version>
<maven_surefire_version>3.0.0-M5</maven_surefire_version> <maven_surefire_version>3.0.0-M5</maven_surefire_version>